1
0

emutls.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-04-27 peterfan Add copyright header.
  9. */
  10. /* ===---------- emutls.c - Implements __emutls_get_address ---------------===
  11. *
  12. * The LLVM Compiler Infrastructure
  13. *
  14. * This file is dual licensed under the MIT and the University of Illinois Open
  15. * Source Licenses. See LICENSE.TXT for details.
  16. *
  17. * ===----------------------------------------------------------------------===
  18. */
  19. #include <pthread.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #define COMPILE_TIME_ASSERT(x)
  24. extern int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
  25. extern int pthread_key_delete(pthread_key_t key);
  26. extern void *pthread_getspecific(pthread_key_t key);
  27. extern int pthread_setspecific(pthread_key_t key, const void *value);
  28. /* Default is not to use posix_memalign, so systems like Android
  29. * can use thread local data without heavier POSIX memory allocators.
  30. */
  31. #ifndef EMUTLS_USE_POSIX_MEMALIGN
  32. #define EMUTLS_USE_POSIX_MEMALIGN 0
  33. #endif
  34. /* For every TLS variable xyz,
  35. * there is one __emutls_control variable named __emutls_v.xyz.
  36. * If xyz has non-zero initial value, __emutls_v.xyz's "value"
  37. * will point to __emutls_t.xyz, which has the initial value.
  38. */
  39. typedef struct __emutls_control
  40. {
  41. size_t size; /* size of the object in bytes */
  42. size_t align; /* alignment of the object in bytes */
  43. union
  44. {
  45. uintptr_t index; /* data[index-1] is the object address */
  46. void *address; /* object address, when in single thread env */
  47. } object;
  48. void *value; /* null or non-zero initial value for the object */
  49. } __emutls_control;
  50. static __inline void *emutls_memalign_alloc(size_t align, size_t size)
  51. {
  52. void *base;
  53. #if EMUTLS_USE_POSIX_MEMALIGN
  54. if (posix_memalign(&base, align, size) != 0)
  55. abort();
  56. #else
  57. #define EXTRA_ALIGN_PTR_BYTES (align - 1 + sizeof(void *))
  58. char *object;
  59. if ((object = malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL)
  60. abort();
  61. base = (void *)(((uintptr_t)(object + EXTRA_ALIGN_PTR_BYTES)) & ~(uintptr_t)(align - 1));
  62. ((void **)base)[-1] = object;
  63. #endif
  64. return base;
  65. }
  66. static __inline void emutls_memalign_free(void *base)
  67. {
  68. #if EMUTLS_USE_POSIX_MEMALIGN
  69. free(base);
  70. #else
  71. /* The mallocated address is in ((void**)base)[-1] */
  72. free(((void **)base)[-1]);
  73. #endif
  74. }
  75. /* Emulated TLS objects are always allocated at run-time. */
  76. static __inline void *emutls_allocate_object(__emutls_control *control)
  77. {
  78. /* Use standard C types, check with gcc's emutls.o. */
  79. typedef unsigned int gcc_word __attribute__((mode(word)));
  80. typedef unsigned int gcc_pointer __attribute__((mode(pointer)));
  81. COMPILE_TIME_ASSERT(sizeof(size_t) == sizeof(gcc_word));
  82. COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(gcc_pointer));
  83. COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(void *));
  84. size_t size = control->size;
  85. size_t align = control->align;
  86. if (align < sizeof(void *))
  87. align = sizeof(void *);
  88. /* Make sure that align is power of 2. */
  89. if ((align & (align - 1)) != 0)
  90. abort();
  91. void *base = emutls_memalign_alloc(align, size);
  92. if (control->value)
  93. memcpy(base, control->value, size);
  94. else
  95. memset(base, 0, size);
  96. return base;
  97. }
  98. static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER;
  99. static size_t emutls_num_object = 0; /* number of allocated TLS objects */
  100. typedef struct emutls_address_array
  101. {
  102. uintptr_t size; /* number of elements in the 'data' array */
  103. void *data[];
  104. } emutls_address_array;
  105. static pthread_key_t emutls_pthread_key;
  106. static void emutls_key_destructor(void *ptr)
  107. {
  108. emutls_address_array *array = (emutls_address_array *)ptr;
  109. uintptr_t i;
  110. for (i = 0; i < array->size; ++i)
  111. {
  112. if (array->data[i])
  113. emutls_memalign_free(array->data[i]);
  114. }
  115. free(ptr);
  116. }
  117. static void emutls_init(void)
  118. {
  119. if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0)
  120. abort();
  121. }
  122. /* Returns control->object.index; set index if not allocated yet. */
  123. static __inline uintptr_t emutls_get_index(__emutls_control *control)
  124. {
  125. uintptr_t index = __atomic_load_n(&control->object.index, __ATOMIC_ACQUIRE);
  126. if (!index)
  127. {
  128. static pthread_once_t once = PTHREAD_ONCE_INIT;
  129. pthread_once(&once, emutls_init);
  130. pthread_mutex_lock(&emutls_mutex);
  131. index = control->object.index;
  132. if (!index)
  133. {
  134. index = ++emutls_num_object;
  135. __atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE);
  136. }
  137. pthread_mutex_unlock(&emutls_mutex);
  138. }
  139. return index;
  140. }
  141. /* Updates newly allocated thread local emutls_address_array. */
  142. static __inline void emutls_check_array_set_size(emutls_address_array *array,
  143. uintptr_t size)
  144. {
  145. if (array == NULL)
  146. abort();
  147. array->size = size;
  148. pthread_setspecific(emutls_pthread_key, (void *)array);
  149. }
  150. /* Returns the new 'data' array size, number of elements,
  151. * which must be no smaller than the given index.
  152. */
  153. static __inline uintptr_t emutls_new_data_array_size(uintptr_t index)
  154. {
  155. /* Need to allocate emutls_address_array with one extra slot
  156. * to store the data array size.
  157. * Round up the emutls_address_array size to multiple of 16.
  158. */
  159. return ((index + 1 + 15) & ~((uintptr_t)15)) - 1;
  160. }
  161. /* Returns the thread local emutls_address_array.
  162. * Extends its size if necessary to hold address at index.
  163. */
  164. static __inline emutls_address_array *
  165. emutls_get_address_array(uintptr_t index)
  166. {
  167. emutls_address_array *array = pthread_getspecific(emutls_pthread_key);
  168. if (array == NULL)
  169. {
  170. uintptr_t new_size = emutls_new_data_array_size(index);
  171. array = calloc(new_size + 1, sizeof(void *));
  172. emutls_check_array_set_size(array, new_size);
  173. }
  174. else if (index > array->size)
  175. {
  176. uintptr_t orig_size = array->size;
  177. uintptr_t new_size = emutls_new_data_array_size(index);
  178. array = realloc(array, (new_size + 1) * sizeof(void *));
  179. if (array)
  180. memset(array->data + orig_size, 0,
  181. (new_size - orig_size) * sizeof(void *));
  182. emutls_check_array_set_size(array, new_size);
  183. }
  184. return array;
  185. }
  186. void *__emutls_get_address(void *control)
  187. {
  188. uintptr_t index = emutls_get_index((__emutls_control *)control);
  189. emutls_address_array *array = emutls_get_address_array(index);
  190. if (array->data[index - 1] == NULL)
  191. array->data[index - 1] = emutls_allocate_object((__emutls_control *)control);
  192. return array->data[index - 1];
  193. }