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 * ===----------------------------------------------------------------------===
18 /* Default is not to use posix_memalign, so systems like Android
19 * can use thread local data without heavier POSIX memory allocators.
21 #ifndef EMUTLS_USE_POSIX_MEMALIGN
22 #define EMUTLS_USE_POSIX_MEMALIGN 0
25 /* For every TLS variable xyz,
26 * there is one __emutls_control variable named __emutls_v.xyz.
27 * If xyz has non-zero initial value, __emutls_v.xyz's "value"
28 * will point to __emutls_t.xyz, which has the initial value.
30 typedef unsigned int gcc_word __attribute__((mode(word)));
31 typedef struct __emutls_control {
32 /* Must use gcc_word here, instead of size_t, to match GCC. When
33 gcc_word is larger than size_t, the upper extra bits are all
34 zeros. We can use variables of size_t to operate on size and
36 gcc_word size; /* size of the object in bytes */
37 gcc_word align; /* alignment of the object in bytes */
39 uintptr_t index; /* data[index-1] is the object address */
40 void* address; /* object address, when in single thread env */
42 void* value; /* null or non-zero initial value for the object */
45 static __inline void *emutls_memalign_alloc(size_t align, size_t size) {
47 #if EMUTLS_USE_POSIX_MEMALIGN
48 if (posix_memalign(&base, align, size) != 0)
51 #define EXTRA_ALIGN_PTR_BYTES (align - 1 + sizeof(void*))
53 if ((object = malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL)
55 base = (void*)(((uintptr_t)(object + EXTRA_ALIGN_PTR_BYTES))
56 & ~(uintptr_t)(align - 1));
58 ((void**)base)[-1] = object;
63 static __inline void emutls_memalign_free(void *base) {
64 #if EMUTLS_USE_POSIX_MEMALIGN
67 /* The mallocated address is in ((void**)base)[-1] */
68 free(((void**)base)[-1]);
72 /* Emulated TLS objects are always allocated at run-time. */
73 static __inline void *emutls_allocate_object(__emutls_control *control) {
74 /* Use standard C types, check with gcc's emutls.o. */
75 typedef unsigned int gcc_pointer __attribute__((mode(pointer)));
76 COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(gcc_pointer));
77 COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(void*));
79 size_t size = control->size;
80 size_t align = control->align;
82 if (align < sizeof(void*))
83 align = sizeof(void*);
84 /* Make sure that align is power of 2. */
85 if ((align & (align - 1)) != 0)
88 base = emutls_memalign_alloc(align, size);
90 memcpy(base, control->value, size);
92 memset(base, 0, size);
96 static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER;
98 static size_t emutls_num_object = 0; /* number of allocated TLS objects */
100 typedef struct emutls_address_array {
101 uintptr_t size; /* number of elements in the 'data' array */
103 } emutls_address_array;
105 static pthread_key_t emutls_pthread_key;
107 static void emutls_key_destructor(void* ptr) {
108 emutls_address_array* array = (emutls_address_array*)ptr;
110 for (i = 0; i < array->size; ++i) {
112 emutls_memalign_free(array->data[i]);
117 static void emutls_init(void) {
118 if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0)
122 /* Returns control->object.index; set index if not allocated yet. */
123 static __inline uintptr_t emutls_get_index(__emutls_control *control) {
124 uintptr_t index = __atomic_load_n(&control->object.index, __ATOMIC_ACQUIRE);
126 static pthread_once_t once = PTHREAD_ONCE_INIT;
127 pthread_once(&once, emutls_init);
128 pthread_mutex_lock(&emutls_mutex);
129 index = control->object.index;
131 index = ++emutls_num_object;
132 __atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE);
134 pthread_mutex_unlock(&emutls_mutex);
139 /* Updates newly allocated thread local emutls_address_array. */
140 static __inline void emutls_check_array_set_size(emutls_address_array *array,
145 pthread_setspecific(emutls_pthread_key, (void*)array);
148 /* Returns the new 'data' array size, number of elements,
149 * which must be no smaller than the given index.
151 static __inline uintptr_t emutls_new_data_array_size(uintptr_t index) {
152 /* Need to allocate emutls_address_array with one extra slot
153 * to store the data array size.
154 * Round up the emutls_address_array size to multiple of 16.
156 return ((index + 1 + 15) & ~((uintptr_t)15)) - 1;
159 /* Returns the thread local emutls_address_array.
160 * Extends its size if necessary to hold address at index.
162 static __inline emutls_address_array *
163 emutls_get_address_array(uintptr_t index) {
164 emutls_address_array* array = pthread_getspecific(emutls_pthread_key);
166 uintptr_t new_size = emutls_new_data_array_size(index);
167 array = malloc(new_size * sizeof(void *) + sizeof(emutls_address_array));
169 memset(array->data, 0, new_size * sizeof(void*));
170 emutls_check_array_set_size(array, new_size);
171 } else if (index > array->size) {
172 uintptr_t orig_size = array->size;
173 uintptr_t new_size = emutls_new_data_array_size(index);
174 array = realloc(array, new_size * sizeof(void *) + sizeof(emutls_address_array));
176 memset(array->data + orig_size, 0,
177 (new_size - orig_size) * sizeof(void*));
178 emutls_check_array_set_size(array, new_size);
183 void* __emutls_get_address(__emutls_control* control) {
184 uintptr_t index = emutls_get_index(control);
185 emutls_address_array* array = emutls_get_address_array(index);
186 if (array->data[index - 1] == NULL)
187 array->data[index - 1] = emutls_allocate_object(control);
188 return array->data[index - 1];