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