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