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