xref: /netbsd-src/lib/libpthread/pthread_tsd.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: pthread_tsd.c,v 1.23 2020/06/11 18:42:02 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2007, 2020 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.23 2020/06/11 18:42:02 ad 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 #include "tsd.h"
43 
44 int pthread_keys_max;
45 static pthread_mutex_t tsd_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static int nextkey;
47 
48 PTQ_HEAD(pthread__tsd_list, pt_specific) *pthread__tsd_list = NULL;
49 void (**pthread__tsd_destructors)(void *) = NULL;
50 
51 __strong_alias(__libc_thr_keycreate,pthread_key_create)
52 __strong_alias(__libc_thr_keydelete,pthread_key_delete)
53 
54 static void
55 /*ARGSUSED*/
56 null_destructor(void *p)
57 {
58 }
59 
60 #include <err.h>
61 #include <stdlib.h>
62 #include <stdio.h>
63 
64 static void
65 pthread_tsd_prefork(void)
66 {
67 	pthread_mutex_lock(&tsd_mutex);
68 }
69 
70 static void
71 pthread_tsd_postfork(void)
72 {
73 	pthread_mutex_unlock(&tsd_mutex);
74 }
75 
76 static void
77 pthread_tsd_postfork_child(void)
78 {
79 	pthread_mutex_init(&tsd_mutex, NULL);
80 }
81 
82 void *
83 pthread_tsd_init(size_t *tlen)
84 {
85 	char *pkm;
86 	size_t alen;
87 	char *arena;
88 
89 	pthread_atfork(pthread_tsd_prefork, pthread_tsd_postfork, pthread_tsd_postfork_child);
90 
91 	if ((pkm = pthread__getenv("PTHREAD_KEYS_MAX")) != NULL) {
92 		pthread_keys_max = (int)strtol(pkm, NULL, 0);
93 		if (pthread_keys_max < _POSIX_THREAD_KEYS_MAX)
94 			pthread_keys_max = _POSIX_THREAD_KEYS_MAX;
95 	} else {
96 		pthread_keys_max = PTHREAD_KEYS_MAX;
97 	}
98 
99 	/*
100 	 * Can't use malloc here yet, because malloc will use the fake
101 	 * libc thread functions to initialize itself, so mmap the space.
102 	 */
103 	*tlen = sizeof(struct __pthread_st)
104 	    + pthread_keys_max * sizeof(struct pt_specific);
105 	alen = *tlen
106 	    + sizeof(*pthread__tsd_list) * pthread_keys_max
107 	    + sizeof(*pthread__tsd_destructors) * pthread_keys_max;
108 
109 	arena = mmap(NULL, alen, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
110 	if (arena == MAP_FAILED) {
111 		pthread_keys_max = 0;
112 		return NULL;
113 	}
114 
115 	pthread__tsd_list = (void *)arena;
116 	arena += sizeof(*pthread__tsd_list) * pthread_keys_max;
117 	pthread__tsd_destructors = (void *)arena;
118 	arena += sizeof(*pthread__tsd_destructors) * pthread_keys_max;
119 	return arena;
120 }
121 
122 int
123 pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
124 {
125 	int i;
126 
127 	if (__predict_false(__uselibcstub))
128 		return __libc_thr_keycreate_stub(key, destructor);
129 
130 	/* Get a lock on the allocation list */
131 	pthread_mutex_lock(&tsd_mutex);
132 
133 	/* Find an available slot:
134 	 * The condition for an available slot is one with the destructor
135 	 * not being NULL. If the desired destructor is NULL we set it to
136 	 * our own internal destructor to satisfy the non NULL condition.
137 	 */
138 	/* 1. Search from "nextkey" to the end of the list. */
139 	for (i = nextkey; i < pthread_keys_max; i++)
140 		if (pthread__tsd_destructors[i] == NULL)
141 			break;
142 
143 	if (i == pthread_keys_max) {
144 		/* 2. If that didn't work, search from the start
145 		 *    of the list back to "nextkey".
146 		 */
147 		for (i = 0; i < nextkey; i++)
148 			if (pthread__tsd_destructors[i] == NULL)
149 				break;
150 
151 		if (i == nextkey) {
152 			/* If we didn't find one here, there isn't one
153 			 * to be found.
154 			 */
155 			pthread_mutex_unlock(&tsd_mutex);
156 			return EAGAIN;
157 		}
158 	}
159 
160 	/* Got one. */
161 	pthread__assert(PTQ_EMPTY(&pthread__tsd_list[i]));
162 	pthread__tsd_destructors[i] = destructor ? destructor : null_destructor;
163 
164 	nextkey = (i + 1) % pthread_keys_max;
165 	pthread_mutex_unlock(&tsd_mutex);
166 	*key = i;
167 
168 	return 0;
169 }
170 
171 /*
172  * Each thread holds an array of pthread_keys_max pt_specific list
173  * elements. When an element is used it is inserted into the appropriate
174  * key bucket of pthread__tsd_list. This means that ptqe_prev == NULL,
175  * means that the element is not threaded, ptqe_prev != NULL it is
176  * already part of the list. If a key is set to a non-NULL value for the
177  * first time, it is added to the list.
178  *
179  * We keep this global array of lists of threads that have called
180  * pthread_set_specific with non-null values, for each key so that
181  * we don't have to check all threads for non-NULL values in
182  * pthread_key_destroy.
183  *
184  * The assumption here is that a concurrent pthread_key_delete is already
185  * undefined behavior. The mutex is taken only once per thread/key
186  * combination.
187  *
188  * We could keep an accounting of the number of specific used
189  * entries per thread, so that we can update pt_havespecific when we delete
190  * the last one, but we don't bother for now
191  */
192 int
193 pthread__add_specific(pthread_t self, pthread_key_t key, const void *value)
194 {
195 	struct pt_specific *pt;
196 
197 	pthread__assert(key >= 0 && key < pthread_keys_max);
198 
199 	pthread__assert(pthread__tsd_destructors[key] != NULL);
200 	pt = &self->pt_specific[key];
201 	self->pt_havespecific = 1;
202 	if (value && !pt->pts_next.ptqe_prev) {
203 		pthread_mutex_lock(&tsd_mutex);
204 		PTQ_INSERT_HEAD(&pthread__tsd_list[key], pt, pts_next);
205 		pthread_mutex_unlock(&tsd_mutex);
206 	}
207 	pt->pts_value = __UNCONST(value);
208 
209 	return 0;
210 }
211 
212 int
213 pthread_key_delete(pthread_key_t key)
214 {
215 	/*
216 	 * This is tricky.  The standard says of pthread_key_create()
217 	 * that new keys have the value NULL associated with them in
218 	 * all threads.  According to people who were present at the
219 	 * standardization meeting, that requirement was written
220 	 * before pthread_key_delete() was introduced, and not
221 	 * reconsidered when it was.
222 	 *
223 	 * See David Butenhof's article in comp.programming.threads:
224 	 * Subject: Re: TSD key reusing issue
225 	 * Message-ID: <u97d8.29$fL6.200@news.cpqcorp.net>
226 	 * Date: Thu, 21 Feb 2002 09:06:17 -0500
227 	 *	 http://groups.google.com/groups?\
228 	 *	 hl=en&selm=u97d8.29%24fL6.200%40news.cpqcorp.net
229 	 *
230 	 * Given:
231 	 *
232 	 * 1: Applications are not required to clear keys in all
233 	 *    threads before calling pthread_key_delete().
234 	 * 2: Clearing pointers without running destructors is a
235 	 *    memory leak.
236 	 * 3: The pthread_key_delete() function is expressly forbidden
237 	 *    to run any destructors.
238 	 *
239 	 * Option 1: Make this function effectively a no-op and
240 	 * prohibit key reuse. This is a possible resource-exhaustion
241 	 * problem given that we have a static storage area for keys,
242 	 * but having a non-static storage area would make
243 	 * pthread_setspecific() expensive (might need to realloc the
244 	 * TSD array).
245 	 *
246 	 * Option 2: Ignore the specified behavior of
247 	 * pthread_key_create() and leave the old values. If an
248 	 * application deletes a key that still has non-NULL values in
249 	 * some threads... it's probably a memory leak and hence
250 	 * incorrect anyway, and we're within our rights to let the
251 	 * application lose. However, it's possible (if unlikely) that
252 	 * the application is storing pointers to non-heap data, or
253 	 * non-pointers that have been wedged into a void pointer, so
254 	 * we can't entirely write off such applications as incorrect.
255 	 * This could also lead to running (new) destructors on old
256 	 * data that was never supposed to be associated with that
257 	 * destructor.
258 	 *
259 	 * Option 3: Follow the specified behavior of
260 	 * pthread_key_create().  Either pthread_key_create() or
261 	 * pthread_key_delete() would then have to clear the values in
262 	 * every thread's slot for that key. In order to guarantee the
263 	 * visibility of the NULL value in other threads, there would
264 	 * have to be synchronization operations in both the clearer
265 	 * and pthread_getspecific().  Putting synchronization in
266 	 * pthread_getspecific() is a big performance lose.  But in
267 	 * reality, only (buggy) reuse of an old key would require
268 	 * this synchronization; for a new key, there has to be a
269 	 * memory-visibility propagating event between the call to
270 	 * pthread_key_create() and pthread_getspecific() with that
271 	 * key, so setting the entries to NULL without synchronization
272 	 * will work, subject to problem (2) above. However, it's kind
273 	 * of slow.
274 	 *
275 	 * Note that the argument in option 3 only applies because we
276 	 * keep TSD in ordinary memory which follows the pthreads
277 	 * visibility rules. The visibility rules are not required by
278 	 * the standard to apply to TSD, so the argument doesn't
279 	 * apply in general, just to this implementation.
280 	 */
281 
282 	/*
283 	 * We do option 3; we find the list of all pt_specific structures
284 	 * threaded on the key we are deleting, unthread them, and set the
285 	 * pointer to NULL. Finally we unthread the entry, freeing it for
286 	 * further use.
287 	 *
288 	 * We don't call the destructor here, it is the responsibility
289 	 * of the application to cleanup the storage:
290 	 * 	http://pubs.opengroup.org/onlinepubs/9699919799/functions/\
291 	 *	pthread_key_delete.html
292 	 */
293 	struct pt_specific *pt;
294 
295 	if (__predict_false(__uselibcstub))
296 		return __libc_thr_keydelete_stub(key);
297 
298 	pthread__assert(key >= 0 && key < pthread_keys_max);
299 
300 	pthread_mutex_lock(&tsd_mutex);
301 
302 	pthread__assert(pthread__tsd_destructors[key] != NULL);
303 
304 	while ((pt = PTQ_FIRST(&pthread__tsd_list[key])) != NULL) {
305 		PTQ_REMOVE(&pthread__tsd_list[key], pt, pts_next);
306 		pt->pts_value = NULL;
307 		pt->pts_next.ptqe_prev = NULL;
308 	}
309 
310 	pthread__tsd_destructors[key] = NULL;
311 	pthread_mutex_unlock(&tsd_mutex);
312 
313 	return 0;
314 }
315 
316 /* Perform thread-exit-time destruction of thread-specific data. */
317 void
318 pthread__destroy_tsd(pthread_t self)
319 {
320 	int i, done, iterations;
321 	void *val;
322 	void (*destructor)(void *);
323 
324 	if (!self->pt_havespecific)
325 		return;
326 
327 	/* Butenhof, section 5.4.2 (page 167):
328 	 *
329 	 * ``Also, Pthreads sets the thread-specific data value for a
330 	 * key to NULL before calling that key's destructor (passing
331 	 * the previous value of the key) when a thread terminates [*].
332 	 * ...
333 	 * [*] That is, unfortunately, not what the standard
334 	 * says. This is one of the problems with formal standards -
335 	 * they say what they say, not what they were intended to
336 	 * say. Somehow, an error crept in, and the sentence
337 	 * specifying that "the implementation clears the
338 	 * thread-specific data value before calling the destructor"
339 	 * was deleted. Nobody noticed, and the standard was approved
340 	 * with the error. So the standard says (by omission) that if
341 	 * you want to write a portable application using
342 	 * thread-specific data, that will not hang on thread
343 	 * termination, you must call pthread_setspecific within your
344 	 * destructor function to change the value to NULL. This would
345 	 * be silly, and any serious implementation of Pthreads will
346 	 * violate the standard in this respect. Of course, the
347 	 * standard will be fixed, probably by the 1003.1n amendment
348 	 * (assorted corrections to 1003.1c-1995), but that will take
349 	 * a while.''
350 	 */
351 
352 	/* We're not required to try very hard */
353 	iterations = PTHREAD_DESTRUCTOR_ITERATIONS;
354 	do {
355 		done = 1;
356 		for (i = 0; i < pthread_keys_max; i++) {
357 			struct pt_specific *pt = &self->pt_specific[i];
358 			if (pt->pts_next.ptqe_prev == NULL)
359 				continue;
360 			pthread_mutex_lock(&tsd_mutex);
361 
362 			if (pt->pts_next.ptqe_prev != NULL)  {
363 				PTQ_REMOVE(&pthread__tsd_list[i], pt, pts_next);
364 				val = pt->pts_value;
365 				pt->pts_value = NULL;
366 				pt->pts_next.ptqe_prev = NULL;
367 				destructor = pthread__tsd_destructors[i];
368 			} else
369 				destructor = NULL;
370 
371 			pthread_mutex_unlock(&tsd_mutex);
372 			if (destructor != NULL && val != NULL) {
373 				done = 0;
374 				(*destructor)(val);
375 			}
376 		}
377 	} while (!done && --iterations);
378 
379 	self->pt_havespecific = 0;
380 }
381 
382 void
383 pthread__copy_tsd(pthread_t self)
384 {
385 	for (size_t key = 0; key < TSD_KEYS_MAX; key++) {
386 
387 		if (__libc_tsd[key].tsd_inuse == 0)
388 			continue;
389 
390 		pthread__assert(pthread__tsd_destructors[key] == NULL);
391 		pthread__tsd_destructors[key] = __libc_tsd[key].tsd_dtor ?
392 		    __libc_tsd[key].tsd_dtor : null_destructor;
393 		nextkey = (key + 1) % pthread_keys_max;
394 
395 		self->pt_havespecific = 1;
396 		struct pt_specific *pt = &self->pt_specific[key];
397 		pt->pts_value = __libc_tsd[key].tsd_val;
398 		__libc_tsd[key].tsd_inuse = 0;
399 	}
400 }
401