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>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
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.
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.
32 #ifndef _THR_PRIVATE_H
33 #define _THR_PRIVATE_H
38 #include <sys/types.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>
56 #define __hidden __attribute__((visibility("hidden")))
59 #include "pthread_md.h"
61 #include "thread_db.h"
63 typedef TAILQ_HEAD(pthreadlist, pthread) pthreadlist;
64 typedef TAILQ_HEAD(atfork_head, pthread_atfork) atfork_head;
65 TAILQ_HEAD(mutex_queue, pthread_mutex);
67 /* Signal to do cancellation */
71 * Kernel fatal error handler macro.
73 #define PANIC(string) _thread_exit(__FILE__,__LINE__,string)
75 /* Output debug messages like this: */
76 #define stdout_debug(args...) _thread_printf(STDOUT_FILENO, ##args)
77 #define stderr_debug(args...) _thread_printf(STDERR_FILENO, ##args)
79 #ifdef _PTHREADS_INVARIANTS
80 #define THR_ASSERT(cond, msg) do { \
81 if (__predict_false(!(cond))) \
85 #define THR_ASSERT(cond, msg)
89 # define STATIC_LIB_REQUIRE(name)
91 # define STATIC_LIB_REQUIRE(name) __asm (".globl " #name)
94 #define TIMESPEC_ADD(dst, src, val) \
96 (dst)->tv_sec = (src)->tv_sec + (val)->tv_sec; \
97 (dst)->tv_nsec = (src)->tv_nsec + (val)->tv_nsec; \
98 if ((dst)->tv_nsec >= 1000000000) { \
100 (dst)->tv_nsec -= 1000000000; \
104 #define TIMESPEC_SUB(dst, src, val) \
106 (dst)->tv_sec = (src)->tv_sec - (val)->tv_sec; \
107 (dst)->tv_nsec = (src)->tv_nsec - (val)->tv_nsec; \
108 if ((dst)->tv_nsec < 0) { \
110 (dst)->tv_nsec += 1000000000; \
114 struct pthread_mutex {
116 * Lock for accesses to this structure.
118 struct umutex m_lock;
119 enum pthread_mutextype m_type;
120 struct pthread *m_owner;
127 * Link for all mutexes a thread currently owns.
129 TAILQ_ENTRY(pthread_mutex) m_qe;
135 #define MUTEX_FLAGS_PRIVATE 0x01
136 #define MUTEX_FLAGS_INITED 0x02
137 #define MUTEX_FLAGS_BUSY 0x04
139 struct pthread_mutex_attr {
140 enum pthread_mutextype m_type;
146 #define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \
147 { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE }
149 struct pthread_cond {
150 struct umutex c_lock;
151 struct ucond c_kerncv;
156 struct pthread_cond_attr {
161 struct pthread_barrier {
162 struct umutex b_lock;
164 volatile int64_t b_cycle;
165 volatile int b_count;
166 volatile int b_waiters;
169 struct pthread_barrierattr {
173 struct pthread_spinlock {
174 struct umutex s_lock;
178 * Flags for condition variables.
180 #define COND_FLAGS_PRIVATE 0x01
181 #define COND_FLAGS_INITED 0x02
182 #define COND_FLAGS_BUSY 0x04
185 * Cleanup definitions.
187 struct pthread_cleanup {
188 struct pthread_cleanup *next;
189 void (*routine)(void *args);
194 #define THR_CLEANUP_PUSH(td, func, arg) { \
195 struct pthread_cleanup __cup; \
197 __cup.routine = func; \
198 __cup.routine_arg = arg; \
200 __cup.next = (td)->cleanup; \
201 (td)->cleanup = &__cup;
203 #define THR_CLEANUP_POP(td, exec) \
204 (td)->cleanup = __cup.next; \
206 __cup.routine(__cup.routine_arg); \
209 struct pthread_atfork {
210 TAILQ_ENTRY(pthread_atfork) qe;
211 void (*prepare)(void);
212 void (*parent)(void);
216 struct pthread_attr {
221 #define THR_STACK_USER 0x100 /* 0xFF reserved for <pthread.h> */
223 void *stackaddr_attr;
224 size_t stacksize_attr;
225 size_t guardsize_attr;
231 * Thread creation state attributes.
233 #define THR_CREATE_RUNNING 0
234 #define THR_CREATE_SUSPENDED 1
237 * Miscellaneous definitions.
239 #define THR_STACK_DEFAULT (sizeof(void *) / 4 * 1024 * 1024)
242 * Maximum size of initial thread's stack. This perhaps deserves to be larger
243 * than the stacks of other threads, since many applications are likely to run
244 * almost entirely on this stack.
246 #define THR_STACK_INITIAL (THR_STACK_DEFAULT * 2)
249 * Define priorities returned by kernel.
251 #define THR_MIN_PRIORITY (_thr_priorities[SCHED_OTHER-1].pri_min)
252 #define THR_MAX_PRIORITY (_thr_priorities[SCHED_OTHER-1].pri_max)
253 #define THR_DEF_PRIORITY (_thr_priorities[SCHED_OTHER-1].pri_default)
255 #define THR_MIN_RR_PRIORITY (_thr_priorities[SCHED_RR-1].pri_min)
256 #define THR_MAX_RR_PRIORITY (_thr_priorities[SCHED_RR-1].pri_max)
257 #define THR_DEF_RR_PRIORITY (_thr_priorities[SCHED_RR-1].pri_default)
259 /* XXX The SCHED_FIFO should have same priority range as SCHED_RR */
260 #define THR_MIN_FIFO_PRIORITY (_thr_priorities[SCHED_FIFO_1].pri_min)
261 #define THR_MAX_FIFO_PRIORITY (_thr_priorities[SCHED_FIFO-1].pri_max)
262 #define THR_DEF_FIFO_PRIORITY (_thr_priorities[SCHED_FIFO-1].pri_default)
264 struct pthread_prio {
270 struct pthread_rwlockattr {
274 struct pthread_rwlock {
275 pthread_mutex_t lock; /* monitor lock */
276 pthread_cond_t read_signal;
277 pthread_cond_t write_signal;
278 int state; /* 0 = idle >0 = # of readers -1 = writer */
290 struct pthread_specific_elem {
296 volatile int allocated;
298 void (*destructor)(void *);
302 * lwpid_t is 32bit but kernel thr API exports tid as long type
303 * in very earily date.
305 #define TID(thread) ((uint32_t) ((thread)->tid))
311 /* Kernel thread id. */
313 #define TID_TERMINATED 1
316 * Lock for accesses to this thread structure.
320 /* Internal condition variable cycle number. */
323 /* How many low level locks the thread held. */
327 * Set to non-zero when this thread has entered a critical
328 * region. We allow for recursive entries into critical regions.
332 /* Signal blocked counter. */
335 /* Queue entry for list of all threads. */
336 TAILQ_ENTRY(pthread) tle; /* link for all threads in process */
338 /* Queue entry for GC lists. */
339 TAILQ_ENTRY(pthread) gcle;
341 /* Hash queue entry. */
342 LIST_ENTRY(pthread) hle;
344 /* Threads reference count. */
348 * Thread start routine, argument, stack pointer and thread
351 void *(*start_routine)(void *);
353 struct pthread_attr attr;
355 #define SHOULD_CANCEL(thr) \
356 ((thr)->cancel_pending && \
357 ((thr)->cancel_point || (thr)->cancel_async) && \
358 (thr)->cancel_enable && (thr)->cancelling == 0)
360 /* Cancellation is enabled */
363 /* Cancellation request is pending */
366 /* Thread is at cancellation point */
369 /* Cancellation should be synchoronized */
372 /* Asynchronouse cancellation is enabled */
375 /* Cancellation is in progress */
378 /* Thread temporary signal mask. */
381 /* Thread is in SIGCANCEL handler. */
382 int in_sigcancel_handler;
384 /* New thread should unblock SIGCANCEL. */
385 int unblock_sigcancel;
387 /* Force new thread to exit. */
391 enum pthread_state state;
394 * Error variable used instead of errno. The function __error()
395 * returns a pointer to this.
400 * The joiner is the thread that is joining to this thread. The
401 * join status keeps track of a join operation to another thread.
403 struct pthread *joiner;
405 /* Miscellaneous flags; only set with scheduling lock held. */
407 #define THR_FLAGS_PRIVATE 0x0001
408 #define THR_FLAGS_NEED_SUSPEND 0x0002 /* thread should be suspended */
409 #define THR_FLAGS_SUSPENDED 0x0004 /* thread is suspended */
411 /* Thread list flags; only set with thread list lock held. */
413 #define TLFLAGS_GC_SAFE 0x0001 /* thread safe for cleaning */
414 #define TLFLAGS_IN_TDLIST 0x0002 /* thread in all thread list */
415 #define TLFLAGS_IN_GCLIST 0x0004 /* thread in gc list */
416 #define TLFLAGS_DETACHED 0x0008 /* thread is detached */
418 /* Queue of currently owned NORMAL or PRIO_INHERIT type mutexes. */
419 struct mutex_queue mutexq;
421 /* Queue of all owned PRIO_PROTECT mutexes. */
422 struct mutex_queue pp_mutexq;
425 struct pthread_specific_elem *specific;
426 int specific_data_count;
428 /* Number rwlocks rdlocks held. */
432 * Current locks bitmap for rtld. */
435 /* Thread control block */
438 /* Cleanup handlers Link List */
439 struct pthread_cleanup *cleanup;
442 * Magic value to help recognize a valid thread structure
443 * from an invalid one:
445 #define THR_MAGIC ((u_int32_t) 0xd09ba115)
448 /* Enable event reporting */
455 td_event_msg_t event_buf;
458 #define THR_IN_CRITICAL(thrd) \
459 (((thrd)->locklevel > 0) || \
460 ((thrd)->critical_count > 0))
462 #define THR_CRITICAL_ENTER(thrd) \
463 (thrd)->critical_count++
465 #define THR_CRITICAL_LEAVE(thrd) \
466 (thrd)->critical_count--; \
469 #define THR_UMUTEX_TRYLOCK(thrd, lck) \
470 _thr_umutex_trylock((lck), TID(thrd))
472 #define THR_UMUTEX_LOCK(thrd, lck) \
473 _thr_umutex_lock((lck), TID(thrd))
475 #define THR_UMUTEX_TIMEDLOCK(thrd, lck, timo) \
476 _thr_umutex_timedlock((lck), TID(thrd), (timo))
478 #define THR_UMUTEX_UNLOCK(thrd, lck) \
479 _thr_umutex_unlock((lck), TID(thrd))
481 #define THR_LOCK_ACQUIRE(thrd, lck) \
483 (thrd)->locklevel++; \
484 _thr_umutex_lock(lck, TID(thrd)); \
487 #ifdef _PTHREADS_INVARIANTS
488 #define THR_ASSERT_LOCKLEVEL(thrd) \
490 if (__predict_false((thrd)->locklevel <= 0)) \
491 _thr_assert_lock_level(); \
494 #define THR_ASSERT_LOCKLEVEL(thrd)
497 #define THR_LOCK_RELEASE(thrd, lck) \
499 THR_ASSERT_LOCKLEVEL(thrd); \
500 _thr_umutex_unlock((lck), TID(thrd)); \
501 (thrd)->locklevel--; \
505 #define THR_LOCK(curthrd) THR_LOCK_ACQUIRE(curthrd, &(curthrd)->lock)
506 #define THR_UNLOCK(curthrd) THR_LOCK_RELEASE(curthrd, &(curthrd)->lock)
507 #define THR_THREAD_LOCK(curthrd, thr) THR_LOCK_ACQUIRE(curthrd, &(thr)->lock)
508 #define THR_THREAD_UNLOCK(curthrd, thr) THR_LOCK_RELEASE(curthrd, &(thr)->lock)
510 #define THREAD_LIST_LOCK(curthrd) \
512 THR_LOCK_ACQUIRE((curthrd), &_thr_list_lock); \
515 #define THREAD_LIST_UNLOCK(curthrd) \
517 THR_LOCK_RELEASE((curthrd), &_thr_list_lock); \
521 * Macros to insert/remove threads to the all thread list and
524 #define THR_LIST_ADD(thrd) do { \
525 if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) == 0) { \
526 TAILQ_INSERT_HEAD(&_thread_list, thrd, tle); \
527 _thr_hash_add(thrd); \
528 (thrd)->tlflags |= TLFLAGS_IN_TDLIST; \
531 #define THR_LIST_REMOVE(thrd) do { \
532 if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) != 0) { \
533 TAILQ_REMOVE(&_thread_list, thrd, tle); \
534 _thr_hash_remove(thrd); \
535 (thrd)->tlflags &= ~TLFLAGS_IN_TDLIST; \
538 #define THR_GCLIST_ADD(thrd) do { \
539 if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) == 0) { \
540 TAILQ_INSERT_HEAD(&_thread_gc_list, thrd, gcle);\
541 (thrd)->tlflags |= TLFLAGS_IN_GCLIST; \
545 #define THR_GCLIST_REMOVE(thrd) do { \
546 if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) != 0) { \
547 TAILQ_REMOVE(&_thread_gc_list, thrd, gcle); \
548 (thrd)->tlflags &= ~TLFLAGS_IN_GCLIST; \
553 #define GC_NEEDED() (_gc_count >= 5)
555 #define SHOULD_REPORT_EVENT(curthr, e) \
556 (curthr->report_events && \
557 (((curthr)->event_mask | _thread_event_mask ) & e) != 0)
559 extern int __isthreaded;
562 * Global variables for the pthread kernel.
565 extern char *_usrstack __hidden;
566 extern struct pthread *_thr_initial __hidden;
569 extern int _libthr_debug;
570 extern int _thread_event_mask;
571 extern struct pthread *_thread_last_event;
573 /* List of all threads: */
574 extern pthreadlist _thread_list;
576 /* List of threads needing GC: */
577 extern pthreadlist _thread_gc_list __hidden;
579 extern int _thread_active_threads;
580 extern atfork_head _thr_atfork_list __hidden;
581 extern struct umutex _thr_atfork_lock __hidden;
583 /* Default thread attributes: */
584 extern struct pthread_attr _pthread_attr_default __hidden;
586 /* Default mutex attributes: */
587 extern struct pthread_mutex_attr _pthread_mutexattr_default __hidden;
589 /* Default condition variable attributes: */
590 extern struct pthread_cond_attr _pthread_condattr_default __hidden;
592 extern struct pthread_prio _thr_priorities[] __hidden;
594 extern pid_t _thr_pid __hidden;
595 extern int _thr_is_smp __hidden;
597 extern size_t _thr_guard_default __hidden;
598 extern size_t _thr_stack_default __hidden;
599 extern size_t _thr_stack_initial __hidden;
600 extern int _thr_page_size __hidden;
601 extern int _thr_spinloops __hidden;
602 extern int _thr_yieldloops __hidden;
604 /* Garbage thread count. */
605 extern int _gc_count __hidden;
607 extern struct umutex _mutex_static_lock __hidden;
608 extern struct umutex _cond_static_lock __hidden;
609 extern struct umutex _rwlock_static_lock __hidden;
610 extern struct umutex _keytable_lock __hidden;
611 extern struct umutex _thr_list_lock __hidden;
612 extern struct umutex _thr_event_lock __hidden;
615 * Function prototype definitions.
618 int _thr_setthreaded(int) __hidden;
619 int _mutex_cv_lock(pthread_mutex_t *, int count) __hidden;
620 int _mutex_cv_unlock(pthread_mutex_t *, int *count) __hidden;
621 int _mutex_reinit(pthread_mutex_t *) __hidden;
622 void _mutex_fork(struct pthread *curthread) __hidden;
623 void _mutex_unlock_private(struct pthread *) __hidden;
624 void _libpthread_init(struct pthread *) __hidden;
625 struct pthread *_thr_alloc(struct pthread *) __hidden;
626 void _thread_exit(const char *, int, const char *) __hidden __dead2;
627 void _thr_exit_cleanup(void) __hidden;
628 int _thr_ref_add(struct pthread *, struct pthread *, int) __hidden;
629 void _thr_ref_delete(struct pthread *, struct pthread *) __hidden;
630 void _thr_ref_delete_unlocked(struct pthread *, struct pthread *) __hidden;
631 int _thr_find_thread(struct pthread *, struct pthread *, int) __hidden;
632 void _thr_rtld_init(void) __hidden;
633 void _thr_rtld_fini(void) __hidden;
634 int _thr_stack_alloc(struct pthread_attr *) __hidden;
635 void _thr_stack_free(struct pthread_attr *) __hidden;
636 void _thr_free(struct pthread *, struct pthread *) __hidden;
637 void _thr_gc(struct pthread *) __hidden;
638 void _thread_cleanupspecific(void) __hidden;
639 void _thread_dump_info(void) __hidden;
640 void _thread_printf(int, const char *, ...) __hidden;
641 void _thr_spinlock_init(void) __hidden;
642 void _thr_cancel_enter(struct pthread *) __hidden;
643 void _thr_cancel_leave(struct pthread *) __hidden;
644 void _thr_cancel_enter_defer(struct pthread *) __hidden;
645 void _thr_cancel_leave_defer(struct pthread *, int) __hidden;
646 void _thr_testcancel(struct pthread *) __hidden;
647 void _thr_signal_block(struct pthread *) __hidden;
648 void _thr_signal_unblock(struct pthread *) __hidden;
649 void _thr_signal_init(void) __hidden;
650 void _thr_signal_deinit(void) __hidden;
651 int _thr_send_sig(struct pthread *, int sig) __hidden;
652 void _thr_list_init(void) __hidden;
653 void _thr_hash_add(struct pthread *) __hidden;
654 void _thr_hash_remove(struct pthread *) __hidden;
655 struct pthread *_thr_hash_find(struct pthread *) __hidden;
656 void _thr_link(struct pthread *, struct pthread *) __hidden;
657 void _thr_unlink(struct pthread *, struct pthread *) __hidden;
658 void _thr_suspend_check(struct pthread *) __hidden;
659 void _thr_assert_lock_level(void) __hidden __dead2;
660 void _thr_ast(struct pthread *) __hidden;
661 void _thr_once_init(void) __hidden;
662 void _thr_report_creation(struct pthread *curthread,
663 struct pthread *newthread) __hidden;
664 void _thr_report_death(struct pthread *curthread) __hidden;
665 int _thr_getscheduler(lwpid_t, int *, struct sched_param *) __hidden;
666 int _thr_setscheduler(lwpid_t, int, const struct sched_param *) __hidden;
667 int _rtp_to_schedparam(const struct rtprio *rtp, int *policy,
668 struct sched_param *param) __hidden;
669 int _schedparam_to_rtp(int policy, const struct sched_param *param,
670 struct rtprio *rtp) __hidden;
671 void _thread_bp_create(void);
672 void _thread_bp_death(void);
673 int _sched_yield(void);
674 int _pthread_getaffinity_np(pthread_t, size_t, cpuset_t *);
675 int _pthread_setaffinity_np(pthread_t, size_t, const cpuset_t *);
677 /* #include <fcntl.h> */
679 int __sys_fcntl(int, int, ...);
680 int __sys_open(const char *, int, ...);
683 /* #include <signal.h> */
685 int __sys_kill(pid_t, int);
686 int __sys_sigaction(int, const struct sigaction *, struct sigaction *);
687 int __sys_sigpending(sigset_t *);
688 int __sys_sigprocmask(int, const sigset_t *, sigset_t *);
689 int __sys_sigsuspend(const sigset_t *);
690 int __sys_sigreturn(ucontext_t *);
691 int __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *);
692 int __sys_sigwait(const sigset_t *, int *);
693 int __sys_sigtimedwait(const sigset_t *, siginfo_t *,
694 const struct timespec *);
695 int __sys_sigwaitinfo(const sigset_t *set, siginfo_t *info);
698 /* #include <time.h> */
700 int __sys_nanosleep(const struct timespec *, struct timespec *);
703 /* #include <unistd.h> */
705 int __sys_close(int);
706 int __sys_fork(void);
707 pid_t __sys_getpid(void);
708 ssize_t __sys_read(int, void *, size_t);
709 ssize_t __sys_write(int, const void *, size_t);
710 void __sys_exit(int);
714 _thr_isthreaded(void)
716 return (__isthreaded != 0);
722 return (_thr_initial != NULL);
726 _thr_check_init(void)
728 if (_thr_initial == NULL)
729 _libpthread_init(NULL);
734 #endif /* !_THR_PRIVATE_H */