1 /* $NetBSD: pthread.c,v 1.132 2012/03/12 20:16:52 joerg Exp $ */ 2 3 /*- 4 * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008 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 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.c,v 1.132 2012/03/12 20:16:52 joerg Exp $"); 34 35 #define __EXPOSE_STACK 1 36 37 #include <sys/param.h> 38 #include <sys/exec_elf.h> 39 #include <sys/mman.h> 40 #include <sys/sysctl.h> 41 #include <sys/lwpctl.h> 42 #include <sys/tls.h> 43 44 #include <assert.h> 45 #include <dlfcn.h> 46 #include <err.h> 47 #include <errno.h> 48 #include <lwp.h> 49 #include <signal.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <syslog.h> 54 #include <ucontext.h> 55 #include <unistd.h> 56 #include <sched.h> 57 58 #include "pthread.h" 59 #include "pthread_int.h" 60 61 pthread_rwlock_t pthread__alltree_lock = PTHREAD_RWLOCK_INITIALIZER; 62 RB_HEAD(__pthread__alltree, __pthread_st) pthread__alltree; 63 64 #ifndef lint 65 static int pthread__cmp(struct __pthread_st *, struct __pthread_st *); 66 RB_PROTOTYPE_STATIC(__pthread__alltree, __pthread_st, pt_alltree, pthread__cmp) 67 #endif 68 69 static void pthread__create_tramp(void *); 70 static void pthread__initthread(pthread_t); 71 static void pthread__scrubthread(pthread_t, char *, int); 72 static void pthread__initmain(pthread_t *); 73 static void pthread__fork_callback(void); 74 static void pthread__reap(pthread_t); 75 static void pthread__child_callback(void); 76 static void pthread__start(void); 77 78 void pthread__init(void); 79 80 int pthread__started; 81 pthread_mutex_t pthread__deadqueue_lock = PTHREAD_MUTEX_INITIALIZER; 82 pthread_queue_t pthread__deadqueue; 83 pthread_queue_t pthread__allqueue; 84 85 static pthread_attr_t pthread_default_attr; 86 static lwpctl_t pthread__dummy_lwpctl = { .lc_curcpu = LWPCTL_CPU_NONE }; 87 static pthread_t pthread__first; 88 89 enum { 90 DIAGASSERT_ABORT = 1<<0, 91 DIAGASSERT_STDERR = 1<<1, 92 DIAGASSERT_SYSLOG = 1<<2 93 }; 94 95 static int pthread__diagassert; 96 97 int pthread__concurrency; 98 int pthread__nspins; 99 int pthread__unpark_max = PTHREAD__UNPARK_MAX; 100 int pthread__dbg; /* set by libpthread_dbg if active */ 101 102 /* 103 * We have to initialize the pthread_stack* variables here because 104 * mutexes are used before pthread_init() and thus pthread__initmain() 105 * are called. Since mutexes only save the stack pointer and not a 106 * pointer to the thread data, it is safe to change the mapping from 107 * stack pointer to thread data afterwards. 108 */ 109 size_t pthread__stacksize; 110 size_t pthread__pagesize; 111 static struct __pthread_st pthread__main; 112 113 int _sys___sigprocmask14(int, const sigset_t *, sigset_t *); 114 115 __strong_alias(__libc_thr_self,pthread_self) 116 __strong_alias(__libc_thr_create,pthread_create) 117 __strong_alias(__libc_thr_exit,pthread_exit) 118 __strong_alias(__libc_thr_errno,pthread__errno) 119 __strong_alias(__libc_thr_setcancelstate,pthread_setcancelstate) 120 __strong_alias(__libc_thr_equal,pthread_equal) 121 __strong_alias(__libc_thr_init,pthread__init) 122 123 /* 124 * Static library kludge. Place a reference to a symbol any library 125 * file which does not already have a reference here. 126 */ 127 extern int pthread__cancel_stub_binder; 128 129 void *pthread__static_lib_binder[] = { 130 &pthread__cancel_stub_binder, 131 pthread_cond_init, 132 pthread_mutex_init, 133 pthread_rwlock_init, 134 pthread_barrier_init, 135 pthread_key_create, 136 pthread_setspecific, 137 }; 138 139 #define NHASHLOCK 64 140 141 static union hashlock { 142 pthread_mutex_t mutex; 143 char pad[64]; 144 } hashlocks[NHASHLOCK] __aligned(64); 145 146 /* 147 * This needs to be started by the library loading code, before main() 148 * gets to run, for various things that use the state of the initial thread 149 * to work properly (thread-specific data is an application-visible example; 150 * spinlock counts for mutexes is an internal example). 151 */ 152 void 153 pthread__init(void) 154 { 155 pthread_t first; 156 char *p; 157 int i, mib[2]; 158 size_t len; 159 extern int __isthreaded; 160 161 pthread__pagesize = (size_t)sysconf(_SC_PAGESIZE); 162 163 mib[0] = CTL_HW; 164 mib[1] = HW_NCPU; 165 166 len = sizeof(pthread__concurrency); 167 if (sysctl(mib, 2, &pthread__concurrency, &len, NULL, 0) == -1) 168 err(1, "sysctl(hw.ncpu"); 169 170 mib[0] = CTL_KERN; 171 mib[1] = KERN_OSREV; 172 173 /* Initialize locks first; they're needed elsewhere. */ 174 pthread__lockprim_init(); 175 for (i = 0; i < NHASHLOCK; i++) { 176 pthread_mutex_init(&hashlocks[i].mutex, NULL); 177 } 178 179 /* Fetch parameters. */ 180 i = (int)_lwp_unpark_all(NULL, 0, NULL); 181 if (i == -1) 182 err(1, "_lwp_unpark_all"); 183 if (i < pthread__unpark_max) 184 pthread__unpark_max = i; 185 186 /* Basic data structure setup */ 187 pthread_attr_init(&pthread_default_attr); 188 PTQ_INIT(&pthread__allqueue); 189 PTQ_INIT(&pthread__deadqueue); 190 RB_INIT(&pthread__alltree); 191 192 /* Create the thread structure corresponding to main() */ 193 pthread__initmain(&first); 194 pthread__initthread(first); 195 pthread__scrubthread(first, NULL, 0); 196 197 first->pt_lid = _lwp_self(); 198 PTQ_INSERT_HEAD(&pthread__allqueue, first, pt_allq); 199 RB_INSERT(__pthread__alltree, &pthread__alltree, first); 200 201 if (_lwp_ctl(LWPCTL_FEATURE_CURCPU, &first->pt_lwpctl) != 0) { 202 err(1, "_lwp_ctl"); 203 } 204 205 /* Start subsystems */ 206 PTHREAD_MD_INIT 207 208 for (p = pthread__getenv("PTHREAD_DIAGASSERT"); p && *p; p++) { 209 switch (*p) { 210 case 'a': 211 pthread__diagassert |= DIAGASSERT_ABORT; 212 break; 213 case 'A': 214 pthread__diagassert &= ~DIAGASSERT_ABORT; 215 break; 216 case 'e': 217 pthread__diagassert |= DIAGASSERT_STDERR; 218 break; 219 case 'E': 220 pthread__diagassert &= ~DIAGASSERT_STDERR; 221 break; 222 case 'l': 223 pthread__diagassert |= DIAGASSERT_SYSLOG; 224 break; 225 case 'L': 226 pthread__diagassert &= ~DIAGASSERT_SYSLOG; 227 break; 228 } 229 } 230 231 /* Tell libc that we're here and it should role-play accordingly. */ 232 pthread__first = first; 233 pthread_atfork(NULL, NULL, pthread__fork_callback); 234 __isthreaded = 1; 235 } 236 237 static void 238 pthread__fork_callback(void) 239 { 240 struct __pthread_st *self; 241 242 /* lwpctl state is not copied across fork. */ 243 if (_lwp_ctl(LWPCTL_FEATURE_CURCPU, &pthread__first->pt_lwpctl)) { 244 err(1, "_lwp_ctl"); 245 } 246 self = pthread__self(); 247 self->pt_lid = _lwp_self(); 248 } 249 250 static void 251 pthread__child_callback(void) 252 { 253 254 /* 255 * Clean up data structures that a forked child process might 256 * trip over. Note that if threads have been created (causing 257 * this handler to be registered) the standards say that the 258 * child will trigger undefined behavior if it makes any 259 * pthread_* calls (or any other calls that aren't 260 * async-signal-safe), so we don't really have to clean up 261 * much. Anything that permits some pthread_* calls to work is 262 * merely being polite. 263 */ 264 pthread__started = 0; 265 } 266 267 static void 268 pthread__start(void) 269 { 270 271 /* 272 * Per-process timers are cleared by fork(); despite the 273 * various restrictions on fork() and threads, it's legal to 274 * fork() before creating any threads. 275 */ 276 pthread_atfork(NULL, NULL, pthread__child_callback); 277 } 278 279 280 /* General-purpose thread data structure sanitization. */ 281 /* ARGSUSED */ 282 static void 283 pthread__initthread(pthread_t t) 284 { 285 286 t->pt_self = t; 287 t->pt_magic = PT_MAGIC; 288 t->pt_willpark = 0; 289 t->pt_unpark = 0; 290 t->pt_nwaiters = 0; 291 t->pt_sleepobj = NULL; 292 t->pt_signalled = 0; 293 t->pt_havespecific = 0; 294 t->pt_early = NULL; 295 t->pt_lwpctl = &pthread__dummy_lwpctl; 296 t->pt_blocking = 0; 297 t->pt_droplock = NULL; 298 299 memcpy(&t->pt_lockops, pthread__lock_ops, sizeof(t->pt_lockops)); 300 pthread_mutex_init(&t->pt_lock, NULL); 301 PTQ_INIT(&t->pt_cleanup_stack); 302 pthread_cond_init(&t->pt_joiners, NULL); 303 memset(&t->pt_specific, 0, sizeof(t->pt_specific)); 304 } 305 306 static void 307 pthread__scrubthread(pthread_t t, char *name, int flags) 308 { 309 310 t->pt_state = PT_STATE_RUNNING; 311 t->pt_exitval = NULL; 312 t->pt_flags = flags; 313 t->pt_cancel = 0; 314 t->pt_errno = 0; 315 t->pt_name = name; 316 t->pt_lid = 0; 317 } 318 319 static int 320 pthread__getstack(pthread_t newthread, const pthread_attr_t *attr) 321 { 322 void *stackbase, *stackbase2, *redzone; 323 size_t stacksize, guardsize; 324 bool allocated; 325 326 if (attr != NULL) { 327 pthread_attr_getstack(attr, &stackbase, &stacksize); 328 } else { 329 stackbase = NULL; 330 stacksize = 0; 331 } 332 if (stacksize == 0) 333 stacksize = pthread__stacksize; 334 335 if (newthread->pt_stack_allocated) { 336 if (newthread->pt_stack.ss_size == stacksize) 337 return 0; 338 stackbase2 = newthread->pt_stack.ss_sp; 339 #ifndef __MACHINE_STACK_GROWS_UP 340 stackbase2 = (char *)stackbase2 - newthread->pt_guardsize; 341 #endif 342 munmap(stackbase2, 343 newthread->pt_stack.ss_size + newthread->pt_guardsize); 344 newthread->pt_stack.ss_sp = NULL; 345 newthread->pt_stack.ss_size = 0; 346 newthread->pt_guardsize = 0; 347 newthread->pt_stack_allocated = false; 348 } 349 350 newthread->pt_stack_allocated = false; 351 352 if (stackbase == NULL) { 353 stacksize = ((stacksize - 1) | (pthread__pagesize - 1)) + 1; 354 guardsize = pthread__pagesize; 355 stackbase = mmap(NULL, stacksize + guardsize, 356 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, (off_t)0); 357 if (stackbase == MAP_FAILED) 358 return ENOMEM; 359 allocated = true; 360 } else { 361 guardsize = 0; 362 allocated = false; 363 } 364 #ifdef __MACHINE_STACK_GROWS_UP 365 redzone = (char *)stackbase + stacksize; 366 stackbase2 = (char *)stackbase; 367 #else 368 redzone = (char *)stackbase; 369 stackbase2 = (char *)stackbase + guardsize; 370 #endif 371 if (allocated && guardsize && 372 mprotect(redzone, guardsize, PROT_NONE) == -1) { 373 munmap(stackbase, stacksize + guardsize); 374 return EPERM; 375 } 376 newthread->pt_stack.ss_size = stacksize; 377 newthread->pt_stack.ss_sp = stackbase2; 378 newthread->pt_guardsize = guardsize; 379 newthread->pt_stack_allocated = allocated; 380 return 0; 381 } 382 383 int 384 pthread_create(pthread_t *thread, const pthread_attr_t *attr, 385 void *(*startfunc)(void *), void *arg) 386 { 387 pthread_t newthread; 388 pthread_attr_t nattr; 389 struct pthread_attr_private *p; 390 char * volatile name; 391 unsigned long flag; 392 void *private_area; 393 int ret; 394 395 /* 396 * It's okay to check this without a lock because there can 397 * only be one thread before it becomes true. 398 */ 399 if (pthread__started == 0) { 400 pthread__start(); 401 pthread__started = 1; 402 } 403 404 if (attr == NULL) 405 nattr = pthread_default_attr; 406 else if (attr->pta_magic == PT_ATTR_MAGIC) 407 nattr = *attr; 408 else 409 return EINVAL; 410 411 /* Fetch misc. attributes from the attr structure. */ 412 name = NULL; 413 if ((p = nattr.pta_private) != NULL) 414 if (p->ptap_name[0] != '\0') 415 if ((name = strdup(p->ptap_name)) == NULL) 416 return ENOMEM; 417 418 newthread = NULL; 419 420 /* 421 * Try to reclaim a dead thread. 422 */ 423 if (!PTQ_EMPTY(&pthread__deadqueue)) { 424 pthread_mutex_lock(&pthread__deadqueue_lock); 425 PTQ_FOREACH(newthread, &pthread__deadqueue, pt_deadq) { 426 /* Still running? */ 427 if (newthread->pt_lwpctl->lc_curcpu == 428 LWPCTL_CPU_EXITED || 429 (_lwp_kill(newthread->pt_lid, 0) == -1 && 430 errno == ESRCH)) 431 break; 432 } 433 if (newthread) 434 PTQ_REMOVE(&pthread__deadqueue, newthread, pt_deadq); 435 pthread_mutex_unlock(&pthread__deadqueue_lock); 436 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 437 if (newthread && newthread->pt_tls) { 438 _rtld_tls_free(newthread->pt_tls); 439 newthread->pt_tls = NULL; 440 } 441 #endif 442 } 443 444 /* 445 * If necessary set up a stack, allocate space for a pthread_st, 446 * and initialize it. 447 */ 448 if (newthread == NULL) { 449 newthread = malloc(sizeof(*newthread)); 450 if (newthread == NULL) { 451 free(name); 452 return ENOMEM; 453 } 454 newthread->pt_stack_allocated = false; 455 456 if (pthread__getstack(newthread, attr)) { 457 free(newthread); 458 free(name); 459 return ENOMEM; 460 } 461 462 /* This is used only when creating the thread. */ 463 _INITCONTEXT_U(&newthread->pt_uc); 464 newthread->pt_uc.uc_stack = newthread->pt_stack; 465 newthread->pt_uc.uc_link = NULL; 466 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 467 newthread->pt_tls = NULL; 468 #endif 469 470 /* Add to list of all threads. */ 471 pthread_rwlock_wrlock(&pthread__alltree_lock); 472 PTQ_INSERT_TAIL(&pthread__allqueue, newthread, pt_allq); 473 RB_INSERT(__pthread__alltree, &pthread__alltree, newthread); 474 pthread_rwlock_unlock(&pthread__alltree_lock); 475 476 /* Will be reset by the thread upon exit. */ 477 pthread__initthread(newthread); 478 } else { 479 if (pthread__getstack(newthread, attr)) { 480 pthread_mutex_lock(&pthread__deadqueue_lock); 481 PTQ_INSERT_TAIL(&pthread__deadqueue, newthread, pt_deadq); 482 pthread_mutex_unlock(&pthread__deadqueue_lock); 483 return ENOMEM; 484 } 485 _INITCONTEXT_U(&newthread->pt_uc); 486 newthread->pt_uc.uc_stack = newthread->pt_stack; 487 newthread->pt_uc.uc_link = NULL; 488 } 489 490 /* 491 * Create the new LWP. 492 */ 493 pthread__scrubthread(newthread, name, nattr.pta_flags); 494 newthread->pt_func = startfunc; 495 newthread->pt_arg = arg; 496 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 497 private_area = newthread->pt_tls = _rtld_tls_allocate(); 498 newthread->pt_tls->tcb_pthread = newthread; 499 #else 500 private_area = newthread; 501 #endif 502 503 _lwp_makecontext(&newthread->pt_uc, pthread__create_tramp, 504 newthread, private_area, newthread->pt_stack.ss_sp, 505 newthread->pt_stack.ss_size); 506 507 flag = LWP_DETACHED; 508 if ((newthread->pt_flags & PT_FLAG_SUSPENDED) != 0 || 509 (nattr.pta_flags & PT_FLAG_EXPLICIT_SCHED) != 0) 510 flag |= LWP_SUSPENDED; 511 ret = _lwp_create(&newthread->pt_uc, flag, &newthread->pt_lid); 512 if (ret != 0) { 513 pthread_mutex_lock(&newthread->pt_lock); 514 /* Will unlock and free name. */ 515 pthread__reap(newthread); 516 return ret; 517 } 518 519 if ((nattr.pta_flags & PT_FLAG_EXPLICIT_SCHED) != 0) { 520 if (p != NULL) { 521 (void)pthread_setschedparam(newthread, p->ptap_policy, 522 &p->ptap_sp); 523 } 524 if ((newthread->pt_flags & PT_FLAG_SUSPENDED) == 0) { 525 (void)_lwp_continue(newthread->pt_lid); 526 } 527 } 528 529 *thread = newthread; 530 531 return 0; 532 } 533 534 535 __dead static void 536 pthread__create_tramp(void *cookie) 537 { 538 pthread_t self; 539 void *retval; 540 541 self = cookie; 542 543 /* 544 * Throw away some stack in a feeble attempt to reduce cache 545 * thrash. May help for SMT processors. XXX We should not 546 * be allocating stacks on fixed 2MB boundaries. Needs a 547 * thread register or decent thread local storage. 548 * 549 * Note that we may race with the kernel in _lwp_create(), 550 * and so pt_lid can be unset at this point, but we don't 551 * care. 552 */ 553 (void)alloca(((unsigned)self->pt_lid & 7) << 8); 554 555 if (self->pt_name != NULL) { 556 pthread_mutex_lock(&self->pt_lock); 557 if (self->pt_name != NULL) 558 (void)_lwp_setname(0, self->pt_name); 559 pthread_mutex_unlock(&self->pt_lock); 560 } 561 562 if (_lwp_ctl(LWPCTL_FEATURE_CURCPU, &self->pt_lwpctl)) { 563 err(1, "_lwp_ctl"); 564 } 565 566 retval = (*self->pt_func)(self->pt_arg); 567 568 pthread_exit(retval); 569 570 /*NOTREACHED*/ 571 pthread__abort(); 572 } 573 574 int 575 pthread_suspend_np(pthread_t thread) 576 { 577 pthread_t self; 578 579 self = pthread__self(); 580 if (self == thread) { 581 return EDEADLK; 582 } 583 if (pthread__find(thread) != 0) 584 return ESRCH; 585 if (_lwp_suspend(thread->pt_lid) == 0) 586 return 0; 587 return errno; 588 } 589 590 int 591 pthread_resume_np(pthread_t thread) 592 { 593 594 if (pthread__find(thread) != 0) 595 return ESRCH; 596 if (_lwp_continue(thread->pt_lid) == 0) 597 return 0; 598 return errno; 599 } 600 601 void 602 pthread_exit(void *retval) 603 { 604 pthread_t self; 605 struct pt_clean_t *cleanup; 606 char *name; 607 608 self = pthread__self(); 609 610 /* Disable cancellability. */ 611 pthread_mutex_lock(&self->pt_lock); 612 self->pt_flags |= PT_FLAG_CS_DISABLED; 613 self->pt_cancel = 0; 614 615 /* Call any cancellation cleanup handlers */ 616 if (!PTQ_EMPTY(&self->pt_cleanup_stack)) { 617 pthread_mutex_unlock(&self->pt_lock); 618 while (!PTQ_EMPTY(&self->pt_cleanup_stack)) { 619 cleanup = PTQ_FIRST(&self->pt_cleanup_stack); 620 PTQ_REMOVE(&self->pt_cleanup_stack, cleanup, ptc_next); 621 (*cleanup->ptc_cleanup)(cleanup->ptc_arg); 622 } 623 pthread_mutex_lock(&self->pt_lock); 624 } 625 626 /* Perform cleanup of thread-specific data */ 627 pthread__destroy_tsd(self); 628 629 /* Signal our exit. */ 630 self->pt_exitval = retval; 631 if (self->pt_flags & PT_FLAG_DETACHED) { 632 self->pt_state = PT_STATE_DEAD; 633 name = self->pt_name; 634 self->pt_name = NULL; 635 pthread_mutex_unlock(&self->pt_lock); 636 if (name != NULL) 637 free(name); 638 pthread_mutex_lock(&pthread__deadqueue_lock); 639 PTQ_INSERT_TAIL(&pthread__deadqueue, self, pt_deadq); 640 pthread_mutex_unlock(&pthread__deadqueue_lock); 641 _lwp_exit(); 642 } else { 643 self->pt_state = PT_STATE_ZOMBIE; 644 pthread_cond_broadcast(&self->pt_joiners); 645 pthread_mutex_unlock(&self->pt_lock); 646 /* Note: name will be freed by the joiner. */ 647 _lwp_exit(); 648 } 649 650 /*NOTREACHED*/ 651 pthread__abort(); 652 exit(1); 653 } 654 655 656 int 657 pthread_join(pthread_t thread, void **valptr) 658 { 659 pthread_t self; 660 int error; 661 662 self = pthread__self(); 663 664 if (pthread__find(thread) != 0) 665 return ESRCH; 666 667 if (thread->pt_magic != PT_MAGIC) 668 return EINVAL; 669 670 if (thread == self) 671 return EDEADLK; 672 673 self->pt_droplock = &thread->pt_lock; 674 pthread_mutex_lock(&thread->pt_lock); 675 for (;;) { 676 if (thread->pt_state == PT_STATE_ZOMBIE) 677 break; 678 if (thread->pt_state == PT_STATE_DEAD) { 679 pthread_mutex_unlock(&thread->pt_lock); 680 self->pt_droplock = NULL; 681 return ESRCH; 682 } 683 if ((thread->pt_flags & PT_FLAG_DETACHED) != 0) { 684 pthread_mutex_unlock(&thread->pt_lock); 685 self->pt_droplock = NULL; 686 return EINVAL; 687 } 688 error = pthread_cond_wait(&thread->pt_joiners, 689 &thread->pt_lock); 690 if (error != 0) { 691 pthread__errorfunc(__FILE__, __LINE__, 692 __func__, "unexpected return from cond_wait()"); 693 } 694 695 } 696 pthread__testcancel(self); 697 if (valptr != NULL) 698 *valptr = thread->pt_exitval; 699 /* pthread__reap() will drop the lock. */ 700 pthread__reap(thread); 701 self->pt_droplock = NULL; 702 703 return 0; 704 } 705 706 static void 707 pthread__reap(pthread_t thread) 708 { 709 char *name; 710 711 name = thread->pt_name; 712 thread->pt_name = NULL; 713 thread->pt_state = PT_STATE_DEAD; 714 pthread_mutex_unlock(&thread->pt_lock); 715 716 pthread_mutex_lock(&pthread__deadqueue_lock); 717 PTQ_INSERT_HEAD(&pthread__deadqueue, thread, pt_deadq); 718 pthread_mutex_unlock(&pthread__deadqueue_lock); 719 720 if (name != NULL) 721 free(name); 722 } 723 724 int 725 pthread_equal(pthread_t t1, pthread_t t2) 726 { 727 728 /* Nothing special here. */ 729 return (t1 == t2); 730 } 731 732 733 int 734 pthread_detach(pthread_t thread) 735 { 736 737 if (pthread__find(thread) != 0) 738 return ESRCH; 739 740 if (thread->pt_magic != PT_MAGIC) 741 return EINVAL; 742 743 pthread_mutex_lock(&thread->pt_lock); 744 thread->pt_flags |= PT_FLAG_DETACHED; 745 if (thread->pt_state == PT_STATE_ZOMBIE) { 746 /* pthread__reap() will drop the lock. */ 747 pthread__reap(thread); 748 } else { 749 /* 750 * Not valid for threads to be waiting in 751 * pthread_join() (there are intractable 752 * sync issues from the application 753 * perspective), but give those threads 754 * a chance anyway. 755 */ 756 pthread_cond_broadcast(&thread->pt_joiners); 757 pthread_mutex_unlock(&thread->pt_lock); 758 } 759 760 return 0; 761 } 762 763 764 int 765 pthread_getname_np(pthread_t thread, char *name, size_t len) 766 { 767 768 if (pthread__find(thread) != 0) 769 return ESRCH; 770 771 if (thread->pt_magic != PT_MAGIC) 772 return EINVAL; 773 774 pthread_mutex_lock(&thread->pt_lock); 775 if (thread->pt_name == NULL) 776 name[0] = '\0'; 777 else 778 strlcpy(name, thread->pt_name, len); 779 pthread_mutex_unlock(&thread->pt_lock); 780 781 return 0; 782 } 783 784 785 int 786 pthread_setname_np(pthread_t thread, const char *name, void *arg) 787 { 788 char *oldname, *cp, newname[PTHREAD_MAX_NAMELEN_NP]; 789 int namelen; 790 791 if (pthread__find(thread) != 0) 792 return ESRCH; 793 794 if (thread->pt_magic != PT_MAGIC) 795 return EINVAL; 796 797 namelen = snprintf(newname, sizeof(newname), name, arg); 798 if (namelen >= PTHREAD_MAX_NAMELEN_NP) 799 return EINVAL; 800 801 cp = strdup(newname); 802 if (cp == NULL) 803 return ENOMEM; 804 805 pthread_mutex_lock(&thread->pt_lock); 806 oldname = thread->pt_name; 807 thread->pt_name = cp; 808 (void)_lwp_setname(thread->pt_lid, cp); 809 pthread_mutex_unlock(&thread->pt_lock); 810 811 if (oldname != NULL) 812 free(oldname); 813 814 return 0; 815 } 816 817 818 819 /* 820 * XXX There should be a way for applications to use the efficent 821 * inline version, but there are opacity/namespace issues. 822 */ 823 pthread_t 824 pthread_self(void) 825 { 826 827 return pthread__self(); 828 } 829 830 831 int 832 pthread_cancel(pthread_t thread) 833 { 834 835 if (pthread__find(thread) != 0) 836 return ESRCH; 837 pthread_mutex_lock(&thread->pt_lock); 838 thread->pt_flags |= PT_FLAG_CS_PENDING; 839 if ((thread->pt_flags & PT_FLAG_CS_DISABLED) == 0) { 840 thread->pt_cancel = 1; 841 pthread_mutex_unlock(&thread->pt_lock); 842 _lwp_wakeup(thread->pt_lid); 843 } else 844 pthread_mutex_unlock(&thread->pt_lock); 845 846 return 0; 847 } 848 849 850 int 851 pthread_setcancelstate(int state, int *oldstate) 852 { 853 pthread_t self; 854 int retval; 855 856 self = pthread__self(); 857 retval = 0; 858 859 pthread_mutex_lock(&self->pt_lock); 860 861 if (oldstate != NULL) { 862 if (self->pt_flags & PT_FLAG_CS_DISABLED) 863 *oldstate = PTHREAD_CANCEL_DISABLE; 864 else 865 *oldstate = PTHREAD_CANCEL_ENABLE; 866 } 867 868 if (state == PTHREAD_CANCEL_DISABLE) { 869 self->pt_flags |= PT_FLAG_CS_DISABLED; 870 if (self->pt_cancel) { 871 self->pt_flags |= PT_FLAG_CS_PENDING; 872 self->pt_cancel = 0; 873 } 874 } else if (state == PTHREAD_CANCEL_ENABLE) { 875 self->pt_flags &= ~PT_FLAG_CS_DISABLED; 876 /* 877 * If a cancellation was requested while cancellation 878 * was disabled, note that fact for future 879 * cancellation tests. 880 */ 881 if (self->pt_flags & PT_FLAG_CS_PENDING) { 882 self->pt_cancel = 1; 883 /* This is not a deferred cancellation point. */ 884 if (self->pt_flags & PT_FLAG_CS_ASYNC) { 885 pthread_mutex_unlock(&self->pt_lock); 886 pthread__cancelled(); 887 } 888 } 889 } else 890 retval = EINVAL; 891 892 pthread_mutex_unlock(&self->pt_lock); 893 894 return retval; 895 } 896 897 898 int 899 pthread_setcanceltype(int type, int *oldtype) 900 { 901 pthread_t self; 902 int retval; 903 904 self = pthread__self(); 905 retval = 0; 906 907 pthread_mutex_lock(&self->pt_lock); 908 909 if (oldtype != NULL) { 910 if (self->pt_flags & PT_FLAG_CS_ASYNC) 911 *oldtype = PTHREAD_CANCEL_ASYNCHRONOUS; 912 else 913 *oldtype = PTHREAD_CANCEL_DEFERRED; 914 } 915 916 if (type == PTHREAD_CANCEL_ASYNCHRONOUS) { 917 self->pt_flags |= PT_FLAG_CS_ASYNC; 918 if (self->pt_cancel) { 919 pthread_mutex_unlock(&self->pt_lock); 920 pthread__cancelled(); 921 } 922 } else if (type == PTHREAD_CANCEL_DEFERRED) 923 self->pt_flags &= ~PT_FLAG_CS_ASYNC; 924 else 925 retval = EINVAL; 926 927 pthread_mutex_unlock(&self->pt_lock); 928 929 return retval; 930 } 931 932 933 void 934 pthread_testcancel(void) 935 { 936 pthread_t self; 937 938 self = pthread__self(); 939 if (self->pt_cancel) 940 pthread__cancelled(); 941 } 942 943 944 /* 945 * POSIX requires that certain functions return an error rather than 946 * invoking undefined behavior even when handed completely bogus 947 * pthread_t values, e.g. stack garbage or (pthread_t)666. This 948 * utility routine searches the list of threads for the pthread_t 949 * value without dereferencing it. 950 */ 951 int 952 pthread__find(pthread_t id) 953 { 954 pthread_t target; 955 956 pthread_rwlock_rdlock(&pthread__alltree_lock); 957 /* LINTED */ 958 target = RB_FIND(__pthread__alltree, &pthread__alltree, id); 959 pthread_rwlock_unlock(&pthread__alltree_lock); 960 961 if (target == NULL || target->pt_state == PT_STATE_DEAD) 962 return ESRCH; 963 964 return 0; 965 } 966 967 968 void 969 pthread__testcancel(pthread_t self) 970 { 971 972 if (self->pt_cancel) 973 pthread__cancelled(); 974 } 975 976 977 void 978 pthread__cancelled(void) 979 { 980 pthread_mutex_t *droplock; 981 pthread_t self; 982 983 self = pthread__self(); 984 droplock = self->pt_droplock; 985 self->pt_droplock = NULL; 986 987 if (droplock != NULL && pthread_mutex_held_np(droplock)) 988 pthread_mutex_unlock(droplock); 989 990 pthread_exit(PTHREAD_CANCELED); 991 } 992 993 994 void 995 pthread__cleanup_push(void (*cleanup)(void *), void *arg, void *store) 996 { 997 pthread_t self; 998 struct pt_clean_t *entry; 999 1000 self = pthread__self(); 1001 entry = store; 1002 entry->ptc_cleanup = cleanup; 1003 entry->ptc_arg = arg; 1004 PTQ_INSERT_HEAD(&self->pt_cleanup_stack, entry, ptc_next); 1005 } 1006 1007 1008 void 1009 pthread__cleanup_pop(int ex, void *store) 1010 { 1011 pthread_t self; 1012 struct pt_clean_t *entry; 1013 1014 self = pthread__self(); 1015 entry = store; 1016 1017 PTQ_REMOVE(&self->pt_cleanup_stack, entry, ptc_next); 1018 if (ex) 1019 (*entry->ptc_cleanup)(entry->ptc_arg); 1020 } 1021 1022 1023 int * 1024 pthread__errno(void) 1025 { 1026 pthread_t self; 1027 1028 self = pthread__self(); 1029 1030 return &(self->pt_errno); 1031 } 1032 1033 ssize_t _sys_write(int, const void *, size_t); 1034 1035 void 1036 pthread__assertfunc(const char *file, int line, const char *function, 1037 const char *expr) 1038 { 1039 char buf[1024]; 1040 int len; 1041 1042 /* 1043 * snprintf should not acquire any locks, or we could 1044 * end up deadlocked if the assert caller held locks. 1045 */ 1046 len = snprintf(buf, 1024, 1047 "assertion \"%s\" failed: file \"%s\", line %d%s%s%s\n", 1048 expr, file, line, 1049 function ? ", function \"" : "", 1050 function ? function : "", 1051 function ? "\"" : ""); 1052 1053 _sys_write(STDERR_FILENO, buf, (size_t)len); 1054 (void)kill(getpid(), SIGABRT); 1055 1056 _exit(1); 1057 } 1058 1059 1060 void 1061 pthread__errorfunc(const char *file, int line, const char *function, 1062 const char *msg) 1063 { 1064 char buf[1024]; 1065 size_t len; 1066 1067 if (pthread__diagassert == 0) 1068 return; 1069 1070 /* 1071 * snprintf should not acquire any locks, or we could 1072 * end up deadlocked if the assert caller held locks. 1073 */ 1074 len = snprintf(buf, 1024, 1075 "%s: Error detected by libpthread: %s.\n" 1076 "Detected by file \"%s\", line %d%s%s%s.\n" 1077 "See pthread(3) for information.\n", 1078 getprogname(), msg, file, line, 1079 function ? ", function \"" : "", 1080 function ? function : "", 1081 function ? "\"" : ""); 1082 1083 if (pthread__diagassert & DIAGASSERT_STDERR) 1084 _sys_write(STDERR_FILENO, buf, len); 1085 1086 if (pthread__diagassert & DIAGASSERT_SYSLOG) 1087 syslog(LOG_DEBUG | LOG_USER, "%s", buf); 1088 1089 if (pthread__diagassert & DIAGASSERT_ABORT) { 1090 (void)kill(getpid(), SIGABRT); 1091 _exit(1); 1092 } 1093 } 1094 1095 /* 1096 * Thread park/unpark operations. The kernel operations are 1097 * modelled after a brief description from "Multithreading in 1098 * the Solaris Operating Environment": 1099 * 1100 * http://www.sun.com/software/whitepapers/solaris9/multithread.pdf 1101 */ 1102 1103 #define OOPS(msg) \ 1104 pthread__errorfunc(__FILE__, __LINE__, __func__, msg) 1105 1106 int 1107 pthread__park(pthread_t self, pthread_mutex_t *lock, 1108 pthread_queue_t *queue, const struct timespec *abstime, 1109 int cancelpt, const void *hint) 1110 { 1111 int rv, error; 1112 void *obj; 1113 1114 /* 1115 * For non-interlocked release of mutexes we need a store 1116 * barrier before incrementing pt_blocking away from zero. 1117 * This is provided by pthread_mutex_unlock(). 1118 */ 1119 self->pt_willpark = 1; 1120 pthread_mutex_unlock(lock); 1121 self->pt_willpark = 0; 1122 self->pt_blocking++; 1123 1124 /* 1125 * Wait until we are awoken by a pending unpark operation, 1126 * a signal, an unpark posted after we have gone asleep, 1127 * or an expired timeout. 1128 * 1129 * It is fine to test the value of pt_sleepobj without 1130 * holding any locks, because: 1131 * 1132 * o Only the blocking thread (this thread) ever sets them 1133 * to a non-NULL value. 1134 * 1135 * o Other threads may set them NULL, but if they do so they 1136 * must also make this thread return from _lwp_park. 1137 * 1138 * o _lwp_park, _lwp_unpark and _lwp_unpark_all are system 1139 * calls and all make use of spinlocks in the kernel. So 1140 * these system calls act as full memory barriers, and will 1141 * ensure that the calling CPU's store buffers are drained. 1142 * In combination with the spinlock release before unpark, 1143 * this means that modification of pt_sleepobj/onq by another 1144 * thread will become globally visible before that thread 1145 * schedules an unpark operation on this thread. 1146 * 1147 * Note: the test in the while() statement dodges the park op if 1148 * we have already been awoken, unless there is another thread to 1149 * awaken. This saves a syscall - if we were already awakened, 1150 * the next call to _lwp_park() would need to return early in order 1151 * to eat the previous wakeup. 1152 */ 1153 rv = 0; 1154 do { 1155 /* 1156 * If we deferred unparking a thread, arrange to 1157 * have _lwp_park() restart it before blocking. 1158 */ 1159 error = _lwp_park(abstime, self->pt_unpark, hint, hint); 1160 self->pt_unpark = 0; 1161 if (error != 0) { 1162 switch (rv = errno) { 1163 case EINTR: 1164 case EALREADY: 1165 rv = 0; 1166 break; 1167 case ETIMEDOUT: 1168 break; 1169 default: 1170 OOPS("_lwp_park failed"); 1171 break; 1172 } 1173 } 1174 /* Check for cancellation. */ 1175 if (cancelpt && self->pt_cancel) 1176 rv = EINTR; 1177 } while (self->pt_sleepobj != NULL && rv == 0); 1178 1179 /* 1180 * If we have been awoken early but are still on the queue, 1181 * then remove ourself. Again, it's safe to do the test 1182 * without holding any locks. 1183 */ 1184 if (__predict_false(self->pt_sleepobj != NULL)) { 1185 pthread_mutex_lock(lock); 1186 if ((obj = self->pt_sleepobj) != NULL) { 1187 PTQ_REMOVE(queue, self, pt_sleep); 1188 self->pt_sleepobj = NULL; 1189 if (obj != NULL && self->pt_early != NULL) 1190 (*self->pt_early)(obj); 1191 } 1192 pthread_mutex_unlock(lock); 1193 } 1194 self->pt_early = NULL; 1195 self->pt_blocking--; 1196 membar_sync(); 1197 1198 return rv; 1199 } 1200 1201 void 1202 pthread__unpark(pthread_queue_t *queue, pthread_t self, 1203 pthread_mutex_t *interlock) 1204 { 1205 pthread_t target; 1206 u_int max; 1207 size_t nwaiters; 1208 1209 max = pthread__unpark_max; 1210 nwaiters = self->pt_nwaiters; 1211 target = PTQ_FIRST(queue); 1212 if (nwaiters == max) { 1213 /* Overflow. */ 1214 (void)_lwp_unpark_all(self->pt_waiters, nwaiters, 1215 __UNVOLATILE(&interlock->ptm_waiters)); 1216 nwaiters = 0; 1217 } 1218 target->pt_sleepobj = NULL; 1219 self->pt_waiters[nwaiters++] = target->pt_lid; 1220 PTQ_REMOVE(queue, target, pt_sleep); 1221 self->pt_nwaiters = nwaiters; 1222 pthread__mutex_deferwake(self, interlock); 1223 } 1224 1225 void 1226 pthread__unpark_all(pthread_queue_t *queue, pthread_t self, 1227 pthread_mutex_t *interlock) 1228 { 1229 pthread_t target; 1230 u_int max; 1231 size_t nwaiters; 1232 1233 max = pthread__unpark_max; 1234 nwaiters = self->pt_nwaiters; 1235 PTQ_FOREACH(target, queue, pt_sleep) { 1236 if (nwaiters == max) { 1237 /* Overflow. */ 1238 (void)_lwp_unpark_all(self->pt_waiters, nwaiters, 1239 __UNVOLATILE(&interlock->ptm_waiters)); 1240 nwaiters = 0; 1241 } 1242 target->pt_sleepobj = NULL; 1243 self->pt_waiters[nwaiters++] = target->pt_lid; 1244 } 1245 self->pt_nwaiters = nwaiters; 1246 PTQ_INIT(queue); 1247 pthread__mutex_deferwake(self, interlock); 1248 } 1249 1250 #undef OOPS 1251 1252 static void 1253 pthread__initmainstack(void) 1254 { 1255 struct rlimit slimit; 1256 const AuxInfo *aux; 1257 size_t size; 1258 1259 _DIAGASSERT(_dlauxinfo() != NULL); 1260 1261 if (getrlimit(RLIMIT_STACK, &slimit) == -1) 1262 err(1, "Couldn't get stack resource consumption limits"); 1263 size = slimit.rlim_cur; 1264 pthread__main.pt_stack.ss_size = size; 1265 1266 for (aux = _dlauxinfo(); aux->a_type != AT_NULL; ++aux) { 1267 if (aux->a_type == AT_STACKBASE) { 1268 pthread__main.pt_stack.ss_sp = (void *)aux->a_v; 1269 #ifdef __MACHINE_STACK_GROWS_UP 1270 pthread__main.pt_stack.ss_sp = (void *)aux->a_v; 1271 #else 1272 pthread__main.pt_stack.ss_sp = (char *)aux->a_v - size; 1273 #endif 1274 break; 1275 } 1276 } 1277 } 1278 1279 /* 1280 * Set up the slightly special stack for the "initial" thread, which 1281 * runs on the normal system stack, and thus gets slightly different 1282 * treatment. 1283 */ 1284 static void 1285 pthread__initmain(pthread_t *newt) 1286 { 1287 char *value; 1288 1289 pthread__initmainstack(); 1290 1291 value = pthread__getenv("PTHREAD_STACKSIZE"); 1292 if (value != NULL) { 1293 pthread__stacksize = atoi(value) * 1024; 1294 if (pthread__stacksize > pthread__main.pt_stack.ss_size) 1295 pthread__stacksize = pthread__main.pt_stack.ss_size; 1296 } 1297 if (pthread__stacksize == 0) 1298 pthread__stacksize = pthread__main.pt_stack.ss_size; 1299 pthread__stacksize += pthread__pagesize - 1; 1300 pthread__stacksize &= ~(pthread__pagesize - 1); 1301 if (pthread__stacksize < 4 * pthread__pagesize) 1302 errx(1, "Stacksize limit is too low, minimum %zd kbyte.", 1303 4 * pthread__pagesize / 1024); 1304 1305 *newt = &pthread__main; 1306 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 1307 # ifdef __HAVE___LWP_GETTCB_FAST 1308 pthread__main.pt_tls = __lwp_gettcb_fast(); 1309 # else 1310 pthread__main.pt_tls = _lwp_getprivate(); 1311 # endif 1312 pthread__main.pt_tls->tcb_pthread = &pthread__main; 1313 #endif 1314 } 1315 1316 #ifndef lint 1317 static int 1318 pthread__cmp(struct __pthread_st *a, struct __pthread_st *b) 1319 { 1320 1321 if ((uintptr_t)a < (uintptr_t)b) 1322 return (-1); 1323 else if (a == b) 1324 return 0; 1325 else 1326 return 1; 1327 } 1328 RB_GENERATE_STATIC(__pthread__alltree, __pthread_st, pt_alltree, pthread__cmp) 1329 #endif 1330 1331 /* Because getenv() wants to use locks. */ 1332 char * 1333 pthread__getenv(const char *name) 1334 { 1335 extern char **environ; 1336 size_t l_name, offset; 1337 1338 l_name = strlen(name); 1339 for (offset = 0; environ[offset] != NULL; offset++) { 1340 if (strncmp(name, environ[offset], l_name) == 0 && 1341 environ[offset][l_name] == '=') { 1342 return environ[offset] + l_name + 1; 1343 } 1344 } 1345 1346 return NULL; 1347 } 1348 1349 pthread_mutex_t * 1350 pthread__hashlock(volatile const void *p) 1351 { 1352 uintptr_t v; 1353 1354 v = (uintptr_t)p; 1355 return &hashlocks[((v >> 9) ^ (v >> 3)) & (NHASHLOCK - 1)].mutex; 1356 } 1357 1358 int 1359 pthread__checkpri(int pri) 1360 { 1361 static int havepri; 1362 static long min, max; 1363 1364 if (!havepri) { 1365 min = sysconf(_SC_SCHED_PRI_MIN); 1366 max = sysconf(_SC_SCHED_PRI_MAX); 1367 havepri = 1; 1368 } 1369 return (pri < min || pri > max) ? EINVAL : 0; 1370 } 1371