xref: /netbsd-src/lib/libpthread/pthread_tsd.c (revision 568eb77ef2a2b1fbe2457d69c7aa6e1dd3d8de8b)
1*568eb77eSriastradh /*	$NetBSD: pthread_tsd.c,v 1.25 2022/04/10 10:38:33 riastradh Exp $	*/
2bd9a18b7Snathanw 
3bd9a18b7Snathanw /*-
430140ed2Sad  * Copyright (c) 2001, 2007, 2020 The NetBSD Foundation, Inc.
5bd9a18b7Snathanw  * All rights reserved.
6bd9a18b7Snathanw  *
7bd9a18b7Snathanw  * This code is derived from software contributed to The NetBSD Foundation
8fe230c9dSchristos  * by Nathan J. Williams, by Andrew Doran, and by Christos Zoulas.
9bd9a18b7Snathanw  *
10bd9a18b7Snathanw  * Redistribution and use in source and binary forms, with or without
11bd9a18b7Snathanw  * modification, are permitted provided that the following conditions
12bd9a18b7Snathanw  * are met:
13bd9a18b7Snathanw  * 1. Redistributions of source code must retain the above copyright
14bd9a18b7Snathanw  *    notice, this list of conditions and the following disclaimer.
15bd9a18b7Snathanw  * 2. Redistributions in binary form must reproduce the above copyright
16bd9a18b7Snathanw  *    notice, this list of conditions and the following disclaimer in the
17bd9a18b7Snathanw  *    documentation and/or other materials provided with the distribution.
18bd9a18b7Snathanw  *
19bd9a18b7Snathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20bd9a18b7Snathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21bd9a18b7Snathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22bd9a18b7Snathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23bd9a18b7Snathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24bd9a18b7Snathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25bd9a18b7Snathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26bd9a18b7Snathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27bd9a18b7Snathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28bd9a18b7Snathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29bd9a18b7Snathanw  * POSSIBILITY OF SUCH DAMAGE.
30bd9a18b7Snathanw  */
31bd9a18b7Snathanw 
32bd9a18b7Snathanw #include <sys/cdefs.h>
33*568eb77eSriastradh __RCSID("$NetBSD: pthread_tsd.c,v 1.25 2022/04/10 10:38:33 riastradh Exp $");
347adb4107Sriastradh 
357adb4107Sriastradh /* Need to use libc-private names for atomic operations. */
367adb4107Sriastradh #include "../../common/lib/libc/atomic/atomic_op_namespace.h"
37bd9a18b7Snathanw 
38bd9a18b7Snathanw /* Functions and structures dealing with thread-specific data */
39bd9a18b7Snathanw #include <errno.h>
40cb27e655Schristos #include <sys/mman.h>
41bd9a18b7Snathanw 
42bd9a18b7Snathanw #include "pthread.h"
43bd9a18b7Snathanw #include "pthread_int.h"
4471d484f9Schristos #include "reentrant.h"
45939c050dSchristos #include "tsd.h"
46bd9a18b7Snathanw 
47841339f0Smanu int pthread_keys_max;
48bd9a18b7Snathanw static pthread_mutex_t tsd_mutex = PTHREAD_MUTEX_INITIALIZER;
49bd9a18b7Snathanw static int nextkey;
5055f47ec3Schristos 
51841339f0Smanu PTQ_HEAD(pthread__tsd_list, pt_specific) *pthread__tsd_list = NULL;
52841339f0Smanu void (**pthread__tsd_destructors)(void *) = NULL;
53bd9a18b7Snathanw 
__strong_alias(__libc_thr_keycreate,pthread_key_create)54bd9a18b7Snathanw __strong_alias(__libc_thr_keycreate,pthread_key_create)
55bd9a18b7Snathanw __strong_alias(__libc_thr_keydelete,pthread_key_delete)
56bd9a18b7Snathanw 
5755f47ec3Schristos static void
5855f47ec3Schristos /*ARGSUSED*/
5955f47ec3Schristos null_destructor(void *p)
6055f47ec3Schristos {
6155f47ec3Schristos }
6255f47ec3Schristos 
6371d484f9Schristos #include <err.h>
6471d484f9Schristos #include <stdlib.h>
65841339f0Smanu #include <stdio.h>
66841339f0Smanu 
67331480e6Skamil static void
pthread_tsd_prefork(void)68331480e6Skamil pthread_tsd_prefork(void)
69331480e6Skamil {
70331480e6Skamil 	pthread_mutex_lock(&tsd_mutex);
71331480e6Skamil }
72331480e6Skamil 
73331480e6Skamil static void
pthread_tsd_postfork(void)74331480e6Skamil pthread_tsd_postfork(void)
75331480e6Skamil {
76331480e6Skamil 	pthread_mutex_unlock(&tsd_mutex);
77331480e6Skamil }
78331480e6Skamil 
7957360565Sjoerg static void
pthread_tsd_postfork_child(void)8057360565Sjoerg pthread_tsd_postfork_child(void)
8157360565Sjoerg {
8257360565Sjoerg 	pthread_mutex_init(&tsd_mutex, NULL);
8357360565Sjoerg }
8457360565Sjoerg 
85cb27e655Schristos void *
pthread_tsd_init(size_t * tlen)86331480e6Skamil pthread_tsd_init(size_t *tlen)
87841339f0Smanu {
88841339f0Smanu 	char *pkm;
89cb27e655Schristos 	size_t alen;
90cb27e655Schristos 	char *arena;
91841339f0Smanu 
9257360565Sjoerg 	pthread_atfork(pthread_tsd_prefork, pthread_tsd_postfork, pthread_tsd_postfork_child);
93331480e6Skamil 
94cb27e655Schristos 	if ((pkm = pthread__getenv("PTHREAD_KEYS_MAX")) != NULL) {
95841339f0Smanu 		pthread_keys_max = (int)strtol(pkm, NULL, 0);
96841339f0Smanu 		if (pthread_keys_max < _POSIX_THREAD_KEYS_MAX)
97841339f0Smanu 			pthread_keys_max = _POSIX_THREAD_KEYS_MAX;
98841339f0Smanu 	} else {
99841339f0Smanu 		pthread_keys_max = PTHREAD_KEYS_MAX;
100841339f0Smanu 	}
101841339f0Smanu 
102cb27e655Schristos 	/*
103cb27e655Schristos 	 * Can't use malloc here yet, because malloc will use the fake
104cb27e655Schristos 	 * libc thread functions to initialize itself, so mmap the space.
105cb27e655Schristos 	 */
106cb27e655Schristos 	*tlen = sizeof(struct __pthread_st)
107cb27e655Schristos 	    + pthread_keys_max * sizeof(struct pt_specific);
108cb27e655Schristos 	alen = *tlen
109cb27e655Schristos 	    + sizeof(*pthread__tsd_list) * pthread_keys_max
110cb27e655Schristos 	    + sizeof(*pthread__tsd_destructors) * pthread_keys_max;
111841339f0Smanu 
1122d85d43bSpooka 	arena = mmap(NULL, alen, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
113cb27e655Schristos 	if (arena == MAP_FAILED) {
114841339f0Smanu 		pthread_keys_max = 0;
115cb27e655Schristos 		return NULL;
116cb27e655Schristos 	}
117841339f0Smanu 
118cb27e655Schristos 	pthread__tsd_list = (void *)arena;
119cb27e655Schristos 	arena += sizeof(*pthread__tsd_list) * pthread_keys_max;
120cb27e655Schristos 	pthread__tsd_destructors = (void *)arena;
121cb27e655Schristos 	arena += sizeof(*pthread__tsd_destructors) * pthread_keys_max;
122cb27e655Schristos 	return arena;
123841339f0Smanu }
124841339f0Smanu 
125bd9a18b7Snathanw int
pthread_key_create(pthread_key_t * key,void (* destructor)(void *))126bd9a18b7Snathanw pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
127bd9a18b7Snathanw {
128bd9a18b7Snathanw 	int i;
129bd9a18b7Snathanw 
13071d484f9Schristos 	if (__predict_false(__uselibcstub))
13171d484f9Schristos 		return __libc_thr_keycreate_stub(key, destructor);
13271d484f9Schristos 
133bd9a18b7Snathanw 	/* Get a lock on the allocation list */
134bd9a18b7Snathanw 	pthread_mutex_lock(&tsd_mutex);
135bd9a18b7Snathanw 
13655f47ec3Schristos 	/* Find an available slot:
13755f47ec3Schristos 	 * The condition for an available slot is one with the destructor
13855f47ec3Schristos 	 * not being NULL. If the desired destructor is NULL we set it to
13955f47ec3Schristos 	 * our own internal destructor to satisfy the non NULL condition.
14055f47ec3Schristos 	 */
141bd9a18b7Snathanw 	/* 1. Search from "nextkey" to the end of the list. */
142841339f0Smanu 	for (i = nextkey; i < pthread_keys_max; i++)
14355f47ec3Schristos 		if (pthread__tsd_destructors[i] == NULL)
144bd9a18b7Snathanw 			break;
145bd9a18b7Snathanw 
146841339f0Smanu 	if (i == pthread_keys_max) {
147bd9a18b7Snathanw 		/* 2. If that didn't work, search from the start
148bd9a18b7Snathanw 		 *    of the list back to "nextkey".
149bd9a18b7Snathanw 		 */
150bd9a18b7Snathanw 		for (i = 0; i < nextkey; i++)
15155f47ec3Schristos 			if (pthread__tsd_destructors[i] == NULL)
152bd9a18b7Snathanw 				break;
153bd9a18b7Snathanw 
154bd9a18b7Snathanw 		if (i == nextkey) {
155bd9a18b7Snathanw 			/* If we didn't find one here, there isn't one
156bd9a18b7Snathanw 			 * to be found.
157bd9a18b7Snathanw 			 */
158bd9a18b7Snathanw 			pthread_mutex_unlock(&tsd_mutex);
159bd9a18b7Snathanw 			return EAGAIN;
160bd9a18b7Snathanw 		}
161bd9a18b7Snathanw 	}
162bd9a18b7Snathanw 
163bd9a18b7Snathanw 	/* Got one. */
16455f47ec3Schristos 	pthread__assert(PTQ_EMPTY(&pthread__tsd_list[i]));
16555f47ec3Schristos 	pthread__tsd_destructors[i] = destructor ? destructor : null_destructor;
16655f47ec3Schristos 
167841339f0Smanu 	nextkey = (i + 1) % pthread_keys_max;
168bd9a18b7Snathanw 	pthread_mutex_unlock(&tsd_mutex);
169bd9a18b7Snathanw 	*key = i;
170bd9a18b7Snathanw 
171bd9a18b7Snathanw 	return 0;
172bd9a18b7Snathanw }
173bd9a18b7Snathanw 
17455f47ec3Schristos /*
175841339f0Smanu  * Each thread holds an array of pthread_keys_max pt_specific list
17655f47ec3Schristos  * elements. When an element is used it is inserted into the appropriate
17755f47ec3Schristos  * key bucket of pthread__tsd_list. This means that ptqe_prev == NULL,
17855f47ec3Schristos  * means that the element is not threaded, ptqe_prev != NULL it is
1791116ee17Sjoerg  * already part of the list. If a key is set to a non-NULL value for the
1801116ee17Sjoerg  * first time, it is added to the list.
18155f47ec3Schristos  *
18255f47ec3Schristos  * We keep this global array of lists of threads that have called
18355f47ec3Schristos  * pthread_set_specific with non-null values, for each key so that
18455f47ec3Schristos  * we don't have to check all threads for non-NULL values in
1851116ee17Sjoerg  * pthread_key_destroy.
1861116ee17Sjoerg  *
1871116ee17Sjoerg  * The assumption here is that a concurrent pthread_key_delete is already
1881116ee17Sjoerg  * undefined behavior. The mutex is taken only once per thread/key
1891116ee17Sjoerg  * combination.
19055f47ec3Schristos  *
19155f47ec3Schristos  * We could keep an accounting of the number of specific used
19255f47ec3Schristos  * entries per thread, so that we can update pt_havespecific when we delete
19355f47ec3Schristos  * the last one, but we don't bother for now
19455f47ec3Schristos  */
19555f47ec3Schristos int
pthread__add_specific(pthread_t self,pthread_key_t key,const void * value)19655f47ec3Schristos pthread__add_specific(pthread_t self, pthread_key_t key, const void *value)
19755f47ec3Schristos {
19855f47ec3Schristos 	struct pt_specific *pt;
19955f47ec3Schristos 
200841339f0Smanu 	pthread__assert(key >= 0 && key < pthread_keys_max);
20155f47ec3Schristos 
20255f47ec3Schristos 	pthread__assert(pthread__tsd_destructors[key] != NULL);
20355f47ec3Schristos 	pt = &self->pt_specific[key];
20455f47ec3Schristos 	self->pt_havespecific = 1;
2051116ee17Sjoerg 	if (value && !pt->pts_next.ptqe_prev) {
2061116ee17Sjoerg 		pthread_mutex_lock(&tsd_mutex);
20755f47ec3Schristos 		PTQ_INSERT_HEAD(&pthread__tsd_list[key], pt, pts_next);
2081116ee17Sjoerg 		pthread_mutex_unlock(&tsd_mutex);
20955f47ec3Schristos 	}
21055f47ec3Schristos 	pt->pts_value = __UNCONST(value);
21155f47ec3Schristos 
21255f47ec3Schristos 	return 0;
21355f47ec3Schristos }
21455f47ec3Schristos 
215bd9a18b7Snathanw int
pthread_key_delete(pthread_key_t key)216bd9a18b7Snathanw pthread_key_delete(pthread_key_t key)
217bd9a18b7Snathanw {
218bd9a18b7Snathanw 	/*
219bd9a18b7Snathanw 	 * This is tricky.  The standard says of pthread_key_create()
220bd9a18b7Snathanw 	 * that new keys have the value NULL associated with them in
221bd9a18b7Snathanw 	 * all threads.  According to people who were present at the
222bd9a18b7Snathanw 	 * standardization meeting, that requirement was written
223bd9a18b7Snathanw 	 * before pthread_key_delete() was introduced, and not
224bd9a18b7Snathanw 	 * reconsidered when it was.
225bd9a18b7Snathanw 	 *
226bd9a18b7Snathanw 	 * See David Butenhof's article in comp.programming.threads:
227bd9a18b7Snathanw 	 * Subject: Re: TSD key reusing issue
228bd9a18b7Snathanw 	 * Message-ID: <u97d8.29$fL6.200@news.cpqcorp.net>
229bd9a18b7Snathanw 	 * Date: Thu, 21 Feb 2002 09:06:17 -0500
230fe230c9dSchristos 	 *	 http://groups.google.com/groups?\
231fe230c9dSchristos 	 *	 hl=en&selm=u97d8.29%24fL6.200%40news.cpqcorp.net
232bd9a18b7Snathanw 	 *
233bd9a18b7Snathanw 	 * Given:
234bd9a18b7Snathanw 	 *
235bd9a18b7Snathanw 	 * 1: Applications are not required to clear keys in all
236bd9a18b7Snathanw 	 *    threads before calling pthread_key_delete().
237bd9a18b7Snathanw 	 * 2: Clearing pointers without running destructors is a
238bd9a18b7Snathanw 	 *    memory leak.
239bd9a18b7Snathanw 	 * 3: The pthread_key_delete() function is expressly forbidden
240bd9a18b7Snathanw 	 *    to run any destructors.
241bd9a18b7Snathanw 	 *
242bd9a18b7Snathanw 	 * Option 1: Make this function effectively a no-op and
243bd9a18b7Snathanw 	 * prohibit key reuse. This is a possible resource-exhaustion
244bd9a18b7Snathanw 	 * problem given that we have a static storage area for keys,
245bd9a18b7Snathanw 	 * but having a non-static storage area would make
246bd9a18b7Snathanw 	 * pthread_setspecific() expensive (might need to realloc the
247bd9a18b7Snathanw 	 * TSD array).
248bd9a18b7Snathanw 	 *
249bd9a18b7Snathanw 	 * Option 2: Ignore the specified behavior of
250bd9a18b7Snathanw 	 * pthread_key_create() and leave the old values. If an
251bd9a18b7Snathanw 	 * application deletes a key that still has non-NULL values in
252bd9a18b7Snathanw 	 * some threads... it's probably a memory leak and hence
253bd9a18b7Snathanw 	 * incorrect anyway, and we're within our rights to let the
254bd9a18b7Snathanw 	 * application lose. However, it's possible (if unlikely) that
255bd9a18b7Snathanw 	 * the application is storing pointers to non-heap data, or
256bd9a18b7Snathanw 	 * non-pointers that have been wedged into a void pointer, so
257bd9a18b7Snathanw 	 * we can't entirely write off such applications as incorrect.
258bd9a18b7Snathanw 	 * This could also lead to running (new) destructors on old
259bd9a18b7Snathanw 	 * data that was never supposed to be associated with that
260bd9a18b7Snathanw 	 * destructor.
261bd9a18b7Snathanw 	 *
262bd9a18b7Snathanw 	 * Option 3: Follow the specified behavior of
263bd9a18b7Snathanw 	 * pthread_key_create().  Either pthread_key_create() or
264bd9a18b7Snathanw 	 * pthread_key_delete() would then have to clear the values in
265bd9a18b7Snathanw 	 * every thread's slot for that key. In order to guarantee the
266bd9a18b7Snathanw 	 * visibility of the NULL value in other threads, there would
267bd9a18b7Snathanw 	 * have to be synchronization operations in both the clearer
268bd9a18b7Snathanw 	 * and pthread_getspecific().  Putting synchronization in
269bd9a18b7Snathanw 	 * pthread_getspecific() is a big performance lose.  But in
270bd9a18b7Snathanw 	 * reality, only (buggy) reuse of an old key would require
271bd9a18b7Snathanw 	 * this synchronization; for a new key, there has to be a
272bd9a18b7Snathanw 	 * memory-visibility propagating event between the call to
273bd9a18b7Snathanw 	 * pthread_key_create() and pthread_getspecific() with that
274bd9a18b7Snathanw 	 * key, so setting the entries to NULL without synchronization
275bd9a18b7Snathanw 	 * will work, subject to problem (2) above. However, it's kind
276bd9a18b7Snathanw 	 * of slow.
277bd9a18b7Snathanw 	 *
278bd9a18b7Snathanw 	 * Note that the argument in option 3 only applies because we
279bd9a18b7Snathanw 	 * keep TSD in ordinary memory which follows the pthreads
280bd9a18b7Snathanw 	 * visibility rules. The visibility rules are not required by
281bd9a18b7Snathanw 	 * the standard to apply to TSD, so the argument doesn't
282bd9a18b7Snathanw 	 * apply in general, just to this implementation.
283bd9a18b7Snathanw 	 */
284bd9a18b7Snathanw 
28555f47ec3Schristos 	/*
28655f47ec3Schristos 	 * We do option 3; we find the list of all pt_specific structures
287fe230c9dSchristos 	 * threaded on the key we are deleting, unthread them, and set the
288fe230c9dSchristos 	 * pointer to NULL. Finally we unthread the entry, freeing it for
289fe230c9dSchristos 	 * further use.
290fe230c9dSchristos 	 *
291fe230c9dSchristos 	 * We don't call the destructor here, it is the responsibility
292fe230c9dSchristos 	 * of the application to cleanup the storage:
293fe230c9dSchristos 	 * 	http://pubs.opengroup.org/onlinepubs/9699919799/functions/\
294fe230c9dSchristos 	 *	pthread_key_delete.html
29555f47ec3Schristos 	 */
29655f47ec3Schristos 	struct pt_specific *pt;
29755f47ec3Schristos 
29871d484f9Schristos 	if (__predict_false(__uselibcstub))
29971d484f9Schristos 		return __libc_thr_keydelete_stub(key);
30071d484f9Schristos 
301841339f0Smanu 	pthread__assert(key >= 0 && key < pthread_keys_max);
30255f47ec3Schristos 
303bd9a18b7Snathanw 	pthread_mutex_lock(&tsd_mutex);
30455f47ec3Schristos 
30555f47ec3Schristos 	pthread__assert(pthread__tsd_destructors[key] != NULL);
30655f47ec3Schristos 
30755f47ec3Schristos 	while ((pt = PTQ_FIRST(&pthread__tsd_list[key])) != NULL) {
30855f47ec3Schristos 		PTQ_REMOVE(&pthread__tsd_list[key], pt, pts_next);
30955f47ec3Schristos 		pt->pts_value = NULL;
31055f47ec3Schristos 		pt->pts_next.ptqe_prev = NULL;
31155f47ec3Schristos 	}
31255f47ec3Schristos 
313bd9a18b7Snathanw 	pthread__tsd_destructors[key] = NULL;
314bd9a18b7Snathanw 	pthread_mutex_unlock(&tsd_mutex);
315bd9a18b7Snathanw 
316bd9a18b7Snathanw 	return 0;
317bd9a18b7Snathanw }
318bd9a18b7Snathanw 
319bd9a18b7Snathanw /* Perform thread-exit-time destruction of thread-specific data. */
320bd9a18b7Snathanw void
pthread__destroy_tsd(pthread_t self)321bd9a18b7Snathanw pthread__destroy_tsd(pthread_t self)
322bd9a18b7Snathanw {
323bd9a18b7Snathanw 	int i, done, iterations;
324bd9a18b7Snathanw 	void *val;
325bd9a18b7Snathanw 	void (*destructor)(void *);
326bd9a18b7Snathanw 
327b8833ff5Sad 	if (!self->pt_havespecific)
328b8833ff5Sad 		return;
329b8833ff5Sad 
330bd9a18b7Snathanw 	/* Butenhof, section 5.4.2 (page 167):
331bd9a18b7Snathanw 	 *
332bd9a18b7Snathanw 	 * ``Also, Pthreads sets the thread-specific data value for a
333bd9a18b7Snathanw 	 * key to NULL before calling that key's destructor (passing
334bd9a18b7Snathanw 	 * the previous value of the key) when a thread terminates [*].
335bd9a18b7Snathanw 	 * ...
336bd9a18b7Snathanw 	 * [*] That is, unfortunately, not what the standard
337bd9a18b7Snathanw 	 * says. This is one of the problems with formal standards -
338bd9a18b7Snathanw 	 * they say what they say, not what they were intended to
339bd9a18b7Snathanw 	 * say. Somehow, an error crept in, and the sentence
340bd9a18b7Snathanw 	 * specifying that "the implementation clears the
341bd9a18b7Snathanw 	 * thread-specific data value before calling the destructor"
342bd9a18b7Snathanw 	 * was deleted. Nobody noticed, and the standard was approved
343bd9a18b7Snathanw 	 * with the error. So the standard says (by omission) that if
344bd9a18b7Snathanw 	 * you want to write a portable application using
345bd9a18b7Snathanw 	 * thread-specific data, that will not hang on thread
346bd9a18b7Snathanw 	 * termination, you must call pthread_setspecific within your
347bd9a18b7Snathanw 	 * destructor function to change the value to NULL. This would
348bd9a18b7Snathanw 	 * be silly, and any serious implementation of Pthreads will
349bd9a18b7Snathanw 	 * violate the standard in this respect. Of course, the
350bd9a18b7Snathanw 	 * standard will be fixed, probably by the 1003.1n amendment
351bd9a18b7Snathanw 	 * (assorted corrections to 1003.1c-1995), but that will take
352bd9a18b7Snathanw 	 * a while.''
353bd9a18b7Snathanw 	 */
354bd9a18b7Snathanw 
355e7a07d38Schristos 	/* We're not required to try very hard */
356e7a07d38Schristos 	iterations = PTHREAD_DESTRUCTOR_ITERATIONS;
357bd9a18b7Snathanw 	do {
358bd9a18b7Snathanw 		done = 1;
359841339f0Smanu 		for (i = 0; i < pthread_keys_max; i++) {
36055f47ec3Schristos 			struct pt_specific *pt = &self->pt_specific[i];
36155f47ec3Schristos 			if (pt->pts_next.ptqe_prev == NULL)
36255f47ec3Schristos 				continue;
363bd9a18b7Snathanw 			pthread_mutex_lock(&tsd_mutex);
36455f47ec3Schristos 
36555f47ec3Schristos 			if (pt->pts_next.ptqe_prev != NULL)  {
36655f47ec3Schristos 				PTQ_REMOVE(&pthread__tsd_list[i], pt, pts_next);
36755f47ec3Schristos 				val = pt->pts_value;
36855f47ec3Schristos 				pt->pts_value = NULL;
36955f47ec3Schristos 				pt->pts_next.ptqe_prev = NULL;
370bd9a18b7Snathanw 				destructor = pthread__tsd_destructors[i];
37155f47ec3Schristos 			} else
37255f47ec3Schristos 				destructor = NULL;
37355f47ec3Schristos 
374bd9a18b7Snathanw 			pthread_mutex_unlock(&tsd_mutex);
3751116ee17Sjoerg 			if (destructor != NULL && val != NULL) {
376bd9a18b7Snathanw 				done = 0;
377bd9a18b7Snathanw 				(*destructor)(val);
378bd9a18b7Snathanw 			}
379bd9a18b7Snathanw 		}
380e7a07d38Schristos 	} while (!done && --iterations);
381b8833ff5Sad 
382b8833ff5Sad 	self->pt_havespecific = 0;
383bd9a18b7Snathanw }
384939c050dSchristos 
385939c050dSchristos void
pthread__copy_tsd(pthread_t self)386939c050dSchristos pthread__copy_tsd(pthread_t self)
387939c050dSchristos {
388939c050dSchristos 	for (size_t key = 0; key < TSD_KEYS_MAX; key++) {
389939c050dSchristos 
390939c050dSchristos 		if (__libc_tsd[key].tsd_inuse == 0)
391939c050dSchristos 			continue;
392939c050dSchristos 
393939c050dSchristos 		pthread__assert(pthread__tsd_destructors[key] == NULL);
394939c050dSchristos 		pthread__tsd_destructors[key] = __libc_tsd[key].tsd_dtor ?
395939c050dSchristos 		    __libc_tsd[key].tsd_dtor : null_destructor;
396939c050dSchristos 		nextkey = (key + 1) % pthread_keys_max;
397939c050dSchristos 
398939c050dSchristos 		self->pt_havespecific = 1;
399939c050dSchristos 		struct pt_specific *pt = &self->pt_specific[key];
400939c050dSchristos 		pt->pts_value = __libc_tsd[key].tsd_val;
401939c050dSchristos 		__libc_tsd[key].tsd_inuse = 0;
402939c050dSchristos 	}
403939c050dSchristos }
404