emutls.c 7.1 KB

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