blob: 49b72e9deaec6fb268a9e13f62e8ea92cc3824e7 [file] [log] [blame]
Elliott Hughes44b53ad2013-02-11 20:18:47 +00001/*
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 */
28
Dan Albert75ef63d2014-11-21 00:18:07 -080029#include <errno.h>
Elliott Hughes44b53ad2013-02-11 20:18:47 +000030#include <pthread.h>
Elliott Hughes44b53ad2013-02-11 20:18:47 +000031
Elliott Hugheseb847bc2013-10-09 15:50:50 -070032#include "private/bionic_tls.h"
Elliott Hughes44b53ad2013-02-11 20:18:47 +000033#include "pthread_internal.h"
Elliott Hughes44b53ad2013-02-11 20:18:47 +000034
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 */
61
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)))
66
67static inline bool IsValidUserKey(pthread_key_t key) {
68 return (key >= TLS_SLOT_FIRST_USER_SLOT && key < BIONIC_TLS_SLOTS);
69}
70
71typedef void (*key_destructor_t)(void*);
72
73struct tls_map_t {
74 bool is_initialized;
75
76 /* bitmap of allocated keys */
77 uint32_t map[TLSMAP_WORDS];
78
79 key_destructor_t key_destructors[BIONIC_TLS_SLOTS];
80};
81
82class ScopedTlsMapAccess {
83 public:
84 ScopedTlsMapAccess() {
85 Lock();
86
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 }
100
101 ~ScopedTlsMapAccess() {
102 Unlock();
103 }
104
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 }
114
115 // We hit PTHREAD_KEYS_MAX. POSIX says EAGAIN for this case.
116 return EAGAIN;
117 }
118
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 }
123
124 bool IsInUse(pthread_key_t key) {
125 return (TLSMAP_WORD(s_tls_map_, key) & TLSMAP_MASK(key)) != 0;
126 }
127
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 }
132
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() {
Elliott Hughes2a0b8732013-10-08 18:50:24 -0700137 void** tls = __get_tls();
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000138
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];
147
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
152
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;
157
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 }
168
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 }
175
176 private:
177 static tls_map_t s_tls_map_;
178 static pthread_mutex_t s_tls_map_lock_;
179
180 void Lock() {
181 pthread_mutex_lock(&s_tls_map_lock_);
182 }
183
184 void Unlock() {
185 pthread_mutex_unlock(&s_tls_map_lock_);
186 }
187};
188
Elliott Hughesce532722013-03-15 16:31:09 -0700189__LIBC_HIDDEN__ tls_map_t ScopedTlsMapAccess::s_tls_map_;
190__LIBC_HIDDEN__ pthread_mutex_t ScopedTlsMapAccess::s_tls_map_lock_;
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000191
192__LIBC_HIDDEN__ void pthread_key_clean_all() {
193 ScopedTlsMapAccess tls_map;
194 tls_map.CleanAll();
195}
196
197int pthread_key_create(pthread_key_t* key, void (*key_destructor)(void*)) {
198 ScopedTlsMapAccess tls_map;
199 return tls_map.CreateKey(key, key_destructor);
200}
201
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.
206int pthread_key_delete(pthread_key_t key) {
207 ScopedTlsMapAccess tls_map;
208
209 if (!IsValidUserKey(key) || !tls_map.IsInUse(key)) {
210 return EINVAL;
211 }
212
213 // Clear value in all threads.
Elliott Hughes1728b232014-05-14 10:02:03 -0700214 pthread_mutex_lock(&g_thread_list_lock);
215 for (pthread_internal_t* t = g_thread_list; t != NULL; t = t->next) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000216 t->tls[key] = NULL;
217 }
218 tls_map.DeleteKey(key);
219
Elliott Hughes1728b232014-05-14 10:02:03 -0700220 pthread_mutex_unlock(&g_thread_list_lock);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000221 return 0;
222}
223
224void* pthread_getspecific(pthread_key_t key) {
225 if (!IsValidUserKey(key)) {
226 return NULL;
227 }
228
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.
Elliott Hughes2a0b8732013-10-08 18:50:24 -0700233 return __get_tls()[key];
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000234}
235
236int pthread_setspecific(pthread_key_t key, const void* ptr) {
237 ScopedTlsMapAccess tls_map;
238
239 if (!IsValidUserKey(key) || !tls_map.IsInUse(key)) {
240 return EINVAL;
241 }
242
Elliott Hughes2a0b8732013-10-08 18:50:24 -0700243 __get_tls()[key] = const_cast<void*>(ptr);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000244 return 0;
245}