emutls.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* TLS emulation.
  2. Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "tconfig.h"
  21. #include "tsystem.h"
  22. #include "coretypes.h"
  23. #include "tm.h"
  24. #include "libgcc_tm.h"
  25. #include "gthr.h"
  26. typedef unsigned int word __attribute__((mode(word)));
  27. typedef unsigned int pointer __attribute__((mode(pointer)));
  28. struct __emutls_object
  29. {
  30. word size;
  31. word align;
  32. union {
  33. pointer offset;
  34. void *ptr;
  35. } loc;
  36. void *templ;
  37. };
  38. struct __emutls_array
  39. {
  40. pointer size;
  41. void **data[];
  42. };
  43. void *__emutls_get_address (struct __emutls_object *);
  44. void __emutls_register_common (struct __emutls_object *, word, word, void *);
  45. #ifdef __GTHREADS
  46. #ifdef __GTHREAD_MUTEX_INIT
  47. static __gthread_mutex_t emutls_mutex = __GTHREAD_MUTEX_INIT;
  48. #else
  49. static __gthread_mutex_t emutls_mutex;
  50. #endif
  51. static __gthread_key_t emutls_key;
  52. static pointer emutls_size;
  53. static void
  54. emutls_destroy (void *ptr)
  55. {
  56. struct __emutls_array *arr = ptr;
  57. pointer size = arr->size;
  58. pointer i;
  59. for (i = 0; i < size; ++i)
  60. {
  61. if (arr->data[i])
  62. free (arr->data[i][-1]);
  63. }
  64. free (ptr);
  65. }
  66. static void
  67. emutls_init (void)
  68. {
  69. #ifndef __GTHREAD_MUTEX_INIT
  70. __GTHREAD_MUTEX_INIT_FUNCTION (&emutls_mutex);
  71. #endif
  72. if (__gthread_key_create (&emutls_key, emutls_destroy) != 0)
  73. abort ();
  74. }
  75. #endif
  76. static void *
  77. emutls_alloc (struct __emutls_object *obj)
  78. {
  79. void *ptr;
  80. void *ret;
  81. /* We could use here posix_memalign if available and adjust
  82. emutls_destroy accordingly. */
  83. if (obj->align <= sizeof (void *))
  84. {
  85. ptr = malloc (obj->size + sizeof (void *));
  86. if (ptr == NULL)
  87. abort ();
  88. ((void **) ptr)[0] = ptr;
  89. ret = ptr + sizeof (void *);
  90. }
  91. else
  92. {
  93. ptr = malloc (obj->size + sizeof (void *) + obj->align - 1);
  94. if (ptr == NULL)
  95. abort ();
  96. ret = (void *) (((pointer) (ptr + sizeof (void *) + obj->align - 1))
  97. & ~(pointer)(obj->align - 1));
  98. ((void **) ret)[-1] = ptr;
  99. }
  100. if (obj->templ)
  101. memcpy (ret, obj->templ, obj->size);
  102. else
  103. memset (ret, 0, obj->size);
  104. return ret;
  105. }
  106. void *
  107. __emutls_get_address (struct __emutls_object *obj)
  108. {
  109. if (! __gthread_active_p ())
  110. {
  111. if (__builtin_expect (obj->loc.ptr == NULL, 0))
  112. obj->loc.ptr = emutls_alloc (obj);
  113. return obj->loc.ptr;
  114. }
  115. #ifndef __GTHREADS
  116. abort ();
  117. #else
  118. pointer offset = __atomic_load_n (&obj->loc.offset, __ATOMIC_ACQUIRE);
  119. if (__builtin_expect (offset == 0, 0))
  120. {
  121. static __gthread_once_t once = __GTHREAD_ONCE_INIT;
  122. __gthread_once (&once, emutls_init);
  123. __gthread_mutex_lock (&emutls_mutex);
  124. offset = obj->loc.offset;
  125. if (offset == 0)
  126. {
  127. offset = ++emutls_size;
  128. __atomic_store_n (&obj->loc.offset, offset, __ATOMIC_RELEASE);
  129. }
  130. __gthread_mutex_unlock (&emutls_mutex);
  131. }
  132. struct __emutls_array *arr = __gthread_getspecific (emutls_key);
  133. if (__builtin_expect (arr == NULL, 0))
  134. {
  135. pointer size = offset + 32;
  136. arr = calloc (size + 1, sizeof (void *));
  137. if (arr == NULL)
  138. abort ();
  139. arr->size = size;
  140. __gthread_setspecific (emutls_key, (void *) arr);
  141. }
  142. else if (__builtin_expect (offset > arr->size, 0))
  143. {
  144. pointer orig_size = arr->size;
  145. pointer size = orig_size * 2;
  146. if (offset > size)
  147. size = offset + 32;
  148. arr = realloc (arr, (size + 1) * sizeof (void *));
  149. if (arr == NULL)
  150. abort ();
  151. arr->size = size;
  152. memset (arr->data + orig_size, 0,
  153. (size - orig_size) * sizeof (void *));
  154. __gthread_setspecific (emutls_key, (void *) arr);
  155. }
  156. void *ret = arr->data[offset - 1];
  157. if (__builtin_expect (ret == NULL, 0))
  158. {
  159. ret = emutls_alloc (obj);
  160. arr->data[offset - 1] = ret;
  161. }
  162. return ret;
  163. #endif
  164. }
  165. void
  166. __emutls_register_common (struct __emutls_object *obj,
  167. word size, word align, void *templ)
  168. {
  169. if (obj->size < size)
  170. {
  171. obj->size = size;
  172. obj->templ = NULL;
  173. }
  174. if (obj->align < align)
  175. obj->align = align;
  176. if (templ && size == obj->size)
  177. obj->templ = templ;
  178. }