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