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