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