1 /* $NetBSD: pthread_tsd.c,v 1.7 2008/04/28 20:23:01 martin 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, and by Andrew Doran. 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.7 2008/04/28 20:23:01 martin Exp $"); 34 35 /* Functions and structures dealing with thread-specific data */ 36 #include <errno.h> 37 38 #include "pthread.h" 39 #include "pthread_int.h" 40 41 static pthread_mutex_t tsd_mutex = PTHREAD_MUTEX_INITIALIZER; 42 static int nextkey; 43 void *pthread__tsd_alloc[PTHREAD_KEYS_MAX]; 44 void (*pthread__tsd_destructors[PTHREAD_KEYS_MAX])(void *); 45 46 __strong_alias(__libc_thr_keycreate,pthread_key_create) 47 __strong_alias(__libc_thr_keydelete,pthread_key_delete) 48 49 int 50 pthread_key_create(pthread_key_t *key, void (*destructor)(void *)) 51 { 52 int i; 53 54 /* Get a lock on the allocation list */ 55 pthread_mutex_lock(&tsd_mutex); 56 57 /* Find an available slot */ 58 /* 1. Search from "nextkey" to the end of the list. */ 59 for (i = nextkey; i < PTHREAD_KEYS_MAX; i++) 60 if (pthread__tsd_alloc[i] == NULL) 61 break; 62 63 if (i == PTHREAD_KEYS_MAX) { 64 /* 2. If that didn't work, search from the start 65 * of the list back to "nextkey". 66 */ 67 for (i = 0; i < nextkey; i++) 68 if (pthread__tsd_alloc[i] == NULL) 69 break; 70 71 if (i == nextkey) { 72 /* If we didn't find one here, there isn't one 73 * to be found. 74 */ 75 pthread_mutex_unlock(&tsd_mutex); 76 return EAGAIN; 77 } 78 } 79 80 /* Got one. */ 81 pthread__tsd_alloc[i] = (void *)__builtin_return_address(0); 82 nextkey = (i + 1) % PTHREAD_KEYS_MAX; 83 pthread__tsd_destructors[i] = destructor; 84 pthread_mutex_unlock(&tsd_mutex); 85 *key = i; 86 87 return 0; 88 } 89 90 int 91 pthread_key_delete(pthread_key_t key) 92 { 93 94 /* 95 * This is tricky. The standard says of pthread_key_create() 96 * that new keys have the value NULL associated with them in 97 * all threads. According to people who were present at the 98 * standardization meeting, that requirement was written 99 * before pthread_key_delete() was introduced, and not 100 * reconsidered when it was. 101 * 102 * See David Butenhof's article in comp.programming.threads: 103 * Subject: Re: TSD key reusing issue 104 * Message-ID: <u97d8.29$fL6.200@news.cpqcorp.net> 105 * Date: Thu, 21 Feb 2002 09:06:17 -0500 106 * http://groups.google.com/groups?hl=en&selm=u97d8.29%24fL6.200%40news.cpqcorp.net 107 * 108 * Given: 109 * 110 * 1: Applications are not required to clear keys in all 111 * threads before calling pthread_key_delete(). 112 * 2: Clearing pointers without running destructors is a 113 * memory leak. 114 * 3: The pthread_key_delete() function is expressly forbidden 115 * to run any destructors. 116 * 117 * Option 1: Make this function effectively a no-op and 118 * prohibit key reuse. This is a possible resource-exhaustion 119 * problem given that we have a static storage area for keys, 120 * but having a non-static storage area would make 121 * pthread_setspecific() expensive (might need to realloc the 122 * TSD array). 123 * 124 * Option 2: Ignore the specified behavior of 125 * pthread_key_create() and leave the old values. If an 126 * application deletes a key that still has non-NULL values in 127 * some threads... it's probably a memory leak and hence 128 * incorrect anyway, and we're within our rights to let the 129 * application lose. However, it's possible (if unlikely) that 130 * the application is storing pointers to non-heap data, or 131 * non-pointers that have been wedged into a void pointer, so 132 * we can't entirely write off such applications as incorrect. 133 * This could also lead to running (new) destructors on old 134 * data that was never supposed to be associated with that 135 * destructor. 136 * 137 * Option 3: Follow the specified behavior of 138 * pthread_key_create(). Either pthread_key_create() or 139 * pthread_key_delete() would then have to clear the values in 140 * every thread's slot for that key. In order to guarantee the 141 * visibility of the NULL value in other threads, there would 142 * have to be synchronization operations in both the clearer 143 * and pthread_getspecific(). Putting synchronization in 144 * pthread_getspecific() is a big performance lose. But in 145 * reality, only (buggy) reuse of an old key would require 146 * this synchronization; for a new key, there has to be a 147 * memory-visibility propagating event between the call to 148 * pthread_key_create() and pthread_getspecific() with that 149 * key, so setting the entries to NULL without synchronization 150 * will work, subject to problem (2) above. However, it's kind 151 * of slow. 152 * 153 * Note that the argument in option 3 only applies because we 154 * keep TSD in ordinary memory which follows the pthreads 155 * visibility rules. The visibility rules are not required by 156 * the standard to apply to TSD, so the argument doesn't 157 * apply in general, just to this implementation. 158 */ 159 160 /* For the momemt, we're going with option 1. */ 161 pthread_mutex_lock(&tsd_mutex); 162 pthread__tsd_destructors[key] = NULL; 163 pthread_mutex_unlock(&tsd_mutex); 164 165 return 0; 166 } 167 168 /* Perform thread-exit-time destruction of thread-specific data. */ 169 void 170 pthread__destroy_tsd(pthread_t self) 171 { 172 int i, done, iterations; 173 void *val; 174 void (*destructor)(void *); 175 176 if (!self->pt_havespecific) 177 return; 178 pthread_mutex_unlock(&self->pt_lock); 179 180 /* Butenhof, section 5.4.2 (page 167): 181 * 182 * ``Also, Pthreads sets the thread-specific data value for a 183 * key to NULL before calling that key's destructor (passing 184 * the previous value of the key) when a thread terminates [*]. 185 * ... 186 * [*] That is, unfortunately, not what the standard 187 * says. This is one of the problems with formal standards - 188 * they say what they say, not what they were intended to 189 * say. Somehow, an error crept in, and the sentence 190 * specifying that "the implementation clears the 191 * thread-specific data value before calling the destructor" 192 * was deleted. Nobody noticed, and the standard was approved 193 * with the error. So the standard says (by omission) that if 194 * you want to write a portable application using 195 * thread-specific data, that will not hang on thread 196 * termination, you must call pthread_setspecific within your 197 * destructor function to change the value to NULL. This would 198 * be silly, and any serious implementation of Pthreads will 199 * violate the standard in this respect. Of course, the 200 * standard will be fixed, probably by the 1003.1n amendment 201 * (assorted corrections to 1003.1c-1995), but that will take 202 * a while.'' 203 */ 204 205 iterations = 4; /* We're not required to try very hard */ 206 do { 207 done = 1; 208 for (i = 0; i < PTHREAD_KEYS_MAX; i++) { 209 if (self->pt_specific[i] != NULL) { 210 pthread_mutex_lock(&tsd_mutex); 211 destructor = pthread__tsd_destructors[i]; 212 pthread_mutex_unlock(&tsd_mutex); 213 if (destructor != NULL) { 214 done = 0; 215 val = self->pt_specific[i]; 216 self->pt_specific[i] = NULL; /* see above */ 217 (*destructor)(val); 218 } 219 } 220 } 221 } while (!done && iterations--); 222 223 self->pt_havespecific = 0; 224 pthread_mutex_lock(&self->pt_lock); 225 } 226