1 /* 2 * Copyright (C) 2005 Daniel M. Eischen <deischen@freebsd.org> 3 * Copyright (c) 2005 David Xu <davidxu@freebsd.org> 4 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>. 5 * 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #ifndef _THR_PRIVATE_H 33 #define _THR_PRIVATE_H 34 35 /* 36 * Include files. 37 */ 38 #include <sys/types.h> 39 #include <sys/time.h> 40 #include <sys/cdefs.h> 41 #include <sys/queue.h> 42 #include <sys/param.h> 43 #include <sys/cpuset.h> 44 #include <machine/atomic.h> 45 #include <errno.h> 46 #include <limits.h> 47 #include <signal.h> 48 #include <stddef.h> 49 #include <stdio.h> 50 #include <unistd.h> 51 #include <ucontext.h> 52 #include <sys/thr.h> 53 #include <pthread.h> 54 55 #define SYM_FB10(sym) __CONCAT(sym, _fb10) 56 #define SYM_FBP10(sym) __CONCAT(sym, _fbp10) 57 #define WEAK_REF(sym, alias) __weak_reference(sym, alias) 58 #define SYM_COMPAT(sym, impl, ver) __sym_compat(sym, impl, ver) 59 #define SYM_DEFAULT(sym, impl, ver) __sym_default(sym, impl, ver) 60 61 #define FB10_COMPAT(func, sym) \ 62 WEAK_REF(func, SYM_FB10(sym)); \ 63 SYM_COMPAT(sym, SYM_FB10(sym), FBSD_1.0) 64 65 #define FB10_COMPAT_PRIVATE(func, sym) \ 66 WEAK_REF(func, SYM_FBP10(sym)); \ 67 SYM_DEFAULT(sym, SYM_FBP10(sym), FBSDprivate_1.0) 68 69 #include "pthread_md.h" 70 #include "thr_umtx.h" 71 #include "thread_db.h" 72 73 #ifdef _PTHREAD_FORCED_UNWIND 74 #define _BSD_SOURCE 75 #include <unwind.h> 76 #endif 77 78 typedef TAILQ_HEAD(pthreadlist, pthread) pthreadlist; 79 typedef TAILQ_HEAD(atfork_head, pthread_atfork) atfork_head; 80 TAILQ_HEAD(mutex_queue, pthread_mutex); 81 82 /* Signal to do cancellation */ 83 #define SIGCANCEL SIGTHR 84 85 /* 86 * Kernel fatal error handler macro. 87 */ 88 #define PANIC(string) _thread_exit(__FILE__,__LINE__,string) 89 90 /* Output debug messages like this: */ 91 #define stdout_debug(args...) _thread_printf(STDOUT_FILENO, ##args) 92 #define stderr_debug(args...) _thread_printf(STDERR_FILENO, ##args) 93 94 #ifdef _PTHREADS_INVARIANTS 95 #define THR_ASSERT(cond, msg) do { \ 96 if (__predict_false(!(cond))) \ 97 PANIC(msg); \ 98 } while (0) 99 #else 100 #define THR_ASSERT(cond, msg) 101 #endif 102 103 #ifdef PIC 104 # define STATIC_LIB_REQUIRE(name) 105 #else 106 # define STATIC_LIB_REQUIRE(name) __asm (".globl " #name) 107 #endif 108 109 #define TIMESPEC_ADD(dst, src, val) \ 110 do { \ 111 (dst)->tv_sec = (src)->tv_sec + (val)->tv_sec; \ 112 (dst)->tv_nsec = (src)->tv_nsec + (val)->tv_nsec; \ 113 if ((dst)->tv_nsec >= 1000000000) { \ 114 (dst)->tv_sec++; \ 115 (dst)->tv_nsec -= 1000000000; \ 116 } \ 117 } while (0) 118 119 #define TIMESPEC_SUB(dst, src, val) \ 120 do { \ 121 (dst)->tv_sec = (src)->tv_sec - (val)->tv_sec; \ 122 (dst)->tv_nsec = (src)->tv_nsec - (val)->tv_nsec; \ 123 if ((dst)->tv_nsec < 0) { \ 124 (dst)->tv_sec--; \ 125 (dst)->tv_nsec += 1000000000; \ 126 } \ 127 } while (0) 128 129 /* Magic cookie set for shared pthread locks and cv's pointers */ 130 #define THR_PSHARED_PTR \ 131 ((void *)(uintptr_t)((1ULL << (NBBY * sizeof(long) - 1)) | 1)) 132 133 /* XXX These values should be same as those defined in pthread.h */ 134 #define THR_MUTEX_INITIALIZER ((struct pthread_mutex *)NULL) 135 #define THR_ADAPTIVE_MUTEX_INITIALIZER ((struct pthread_mutex *)1) 136 #define THR_MUTEX_DESTROYED ((struct pthread_mutex *)2) 137 #define THR_COND_INITIALIZER ((struct pthread_cond *)NULL) 138 #define THR_COND_DESTROYED ((struct pthread_cond *)1) 139 #define THR_RWLOCK_INITIALIZER ((struct pthread_rwlock *)NULL) 140 #define THR_RWLOCK_DESTROYED ((struct pthread_rwlock *)1) 141 142 #define PMUTEX_FLAG_TYPE_MASK 0x0ff 143 #define PMUTEX_FLAG_PRIVATE 0x100 144 #define PMUTEX_FLAG_DEFERED 0x200 145 #define PMUTEX_TYPE(mtxflags) ((mtxflags) & PMUTEX_FLAG_TYPE_MASK) 146 147 #define MAX_DEFER_WAITERS 50 148 149 struct pthread_mutex { 150 /* 151 * Lock for accesses to this structure. 152 */ 153 struct umutex m_lock; 154 int m_flags; 155 uint32_t m_owner; 156 int m_count; 157 int m_spinloops; 158 int m_yieldloops; 159 /* 160 * Link for all mutexes a thread currently owns, of the same 161 * prio type. 162 */ 163 TAILQ_ENTRY(pthread_mutex) m_qe; 164 /* Link for all private mutexes a thread currently owns. */ 165 TAILQ_ENTRY(pthread_mutex) m_pqe; 166 }; 167 168 struct pthread_mutex_attr { 169 enum pthread_mutextype m_type; 170 int m_protocol; 171 int m_ceiling; 172 int m_pshared; 173 }; 174 175 #define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \ 176 { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE } 177 178 struct pthread_cond { 179 __uint32_t __has_user_waiters; 180 __uint32_t __has_kern_waiters; 181 __uint32_t __flags; 182 __uint32_t __clock_id; 183 }; 184 185 struct pthread_cond_attr { 186 int c_pshared; 187 int c_clockid; 188 }; 189 190 struct pthread_barrier { 191 struct umutex b_lock; 192 struct ucond b_cv; 193 int64_t b_cycle; 194 int b_count; 195 int b_waiters; 196 int b_refcount; 197 int b_destroying; 198 }; 199 200 struct pthread_barrierattr { 201 int pshared; 202 }; 203 204 struct pthread_spinlock { 205 struct umutex s_lock; 206 }; 207 208 /* 209 * Flags for condition variables. 210 */ 211 #define COND_FLAGS_PRIVATE 0x01 212 #define COND_FLAGS_INITED 0x02 213 #define COND_FLAGS_BUSY 0x04 214 215 /* 216 * Cleanup definitions. 217 */ 218 struct pthread_cleanup { 219 struct pthread_cleanup *prev; 220 void (*routine)(void *); 221 void *routine_arg; 222 int onheap; 223 }; 224 225 #define THR_CLEANUP_PUSH(td, func, arg) { \ 226 struct pthread_cleanup __cup; \ 227 \ 228 __cup.routine = func; \ 229 __cup.routine_arg = arg; \ 230 __cup.onheap = 0; \ 231 __cup.prev = (td)->cleanup; \ 232 (td)->cleanup = &__cup; 233 234 #define THR_CLEANUP_POP(td, exec) \ 235 (td)->cleanup = __cup.prev; \ 236 if ((exec) != 0) \ 237 __cup.routine(__cup.routine_arg); \ 238 } 239 240 struct pthread_atfork { 241 TAILQ_ENTRY(pthread_atfork) qe; 242 void (*prepare)(void); 243 void (*parent)(void); 244 void (*child)(void); 245 }; 246 247 struct pthread_attr { 248 #define pthread_attr_start_copy sched_policy 249 int sched_policy; 250 int sched_inherit; 251 int prio; 252 int suspend; 253 #define THR_STACK_USER 0x100 /* 0xFF reserved for <pthread.h> */ 254 int flags; 255 void *stackaddr_attr; 256 size_t stacksize_attr; 257 size_t guardsize_attr; 258 #define pthread_attr_end_copy cpuset 259 cpuset_t *cpuset; 260 size_t cpusetsize; 261 }; 262 263 struct wake_addr { 264 struct wake_addr *link; 265 unsigned int value; 266 char pad[12]; 267 }; 268 269 struct sleepqueue { 270 TAILQ_HEAD(, pthread) sq_blocked; 271 SLIST_HEAD(, sleepqueue) sq_freeq; 272 LIST_ENTRY(sleepqueue) sq_hash; 273 SLIST_ENTRY(sleepqueue) sq_flink; 274 void *sq_wchan; 275 int sq_type; 276 }; 277 278 /* 279 * Thread creation state attributes. 280 */ 281 #define THR_CREATE_RUNNING 0 282 #define THR_CREATE_SUSPENDED 1 283 284 /* 285 * Miscellaneous definitions. 286 */ 287 #define THR_STACK_DEFAULT (sizeof(void *) / 4 * 1024 * 1024) 288 289 /* 290 * Maximum size of initial thread's stack. This perhaps deserves to be larger 291 * than the stacks of other threads, since many applications are likely to run 292 * almost entirely on this stack. 293 */ 294 #define THR_STACK_INITIAL (THR_STACK_DEFAULT * 2) 295 296 /* 297 * Define priorities returned by kernel. 298 */ 299 #define THR_MIN_PRIORITY (_thr_priorities[SCHED_OTHER-1].pri_min) 300 #define THR_MAX_PRIORITY (_thr_priorities[SCHED_OTHER-1].pri_max) 301 #define THR_DEF_PRIORITY (_thr_priorities[SCHED_OTHER-1].pri_default) 302 303 #define THR_MIN_RR_PRIORITY (_thr_priorities[SCHED_RR-1].pri_min) 304 #define THR_MAX_RR_PRIORITY (_thr_priorities[SCHED_RR-1].pri_max) 305 #define THR_DEF_RR_PRIORITY (_thr_priorities[SCHED_RR-1].pri_default) 306 307 /* XXX The SCHED_FIFO should have same priority range as SCHED_RR */ 308 #define THR_MIN_FIFO_PRIORITY (_thr_priorities[SCHED_FIFO_1].pri_min) 309 #define THR_MAX_FIFO_PRIORITY (_thr_priorities[SCHED_FIFO-1].pri_max) 310 #define THR_DEF_FIFO_PRIORITY (_thr_priorities[SCHED_FIFO-1].pri_default) 311 312 struct pthread_prio { 313 int pri_min; 314 int pri_max; 315 int pri_default; 316 }; 317 318 struct pthread_rwlockattr { 319 int pshared; 320 }; 321 322 struct pthread_rwlock { 323 struct urwlock lock; 324 uint32_t owner; 325 }; 326 327 /* 328 * Thread states. 329 */ 330 enum pthread_state { 331 PS_RUNNING, 332 PS_DEAD 333 }; 334 335 struct pthread_specific_elem { 336 const void *data; 337 int seqno; 338 }; 339 340 struct pthread_key { 341 volatile int allocated; 342 int seqno; 343 void (*destructor)(void *); 344 }; 345 346 /* 347 * lwpid_t is 32bit but kernel thr API exports tid as long type 348 * to preserve the ABI for M:N model in very early date (r131431). 349 */ 350 #define TID(thread) ((uint32_t) ((thread)->tid)) 351 352 /* 353 * Thread structure. 354 */ 355 struct pthread { 356 #define _pthread_startzero tid 357 /* Kernel thread id. */ 358 long tid; 359 #define TID_TERMINATED 1 360 361 /* 362 * Lock for accesses to this thread structure. 363 */ 364 struct umutex lock; 365 366 /* Internal condition variable cycle number. */ 367 uint32_t cycle; 368 369 /* How many low level locks the thread held. */ 370 int locklevel; 371 372 /* 373 * Set to non-zero when this thread has entered a critical 374 * region. We allow for recursive entries into critical regions. 375 */ 376 int critical_count; 377 378 /* Signal blocked counter. */ 379 int sigblock; 380 381 /* Queue entry for list of all threads. */ 382 TAILQ_ENTRY(pthread) tle; /* link for all threads in process */ 383 384 /* Queue entry for GC lists. */ 385 TAILQ_ENTRY(pthread) gcle; 386 387 /* Hash queue entry. */ 388 LIST_ENTRY(pthread) hle; 389 390 /* Sleep queue entry */ 391 TAILQ_ENTRY(pthread) wle; 392 393 /* Threads reference count. */ 394 int refcount; 395 396 /* 397 * Thread start routine, argument, stack pointer and thread 398 * attributes. 399 */ 400 void *(*start_routine)(void *); 401 void *arg; 402 struct pthread_attr attr; 403 404 #define SHOULD_CANCEL(thr) \ 405 ((thr)->cancel_pending && (thr)->cancel_enable && \ 406 (thr)->no_cancel == 0) 407 408 /* Cancellation is enabled */ 409 int cancel_enable; 410 411 /* Cancellation request is pending */ 412 int cancel_pending; 413 414 /* Thread is at cancellation point */ 415 int cancel_point; 416 417 /* Cancellation is temporarily disabled */ 418 int no_cancel; 419 420 /* Asynchronouse cancellation is enabled */ 421 int cancel_async; 422 423 /* Cancellation is in progress */ 424 int cancelling; 425 426 /* Thread temporary signal mask. */ 427 sigset_t sigmask; 428 429 /* Thread should unblock SIGCANCEL. */ 430 int unblock_sigcancel; 431 432 /* In sigsuspend state */ 433 int in_sigsuspend; 434 435 /* deferred signal info */ 436 siginfo_t deferred_siginfo; 437 438 /* signal mask to restore. */ 439 sigset_t deferred_sigmask; 440 441 /* the sigaction should be used for deferred signal. */ 442 struct sigaction deferred_sigact; 443 444 /* deferred signal delivery is performed, do not reenter. */ 445 int deferred_run; 446 447 /* Force new thread to exit. */ 448 int force_exit; 449 450 /* Thread state: */ 451 enum pthread_state state; 452 453 /* 454 * Error variable used instead of errno. The function __error() 455 * returns a pointer to this. 456 */ 457 int error; 458 459 /* 460 * The joiner is the thread that is joining to this thread. The 461 * join status keeps track of a join operation to another thread. 462 */ 463 struct pthread *joiner; 464 465 /* Miscellaneous flags; only set with scheduling lock held. */ 466 int flags; 467 #define THR_FLAGS_PRIVATE 0x0001 468 #define THR_FLAGS_NEED_SUSPEND 0x0002 /* thread should be suspended */ 469 #define THR_FLAGS_SUSPENDED 0x0004 /* thread is suspended */ 470 #define THR_FLAGS_DETACHED 0x0008 /* thread is detached */ 471 472 /* Thread list flags; only set with thread list lock held. */ 473 int tlflags; 474 #define TLFLAGS_GC_SAFE 0x0001 /* thread safe for cleaning */ 475 #define TLFLAGS_IN_TDLIST 0x0002 /* thread in all thread list */ 476 #define TLFLAGS_IN_GCLIST 0x0004 /* thread in gc list */ 477 478 /* 479 * Queues of the owned mutexes. Private queue must have index 480 * + 1 of the corresponding full queue. 481 */ 482 #define TMQ_NORM 0 /* NORMAL or PRIO_INHERIT normal */ 483 #define TMQ_NORM_PRIV 1 /* NORMAL or PRIO_INHERIT normal priv */ 484 #define TMQ_NORM_PP 2 /* PRIO_PROTECT normal mutexes */ 485 #define TMQ_NORM_PP_PRIV 3 /* PRIO_PROTECT normal priv */ 486 #define TMQ_NITEMS 4 487 struct mutex_queue mq[TMQ_NITEMS]; 488 489 void *ret; 490 struct pthread_specific_elem *specific; 491 int specific_data_count; 492 493 /* Number rwlocks rdlocks held. */ 494 int rdlock_count; 495 496 /* 497 * Current locks bitmap for rtld. */ 498 int rtld_bits; 499 500 /* Thread control block */ 501 struct tcb *tcb; 502 503 /* Cleanup handlers Link List */ 504 struct pthread_cleanup *cleanup; 505 506 #ifdef _PTHREAD_FORCED_UNWIND 507 struct _Unwind_Exception ex; 508 void *unwind_stackend; 509 int unwind_disabled; 510 #endif 511 512 /* 513 * Magic value to help recognize a valid thread structure 514 * from an invalid one: 515 */ 516 #define THR_MAGIC ((u_int32_t) 0xd09ba115) 517 u_int32_t magic; 518 519 /* Enable event reporting */ 520 int report_events; 521 522 /* Event mask */ 523 int event_mask; 524 525 /* Event */ 526 td_event_msg_t event_buf; 527 528 /* Wait channel */ 529 void *wchan; 530 531 /* Referenced mutex. */ 532 struct pthread_mutex *mutex_obj; 533 534 /* Thread will sleep. */ 535 int will_sleep; 536 537 /* Number of threads deferred. */ 538 int nwaiter_defer; 539 540 /* Deferred threads from pthread_cond_signal. */ 541 unsigned int *defer_waiters[MAX_DEFER_WAITERS]; 542 #define _pthread_endzero wake_addr 543 544 struct wake_addr *wake_addr; 545 #define WAKE_ADDR(td) ((td)->wake_addr) 546 547 /* Sleep queue */ 548 struct sleepqueue *sleepqueue; 549 550 }; 551 552 #define THR_SHOULD_GC(thrd) \ 553 ((thrd)->refcount == 0 && (thrd)->state == PS_DEAD && \ 554 ((thrd)->flags & THR_FLAGS_DETACHED) != 0) 555 556 #define THR_IN_CRITICAL(thrd) \ 557 (((thrd)->locklevel > 0) || \ 558 ((thrd)->critical_count > 0)) 559 560 #define THR_CRITICAL_ENTER(thrd) \ 561 (thrd)->critical_count++ 562 563 #define THR_CRITICAL_LEAVE(thrd) \ 564 do { \ 565 (thrd)->critical_count--; \ 566 _thr_ast(thrd); \ 567 } while (0) 568 569 #define THR_UMUTEX_TRYLOCK(thrd, lck) \ 570 _thr_umutex_trylock((lck), TID(thrd)) 571 572 #define THR_UMUTEX_LOCK(thrd, lck) \ 573 _thr_umutex_lock((lck), TID(thrd)) 574 575 #define THR_UMUTEX_TIMEDLOCK(thrd, lck, timo) \ 576 _thr_umutex_timedlock((lck), TID(thrd), (timo)) 577 578 #define THR_UMUTEX_UNLOCK(thrd, lck) \ 579 _thr_umutex_unlock((lck), TID(thrd)) 580 581 #define THR_LOCK_ACQUIRE(thrd, lck) \ 582 do { \ 583 (thrd)->locklevel++; \ 584 _thr_umutex_lock(lck, TID(thrd)); \ 585 } while (0) 586 587 #define THR_LOCK_ACQUIRE_SPIN(thrd, lck) \ 588 do { \ 589 (thrd)->locklevel++; \ 590 _thr_umutex_lock_spin(lck, TID(thrd)); \ 591 } while (0) 592 593 #ifdef _PTHREADS_INVARIANTS 594 #define THR_ASSERT_LOCKLEVEL(thrd) \ 595 do { \ 596 if (__predict_false((thrd)->locklevel <= 0)) \ 597 _thr_assert_lock_level(); \ 598 } while (0) 599 #else 600 #define THR_ASSERT_LOCKLEVEL(thrd) 601 #endif 602 603 #define THR_LOCK_RELEASE(thrd, lck) \ 604 do { \ 605 THR_ASSERT_LOCKLEVEL(thrd); \ 606 _thr_umutex_unlock((lck), TID(thrd)); \ 607 (thrd)->locklevel--; \ 608 _thr_ast(thrd); \ 609 } while (0) 610 611 #define THR_LOCK(curthrd) THR_LOCK_ACQUIRE(curthrd, &(curthrd)->lock) 612 #define THR_UNLOCK(curthrd) THR_LOCK_RELEASE(curthrd, &(curthrd)->lock) 613 #define THR_THREAD_LOCK(curthrd, thr) THR_LOCK_ACQUIRE(curthrd, &(thr)->lock) 614 #define THR_THREAD_UNLOCK(curthrd, thr) THR_LOCK_RELEASE(curthrd, &(thr)->lock) 615 616 #define THREAD_LIST_RDLOCK(curthrd) \ 617 do { \ 618 (curthrd)->locklevel++; \ 619 _thr_rwl_rdlock(&_thr_list_lock); \ 620 } while (0) 621 622 #define THREAD_LIST_WRLOCK(curthrd) \ 623 do { \ 624 (curthrd)->locklevel++; \ 625 _thr_rwl_wrlock(&_thr_list_lock); \ 626 } while (0) 627 628 #define THREAD_LIST_UNLOCK(curthrd) \ 629 do { \ 630 _thr_rwl_unlock(&_thr_list_lock); \ 631 (curthrd)->locklevel--; \ 632 _thr_ast(curthrd); \ 633 } while (0) 634 635 /* 636 * Macros to insert/remove threads to the all thread list and 637 * the gc list. 638 */ 639 #define THR_LIST_ADD(thrd) do { \ 640 if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) == 0) { \ 641 TAILQ_INSERT_HEAD(&_thread_list, thrd, tle); \ 642 _thr_hash_add(thrd); \ 643 (thrd)->tlflags |= TLFLAGS_IN_TDLIST; \ 644 } \ 645 } while (0) 646 #define THR_LIST_REMOVE(thrd) do { \ 647 if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) != 0) { \ 648 TAILQ_REMOVE(&_thread_list, thrd, tle); \ 649 _thr_hash_remove(thrd); \ 650 (thrd)->tlflags &= ~TLFLAGS_IN_TDLIST; \ 651 } \ 652 } while (0) 653 #define THR_GCLIST_ADD(thrd) do { \ 654 if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) == 0) { \ 655 TAILQ_INSERT_HEAD(&_thread_gc_list, thrd, gcle);\ 656 (thrd)->tlflags |= TLFLAGS_IN_GCLIST; \ 657 _gc_count++; \ 658 } \ 659 } while (0) 660 #define THR_GCLIST_REMOVE(thrd) do { \ 661 if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) != 0) { \ 662 TAILQ_REMOVE(&_thread_gc_list, thrd, gcle); \ 663 (thrd)->tlflags &= ~TLFLAGS_IN_GCLIST; \ 664 _gc_count--; \ 665 } \ 666 } while (0) 667 668 #define THR_REF_ADD(curthread, pthread) { \ 669 THR_CRITICAL_ENTER(curthread); \ 670 pthread->refcount++; \ 671 } while (0) 672 673 #define THR_REF_DEL(curthread, pthread) { \ 674 pthread->refcount--; \ 675 THR_CRITICAL_LEAVE(curthread); \ 676 } while (0) 677 678 #define GC_NEEDED() (_gc_count >= 5) 679 680 #define SHOULD_REPORT_EVENT(curthr, e) \ 681 (curthr->report_events && \ 682 (((curthr)->event_mask | _thread_event_mask ) & e) != 0) 683 684 extern int __isthreaded; 685 686 /* 687 * Global variables for the pthread kernel. 688 */ 689 690 extern char *_usrstack __hidden; 691 extern struct pthread *_thr_initial __hidden; 692 693 /* For debugger */ 694 extern int _libthr_debug; 695 extern int _thread_event_mask; 696 extern struct pthread *_thread_last_event; 697 698 /* List of all threads: */ 699 extern pthreadlist _thread_list; 700 701 /* List of threads needing GC: */ 702 extern pthreadlist _thread_gc_list __hidden; 703 704 extern int _thread_active_threads; 705 extern atfork_head _thr_atfork_list __hidden; 706 extern struct urwlock _thr_atfork_lock __hidden; 707 708 /* Default thread attributes: */ 709 extern struct pthread_attr _pthread_attr_default __hidden; 710 711 /* Default mutex attributes: */ 712 extern struct pthread_mutex_attr _pthread_mutexattr_default __hidden; 713 extern struct pthread_mutex_attr _pthread_mutexattr_adaptive_default __hidden; 714 715 /* Default condition variable attributes: */ 716 extern struct pthread_cond_attr _pthread_condattr_default __hidden; 717 718 extern struct pthread_prio _thr_priorities[] __hidden; 719 720 extern pid_t _thr_pid __hidden; 721 extern int _thr_is_smp __hidden; 722 723 extern size_t _thr_guard_default __hidden; 724 extern size_t _thr_stack_default __hidden; 725 extern size_t _thr_stack_initial __hidden; 726 extern int _thr_page_size __hidden; 727 extern int _thr_spinloops __hidden; 728 extern int _thr_yieldloops __hidden; 729 extern int _thr_queuefifo __hidden; 730 731 /* Garbage thread count. */ 732 extern int _gc_count __hidden; 733 734 extern struct umutex _mutex_static_lock __hidden; 735 extern struct umutex _cond_static_lock __hidden; 736 extern struct umutex _rwlock_static_lock __hidden; 737 extern struct umutex _keytable_lock __hidden; 738 extern struct urwlock _thr_list_lock __hidden; 739 extern struct umutex _thr_event_lock __hidden; 740 extern struct umutex _suspend_all_lock __hidden; 741 extern int _suspend_all_waiters __hidden; 742 extern int _suspend_all_cycle __hidden; 743 extern struct pthread *_single_thread __hidden; 744 745 /* 746 * Function prototype definitions. 747 */ 748 __BEGIN_DECLS 749 int _thr_setthreaded(int) __hidden; 750 int _mutex_cv_lock(struct pthread_mutex *, int) __hidden; 751 int _mutex_cv_unlock(struct pthread_mutex *, int *, int *) __hidden; 752 int _mutex_cv_attach(struct pthread_mutex *, int) __hidden; 753 int _mutex_cv_detach(struct pthread_mutex *, int *) __hidden; 754 int _mutex_owned(struct pthread *, const struct pthread_mutex *) __hidden; 755 int _mutex_reinit(pthread_mutex_t *) __hidden; 756 void _mutex_fork(struct pthread *curthread) __hidden; 757 void _libpthread_init(struct pthread *) __hidden; 758 struct pthread *_thr_alloc(struct pthread *) __hidden; 759 void _thread_exit(const char *, int, const char *) __hidden __dead2; 760 int _thr_ref_add(struct pthread *, struct pthread *, int) __hidden; 761 void _thr_ref_delete(struct pthread *, struct pthread *) __hidden; 762 void _thr_ref_delete_unlocked(struct pthread *, struct pthread *) __hidden; 763 int _thr_find_thread(struct pthread *, struct pthread *, int) __hidden; 764 void _thr_rtld_init(void) __hidden; 765 void _thr_rtld_postfork_child(void) __hidden; 766 int _thr_stack_alloc(struct pthread_attr *) __hidden; 767 void _thr_stack_free(struct pthread_attr *) __hidden; 768 void _thr_free(struct pthread *, struct pthread *) __hidden; 769 void _thr_gc(struct pthread *) __hidden; 770 void _thread_cleanupspecific(void) __hidden; 771 void _thread_printf(int, const char *, ...) __hidden; 772 void _thr_spinlock_init(void) __hidden; 773 void _thr_cancel_enter(struct pthread *) __hidden; 774 void _thr_cancel_enter2(struct pthread *, int) __hidden; 775 void _thr_cancel_leave(struct pthread *, int) __hidden; 776 void _thr_testcancel(struct pthread *) __hidden; 777 void _thr_signal_block(struct pthread *) __hidden; 778 void _thr_signal_unblock(struct pthread *) __hidden; 779 void _thr_signal_init(int) __hidden; 780 void _thr_signal_deinit(void) __hidden; 781 int _thr_send_sig(struct pthread *, int sig) __hidden; 782 void _thr_list_init(void) __hidden; 783 void _thr_hash_add(struct pthread *) __hidden; 784 void _thr_hash_remove(struct pthread *) __hidden; 785 struct pthread *_thr_hash_find(struct pthread *) __hidden; 786 void _thr_link(struct pthread *, struct pthread *) __hidden; 787 void _thr_unlink(struct pthread *, struct pthread *) __hidden; 788 void _thr_assert_lock_level(void) __hidden __dead2; 789 void _thr_ast(struct pthread *) __hidden; 790 void _thr_once_init(void) __hidden; 791 void _thr_report_creation(struct pthread *curthread, 792 struct pthread *newthread) __hidden; 793 void _thr_report_death(struct pthread *curthread) __hidden; 794 int _thr_getscheduler(lwpid_t, int *, struct sched_param *) __hidden; 795 int _thr_setscheduler(lwpid_t, int, const struct sched_param *) __hidden; 796 void _thr_signal_prefork(void) __hidden; 797 void _thr_signal_postfork(void) __hidden; 798 void _thr_signal_postfork_child(void) __hidden; 799 void _thr_suspend_all_lock(struct pthread *) __hidden; 800 void _thr_suspend_all_unlock(struct pthread *) __hidden; 801 void _thr_try_gc(struct pthread *, struct pthread *) __hidden; 802 int _rtp_to_schedparam(const struct rtprio *rtp, int *policy, 803 struct sched_param *param) __hidden; 804 int _schedparam_to_rtp(int policy, const struct sched_param *param, 805 struct rtprio *rtp) __hidden; 806 void _thread_bp_create(void); 807 void _thread_bp_death(void); 808 int _sched_yield(void); 809 810 void _pthread_cleanup_push(void (*)(void *), void *); 811 void _pthread_cleanup_pop(int); 812 void _pthread_exit_mask(void *status, sigset_t *mask) __dead2 __hidden; 813 void _pthread_cancel_enter(int maycancel); 814 void _pthread_cancel_leave(int maycancel); 815 816 /* #include <fcntl.h> */ 817 #ifdef _SYS_FCNTL_H_ 818 int __sys_fcntl(int, int, ...); 819 int __sys_openat(int, const char *, int, ...); 820 #endif 821 822 /* #include <signal.h> */ 823 #ifdef _SIGNAL_H_ 824 int __sys_kill(pid_t, int); 825 int __sys_sigaction(int, const struct sigaction *, struct sigaction *); 826 int __sys_sigpending(sigset_t *); 827 int __sys_sigprocmask(int, const sigset_t *, sigset_t *); 828 int __sys_sigsuspend(const sigset_t *); 829 int __sys_sigreturn(const ucontext_t *); 830 int __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *); 831 int __sys_sigwait(const sigset_t *, int *); 832 int __sys_sigtimedwait(const sigset_t *, siginfo_t *, 833 const struct timespec *); 834 int __sys_sigwaitinfo(const sigset_t *set, siginfo_t *info); 835 #endif 836 837 /* #include <time.h> */ 838 #ifdef _TIME_H_ 839 int __sys_nanosleep(const struct timespec *, struct timespec *); 840 #endif 841 842 /* #include <sys/ucontext.h> */ 843 #ifdef _SYS_UCONTEXT_H_ 844 int __sys_setcontext(const ucontext_t *ucp); 845 int __sys_swapcontext(ucontext_t *oucp, const ucontext_t *ucp); 846 #endif 847 848 /* #include <unistd.h> */ 849 #ifdef _UNISTD_H_ 850 int __sys_close(int); 851 int __sys_fork(void); 852 pid_t __sys_getpid(void); 853 ssize_t __sys_read(int, void *, size_t); 854 void __sys_exit(int); 855 #endif 856 857 static inline int 858 _thr_isthreaded(void) 859 { 860 return (__isthreaded != 0); 861 } 862 863 static inline int 864 _thr_is_inited(void) 865 { 866 return (_thr_initial != NULL); 867 } 868 869 static inline void 870 _thr_check_init(void) 871 { 872 if (_thr_initial == NULL) 873 _libpthread_init(NULL); 874 } 875 876 struct wake_addr *_thr_alloc_wake_addr(void); 877 void _thr_release_wake_addr(struct wake_addr *); 878 int _thr_sleep(struct pthread *, int, const struct timespec *); 879 880 void _thr_wake_addr_init(void) __hidden; 881 882 static inline void 883 _thr_clear_wake(struct pthread *td) 884 { 885 td->wake_addr->value = 0; 886 } 887 888 static inline int 889 _thr_is_woken(struct pthread *td) 890 { 891 return td->wake_addr->value != 0; 892 } 893 894 static inline void 895 _thr_set_wake(unsigned int *waddr) 896 { 897 *waddr = 1; 898 _thr_umtx_wake(waddr, INT_MAX, 0); 899 } 900 901 void _thr_wake_all(unsigned int *waddrs[], int) __hidden; 902 903 static inline struct pthread * 904 _sleepq_first(struct sleepqueue *sq) 905 { 906 return TAILQ_FIRST(&sq->sq_blocked); 907 } 908 909 void _sleepq_init(void) __hidden; 910 struct sleepqueue *_sleepq_alloc(void) __hidden; 911 void _sleepq_free(struct sleepqueue *) __hidden; 912 void _sleepq_lock(void *) __hidden; 913 void _sleepq_unlock(void *) __hidden; 914 struct sleepqueue *_sleepq_lookup(void *) __hidden; 915 void _sleepq_add(void *, struct pthread *) __hidden; 916 int _sleepq_remove(struct sleepqueue *, struct pthread *) __hidden; 917 void _sleepq_drop(struct sleepqueue *, 918 void (*cb)(struct pthread *, void *arg), void *) __hidden; 919 920 int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex, 921 void *(calloc_cb)(size_t, size_t)); 922 923 struct dl_phdr_info; 924 void __pthread_cxa_finalize(struct dl_phdr_info *phdr_info); 925 void _thr_tsd_unload(struct dl_phdr_info *phdr_info) __hidden; 926 void _thr_sigact_unload(struct dl_phdr_info *phdr_info) __hidden; 927 void _thr_stack_fix_protection(struct pthread *thrd); 928 929 int *__error_threaded(void) __hidden; 930 void __thr_interpose_libc(void) __hidden; 931 pid_t __thr_fork(void); 932 int __thr_setcontext(const ucontext_t *ucp); 933 int __thr_sigaction(int sig, const struct sigaction *act, 934 struct sigaction *oact) __hidden; 935 int __thr_sigprocmask(int how, const sigset_t *set, sigset_t *oset); 936 int __thr_sigsuspend(const sigset_t * set); 937 int __thr_sigtimedwait(const sigset_t *set, siginfo_t *info, 938 const struct timespec * timeout); 939 int __thr_sigwait(const sigset_t *set, int *sig); 940 int __thr_sigwaitinfo(const sigset_t *set, siginfo_t *info); 941 int __thr_swapcontext(ucontext_t *oucp, const ucontext_t *ucp); 942 943 void __thr_map_stacks_exec(void); 944 945 struct _spinlock; 946 void __thr_spinunlock(struct _spinlock *lck); 947 void __thr_spinlock(struct _spinlock *lck); 948 949 struct tcb *_tcb_ctor(struct pthread *, int); 950 void _tcb_dtor(struct tcb *); 951 952 void __thr_pshared_init(void) __hidden; 953 void *__thr_pshared_offpage(void *key, int doalloc) __hidden; 954 void __thr_pshared_destroy(void *key) __hidden; 955 956 __END_DECLS 957 958 #endif /* !_THR_PRIVATE_H */ 959