1 /* ===---------- emutls.c - Implements __emutls_get_address ---------------===
3 * The LLVM Compiler Infrastructure
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
8 * ===----------------------------------------------------------------------===
17 typedef struct emutls_address_array {
18 uintptr_t size; /* number of elements in the 'data' array */
20 } emutls_address_array;
22 static void emutls_shutdown(emutls_address_array *array);
28 static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER;
29 static pthread_key_t emutls_pthread_key;
31 typedef unsigned int gcc_word __attribute__((mode(word)));
32 typedef unsigned int gcc_pointer __attribute__((mode(pointer)));
34 /* Default is not to use posix_memalign, so systems like Android
35 * can use thread local data without heavier POSIX memory allocators.
37 #ifndef EMUTLS_USE_POSIX_MEMALIGN
38 #define EMUTLS_USE_POSIX_MEMALIGN 0
41 static __inline void *emutls_memalign_alloc(size_t align, size_t size) {
43 #if EMUTLS_USE_POSIX_MEMALIGN
44 if (posix_memalign(&base, align, size) != 0)
47 #define EXTRA_ALIGN_PTR_BYTES (align - 1 + sizeof(void*))
49 if ((object = (char*)malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL)
51 base = (void*)(((uintptr_t)(object + EXTRA_ALIGN_PTR_BYTES))
52 & ~(uintptr_t)(align - 1));
54 ((void**)base)[-1] = object;
59 static __inline void emutls_memalign_free(void *base) {
60 #if EMUTLS_USE_POSIX_MEMALIGN
63 /* The mallocated address is in ((void**)base)[-1] */
64 free(((void**)base)[-1]);
68 static void emutls_key_destructor(void* ptr) {
69 emutls_shutdown((emutls_address_array*)ptr);
73 static __inline void emutls_init(void) {
74 if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0)
78 static __inline void emutls_init_once(void) {
79 static pthread_once_t once = PTHREAD_ONCE_INIT;
80 pthread_once(&once, emutls_init);
83 static __inline void emutls_lock() {
84 pthread_mutex_lock(&emutls_mutex);
87 static __inline void emutls_unlock() {
88 pthread_mutex_unlock(&emutls_mutex);
91 static __inline void emutls_setspecific(emutls_address_array *value) {
92 pthread_setspecific(emutls_pthread_key, (void*) value);
95 static __inline emutls_address_array* emutls_getspecific() {
96 return (emutls_address_array*) pthread_getspecific(emutls_pthread_key);
105 #include <immintrin.h>
107 static LPCRITICAL_SECTION emutls_mutex;
108 static DWORD emutls_tls_index = TLS_OUT_OF_INDEXES;
110 typedef uintptr_t gcc_word;
111 typedef void * gcc_pointer;
113 static void win_error(DWORD last_err, const char *hint) {
115 if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
116 FORMAT_MESSAGE_FROM_SYSTEM |
117 FORMAT_MESSAGE_MAX_WIDTH_MASK,
118 NULL, last_err, 0, (LPSTR)&buffer, 1, NULL)) {
119 fprintf(stderr, "Windows error: %s\n", buffer);
121 fprintf(stderr, "Unkown Windows error: %s\n", hint);
126 static __inline void win_abort(DWORD last_err, const char *hint) {
127 win_error(last_err, hint);
131 static __inline void *emutls_memalign_alloc(size_t align, size_t size) {
132 void *base = _aligned_malloc(size, align);
134 win_abort(GetLastError(), "_aligned_malloc");
138 static __inline void emutls_memalign_free(void *base) {
142 static void emutls_exit(void) {
144 DeleteCriticalSection(emutls_mutex);
145 _aligned_free(emutls_mutex);
148 if (emutls_tls_index != TLS_OUT_OF_INDEXES) {
149 emutls_shutdown((emutls_address_array*)TlsGetValue(emutls_tls_index));
150 TlsFree(emutls_tls_index);
151 emutls_tls_index = TLS_OUT_OF_INDEXES;
155 #pragma warning (push)
156 #pragma warning (disable : 4100)
157 static BOOL CALLBACK emutls_init(PINIT_ONCE p0, PVOID p1, PVOID *p2) {
158 emutls_mutex = (LPCRITICAL_SECTION)_aligned_malloc(sizeof(CRITICAL_SECTION), 16);
160 win_error(GetLastError(), "_aligned_malloc");
163 InitializeCriticalSection(emutls_mutex);
165 emutls_tls_index = TlsAlloc();
166 if (emutls_tls_index == TLS_OUT_OF_INDEXES) {
168 win_error(GetLastError(), "TlsAlloc");
171 atexit(&emutls_exit);
175 static __inline void emutls_init_once(void) {
176 static INIT_ONCE once;
177 InitOnceExecuteOnce(&once, emutls_init, NULL, NULL);
180 static __inline void emutls_lock() {
181 EnterCriticalSection(emutls_mutex);
184 static __inline void emutls_unlock() {
185 LeaveCriticalSection(emutls_mutex);
188 static __inline void emutls_setspecific(emutls_address_array *value) {
189 if (TlsSetValue(emutls_tls_index, (LPVOID) value) == 0)
190 win_abort(GetLastError(), "TlsSetValue");
193 static __inline emutls_address_array* emutls_getspecific() {
194 LPVOID value = TlsGetValue(emutls_tls_index);
196 const DWORD err = GetLastError();
197 if (err != ERROR_SUCCESS)
198 win_abort(err, "TlsGetValue");
200 return (emutls_address_array*) value;
203 /* Provide atomic load/store functions for emutls_get_index if built with MSVC.
205 #if !defined(__ATOMIC_RELEASE)
207 enum { __ATOMIC_ACQUIRE = 2, __ATOMIC_RELEASE = 3 };
209 static __inline uintptr_t __atomic_load_n(void *ptr, unsigned type) {
210 assert(type == __ATOMIC_ACQUIRE);
212 return (uintptr_t) _load_be_u64(ptr);
214 return (uintptr_t) _load_be_u32(ptr);
218 static __inline void __atomic_store_n(void *ptr, uintptr_t val, unsigned type) {
219 assert(type == __ATOMIC_RELEASE);
221 _store_be_u64(ptr, val);
223 _store_be_u32(ptr, val);
229 #pragma warning (pop)
233 static size_t emutls_num_object = 0; /* number of allocated TLS objects */
235 /* Free the allocated TLS data
237 static void emutls_shutdown(emutls_address_array *array) {
240 for (i = 0; i < array->size; ++i) {
242 emutls_memalign_free(array->data[i]);
247 /* For every TLS variable xyz,
248 * there is one __emutls_control variable named __emutls_v.xyz.
249 * If xyz has non-zero initial value, __emutls_v.xyz's "value"
250 * will point to __emutls_t.xyz, which has the initial value.
252 typedef struct __emutls_control {
253 /* Must use gcc_word here, instead of size_t, to match GCC. When
254 gcc_word is larger than size_t, the upper extra bits are all
255 zeros. We can use variables of size_t to operate on size and
257 gcc_word size; /* size of the object in bytes */
258 gcc_word align; /* alignment of the object in bytes */
260 uintptr_t index; /* data[index-1] is the object address */
261 void* address; /* object address, when in single thread env */
263 void* value; /* null or non-zero initial value for the object */
266 /* Emulated TLS objects are always allocated at run-time. */
267 static __inline void *emutls_allocate_object(__emutls_control *control) {
268 /* Use standard C types, check with gcc's emutls.o. */
269 COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(gcc_pointer));
270 COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(void*));
272 size_t size = control->size;
273 size_t align = control->align;
275 if (align < sizeof(void*))
276 align = sizeof(void*);
277 /* Make sure that align is power of 2. */
278 if ((align & (align - 1)) != 0)
281 base = emutls_memalign_alloc(align, size);
283 memcpy(base, control->value, size);
285 memset(base, 0, size);
290 /* Returns control->object.index; set index if not allocated yet. */
291 static __inline uintptr_t emutls_get_index(__emutls_control *control) {
292 uintptr_t index = __atomic_load_n(&control->object.index, __ATOMIC_ACQUIRE);
296 index = control->object.index;
298 index = ++emutls_num_object;
299 __atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE);
306 /* Updates newly allocated thread local emutls_address_array. */
307 static __inline void emutls_check_array_set_size(emutls_address_array *array,
312 emutls_setspecific(array);
315 /* Returns the new 'data' array size, number of elements,
316 * which must be no smaller than the given index.
318 static __inline uintptr_t emutls_new_data_array_size(uintptr_t index) {
319 /* Need to allocate emutls_address_array with one extra slot
320 * to store the data array size.
321 * Round up the emutls_address_array size to multiple of 16.
323 return ((index + 1 + 15) & ~((uintptr_t)15)) - 1;
326 /* Returns the size in bytes required for an emutls_address_array with
327 * N number of elements for data field.
329 static __inline uintptr_t emutls_asize(uintptr_t N) {
330 return N * sizeof(void *) + sizeof(emutls_address_array);
333 /* Returns the thread local emutls_address_array.
334 * Extends its size if necessary to hold address at index.
336 static __inline emutls_address_array *
337 emutls_get_address_array(uintptr_t index) {
338 emutls_address_array* array = emutls_getspecific();
340 uintptr_t new_size = emutls_new_data_array_size(index);
341 array = (emutls_address_array*) malloc(emutls_asize(new_size));
343 memset(array->data, 0, new_size * sizeof(void*));
344 emutls_check_array_set_size(array, new_size);
345 } else if (index > array->size) {
346 uintptr_t orig_size = array->size;
347 uintptr_t new_size = emutls_new_data_array_size(index);
348 array = (emutls_address_array*) realloc(array, emutls_asize(new_size));
350 memset(array->data + orig_size, 0,
351 (new_size - orig_size) * sizeof(void*));
352 emutls_check_array_set_size(array, new_size);
357 void* __emutls_get_address(__emutls_control* control) {
358 uintptr_t index = emutls_get_index(control);
359 emutls_address_array* array = emutls_get_address_array(index--);
360 if (array->data[index] == NULL)
361 array->data[index] = emutls_allocate_object(control);
362 return array->data[index];