pthread_tsd.c revision 1.15.8.1 1 /* $NetBSD: pthread_tsd.c,v 1.15.8.1 2017/07/24 05:47:59 snj Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams, by Andrew Doran, and by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: pthread_tsd.c,v 1.15.8.1 2017/07/24 05:47:59 snj Exp $");
34
35 /* Functions and structures dealing with thread-specific data */
36 #include <errno.h>
37 #include <sys/mman.h>
38
39 #include "pthread.h"
40 #include "pthread_int.h"
41 #include "reentrant.h"
42
43 int pthread_keys_max;
44 static pthread_mutex_t tsd_mutex = PTHREAD_MUTEX_INITIALIZER;
45 static int nextkey;
46
47 PTQ_HEAD(pthread__tsd_list, pt_specific) *pthread__tsd_list = NULL;
48 void (**pthread__tsd_destructors)(void *) = NULL;
49
50 __strong_alias(__libc_thr_keycreate,pthread_key_create)
51 __strong_alias(__libc_thr_keydelete,pthread_key_delete)
52
53 static void
54 /*ARGSUSED*/
55 null_destructor(void *p)
56 {
57 }
58
59 #include <err.h>
60 #include <stdlib.h>
61 #include <stdio.h>
62
63 void *
64 pthread_tsd_init(size_t *tlen)
65 {
66 char *pkm;
67 size_t alen;
68 char *arena;
69
70 if ((pkm = pthread__getenv("PTHREAD_KEYS_MAX")) != NULL) {
71 pthread_keys_max = (int)strtol(pkm, NULL, 0);
72 if (pthread_keys_max < _POSIX_THREAD_KEYS_MAX)
73 pthread_keys_max = _POSIX_THREAD_KEYS_MAX;
74 } else {
75 pthread_keys_max = PTHREAD_KEYS_MAX;
76 }
77
78 /*
79 * Can't use malloc here yet, because malloc will use the fake
80 * libc thread functions to initialize itself, so mmap the space.
81 */
82 *tlen = sizeof(struct __pthread_st)
83 + pthread_keys_max * sizeof(struct pt_specific);
84 alen = *tlen
85 + sizeof(*pthread__tsd_list) * pthread_keys_max
86 + sizeof(*pthread__tsd_destructors) * pthread_keys_max;
87
88 arena = mmap(NULL, alen, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
89 if (arena == MAP_FAILED) {
90 pthread_keys_max = 0;
91 return NULL;
92 }
93
94 pthread__tsd_list = (void *)arena;
95 arena += sizeof(*pthread__tsd_list) * pthread_keys_max;
96 pthread__tsd_destructors = (void *)arena;
97 arena += sizeof(*pthread__tsd_destructors) * pthread_keys_max;
98 return arena;
99 }
100
101 int
102 pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
103 {
104 int i;
105
106 if (__predict_false(__uselibcstub))
107 return __libc_thr_keycreate_stub(key, destructor);
108
109 /* Get a lock on the allocation list */
110 pthread_mutex_lock(&tsd_mutex);
111
112 /* Find an available slot:
113 * The condition for an available slot is one with the destructor
114 * not being NULL. If the desired destructor is NULL we set it to
115 * our own internal destructor to satisfy the non NULL condition.
116 */
117 /* 1. Search from "nextkey" to the end of the list. */
118 for (i = nextkey; i < pthread_keys_max; i++)
119 if (pthread__tsd_destructors[i] == NULL)
120 break;
121
122 if (i == pthread_keys_max) {
123 /* 2. If that didn't work, search from the start
124 * of the list back to "nextkey".
125 */
126 for (i = 0; i < nextkey; i++)
127 if (pthread__tsd_destructors[i] == NULL)
128 break;
129
130 if (i == nextkey) {
131 /* If we didn't find one here, there isn't one
132 * to be found.
133 */
134 pthread_mutex_unlock(&tsd_mutex);
135 return EAGAIN;
136 }
137 }
138
139 /* Got one. */
140 pthread__assert(PTQ_EMPTY(&pthread__tsd_list[i]));
141 pthread__tsd_destructors[i] = destructor ? destructor : null_destructor;
142
143 nextkey = (i + 1) % pthread_keys_max;
144 pthread_mutex_unlock(&tsd_mutex);
145 *key = i;
146
147 return 0;
148 }
149
150 /*
151 * Each thread holds an array of pthread_keys_max pt_specific list
152 * elements. When an element is used it is inserted into the appropriate
153 * key bucket of pthread__tsd_list. This means that ptqe_prev == NULL,
154 * means that the element is not threaded, ptqe_prev != NULL it is
155 * already part of the list. When we set to a NULL value we delete from the
156 * list if it was in the list, and when we set to non-NULL value, we insert
157 * in the list if it was not already there.
158 *
159 * We keep this global array of lists of threads that have called
160 * pthread_set_specific with non-null values, for each key so that
161 * we don't have to check all threads for non-NULL values in
162 * pthread_key_destroy
163 *
164 * We could keep an accounting of the number of specific used
165 * entries per thread, so that we can update pt_havespecific when we delete
166 * the last one, but we don't bother for now
167 */
168 int
169 pthread__add_specific(pthread_t self, pthread_key_t key, const void *value)
170 {
171 struct pt_specific *pt;
172
173 pthread__assert(key >= 0 && key < pthread_keys_max);
174
175 pthread_mutex_lock(&tsd_mutex);
176 pthread__assert(pthread__tsd_destructors[key] != NULL);
177 pt = &self->pt_specific[key];
178 self->pt_havespecific = 1;
179 if (value) {
180 if (pt->pts_next.ptqe_prev == NULL)
181 PTQ_INSERT_HEAD(&pthread__tsd_list[key], pt, pts_next);
182 } else {
183 if (pt->pts_next.ptqe_prev != NULL) {
184 PTQ_REMOVE(&pthread__tsd_list[key], pt, pts_next);
185 pt->pts_next.ptqe_prev = NULL;
186 }
187 }
188 pt->pts_value = __UNCONST(value);
189 pthread_mutex_unlock(&tsd_mutex);
190
191 return 0;
192 }
193
194 int
195 pthread_key_delete(pthread_key_t key)
196 {
197 /*
198 * This is tricky. The standard says of pthread_key_create()
199 * that new keys have the value NULL associated with them in
200 * all threads. According to people who were present at the
201 * standardization meeting, that requirement was written
202 * before pthread_key_delete() was introduced, and not
203 * reconsidered when it was.
204 *
205 * See David Butenhof's article in comp.programming.threads:
206 * Subject: Re: TSD key reusing issue
207 * Message-ID: <u97d8.29$fL6.200 (at) news.cpqcorp.net>
208 * Date: Thu, 21 Feb 2002 09:06:17 -0500
209 * http://groups.google.com/groups?\
210 * hl=en&selm=u97d8.29%24fL6.200%40news.cpqcorp.net
211 *
212 * Given:
213 *
214 * 1: Applications are not required to clear keys in all
215 * threads before calling pthread_key_delete().
216 * 2: Clearing pointers without running destructors is a
217 * memory leak.
218 * 3: The pthread_key_delete() function is expressly forbidden
219 * to run any destructors.
220 *
221 * Option 1: Make this function effectively a no-op and
222 * prohibit key reuse. This is a possible resource-exhaustion
223 * problem given that we have a static storage area for keys,
224 * but having a non-static storage area would make
225 * pthread_setspecific() expensive (might need to realloc the
226 * TSD array).
227 *
228 * Option 2: Ignore the specified behavior of
229 * pthread_key_create() and leave the old values. If an
230 * application deletes a key that still has non-NULL values in
231 * some threads... it's probably a memory leak and hence
232 * incorrect anyway, and we're within our rights to let the
233 * application lose. However, it's possible (if unlikely) that
234 * the application is storing pointers to non-heap data, or
235 * non-pointers that have been wedged into a void pointer, so
236 * we can't entirely write off such applications as incorrect.
237 * This could also lead to running (new) destructors on old
238 * data that was never supposed to be associated with that
239 * destructor.
240 *
241 * Option 3: Follow the specified behavior of
242 * pthread_key_create(). Either pthread_key_create() or
243 * pthread_key_delete() would then have to clear the values in
244 * every thread's slot for that key. In order to guarantee the
245 * visibility of the NULL value in other threads, there would
246 * have to be synchronization operations in both the clearer
247 * and pthread_getspecific(). Putting synchronization in
248 * pthread_getspecific() is a big performance lose. But in
249 * reality, only (buggy) reuse of an old key would require
250 * this synchronization; for a new key, there has to be a
251 * memory-visibility propagating event between the call to
252 * pthread_key_create() and pthread_getspecific() with that
253 * key, so setting the entries to NULL without synchronization
254 * will work, subject to problem (2) above. However, it's kind
255 * of slow.
256 *
257 * Note that the argument in option 3 only applies because we
258 * keep TSD in ordinary memory which follows the pthreads
259 * visibility rules. The visibility rules are not required by
260 * the standard to apply to TSD, so the argument doesn't
261 * apply in general, just to this implementation.
262 */
263
264 /*
265 * We do option 3; we find the list of all pt_specific structures
266 * threaded on the key we are deleting, unthread them, and set the
267 * pointer to NULL. Finally we unthread the entry, freeing it for
268 * further use.
269 *
270 * We don't call the destructor here, it is the responsibility
271 * of the application to cleanup the storage:
272 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/\
273 * pthread_key_delete.html
274 */
275 struct pt_specific *pt;
276
277 if (__predict_false(__uselibcstub))
278 return __libc_thr_keydelete_stub(key);
279
280 pthread__assert(key >= 0 && key < pthread_keys_max);
281
282 pthread_mutex_lock(&tsd_mutex);
283
284 pthread__assert(pthread__tsd_destructors[key] != NULL);
285
286 while ((pt = PTQ_FIRST(&pthread__tsd_list[key])) != NULL) {
287 PTQ_REMOVE(&pthread__tsd_list[key], pt, pts_next);
288 pt->pts_value = NULL;
289 pt->pts_next.ptqe_prev = NULL;
290 }
291
292 pthread__tsd_destructors[key] = NULL;
293 pthread_mutex_unlock(&tsd_mutex);
294
295 return 0;
296 }
297
298 /* Perform thread-exit-time destruction of thread-specific data. */
299 void
300 pthread__destroy_tsd(pthread_t self)
301 {
302 int i, done, iterations;
303 void *val;
304 void (*destructor)(void *);
305
306 if (!self->pt_havespecific)
307 return;
308 pthread_mutex_unlock(&self->pt_lock);
309
310 /* Butenhof, section 5.4.2 (page 167):
311 *
312 * ``Also, Pthreads sets the thread-specific data value for a
313 * key to NULL before calling that key's destructor (passing
314 * the previous value of the key) when a thread terminates [*].
315 * ...
316 * [*] That is, unfortunately, not what the standard
317 * says. This is one of the problems with formal standards -
318 * they say what they say, not what they were intended to
319 * say. Somehow, an error crept in, and the sentence
320 * specifying that "the implementation clears the
321 * thread-specific data value before calling the destructor"
322 * was deleted. Nobody noticed, and the standard was approved
323 * with the error. So the standard says (by omission) that if
324 * you want to write a portable application using
325 * thread-specific data, that will not hang on thread
326 * termination, you must call pthread_setspecific within your
327 * destructor function to change the value to NULL. This would
328 * be silly, and any serious implementation of Pthreads will
329 * violate the standard in this respect. Of course, the
330 * standard will be fixed, probably by the 1003.1n amendment
331 * (assorted corrections to 1003.1c-1995), but that will take
332 * a while.''
333 */
334
335 /* We're not required to try very hard */
336 iterations = PTHREAD_DESTRUCTOR_ITERATIONS;
337 do {
338 done = 1;
339 for (i = 0; i < pthread_keys_max; i++) {
340 struct pt_specific *pt = &self->pt_specific[i];
341 if (pt->pts_next.ptqe_prev == NULL)
342 continue;
343 pthread_mutex_lock(&tsd_mutex);
344
345 if (pt->pts_next.ptqe_prev != NULL) {
346 PTQ_REMOVE(&pthread__tsd_list[i], pt, pts_next);
347 val = pt->pts_value;
348 pt->pts_value = NULL;
349 pt->pts_next.ptqe_prev = NULL;
350 destructor = pthread__tsd_destructors[i];
351 } else
352 destructor = NULL;
353
354 pthread_mutex_unlock(&tsd_mutex);
355 if (destructor != NULL) {
356 done = 0;
357 (*destructor)(val);
358 }
359 }
360 } while (!done && --iterations);
361
362 self->pt_havespecific = 0;
363 pthread_mutex_lock(&self->pt_lock);
364 }
365