emutls.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. extern int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
  24. extern int pthread_key_delete(pthread_key_t key);
  25. extern void *pthread_getspecific(pthread_key_t key);
  26. extern int pthread_setspecific(pthread_key_t key, const void *value);
  27. /* Default is not to use posix_memalign, so systems like Android
  28. * can use thread local data without heavier POSIX memory allocators.
  29. */
  30. #ifndef EMUTLS_USE_POSIX_MEMALIGN
  31. #define EMUTLS_USE_POSIX_MEMALIGN 0
  32. #endif
  33. /* For every TLS variable xyz,
  34. * there is one __emutls_control variable named __emutls_v.xyz.
  35. * If xyz has non-zero initial value, __emutls_v.xyz's "value"
  36. * will point to __emutls_t.xyz, which has the initial value.
  37. */
  38. typedef struct __emutls_control
  39. {
  40. size_t size; /* size of the object in bytes */
  41. size_t align; /* alignment of the object in bytes */
  42. union
  43. {
  44. uintptr_t index; /* data[index-1] is the object address */
  45. void *address; /* object address, when in single thread env */
  46. } object;
  47. void *value; /* null or non-zero initial value for the object */
  48. } __emutls_control;
  49. static __inline void *emutls_memalign_alloc(size_t align, size_t size)
  50. {
  51. void *base;
  52. #if EMUTLS_USE_POSIX_MEMALIGN
  53. if (posix_memalign(&base, align, size) != 0)
  54. abort();
  55. #else
  56. #define EXTRA_ALIGN_PTR_BYTES (align - 1 + sizeof(void *))
  57. char *object;
  58. if ((object = (char *)malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL)
  59. abort();
  60. base = (void *)(((uintptr_t)(object + EXTRA_ALIGN_PTR_BYTES)) & ~(uintptr_t)(align - 1));
  61. ((void **)base)[-1] = object;
  62. #endif
  63. return base;
  64. }
  65. static __inline void emutls_memalign_free(void *base)
  66. {
  67. #if EMUTLS_USE_POSIX_MEMALIGN
  68. free(base);
  69. #else
  70. /* The mallocated address is in ((void**)base)[-1] */
  71. free(((void **)base)[-1]);
  72. #endif
  73. }
  74. /* Emulated TLS objects are always allocated at run-time. */
  75. static __inline void *emutls_allocate_object(__emutls_control *control)
  76. {
  77. size_t size = control->size;
  78. size_t align = control->align;
  79. if (align < sizeof(void *))
  80. align = sizeof(void *);
  81. /* Make sure that align is power of 2. */
  82. if ((align & (align - 1)) != 0)
  83. abort();
  84. void *base = emutls_memalign_alloc(align, size);
  85. if (control->value)
  86. memcpy(base, control->value, size);
  87. else
  88. memset(base, 0, size);
  89. return base;
  90. }
  91. static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER;
  92. static size_t emutls_num_object = 0; /* number of allocated TLS objects */
  93. typedef struct emutls_address_array
  94. {
  95. uintptr_t size; /* number of elements in the 'data' array */
  96. void *data[];
  97. } emutls_address_array;
  98. static pthread_key_t emutls_pthread_key;
  99. static void emutls_key_destructor(void *ptr)
  100. {
  101. emutls_address_array *array = (emutls_address_array *)ptr;
  102. uintptr_t i;
  103. for (i = 0; i < array->size; ++i)
  104. {
  105. if (array->data[i])
  106. emutls_memalign_free(array->data[i]);
  107. }
  108. free(ptr);
  109. }
  110. static void emutls_init(void)
  111. {
  112. if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0)
  113. abort();
  114. }
  115. /* Returns control->object.index; set index if not allocated yet. */
  116. static __inline uintptr_t emutls_get_index(__emutls_control *control)
  117. {
  118. uintptr_t index = __atomic_load_n(&control->object.index, __ATOMIC_ACQUIRE);
  119. if (!index)
  120. {
  121. static pthread_once_t once = PTHREAD_ONCE_INIT;
  122. pthread_once(&once, emutls_init);
  123. pthread_mutex_lock(&emutls_mutex);
  124. index = control->object.index;
  125. if (!index)
  126. {
  127. index = ++emutls_num_object;
  128. __atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE);
  129. }
  130. pthread_mutex_unlock(&emutls_mutex);
  131. }
  132. return index;
  133. }
  134. /* Updates newly allocated thread local emutls_address_array. */
  135. static __inline void emutls_check_array_set_size(emutls_address_array *array,
  136. uintptr_t size)
  137. {
  138. if (array == NULL)
  139. abort();
  140. array->size = size;
  141. pthread_setspecific(emutls_pthread_key, (void *)array);
  142. }
  143. /* Returns the new 'data' array size, number of elements,
  144. * which must be no smaller than the given index.
  145. */
  146. static __inline uintptr_t emutls_new_data_array_size(uintptr_t index)
  147. {
  148. /* Need to allocate emutls_address_array with one extra slot
  149. * to store the data array size.
  150. * Round up the emutls_address_array size to multiple of 16.
  151. */
  152. return ((index + 1 + 15) & ~((uintptr_t)15)) - 1;
  153. }
  154. /* Returns the thread local emutls_address_array.
  155. * Extends its size if necessary to hold address at index.
  156. */
  157. static __inline emutls_address_array *
  158. emutls_get_address_array(uintptr_t index)
  159. {
  160. emutls_address_array *array = pthread_getspecific(emutls_pthread_key);
  161. if (array == NULL)
  162. {
  163. uintptr_t new_size = emutls_new_data_array_size(index);
  164. array = (emutls_address_array *)calloc(new_size + 1, sizeof(void *));
  165. emutls_check_array_set_size(array, new_size);
  166. }
  167. else if (index > array->size)
  168. {
  169. uintptr_t orig_size = array->size;
  170. uintptr_t new_size = emutls_new_data_array_size(index);
  171. array = (emutls_address_array *)realloc(array, (new_size + 1) * sizeof(void *));
  172. if (array)
  173. memset(array->data + orig_size, 0,
  174. (new_size - orig_size) * sizeof(void *));
  175. emutls_check_array_set_size(array, new_size);
  176. }
  177. return array;
  178. }
  179. void *__emutls_get_address(void *control)
  180. {
  181. uintptr_t index = emutls_get_index((__emutls_control *)control);
  182. emutls_address_array *array = emutls_get_address_array(index);
  183. if (array->data[index - 1] == NULL)
  184. array->data[index - 1] = emutls_allocate_object((__emutls_control *)control);
  185. return array->data[index - 1];
  186. }