Home | History | Annotate | Line # | Download | only in libpthread
pthread_tsd.c revision 1.7.4.3
      1  1.7.4.3     yamt /*	$NetBSD: pthread_tsd.c,v 1.7.4.3 2014/05/22 11:36:59 yamt Exp $	*/
      2      1.1  nathanw 
      3      1.1  nathanw /*-
      4      1.3       ad  * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
      5      1.1  nathanw  * All rights reserved.
      6      1.1  nathanw  *
      7      1.1  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.7.4.2     yamt  * by Nathan J. Williams, by Andrew Doran, and by Christos Zoulas.
      9      1.1  nathanw  *
     10      1.1  nathanw  * Redistribution and use in source and binary forms, with or without
     11      1.1  nathanw  * modification, are permitted provided that the following conditions
     12      1.1  nathanw  * are met:
     13      1.1  nathanw  * 1. Redistributions of source code must retain the above copyright
     14      1.1  nathanw  *    notice, this list of conditions and the following disclaimer.
     15      1.1  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  nathanw  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  nathanw  *    documentation and/or other materials provided with the distribution.
     18      1.1  nathanw  *
     19      1.1  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1  nathanw  */
     31      1.1  nathanw 
     32      1.1  nathanw #include <sys/cdefs.h>
     33  1.7.4.3     yamt __RCSID("$NetBSD: pthread_tsd.c,v 1.7.4.3 2014/05/22 11:36:59 yamt Exp $");
     34      1.1  nathanw 
     35      1.1  nathanw /* Functions and structures dealing with thread-specific data */
     36      1.1  nathanw #include <errno.h>
     37      1.1  nathanw 
     38      1.1  nathanw #include "pthread.h"
     39      1.1  nathanw #include "pthread_int.h"
     40  1.7.4.3     yamt #include "reentrant.h"
     41      1.1  nathanw 
     42  1.7.4.2     yamt 
     43      1.1  nathanw static pthread_mutex_t tsd_mutex = PTHREAD_MUTEX_INITIALIZER;
     44      1.1  nathanw static int nextkey;
     45  1.7.4.2     yamt 
     46  1.7.4.2     yamt PTQ_HEAD(pthread__tsd_list, pt_specific)
     47  1.7.4.2     yamt     pthread__tsd_list[PTHREAD_KEYS_MAX];
     48      1.1  nathanw void (*pthread__tsd_destructors[PTHREAD_KEYS_MAX])(void *);
     49      1.1  nathanw 
     50      1.1  nathanw __strong_alias(__libc_thr_keycreate,pthread_key_create)
     51      1.1  nathanw __strong_alias(__libc_thr_keydelete,pthread_key_delete)
     52      1.1  nathanw 
     53  1.7.4.2     yamt static void
     54  1.7.4.2     yamt /*ARGSUSED*/
     55  1.7.4.2     yamt null_destructor(void *p)
     56  1.7.4.2     yamt {
     57  1.7.4.2     yamt }
     58  1.7.4.2     yamt 
     59  1.7.4.3     yamt #include <err.h>
     60  1.7.4.3     yamt #include <stdlib.h>
     61      1.1  nathanw int
     62      1.1  nathanw pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
     63      1.1  nathanw {
     64      1.1  nathanw 	int i;
     65      1.1  nathanw 
     66  1.7.4.3     yamt 	if (__predict_false(__uselibcstub))
     67  1.7.4.3     yamt 		return __libc_thr_keycreate_stub(key, destructor);
     68  1.7.4.3     yamt 
     69      1.1  nathanw 	/* Get a lock on the allocation list */
     70      1.1  nathanw 	pthread_mutex_lock(&tsd_mutex);
     71      1.1  nathanw 
     72  1.7.4.2     yamt 	/* Find an available slot:
     73  1.7.4.2     yamt 	 * The condition for an available slot is one with the destructor
     74  1.7.4.2     yamt 	 * not being NULL. If the desired destructor is NULL we set it to
     75  1.7.4.2     yamt 	 * our own internal destructor to satisfy the non NULL condition.
     76  1.7.4.2     yamt 	 */
     77      1.1  nathanw 	/* 1. Search from "nextkey" to the end of the list. */
     78      1.1  nathanw 	for (i = nextkey; i < PTHREAD_KEYS_MAX; i++)
     79  1.7.4.2     yamt 		if (pthread__tsd_destructors[i] == NULL)
     80      1.1  nathanw 			break;
     81      1.1  nathanw 
     82      1.1  nathanw 	if (i == PTHREAD_KEYS_MAX) {
     83      1.1  nathanw 		/* 2. If that didn't work, search from the start
     84      1.1  nathanw 		 *    of the list back to "nextkey".
     85      1.1  nathanw 		 */
     86      1.1  nathanw 		for (i = 0; i < nextkey; i++)
     87  1.7.4.2     yamt 			if (pthread__tsd_destructors[i] == NULL)
     88      1.1  nathanw 				break;
     89      1.1  nathanw 
     90      1.1  nathanw 		if (i == nextkey) {
     91      1.1  nathanw 			/* If we didn't find one here, there isn't one
     92      1.1  nathanw 			 * to be found.
     93      1.1  nathanw 			 */
     94      1.1  nathanw 			pthread_mutex_unlock(&tsd_mutex);
     95      1.1  nathanw 			return EAGAIN;
     96      1.1  nathanw 		}
     97      1.1  nathanw 	}
     98      1.1  nathanw 
     99      1.1  nathanw 	/* Got one. */
    100  1.7.4.2     yamt 	pthread__assert(PTQ_EMPTY(&pthread__tsd_list[i]));
    101  1.7.4.2     yamt 	pthread__tsd_destructors[i] = destructor ? destructor : null_destructor;
    102  1.7.4.2     yamt 
    103      1.1  nathanw 	nextkey = (i + 1) % PTHREAD_KEYS_MAX;
    104      1.1  nathanw 	pthread_mutex_unlock(&tsd_mutex);
    105      1.1  nathanw 	*key = i;
    106      1.1  nathanw 
    107      1.1  nathanw 	return 0;
    108      1.1  nathanw }
    109      1.1  nathanw 
    110  1.7.4.2     yamt /*
    111  1.7.4.2     yamt  * Each thread holds an array of PTHREAD_KEYS_MAX pt_specific list
    112  1.7.4.2     yamt  * elements. When an element is used it is inserted into the appropriate
    113  1.7.4.2     yamt  * key bucket of pthread__tsd_list. This means that ptqe_prev == NULL,
    114  1.7.4.2     yamt  * means that the element is not threaded, ptqe_prev != NULL it is
    115  1.7.4.2     yamt  * already part of the list. When we set to a NULL value we delete from the
    116  1.7.4.2     yamt  * list if it was in the list, and when we set to non-NULL value, we insert
    117  1.7.4.2     yamt  * in the list if it was not already there.
    118  1.7.4.2     yamt  *
    119  1.7.4.2     yamt  * We keep this global array of lists of threads that have called
    120  1.7.4.2     yamt  * pthread_set_specific with non-null values, for each key so that
    121  1.7.4.2     yamt  * we don't have to check all threads for non-NULL values in
    122  1.7.4.2     yamt  * pthread_key_destroy
    123  1.7.4.2     yamt  *
    124  1.7.4.2     yamt  * We could keep an accounting of the number of specific used
    125  1.7.4.2     yamt  * entries per thread, so that we can update pt_havespecific when we delete
    126  1.7.4.2     yamt  * the last one, but we don't bother for now
    127  1.7.4.2     yamt  */
    128  1.7.4.2     yamt int
    129  1.7.4.2     yamt pthread__add_specific(pthread_t self, pthread_key_t key, const void *value)
    130  1.7.4.2     yamt {
    131  1.7.4.2     yamt 	struct pt_specific *pt;
    132  1.7.4.2     yamt 
    133  1.7.4.2     yamt 	pthread__assert(key >= 0 && key < PTHREAD_KEYS_MAX);
    134  1.7.4.2     yamt 
    135  1.7.4.2     yamt 	pthread_mutex_lock(&tsd_mutex);
    136  1.7.4.2     yamt 	pthread__assert(pthread__tsd_destructors[key] != NULL);
    137  1.7.4.2     yamt 	pt = &self->pt_specific[key];
    138  1.7.4.2     yamt 	self->pt_havespecific = 1;
    139  1.7.4.2     yamt 	if (value) {
    140  1.7.4.2     yamt 		if (pt->pts_next.ptqe_prev == NULL)
    141  1.7.4.2     yamt 			PTQ_INSERT_HEAD(&pthread__tsd_list[key], pt, pts_next);
    142  1.7.4.2     yamt 	} else {
    143  1.7.4.2     yamt 		if (pt->pts_next.ptqe_prev != NULL) {
    144  1.7.4.2     yamt 			PTQ_REMOVE(&pthread__tsd_list[key], pt, pts_next);
    145  1.7.4.2     yamt 			pt->pts_next.ptqe_prev = NULL;
    146  1.7.4.2     yamt 		}
    147  1.7.4.2     yamt 	}
    148  1.7.4.2     yamt 	pt->pts_value = __UNCONST(value);
    149  1.7.4.2     yamt 	pthread_mutex_unlock(&tsd_mutex);
    150  1.7.4.2     yamt 
    151  1.7.4.2     yamt 	return 0;
    152  1.7.4.2     yamt }
    153  1.7.4.2     yamt 
    154      1.1  nathanw int
    155      1.1  nathanw pthread_key_delete(pthread_key_t key)
    156      1.1  nathanw {
    157      1.1  nathanw 	/*
    158      1.1  nathanw 	 * This is tricky.  The standard says of pthread_key_create()
    159      1.1  nathanw 	 * that new keys have the value NULL associated with them in
    160      1.1  nathanw 	 * all threads.  According to people who were present at the
    161      1.1  nathanw 	 * standardization meeting, that requirement was written
    162      1.1  nathanw 	 * before pthread_key_delete() was introduced, and not
    163      1.1  nathanw 	 * reconsidered when it was.
    164      1.1  nathanw 	 *
    165      1.1  nathanw 	 * See David Butenhof's article in comp.programming.threads:
    166      1.1  nathanw 	 * Subject: Re: TSD key reusing issue
    167      1.1  nathanw 	 * Message-ID: <u97d8.29$fL6.200 (at) news.cpqcorp.net>
    168      1.1  nathanw 	 * Date: Thu, 21 Feb 2002 09:06:17 -0500
    169  1.7.4.2     yamt 	 *	 http://groups.google.com/groups?\
    170  1.7.4.2     yamt 	 *	 hl=en&selm=u97d8.29%24fL6.200%40news.cpqcorp.net
    171      1.1  nathanw 	 *
    172      1.1  nathanw 	 * Given:
    173      1.1  nathanw 	 *
    174      1.1  nathanw 	 * 1: Applications are not required to clear keys in all
    175      1.1  nathanw 	 *    threads before calling pthread_key_delete().
    176      1.1  nathanw 	 * 2: Clearing pointers without running destructors is a
    177      1.1  nathanw 	 *    memory leak.
    178      1.1  nathanw 	 * 3: The pthread_key_delete() function is expressly forbidden
    179      1.1  nathanw 	 *    to run any destructors.
    180      1.1  nathanw 	 *
    181      1.1  nathanw 	 * Option 1: Make this function effectively a no-op and
    182      1.1  nathanw 	 * prohibit key reuse. This is a possible resource-exhaustion
    183      1.1  nathanw 	 * problem given that we have a static storage area for keys,
    184      1.1  nathanw 	 * but having a non-static storage area would make
    185      1.1  nathanw 	 * pthread_setspecific() expensive (might need to realloc the
    186      1.1  nathanw 	 * TSD array).
    187      1.1  nathanw 	 *
    188      1.1  nathanw 	 * Option 2: Ignore the specified behavior of
    189      1.1  nathanw 	 * pthread_key_create() and leave the old values. If an
    190      1.1  nathanw 	 * application deletes a key that still has non-NULL values in
    191      1.1  nathanw 	 * some threads... it's probably a memory leak and hence
    192      1.1  nathanw 	 * incorrect anyway, and we're within our rights to let the
    193      1.1  nathanw 	 * application lose. However, it's possible (if unlikely) that
    194      1.1  nathanw 	 * the application is storing pointers to non-heap data, or
    195      1.1  nathanw 	 * non-pointers that have been wedged into a void pointer, so
    196      1.1  nathanw 	 * we can't entirely write off such applications as incorrect.
    197      1.1  nathanw 	 * This could also lead to running (new) destructors on old
    198      1.1  nathanw 	 * data that was never supposed to be associated with that
    199      1.1  nathanw 	 * destructor.
    200      1.1  nathanw 	 *
    201      1.1  nathanw 	 * Option 3: Follow the specified behavior of
    202      1.1  nathanw 	 * pthread_key_create().  Either pthread_key_create() or
    203      1.1  nathanw 	 * pthread_key_delete() would then have to clear the values in
    204      1.1  nathanw 	 * every thread's slot for that key. In order to guarantee the
    205      1.1  nathanw 	 * visibility of the NULL value in other threads, there would
    206      1.1  nathanw 	 * have to be synchronization operations in both the clearer
    207      1.1  nathanw 	 * and pthread_getspecific().  Putting synchronization in
    208      1.1  nathanw 	 * pthread_getspecific() is a big performance lose.  But in
    209      1.1  nathanw 	 * reality, only (buggy) reuse of an old key would require
    210      1.1  nathanw 	 * this synchronization; for a new key, there has to be a
    211      1.1  nathanw 	 * memory-visibility propagating event between the call to
    212      1.1  nathanw 	 * pthread_key_create() and pthread_getspecific() with that
    213      1.1  nathanw 	 * key, so setting the entries to NULL without synchronization
    214      1.1  nathanw 	 * will work, subject to problem (2) above. However, it's kind
    215      1.1  nathanw 	 * of slow.
    216      1.1  nathanw 	 *
    217      1.1  nathanw 	 * Note that the argument in option 3 only applies because we
    218      1.1  nathanw 	 * keep TSD in ordinary memory which follows the pthreads
    219      1.1  nathanw 	 * visibility rules. The visibility rules are not required by
    220      1.1  nathanw 	 * the standard to apply to TSD, so the argument doesn't
    221      1.1  nathanw 	 * apply in general, just to this implementation.
    222      1.1  nathanw 	 */
    223      1.1  nathanw 
    224  1.7.4.2     yamt 	/*
    225  1.7.4.2     yamt 	 * We do option 3; we find the list of all pt_specific structures
    226  1.7.4.2     yamt 	 * threaded on the key we are deleting, unthread them, and set the
    227  1.7.4.2     yamt 	 * pointer to NULL. Finally we unthread the entry, freeing it for
    228  1.7.4.2     yamt 	 * further use.
    229  1.7.4.2     yamt 	 *
    230  1.7.4.2     yamt 	 * We don't call the destructor here, it is the responsibility
    231  1.7.4.2     yamt 	 * of the application to cleanup the storage:
    232  1.7.4.2     yamt 	 * 	http://pubs.opengroup.org/onlinepubs/9699919799/functions/\
    233  1.7.4.2     yamt 	 *	pthread_key_delete.html
    234  1.7.4.2     yamt 	 */
    235  1.7.4.2     yamt 	struct pt_specific *pt;
    236  1.7.4.2     yamt 
    237  1.7.4.3     yamt 	if (__predict_false(__uselibcstub))
    238  1.7.4.3     yamt 		return __libc_thr_keydelete_stub(key);
    239  1.7.4.3     yamt 
    240  1.7.4.2     yamt 	pthread__assert(key >= 0 && key < PTHREAD_KEYS_MAX);
    241  1.7.4.2     yamt 
    242      1.1  nathanw 	pthread_mutex_lock(&tsd_mutex);
    243  1.7.4.2     yamt 
    244  1.7.4.2     yamt 	pthread__assert(pthread__tsd_destructors[key] != NULL);
    245  1.7.4.2     yamt 
    246  1.7.4.2     yamt 	while ((pt = PTQ_FIRST(&pthread__tsd_list[key])) != NULL) {
    247  1.7.4.2     yamt 		PTQ_REMOVE(&pthread__tsd_list[key], pt, pts_next);
    248  1.7.4.2     yamt 		pt->pts_value = NULL;
    249  1.7.4.2     yamt 		pt->pts_next.ptqe_prev = NULL;
    250  1.7.4.2     yamt 	}
    251  1.7.4.2     yamt 
    252      1.1  nathanw 	pthread__tsd_destructors[key] = NULL;
    253      1.1  nathanw 	pthread_mutex_unlock(&tsd_mutex);
    254      1.1  nathanw 
    255      1.1  nathanw 	return 0;
    256      1.1  nathanw }
    257      1.1  nathanw 
    258      1.1  nathanw /* Perform thread-exit-time destruction of thread-specific data. */
    259      1.1  nathanw void
    260      1.1  nathanw pthread__destroy_tsd(pthread_t self)
    261      1.1  nathanw {
    262      1.1  nathanw 	int i, done, iterations;
    263      1.1  nathanw 	void *val;
    264      1.1  nathanw 	void (*destructor)(void *);
    265      1.1  nathanw 
    266      1.3       ad 	if (!self->pt_havespecific)
    267      1.3       ad 		return;
    268      1.4       ad 	pthread_mutex_unlock(&self->pt_lock);
    269      1.3       ad 
    270      1.1  nathanw 	/* Butenhof, section 5.4.2 (page 167):
    271      1.1  nathanw 	 *
    272      1.1  nathanw 	 * ``Also, Pthreads sets the thread-specific data value for a
    273      1.1  nathanw 	 * key to NULL before calling that key's destructor (passing
    274      1.1  nathanw 	 * the previous value of the key) when a thread terminates [*].
    275      1.1  nathanw 	 * ...
    276      1.1  nathanw 	 * [*] That is, unfortunately, not what the standard
    277      1.1  nathanw 	 * says. This is one of the problems with formal standards -
    278      1.1  nathanw 	 * they say what they say, not what they were intended to
    279      1.1  nathanw 	 * say. Somehow, an error crept in, and the sentence
    280      1.1  nathanw 	 * specifying that "the implementation clears the
    281      1.1  nathanw 	 * thread-specific data value before calling the destructor"
    282      1.1  nathanw 	 * was deleted. Nobody noticed, and the standard was approved
    283      1.1  nathanw 	 * with the error. So the standard says (by omission) that if
    284      1.1  nathanw 	 * you want to write a portable application using
    285      1.1  nathanw 	 * thread-specific data, that will not hang on thread
    286      1.1  nathanw 	 * termination, you must call pthread_setspecific within your
    287      1.1  nathanw 	 * destructor function to change the value to NULL. This would
    288      1.1  nathanw 	 * be silly, and any serious implementation of Pthreads will
    289      1.1  nathanw 	 * violate the standard in this respect. Of course, the
    290      1.1  nathanw 	 * standard will be fixed, probably by the 1003.1n amendment
    291      1.1  nathanw 	 * (assorted corrections to 1003.1c-1995), but that will take
    292      1.1  nathanw 	 * a while.''
    293      1.1  nathanw 	 */
    294      1.1  nathanw 
    295      1.1  nathanw 	iterations = 4; /* We're not required to try very hard */
    296      1.1  nathanw 	do {
    297      1.1  nathanw 		done = 1;
    298      1.1  nathanw 		for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
    299  1.7.4.2     yamt 			struct pt_specific *pt = &self->pt_specific[i];
    300  1.7.4.2     yamt 			if (pt->pts_next.ptqe_prev == NULL)
    301  1.7.4.2     yamt 				continue;
    302  1.7.4.2     yamt 			pthread_mutex_lock(&tsd_mutex);
    303  1.7.4.2     yamt 
    304  1.7.4.2     yamt 			if (pt->pts_next.ptqe_prev != NULL)  {
    305  1.7.4.2     yamt 				PTQ_REMOVE(&pthread__tsd_list[i], pt, pts_next);
    306  1.7.4.2     yamt 				val = pt->pts_value;
    307  1.7.4.2     yamt 				pt->pts_value = NULL;
    308  1.7.4.2     yamt 				pt->pts_next.ptqe_prev = NULL;
    309      1.1  nathanw 				destructor = pthread__tsd_destructors[i];
    310  1.7.4.2     yamt 			} else
    311  1.7.4.2     yamt 				destructor = NULL;
    312  1.7.4.2     yamt 
    313  1.7.4.2     yamt 			pthread_mutex_unlock(&tsd_mutex);
    314  1.7.4.2     yamt 			if (destructor != NULL) {
    315  1.7.4.2     yamt 				done = 0;
    316  1.7.4.2     yamt 				(*destructor)(val);
    317      1.1  nathanw 			}
    318      1.1  nathanw 		}
    319      1.1  nathanw 	} while (!done && iterations--);
    320      1.3       ad 
    321      1.3       ad 	self->pt_havespecific = 0;
    322      1.4       ad 	pthread_mutex_lock(&self->pt_lock);
    323      1.1  nathanw }
    324