1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
29 #include <errno.h>
30 #include <pthread.h>
32 #include "private/bionic_tls.h"
33 #include "pthread_internal.h"
35 /* A technical note regarding our thread-local-storage (TLS) implementation:
36 *
37 * There can be up to BIONIC_TLS_SLOTS independent TLS keys in a given process,
38 * The keys below TLS_SLOT_FIRST_USER_SLOT are reserved for Bionic to hold
39 * special thread-specific variables like errno or a pointer to
40 * the current thread's descriptor. These entries cannot be accessed through
41 * pthread_getspecific() / pthread_setspecific() or pthread_key_delete()
42 *
43 * The 'tls_map_t' type defined below implements a shared global map of
44 * currently created/allocated TLS keys and the destructors associated
45 * with them.
46 *
47 * The global TLS map simply contains a bitmap of allocated keys, and
48 * an array of destructors.
49 *
50 * Each thread has a TLS area that is a simple array of BIONIC_TLS_SLOTS void*
51 * pointers. the TLS area of the main thread is stack-allocated in
52 * __libc_init_common, while the TLS area of other threads is placed at
53 * the top of their stack in pthread_create.
54 *
55 * When pthread_key_delete() is called it will erase the key's bitmap bit
56 * and its destructor, and will also clear the key data in the TLS area of
57 * all created threads. As mandated by Posix, it is the responsibility of
58 * the caller of pthread_key_delete() to properly reclaim the objects that
59 * were pointed to by these data fields (either before or after the call).
60 */
62 #define TLSMAP_BITS 32
63 #define TLSMAP_WORDS ((BIONIC_TLS_SLOTS+TLSMAP_BITS-1)/TLSMAP_BITS)
64 #define TLSMAP_WORD(m,k) (m).map[(k)/TLSMAP_BITS]
65 #define TLSMAP_MASK(k) (1U << ((k)&(TLSMAP_BITS-1)))
67 static inline bool IsValidUserKey(pthread_key_t key) {
68 return (key >= TLS_SLOT_FIRST_USER_SLOT && key < BIONIC_TLS_SLOTS);
69 }
71 typedef void (*key_destructor_t)(void*);
73 struct tls_map_t {
74 bool is_initialized;
76 /* bitmap of allocated keys */
77 uint32_t map[TLSMAP_WORDS];
79 key_destructor_t key_destructors[BIONIC_TLS_SLOTS];
80 };
82 class ScopedTlsMapAccess {
83 public:
84 ScopedTlsMapAccess() {
85 Lock();
87 // If this is the first time the TLS map has been accessed,
88 // mark the slots belonging to well-known keys as being in use.
89 // This isn't currently necessary because the well-known keys
90 // can only be accessed directly by bionic itself, do not have
91 // destructors, and all the functions that touch the TLS map
92 // start after the maximum well-known slot.
93 if (!s_tls_map_.is_initialized) {
94 for (pthread_key_t key = 0; key < TLS_SLOT_FIRST_USER_SLOT; ++key) {
95 SetInUse(key, NULL);
96 }
97 s_tls_map_.is_initialized = true;
98 }
99 }
101 ~ScopedTlsMapAccess() {
102 Unlock();
103 }
105 int CreateKey(pthread_key_t* result, void (*key_destructor)(void*)) {
106 // Take the first unallocated key.
107 for (int key = 0; key < BIONIC_TLS_SLOTS; ++key) {
108 if (!IsInUse(key)) {
109 SetInUse(key, key_destructor);
110 *result = key;
111 return 0;
112 }
113 }
115 // We hit PTHREAD_KEYS_MAX. POSIX says EAGAIN for this case.
116 return EAGAIN;
117 }
119 void DeleteKey(pthread_key_t key) {
120 TLSMAP_WORD(s_tls_map_, key) &= ~TLSMAP_MASK(key);
121 s_tls_map_.key_destructors[key] = NULL;
122 }
124 bool IsInUse(pthread_key_t key) {
125 return (TLSMAP_WORD(s_tls_map_, key) & TLSMAP_MASK(key)) != 0;
126 }
128 void SetInUse(pthread_key_t key, void (*key_destructor)(void*)) {
129 TLSMAP_WORD(s_tls_map_, key) |= TLSMAP_MASK(key);
130 s_tls_map_.key_destructors[key] = key_destructor;
131 }
133 // Called from pthread_exit() to remove all TLS key data
134 // from this thread's TLS area. This must call the destructor of all keys
135 // that have a non-NULL data value and a non-NULL destructor.
136 void CleanAll() {
137 void** tls = __get_tls();
139 // Because destructors can do funky things like deleting/creating other
140 // keys, we need to implement this in a loop.
141 for (int rounds = PTHREAD_DESTRUCTOR_ITERATIONS; rounds > 0; --rounds) {
142 size_t called_destructor_count = 0;
143 for (int key = 0; key < BIONIC_TLS_SLOTS; ++key) {
144 if (IsInUse(key)) {
145 void* data = tls[key];
146 void (*key_destructor)(void*) = s_tls_map_.key_destructors[key];
148 if (data != NULL && key_destructor != NULL) {
149 // we need to clear the key data now, this will prevent the
150 // destructor (or a later one) from seeing the old value if
151 // it calls pthread_getspecific() for some odd reason
153 // we do not do this if 'key_destructor == NULL' just in case another
154 // destructor function might be responsible for manually
155 // releasing the corresponding data.
156 tls[key] = NULL;
158 // because the destructor is free to call pthread_key_create
159 // and/or pthread_key_delete, we need to temporarily unlock
160 // the TLS map
161 Unlock();
162 (*key_destructor)(data);
163 Lock();
164 ++called_destructor_count;
165 }
166 }
167 }
169 // If we didn't call any destructors, there is no need to check the TLS data again.
170 if (called_destructor_count == 0) {
171 break;
172 }
173 }
174 }
176 private:
177 static tls_map_t s_tls_map_;
178 static pthread_mutex_t s_tls_map_lock_;
180 void Lock() {
181 pthread_mutex_lock(&s_tls_map_lock_);
182 }
184 void Unlock() {
185 pthread_mutex_unlock(&s_tls_map_lock_);
186 }
187 };
189 __LIBC_HIDDEN__ tls_map_t ScopedTlsMapAccess::s_tls_map_;
190 __LIBC_HIDDEN__ pthread_mutex_t ScopedTlsMapAccess::s_tls_map_lock_;
192 __LIBC_HIDDEN__ void pthread_key_clean_all() {
193 ScopedTlsMapAccess tls_map;
194 tls_map.CleanAll();
195 }
197 int pthread_key_create(pthread_key_t* key, void (*key_destructor)(void*)) {
198 ScopedTlsMapAccess tls_map;
199 return tls_map.CreateKey(key, key_destructor);
200 }
202 // Deletes a pthread_key_t. note that the standard mandates that this does
203 // not call the destructors for non-NULL key values. Instead, it is the
204 // responsibility of the caller to properly dispose of the corresponding data
205 // and resources, using any means it finds suitable.
206 int pthread_key_delete(pthread_key_t key) {
207 ScopedTlsMapAccess tls_map;
209 if (!IsValidUserKey(key) || !tls_map.IsInUse(key)) {
210 return EINVAL;
211 }
213 // Clear value in all threads.
214 pthread_mutex_lock(&g_thread_list_lock);
215 for (pthread_internal_t* t = g_thread_list; t != NULL; t = t->next) {
216 t->tls[key] = NULL;
217 }
218 tls_map.DeleteKey(key);
220 pthread_mutex_unlock(&g_thread_list_lock);
221 return 0;
222 }
224 void* pthread_getspecific(pthread_key_t key) {
225 if (!IsValidUserKey(key)) {
226 return NULL;
227 }
229 // For performance reasons, we do not lock/unlock the global TLS map
230 // to check that the key is properly allocated. If the key was not
231 // allocated, the value read from the TLS should always be NULL
232 // due to pthread_key_delete() clearing the values for all threads.
233 return __get_tls()[key];
234 }
236 int pthread_setspecific(pthread_key_t key, const void* ptr) {
237 ScopedTlsMapAccess tls_map;
239 if (!IsValidUserKey(key) || !tls_map.IsInUse(key)) {
240 return EINVAL;
241 }
243 __get_tls()[key] = const_cast<void*>(ptr);
244 return 0;
245 }