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