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);
106 static LPCRITICAL_SECTION emutls_mutex;
107 static DWORD emutls_tls_index = TLS_OUT_OF_INDEXES;
109 typedef uintptr_t gcc_word;
110 typedef void * gcc_pointer;
112 static void win_error(DWORD last_err, const char *hint) {
114 if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
115 FORMAT_MESSAGE_FROM_SYSTEM |
116 FORMAT_MESSAGE_MAX_WIDTH_MASK,
117 NULL, last_err, 0, (LPSTR)&buffer, 1, NULL)) {
118 fprintf(stderr, "Windows error: %s\n", buffer);
120 fprintf(stderr, "Unkown Windows error: %s\n", hint);
125 static __inline void win_abort(DWORD last_err, const char *hint) {
126 win_error(last_err, hint);
130 static __inline void *emutls_memalign_alloc(size_t align, size_t size) {
131 void *base = _aligned_malloc(size, align);
133 win_abort(GetLastError(), "_aligned_malloc");
137 static __inline void emutls_memalign_free(void *base) {
141 static void emutls_exit(void) {
143 DeleteCriticalSection(emutls_mutex);
144 _aligned_free(emutls_mutex);
147 if (emutls_tls_index != TLS_OUT_OF_INDEXES) {
148 emutls_shutdown((emutls_address_array*)TlsGetValue(emutls_tls_index));
149 TlsFree(emutls_tls_index);
150 emutls_tls_index = TLS_OUT_OF_INDEXES;
154 #pragma warning (push)
155 #pragma warning (disable : 4100)
156 static BOOL CALLBACK emutls_init(PINIT_ONCE p0, PVOID p1, PVOID *p2) {
157 emutls_mutex = (LPCRITICAL_SECTION)_aligned_malloc(sizeof(CRITICAL_SECTION), 16);
159 win_error(GetLastError(), "_aligned_malloc");
162 InitializeCriticalSection(emutls_mutex);
164 emutls_tls_index = TlsAlloc();
165 if (emutls_tls_index == TLS_OUT_OF_INDEXES) {
167 win_error(GetLastError(), "TlsAlloc");
170 atexit(&emutls_exit);
174 static __inline void emutls_init_once(void) {
175 static INIT_ONCE once;
176 InitOnceExecuteOnce(&once, emutls_init, NULL, NULL);
179 static __inline void emutls_lock() {
180 EnterCriticalSection(emutls_mutex);
183 static __inline void emutls_unlock() {
184 LeaveCriticalSection(emutls_mutex);
187 static __inline void emutls_setspecific(emutls_address_array *value) {
188 if (TlsSetValue(emutls_tls_index, (LPVOID) value) == 0)
189 win_abort(GetLastError(), "TlsSetValue");
192 static __inline emutls_address_array* emutls_getspecific() {
193 LPVOID value = TlsGetValue(emutls_tls_index);
195 const DWORD err = GetLastError();
196 if (err != ERROR_SUCCESS)
197 win_abort(err, "TlsGetValue");
199 return (emutls_address_array*) value;
202 /* Provide atomic load/store functions for emutls_get_index if built with MSVC.
204 #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);
211 // These return the previous value - but since we do an OR with 0,
212 // it's equivalent to a plain load.
214 return InterlockedOr64(ptr, 0);
216 return InterlockedOr(ptr, 0);
220 static __inline void __atomic_store_n(void *ptr, uintptr_t val, unsigned type) {
221 assert(type == __ATOMIC_RELEASE);
222 InterlockedExchangePointer((void *volatile *)ptr, (void *)val);
227 #pragma warning (pop)
231 static size_t emutls_num_object = 0; /* number of allocated TLS objects */
233 /* Free the allocated TLS data
235 static void emutls_shutdown(emutls_address_array *array) {
238 for (i = 0; i < array->size; ++i) {
240 emutls_memalign_free(array->data[i]);
245 /* For every TLS variable xyz,
246 * there is one __emutls_control variable named __emutls_v.xyz.
247 * If xyz has non-zero initial value, __emutls_v.xyz's "value"
248 * will point to __emutls_t.xyz, which has the initial value.
250 typedef struct __emutls_control {
251 /* Must use gcc_word here, instead of size_t, to match GCC. When
252 gcc_word is larger than size_t, the upper extra bits are all
253 zeros. We can use variables of size_t to operate on size and
255 gcc_word size; /* size of the object in bytes */
256 gcc_word align; /* alignment of the object in bytes */
258 uintptr_t index; /* data[index-1] is the object address */
259 void* address; /* object address, when in single thread env */
261 void* value; /* null or non-zero initial value for the object */
264 /* Emulated TLS objects are always allocated at run-time. */
265 static __inline void *emutls_allocate_object(__emutls_control *control) {
266 /* Use standard C types, check with gcc's emutls.o. */
267 COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(gcc_pointer));
268 COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(void*));
270 size_t size = control->size;
271 size_t align = control->align;
273 if (align < sizeof(void*))
274 align = sizeof(void*);
275 /* Make sure that align is power of 2. */
276 if ((align & (align - 1)) != 0)
279 base = emutls_memalign_alloc(align, size);
281 memcpy(base, control->value, size);
283 memset(base, 0, size);
288 /* Returns control->object.index; set index if not allocated yet. */
289 static __inline uintptr_t emutls_get_index(__emutls_control *control) {
290 uintptr_t index = __atomic_load_n(&control->object.index, __ATOMIC_ACQUIRE);
294 index = control->object.index;
296 index = ++emutls_num_object;
297 __atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE);
304 /* Updates newly allocated thread local emutls_address_array. */
305 static __inline void emutls_check_array_set_size(emutls_address_array *array,
310 emutls_setspecific(array);
313 /* Returns the new 'data' array size, number of elements,
314 * which must be no smaller than the given index.
316 static __inline uintptr_t emutls_new_data_array_size(uintptr_t index) {
317 /* Need to allocate emutls_address_array with one extra slot
318 * to store the data array size.
319 * Round up the emutls_address_array size to multiple of 16.
321 return ((index + 1 + 15) & ~((uintptr_t)15)) - 1;
324 /* Returns the size in bytes required for an emutls_address_array with
325 * N number of elements for data field.
327 static __inline uintptr_t emutls_asize(uintptr_t N) {
328 return N * sizeof(void *) + sizeof(emutls_address_array);
331 /* Returns the thread local emutls_address_array.
332 * Extends its size if necessary to hold address at index.
334 static __inline emutls_address_array *
335 emutls_get_address_array(uintptr_t index) {
336 emutls_address_array* array = emutls_getspecific();
338 uintptr_t new_size = emutls_new_data_array_size(index);
339 array = (emutls_address_array*) malloc(emutls_asize(new_size));
341 memset(array->data, 0, new_size * sizeof(void*));
342 emutls_check_array_set_size(array, new_size);
343 } else if (index > array->size) {
344 uintptr_t orig_size = array->size;
345 uintptr_t new_size = emutls_new_data_array_size(index);
346 array = (emutls_address_array*) realloc(array, emutls_asize(new_size));
348 memset(array->data + orig_size, 0,
349 (new_size - orig_size) * sizeof(void*));
350 emutls_check_array_set_size(array, new_size);
355 void* __emutls_get_address(__emutls_control* control) {
356 uintptr_t index = emutls_get_index(control);
357 emutls_address_array* array = emutls_get_address_array(index--);
358 if (array->data[index] == NULL)
359 array->data[index] = emutls_allocate_object(control);
360 return array->data[index];