blob: 5841910fb0e681e1f890e183c84ca1b520398953 [file] [log] [blame]
Chih-Hung Hsieh4814b9c2015-08-31 17:14:07 +00001/* ===---------- emutls.c - Implements __emutls_get_address ---------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
7 *
8 * ===----------------------------------------------------------------------===
9 */
10#include <pthread.h>
11#include <stdint.h>
12#include <stdlib.h>
13#include <string.h>
14
15#include "int_util.h"
16
17/* Default is not to use posix_memalign, so systems like Android
18 * can use thread local data without heavier POSIX memory allocators.
19 */
20#ifndef EMUTLS_USE_POSIX_MEMALIGN
21#define EMUTLS_USE_POSIX_MEMALIGN 0
22#endif
23
24/* For every TLS variable xyz,
25 * there is one __emutls_control variable named __emutls_v.xyz.
26 * If xyz has non-zero initial value, __emutls_v.xyz's "value"
27 * will point to __emutls_t.xyz, which has the initial value.
28 */
29typedef struct __emutls_control {
30 size_t size; /* size of the object in bytes */
31 size_t align; /* alignment of the object in bytes */
32 union {
33 uintptr_t index; /* data[index-1] is the object address */
34 void* address; /* object address, when in single thread env */
35 } object;
36 void* value; /* null or non-zero initial value for the object */
37} __emutls_control;
38
39static inline void* emutls_memalign_alloc(size_t align, size_t size) {
40 void *base;
41#if EMUTLS_USE_POSIX_MEMALIGN
42 if (posix_memalign(&base, align, size) != 0)
43 abort();
44#else
45 #define EXTRA_ALIGN_PTR_BYTES (align - 1 + sizeof(void*))
46 char* object;
47 if ((object = malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL)
48 abort();
49 base = (void*)(((uintptr_t)(object + EXTRA_ALIGN_PTR_BYTES))
50 & ~(uintptr_t)(align - 1));
51
52 ((void**)base)[-1] = object;
53#endif
54 return base;
55}
56
57static inline void emutls_memalign_free(void* base) {
58#if EMUTLS_USE_POSIX_MEMALIGN
59 free(base);
60#else
61 /* The mallocated address is in ((void**)base)[-1] */
62 free(((void**)base)[-1]);
63#endif
64}
65
66/* Emulated TLS objects are always allocated at run-time. */
67static inline void* emutls_allocate_object(__emutls_control* control) {
68 /* Use standard C types, check with gcc's emutls.o. */
69 typedef unsigned int gcc_word __attribute__((mode(word)));
70 typedef unsigned int gcc_pointer __attribute__((mode(pointer)));
71 COMPILE_TIME_ASSERT(sizeof(size_t) == sizeof(gcc_word));
72 COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(gcc_pointer));
73 COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(void*));
74
75 size_t size = control->size;
76 size_t align = control->align;
77 if (align < sizeof(void*))
78 align = sizeof(void*);
79 /* Make sure that align is power of 2. */
80 if ((align & (align - 1)) != 0)
81 abort();
82
83 void* base = emutls_memalign_alloc(align, size);
84 if (control->value)
85 memcpy(base, control->value, size);
86 else
87 memset(base, 0, size);
88 return base;
89}
90
91static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER;
92
93static size_t emutls_num_object = 0; /* number of allocated TLS objects */
94
95typedef struct emutls_address_array {
96 uintptr_t size; /* number of elements in the 'data' array */
97 void* data[];
98} emutls_address_array;
99
100static pthread_key_t emutls_pthread_key;
101
102static void emutls_key_destructor(void* ptr) {
103 emutls_address_array* array = (emutls_address_array*)ptr;
104 uintptr_t i;
105 for (i = 0; i < array->size; ++i) {
106 if (array->data[i])
107 emutls_memalign_free(array->data[i]);
108 }
109 free(ptr);
110}
111
112static void emutls_init(void) {
113 if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0)
114 abort();
115}
116
117/* Returns control->object.index; set index if not allocated yet. */
118static inline uintptr_t emutls_get_index(__emutls_control* control) {
119 uintptr_t index = __atomic_load_n(&control->object.index, __ATOMIC_ACQUIRE);
120 if (!index) {
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 index = ++emutls_num_object;
127 __atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE);
128 }
129 pthread_mutex_unlock(&emutls_mutex);
130 }
131 return index;
132}
133
134/* Updates newly allocated thread local emutls_address_array. */
135static inline void emutls_check_array_set_size(emutls_address_array* array,
136 uintptr_t size) {
137 if (array == NULL)
138 abort();
139 array->size = size;
140 pthread_setspecific(emutls_pthread_key, (void*)array);
141}
142
143/* Returns the new 'data' array size, number of elements,
144 * which must be no smaller than the given index.
145 */
146static inline uintptr_t emutls_new_data_array_size(uintptr_t index) {
147 /* Need to allocate emutls_address_array with one extra slot
148 * to store the data array size.
149 * Round up the emutls_address_array size to multiple of 16.
150 */
151 return ((index + 1 + 15) & ~((uintptr_t)15)) - 1;
152}
153
154/* Returns the thread local emutls_address_array.
155 * Extends its size if necessary to hold address at index.
156 */
157static inline emutls_address_array* emutls_get_address_array(uintptr_t index) {
158 emutls_address_array* array = pthread_getspecific(emutls_pthread_key);
159 if (array == NULL) {
160 uintptr_t new_size = emutls_new_data_array_size(index);
161 array = calloc(new_size + 1, sizeof(void*));
162 emutls_check_array_set_size(array, new_size);
163 } else if (index > array->size) {
164 uintptr_t orig_size = array->size;
165 uintptr_t new_size = emutls_new_data_array_size(index);
166 array = realloc(array, (new_size + 1) * sizeof(void*));
167 if (array)
168 memset(array->data + orig_size, 0,
169 (new_size - orig_size) * sizeof(void*));
170 emutls_check_array_set_size(array, new_size);
171 }
172 return array;
173}
174
175void* __emutls_get_address(__emutls_control* control) {
176 uintptr_t index = emutls_get_index(control);
177 emutls_address_array* array = emutls_get_address_array(index);
178 if (array->data[index - 1] == NULL)
179 array->data[index - 1] = emutls_allocate_object(control);
180 return array->data[index - 1];
181}