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