2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by John Birrell.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Private thread definitions for the uthread kernel.
34 * $FreeBSD: src/lib/libc_r/uthread/pthread_private.h,v 1.36.2.21 2002/10/22 14:44:02 fjoe Exp $
35 * $DragonFly: src/lib/libc_r/uthread/pthread_private.h,v 1.7 2005/05/02 20:40:50 joerg Exp $
38 #ifndef _PTHREAD_PRIVATE_H
39 #define _PTHREAD_PRIVATE_H
42 * Evaluate the storage class specifier.
44 #ifdef GLOBAL_PTHREAD_PRIVATE
56 #include <sys/queue.h>
57 #include <sys/types.h>
59 #include <sys/cdefs.h>
62 #include <pthread_np.h>
64 #include <machine/tls.h>
67 * Define machine dependent macros to get and set the stack pointer
68 * from the supported contexts. Also define a macro to set the return
69 * address in a jmp_buf context.
71 * XXX - These need to be moved into architecture dependent support files.
74 #define GET_STACK_JB(jb) ((unsigned long)((jb)[0]._jb[2]))
75 #define GET_STACK_SJB(sjb) ((unsigned long)((sjb)[0]._sjb[2]))
76 #define GET_STACK_UC(ucp) ((unsigned long)((ucp)->uc_mcontext.mc_esp))
77 #define SET_STACK_JB(jb, stk) (jb)[0]._jb[2] = (int)(stk)
78 #define SET_STACK_SJB(sjb, stk) (sjb)[0]._sjb[2] = (int)(stk)
79 #define SET_STACK_UC(ucp, stk) (ucp)->uc_mcontext.mc_esp = (int)(stk)
80 #define FP_SAVE_UC(ucp) do { \
82 fdata = (char *) (ucp)->uc_mcontext.mc_fpregs; \
83 __asm__("fnsave %0": :"m"(*fdata)); \
85 #define FP_RESTORE_UC(ucp) do { \
87 fdata = (char *) (ucp)->uc_mcontext.mc_fpregs; \
88 __asm__("frstor %0": :"m"(*fdata)); \
90 #define SET_RETURN_ADDR_JB(jb, ra) (jb)[0]._jb[0] = (int)(ra)
92 #error "Don't recognize this architecture!"
96 * Kernel fatal error handler macro.
98 #define PANIC(string) _thread_exit(__FILE__,__LINE__,string)
101 /* Output debug messages like this: */
102 #define stdout_debug(args...) do { \
104 snprintf(buf, sizeof(buf), ##args); \
105 __sys_write(1, buf, strlen(buf)); \
107 #define stderr_debug(args...) do { \
109 snprintf(buf, sizeof(buf), ##args); \
110 __sys_write(2, buf, strlen(buf)); \
116 * Priority queue manipulation macros (using pqe link):
118 #define PTHREAD_PRIOQ_INSERT_HEAD(thrd) _pq_insert_head(&_readyq,thrd)
119 #define PTHREAD_PRIOQ_INSERT_TAIL(thrd) _pq_insert_tail(&_readyq,thrd)
120 #define PTHREAD_PRIOQ_REMOVE(thrd) _pq_remove(&_readyq,thrd)
121 #define PTHREAD_PRIOQ_FIRST() _pq_first(&_readyq)
124 * Waiting queue manipulation macros (using pqe link):
126 #define PTHREAD_WAITQ_REMOVE(thrd) _waitq_remove(thrd)
127 #define PTHREAD_WAITQ_INSERT(thrd) _waitq_insert(thrd)
129 #if defined(_PTHREADS_INVARIANTS)
130 #define PTHREAD_WAITQ_CLEARACTIVE() _waitq_clearactive()
131 #define PTHREAD_WAITQ_SETACTIVE() _waitq_setactive()
133 #define PTHREAD_WAITQ_CLEARACTIVE()
134 #define PTHREAD_WAITQ_SETACTIVE()
138 * Work queue manipulation macros (using qe link):
140 #define PTHREAD_WORKQ_INSERT(thrd) do { \
141 TAILQ_INSERT_TAIL(&_workq,thrd,qe); \
142 (thrd)->flags |= PTHREAD_FLAGS_IN_WORKQ; \
144 #define PTHREAD_WORKQ_REMOVE(thrd) do { \
145 TAILQ_REMOVE(&_workq,thrd,qe); \
146 (thrd)->flags &= ~PTHREAD_FLAGS_IN_WORKQ; \
151 * State change macro without scheduling queue change:
153 #define PTHREAD_SET_STATE(thrd, newstate) do { \
154 (thrd)->state = newstate; \
155 (thrd)->fname = __FILE__; \
156 (thrd)->lineno = __LINE__; \
160 * State change macro with scheduling queue change - This must be
161 * called with preemption deferred (see thread_kern_sched_[un]defer).
163 #if defined(_PTHREADS_INVARIANTS)
165 #define PTHREAD_ASSERT(cond, msg) do { \
169 #define PTHREAD_ASSERT_NOT_IN_SYNCQ(thrd) \
170 PTHREAD_ASSERT((((thrd)->flags & PTHREAD_FLAGS_IN_SYNCQ) == 0), \
171 "Illegal call from signal handler");
172 #define PTHREAD_NEW_STATE(thrd, newstate) do { \
173 if (_thread_kern_new_state != 0) \
174 PANIC("Recursive PTHREAD_NEW_STATE"); \
175 _thread_kern_new_state = 1; \
176 if ((thrd)->state != newstate) { \
177 if ((thrd)->state == PS_RUNNING) { \
178 PTHREAD_PRIOQ_REMOVE(thrd); \
179 PTHREAD_SET_STATE(thrd, newstate); \
180 PTHREAD_WAITQ_INSERT(thrd); \
181 } else if (newstate == PS_RUNNING) { \
182 PTHREAD_WAITQ_REMOVE(thrd); \
183 PTHREAD_SET_STATE(thrd, newstate); \
184 PTHREAD_PRIOQ_INSERT_TAIL(thrd); \
187 _thread_kern_new_state = 0; \
190 #define PTHREAD_ASSERT(cond, msg)
191 #define PTHREAD_ASSERT_NOT_IN_SYNCQ(thrd)
192 #define PTHREAD_NEW_STATE(thrd, newstate) do { \
193 if ((thrd)->state != newstate) { \
194 if ((thrd)->state == PS_RUNNING) { \
195 PTHREAD_PRIOQ_REMOVE(thrd); \
196 PTHREAD_WAITQ_INSERT(thrd); \
197 } else if (newstate == PS_RUNNING) { \
198 PTHREAD_WAITQ_REMOVE(thrd); \
199 PTHREAD_PRIOQ_INSERT_TAIL(thrd); \
202 PTHREAD_SET_STATE(thrd, newstate); \
207 * Define the signals to be used for scheduling.
209 #if defined(_PTHREADS_COMPAT_SCHED)
210 #define _ITIMER_SCHED_TIMER ITIMER_VIRTUAL
211 #define _SCHED_SIGNAL SIGVTALRM
213 #define _ITIMER_SCHED_TIMER ITIMER_PROF
214 #define _SCHED_SIGNAL SIGPROF
220 * XXX It'd be nice if these were contained in uthread_priority_queue.[ch].
222 typedef struct pq_list {
223 TAILQ_HEAD(, pthread) pl_head; /* list of threads at this priority */
224 TAILQ_ENTRY(pq_list) pl_link; /* link for queue of priority lists */
225 int pl_prio; /* the priority of this list */
226 int pl_queued; /* is this in the priority queue */
229 typedef struct pq_queue {
230 TAILQ_HEAD(, pq_list) pq_queue; /* queue of priority lists */
231 pq_list_t *pq_lists; /* array of all priority lists */
232 int pq_size; /* number of priority lists */
237 * TailQ initialization values.
239 #define TAILQ_INITIALIZER { NULL, NULL }
244 union pthread_mutex_data {
249 struct pthread_mutex {
250 enum pthread_mutextype m_type;
252 TAILQ_HEAD(mutex_head, pthread) m_queue;
253 struct pthread *m_owner;
254 union pthread_mutex_data m_data;
259 * Used for priority inheritence and protection.
261 * m_prio - For priority inheritence, the highest active
262 * priority (threads locking the mutex inherit
263 * this priority). For priority protection, the
264 * ceiling priority of this mutex.
265 * m_saved_prio - mutex owners inherited priority before
266 * taking the mutex, restored when the owner
273 * Link for list of all mutexes a thread currently owns.
275 TAILQ_ENTRY(pthread_mutex) m_qe;
278 * Lock for accesses to this structure.
286 #define MUTEX_FLAGS_PRIVATE 0x01
287 #define MUTEX_FLAGS_INITED 0x02
288 #define MUTEX_FLAGS_BUSY 0x04
291 * Static mutex initialization values.
293 #define PTHREAD_MUTEX_STATIC_INITIALIZER \
294 { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, TAILQ_INITIALIZER, \
295 NULL, { NULL }, MUTEX_FLAGS_PRIVATE, 0, 0, 0, TAILQ_INITIALIZER, \
296 _SPINLOCK_INITIALIZER }
298 struct pthread_mutex_attr {
299 enum pthread_mutextype m_type;
306 * Condition variable definitions.
308 enum pthread_cond_type {
313 struct pthread_cond {
314 enum pthread_cond_type c_type;
315 TAILQ_HEAD(cond_head, pthread) c_queue;
316 pthread_mutex_t c_mutex;
322 * Lock for accesses to this structure.
327 struct pthread_cond_attr {
328 enum pthread_cond_type c_type;
333 * Flags for condition variables.
335 #define COND_FLAGS_PRIVATE 0x01
336 #define COND_FLAGS_INITED 0x02
337 #define COND_FLAGS_BUSY 0x04
340 * Static cond initialization values.
342 #define PTHREAD_COND_STATIC_INITIALIZER \
343 { COND_TYPE_FAST, TAILQ_INITIALIZER, NULL, NULL, \
344 0, 0, _SPINLOCK_INITIALIZER }
347 * Semaphore definitions.
350 #define SEM_MAGIC ((u_int32_t) 0x09fa4012)
352 pthread_mutex_t lock;
353 pthread_cond_t gtzero;
359 * Cleanup definitions.
361 struct pthread_cleanup {
362 struct pthread_cleanup *next;
363 void (*routine)(void *);
367 struct pthread_attr {
375 void (*cleanup_attr)(void *);
376 void *stackaddr_attr;
377 size_t stacksize_attr;
381 * Thread creation state attributes.
383 #define PTHREAD_CREATE_RUNNING 0
384 #define PTHREAD_CREATE_SUSPENDED 1
387 * Miscellaneous definitions.
389 #define PTHREAD_STACK_DEFAULT 65536
391 * Size of red zone at the end of each stack. In actuality, this "red zone" is
392 * merely an unmapped region, except in the case of the initial stack. Since
393 * mmap() makes it possible to specify the maximum growth of a MAP_STACK region,
394 * an unmapped gap between thread stacks achieves the same effect as explicitly
397 #define PTHREAD_STACK_GUARD PAGE_SIZE
400 * Maximum size of initial thread's stack. This perhaps deserves to be larger
401 * than the stacks of other threads, since many applications are likely to run
402 * almost entirely on this stack.
404 #define PTHREAD_STACK_INITIAL 0x100000
406 /* Size of the scheduler stack: */
407 #define SCHED_STACK_SIZE PAGE_SIZE
410 * Define the different priority ranges. All applications have thread
411 * priorities constrained within 0-31. The threads library raises the
412 * priority when delivering signals in order to ensure that signal
413 * delivery happens (from the POSIX spec) "as soon as possible".
414 * In the future, the threads library will also be able to map specific
415 * threads into real-time (cooperating) processes or kernel threads.
416 * The RT and SIGNAL priorities will be used internally and added to
417 * thread base priorities so that the scheduling queue can handle both
418 * normal and RT priority threads with and without signal handling.
420 * The approach taken is that, within each class, signal delivery
421 * always has priority over thread execution.
423 #define PTHREAD_DEFAULT_PRIORITY 15
424 #define PTHREAD_MIN_PRIORITY 0
425 #define PTHREAD_MAX_PRIORITY 31 /* 0x1F */
426 #define PTHREAD_SIGNAL_PRIORITY 32 /* 0x20 */
427 #define PTHREAD_RT_PRIORITY 64 /* 0x40 */
428 #define PTHREAD_FIRST_PRIORITY PTHREAD_MIN_PRIORITY
429 #define PTHREAD_LAST_PRIORITY \
430 (PTHREAD_MAX_PRIORITY + PTHREAD_SIGNAL_PRIORITY + PTHREAD_RT_PRIORITY)
431 #define PTHREAD_BASE_PRIORITY(prio) ((prio) & PTHREAD_MAX_PRIORITY)
434 * Clock resolution in microseconds.
436 #define CLOCK_RES_USEC 10000
437 #define CLOCK_RES_USEC_MIN 1000
440 * Time slice period in microseconds.
442 #define TIMESLICE_USEC 20000
445 * Define a thread-safe macro to get the current time of day
446 * which is updated at regular intervals by the scheduling signal
449 #define GET_CURRENT_TOD(tv) \
451 tv.tv_sec = _sched_tod.tv_sec; \
452 tv.tv_usec = _sched_tod.tv_usec; \
453 } while (tv.tv_sec != _sched_tod.tv_sec)
458 volatile int allocated;
460 void (*destructor)(void *);
463 struct pthread_rwlockattr {
467 struct pthread_rwlock {
468 pthread_mutex_t lock; /* monitor lock */
469 int state; /* 0 = idle >0 = # of readers -1 = writer */
470 pthread_cond_t read_signal;
471 pthread_cond_t write_signal;
504 * File descriptor locking definitions.
508 #define FD_RDWR (FD_READ | FD_WRITE)
511 * File descriptor table structure.
513 struct fd_table_entry {
515 * Lock for accesses to this file descriptor table
516 * entry. This is passed to _spinlock() to provide atomic
517 * access to this structure. It does *not* represent the
518 * state of the lock on the file descriptor.
521 TAILQ_HEAD(, pthread) r_queue; /* Read queue. */
522 TAILQ_HEAD(, pthread) w_queue; /* Write queue. */
523 struct pthread *r_owner; /* Ptr to thread owning read lock. */
524 struct pthread *w_owner; /* Ptr to thread owning write lock. */
525 char *r_fname; /* Ptr to read lock source file name */
526 int r_lineno; /* Read lock source line number. */
527 char *w_fname; /* Ptr to write lock source file name */
528 int w_lineno; /* Write lock source line number. */
529 int r_lockcount; /* Count for FILE read locks. */
530 int w_lockcount; /* Count for FILE write locks. */
531 int flags; /* Flags used in open. */
534 struct pthread_poll_data {
539 union pthread_wait_data {
540 pthread_mutex_t mutex;
542 const sigset_t *sigwait; /* Waiting on a signal in sigwait */
544 short fd; /* Used when thread waiting on fd */
545 short branch; /* Line number, for debugging. */
546 char *fname; /* Source file name for debugging.*/
549 struct pthread_poll_data *poll_data;
550 spinlock_t *spinlock;
551 struct pthread *thread;
555 * Define a continuation routine that can be used to perform a
556 * transfer of control:
558 typedef void (*thread_continuation_t) (void *);
560 struct pthread_signal_frame;
562 struct pthread_state_data {
563 struct pthread_signal_frame *psd_curframe;
564 sigset_t psd_sigmask;
565 struct timespec psd_wakeup_time;
566 union pthread_wait_data psd_wait_data;
567 enum pthread_state psd_state;
571 int psd_sigmask_seqno;
573 int psd_sig_defer_count;
574 /* XXX - What about thread->timeout and/or thread->error? */
578 struct pthread *thread;
584 * The frame that is added to the top of a threads stack when setting up
585 * up the thread to run a signal handler.
587 struct pthread_signal_frame {
589 * This stores the threads state before the signal.
591 struct pthread_state_data saved_state;
594 * Threads return context; we use only jmp_buf's for now.
600 int signo; /* signal, arg 1 to sighandler */
601 int sig_has_args; /* use signal args if true */
611 * Magic value to help recognize a valid thread structure
612 * from an invalid one:
614 #define PTHREAD_MAGIC ((u_int32_t) 0xd09ba115)
617 u_int64_t uniqueid; /* for gdb */
621 * Lock for accesses to this thread structure.
625 /* Queue entry for list of all threads: */
626 TAILQ_ENTRY(pthread) tle;
628 /* Queue entry for list of dead threads: */
629 TAILQ_ENTRY(pthread) dle;
632 * Thread start routine, argument, stack pointer and thread
635 void *(*start_routine)(void *);
638 struct pthread_attr attr;
641 * Threads return context; we use only jmp_buf's for now.
649 * Used for tracking delivery of signal handlers.
651 struct pthread_signal_frame *curframe;
654 * Cancelability flags - the lower 2 bits are used by cancel
655 * definitions in pthread.h
657 #define PTHREAD_AT_CANCEL_POINT 0x0004
658 #define PTHREAD_CANCELLING 0x0008
659 #define PTHREAD_CANCEL_NEEDED 0x0010
662 thread_continuation_t continuation;
665 * Current signal mask and pending signals.
673 enum pthread_state state;
675 /* Scheduling clock when this thread was last made active. */
678 /* Scheduling clock when this thread was last made inactive. */
682 * Number of microseconds accumulated by this thread when
683 * time slicing is active.
688 * Time to wake up thread. This is used for sleeping threads and
689 * for any operation which may time out (such as select).
691 struct timespec wakeup_time;
693 /* TRUE if operation has timed out. */
697 * Error variable used instead of errno. The function __error()
698 * returns a pointer to this.
703 * The joiner is the thread that is joining to this thread. The
704 * join status keeps track of a join operation to another thread.
706 struct pthread *joiner;
707 struct join_status join_status;
710 * The current thread can belong to only one scheduling queue at
711 * a time (ready or waiting queue). It can also belong to:
713 * o A queue of threads waiting for a mutex
714 * o A queue of threads waiting for a condition variable
715 * o A queue of threads waiting for a file descriptor lock
716 * o A queue of threads needing work done by the kernel thread
717 * (waiting for a spinlock or file I/O)
719 * A thread can also be joining a thread (the joiner field above).
721 * It must not be possible for a thread to belong to any of the
722 * above queues while it is handling a signal. Signal handlers
723 * may longjmp back to previous stack frames circumventing normal
724 * control flow. This could corrupt queue integrity if the thread
725 * retains membership in the queue. Therefore, if a thread is a
726 * member of one of these queues when a signal handler is invoked,
727 * it must remove itself from the queue before calling the signal
728 * handler and reinsert itself after normal return of the handler.
730 * Use pqe for the scheduling queue link (both ready and waiting),
731 * sqe for synchronization (mutex and condition variable) queue
732 * links, and qe for all other links.
734 TAILQ_ENTRY(pthread) pqe; /* priority queue link */
735 TAILQ_ENTRY(pthread) sqe; /* synchronization queue link */
736 TAILQ_ENTRY(pthread) qe; /* all other queues link */
739 union pthread_wait_data data;
742 * Allocated for converting select into poll.
744 struct pthread_poll_data poll_data;
747 * Set to TRUE if a blocking operation was
748 * interrupted by a signal:
752 /* Signal number when in state PS_SIGWAIT: */
756 * Set to non-zero when this thread has deferred signals.
757 * We allow for recursive deferral.
762 * Set to TRUE if this thread should yield after undeferring
765 int yield_on_sig_undefer;
767 /* Miscellaneous flags; only set with signals deferred. */
769 #define PTHREAD_FLAGS_PRIVATE 0x0001
770 #define PTHREAD_EXITING 0x0002
771 #define PTHREAD_FLAGS_IN_WAITQ 0x0004 /* in waiting queue using pqe link */
772 #define PTHREAD_FLAGS_IN_PRIOQ 0x0008 /* in priority queue using pqe link */
773 #define PTHREAD_FLAGS_IN_WORKQ 0x0010 /* in work queue using qe link */
774 #define PTHREAD_FLAGS_IN_FILEQ 0x0020 /* in file lock queue using qe link */
775 #define PTHREAD_FLAGS_IN_FDQ 0x0040 /* in fd lock queue using qe link */
776 #define PTHREAD_FLAGS_IN_CONDQ 0x0080 /* in condition queue using sqe link*/
777 #define PTHREAD_FLAGS_IN_MUTEXQ 0x0100 /* in mutex queue using sqe link */
778 #define PTHREAD_FLAGS_SUSPENDED 0x0200 /* thread is suspended */
779 #define PTHREAD_FLAGS_TRACE 0x0400 /* for debugging purposes */
780 #define PTHREAD_FLAGS_IN_SYNCQ \
781 (PTHREAD_FLAGS_IN_CONDQ | PTHREAD_FLAGS_IN_MUTEXQ)
784 * Base priority is the user setable and retrievable priority
785 * of the thread. It is only affected by explicit calls to
786 * set thread priority and upon thread creation via a thread
787 * attribute or default priority.
792 * Inherited priority is the priority a thread inherits by
793 * taking a priority inheritence or protection mutex. It
794 * is not affected by base priority changes. Inherited
795 * priority defaults to and remains 0 until a mutex is taken
796 * that is being waited on by any other thread whose priority
799 char inherited_priority;
802 * Active priority is always the maximum of the threads base
803 * priority and inherited priority. When there is a change
804 * in either the base or inherited priority, the active
805 * priority must be recalculated.
807 char active_priority;
809 /* Number of priority ceiling or protection mutexes owned. */
810 int priority_mutex_count;
813 * Queue of currently owned mutexes.
815 TAILQ_HEAD(, pthread_mutex) mutexq;
818 const void **specific_data;
819 int specific_data_count;
821 /* Cleanup handlers Link List */
822 struct pthread_cleanup *cleanup;
823 char *fname; /* Ptr to source file name */
824 int lineno; /* Source line number. */
827 /* Spare thread stack. */
829 SLIST_ENTRY(stack) qe; /* Queue entry for this stack. */
833 * Global variables for the uthread kernel.
836 SCLASS void *_usrstack
837 #ifdef GLOBAL_PTHREAD_PRIVATE
843 /* Kernel thread structure used when there are no running threads: */
844 SCLASS struct pthread _thread_kern_thread;
846 /* Ptr to the thread structure for the running thread: */
847 SCLASS struct pthread * volatile _thread_run
848 #ifdef GLOBAL_PTHREAD_PRIVATE
849 = &_thread_kern_thread;
854 /* Ptr to the thread structure for the last user thread to run: */
855 SCLASS struct pthread * volatile _last_user_thread
856 #ifdef GLOBAL_PTHREAD_PRIVATE
857 = &_thread_kern_thread;
862 /* List of all threads: */
863 SCLASS TAILQ_HEAD(, pthread) _thread_list
864 #ifdef GLOBAL_PTHREAD_PRIVATE
865 = TAILQ_HEAD_INITIALIZER(_thread_list);
871 * Array of kernel pipe file descriptors that are used to ensure that
872 * no signals are missed in calls to _select.
874 SCLASS int _thread_kern_pipe[2]
875 #ifdef GLOBAL_PTHREAD_PRIVATE
883 SCLASS int volatile _queue_signals
884 #ifdef GLOBAL_PTHREAD_PRIVATE
889 SCLASS int _thread_kern_in_sched
890 #ifdef GLOBAL_PTHREAD_PRIVATE
896 SCLASS int _sig_in_handler
897 #ifdef GLOBAL_PTHREAD_PRIVATE
903 /* Time of day at last scheduling timer signal: */
904 SCLASS struct timeval volatile _sched_tod
905 #ifdef GLOBAL_PTHREAD_PRIVATE
912 * Current scheduling timer ticks; used as resource usage.
914 SCLASS unsigned int volatile _sched_ticks
915 #ifdef GLOBAL_PTHREAD_PRIVATE
922 SCLASS TAILQ_HEAD(, pthread) _dead_list
923 #ifdef GLOBAL_PTHREAD_PRIVATE
924 = TAILQ_HEAD_INITIALIZER(_dead_list);
929 /* Initial thread: */
930 SCLASS struct pthread *_thread_initial
931 #ifdef GLOBAL_PTHREAD_PRIVATE
937 /* Default thread attributes: */
938 SCLASS struct pthread_attr pthread_attr_default
939 #ifdef GLOBAL_PTHREAD_PRIVATE
940 = { SCHED_RR, 0, TIMESLICE_USEC, PTHREAD_DEFAULT_PRIORITY, PTHREAD_CREATE_RUNNING,
941 PTHREAD_CREATE_JOINABLE, NULL, NULL, NULL, PTHREAD_STACK_DEFAULT };
946 /* Default mutex attributes: */
947 SCLASS struct pthread_mutex_attr pthread_mutexattr_default
948 #ifdef GLOBAL_PTHREAD_PRIVATE
949 = { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, 0 };
954 /* Default condition variable attributes: */
955 SCLASS struct pthread_cond_attr pthread_condattr_default
956 #ifdef GLOBAL_PTHREAD_PRIVATE
957 = { COND_TYPE_FAST, 0 };
963 * Standard I/O file descriptors need special flag treatment since
964 * setting one to non-blocking does all on *BSD. Sigh. This array
965 * is used to store the initial flag settings.
967 SCLASS int _pthread_stdio_flags[3];
969 /* File table information: */
970 SCLASS struct fd_table_entry **_thread_fd_table
971 #ifdef GLOBAL_PTHREAD_PRIVATE
977 /* Table for polling file descriptors: */
978 SCLASS struct pollfd *_thread_pfd_table
979 #ifdef GLOBAL_PTHREAD_PRIVATE
985 SCLASS const int dtablecount
986 #ifdef GLOBAL_PTHREAD_PRIVATE
987 = 4096/sizeof(struct fd_table_entry);
991 SCLASS int _thread_dtablesize /* Descriptor table size. */
992 #ifdef GLOBAL_PTHREAD_PRIVATE
998 SCLASS int _clock_res_usec /* Clock resolution in usec. */
999 #ifdef GLOBAL_PTHREAD_PRIVATE
1005 /* Garbage collector mutex and condition variable. */
1006 SCLASS pthread_mutex_t _gc_mutex
1007 #ifdef GLOBAL_PTHREAD_PRIVATE
1011 SCLASS pthread_cond_t _gc_cond
1012 #ifdef GLOBAL_PTHREAD_PRIVATE
1018 * Array of signal actions for this process.
1020 SCLASS struct sigaction _thread_sigact[NSIG];
1023 * Array of counts of dummy handlers for SIG_DFL signals. This is used to
1024 * assure that there is always a dummy signal handler installed while there is a
1025 * thread sigwait()ing on the corresponding signal.
1027 SCLASS int _thread_dfl_count[NSIG];
1030 * Pending signals and mask for this process:
1032 SCLASS sigset_t _process_sigpending;
1033 SCLASS sigset_t _process_sigmask
1034 #ifdef GLOBAL_PTHREAD_PRIVATE
1040 * Scheduling queues:
1042 SCLASS pq_queue_t _readyq;
1043 SCLASS TAILQ_HEAD(, pthread) _waitingq;
1048 SCLASS TAILQ_HEAD(, pthread) _workq;
1050 /* Tracks the number of threads blocked while waiting for a spinlock. */
1051 SCLASS volatile int _spinblock_count
1052 #ifdef GLOBAL_PTHREAD_PRIVATE
1057 /* Used to maintain pending and active signals: */
1059 int pending; /* Is this a pending signal? */
1061 * A handler is currently active for
1062 * this signal; ignore subsequent
1063 * signals until the handler is done.
1065 int signo; /* arg 1 to signal handler */
1066 siginfo_t siginfo; /* arg 2 to signal handler */
1067 ucontext_t uc; /* arg 3 to signal handler */
1070 SCLASS struct sigstatus _thread_sigq[NSIG];
1072 /* Indicates that the signal queue needs to be checked. */
1073 SCLASS volatile int _sigq_check_reqd
1074 #ifdef GLOBAL_PTHREAD_PRIVATE
1079 /* Thread switch hook. */
1080 SCLASS pthread_switch_routine_t _sched_switch_hook
1081 #ifdef GLOBAL_PTHREAD_PRIVATE
1087 * Spare stack queue. Stacks of default size are cached in order to reduce
1088 * thread creation time. Spare stacks are used in LIFO order to increase cache
1091 SCLASS SLIST_HEAD(, stack) _stackq;
1094 * Base address of next unallocated default-size {stack, red zone}. Stacks are
1095 * allocated contiguously, starting below the bottom of the main stack. When a
1096 * new stack is created, a red zone is created (actually, the red zone is simply
1097 * left unmapped) below the bottom of the stack, such that the stack will not be
1098 * able to grow all the way to the top of the next stack. This isn't
1099 * fool-proof. It is possible for a stack to grow by a large amount, such that
1100 * it grows into the next stack, and as long as the memory within the red zone
1101 * is never accessed, nothing will prevent one thread stack from trouncing all
1104 SCLASS void * _next_stack
1105 #ifdef GLOBAL_PTHREAD_PRIVATE
1106 /* main stack top - main stack size - stack size - (red zone + main stack red zone) */
1107 = (void *) USRSTACK - PTHREAD_STACK_INITIAL - PTHREAD_STACK_DEFAULT - (2 * PTHREAD_STACK_GUARD)
1112 * Declare the kernel scheduler jump buffer and stack:
1114 SCLASS jmp_buf _thread_kern_sched_jb;
1116 SCLASS void * _thread_kern_sched_stack
1117 #ifdef GLOBAL_PTHREAD_PRIVATE
1123 /* Used for _PTHREADS_INVARIANTS checking. */
1124 SCLASS int _thread_kern_new_state
1125 #ifdef GLOBAL_PTHREAD_PRIVATE
1130 /* Undefine the storage class specifier: */
1134 #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock_debug(_fd, _type, \
1135 _ts, __FILE__, __LINE__)
1136 #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock_debug(_fd, _type, \
1139 #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock(_fd, _type, _ts)
1140 #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock(_fd, _type)
1144 * Function prototype definitions.
1147 char *__ttyname_basic(int);
1148 char *__ttyname_r_basic(int, char *, size_t);
1149 char *ttyname_r(int, char *, size_t);
1150 void _cond_wait_backout(pthread_t);
1151 void _fd_lock_backout(pthread_t);
1152 int _find_thread(pthread_t);
1153 struct pthread *_get_curthread(void);
1154 void _set_curthread(struct pthread *);
1155 void _flockfile_backout(struct pthread *);
1156 void _funlock_owned(struct pthread *);
1157 int _thread_create(pthread_t *,const pthread_attr_t *,void *(*start_routine)(void *),void *,pthread_t);
1158 int _mutex_cv_lock(pthread_mutex_t *);
1159 int _mutex_cv_unlock(pthread_mutex_t *);
1160 void _mutex_lock_backout(pthread_t);
1161 void _mutex_notify_priochange(pthread_t);
1162 int _mutex_reinit(pthread_mutex_t *);
1163 void _mutex_unlock_private(pthread_t);
1164 int _cond_reinit(pthread_cond_t *);
1165 int _pq_alloc(struct pq_queue *, int, int);
1166 int _pq_init(struct pq_queue *);
1167 void _pq_remove(struct pq_queue *pq, struct pthread *);
1168 void _pq_insert_head(struct pq_queue *pq, struct pthread *);
1169 void _pq_insert_tail(struct pq_queue *pq, struct pthread *);
1170 struct pthread *_pq_first(struct pq_queue *pq);
1171 void *_pthread_getspecific(pthread_key_t);
1172 int _pthread_key_create(pthread_key_t *, void (*) (void *));
1173 int _pthread_key_delete(pthread_key_t);
1174 int _pthread_mutex_destroy(pthread_mutex_t *);
1175 int _pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
1176 int _pthread_mutex_lock(pthread_mutex_t *);
1177 int _pthread_mutex_trylock(pthread_mutex_t *);
1178 int _pthread_mutex_unlock(pthread_mutex_t *);
1179 int _pthread_once(pthread_once_t *, void (*) (void));
1180 int _pthread_setspecific(pthread_key_t, const void *);
1181 int _pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *);
1182 int _pthread_cond_destroy(pthread_cond_t *);
1183 int _pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
1184 int _pthread_cond_timedwait(pthread_cond_t *, pthread_mutex_t *,
1185 const struct timespec *);
1186 int _pthread_cond_signal(pthread_cond_t *);
1187 int _pthread_cond_broadcast(pthread_cond_t *);
1188 void _waitq_insert(pthread_t pthread);
1189 void _waitq_remove(pthread_t pthread);
1190 #if defined(_PTHREADS_INVARIANTS)
1191 void _waitq_setactive(void);
1192 void _waitq_clearactive(void);
1194 void _thread_exit(char *, int, char *);
1195 void _thread_exit_cleanup(void);
1196 int _thread_fd_getflags(int);
1197 int _thread_fd_lock(int, int, struct timespec *);
1198 int _thread_fd_lock_debug(int, int, struct timespec *,char *fname,int lineno);
1199 void _thread_fd_setflags(int, int);
1200 int _thread_fd_table_init(int fd);
1201 void _thread_fd_unlock(int, int);
1202 void _thread_fd_unlock_debug(int, int, char *, int);
1203 void _thread_fd_unlock_owned(pthread_t);
1204 void *_thread_cleanup(pthread_t);
1205 void _thread_cleanupspecific(void);
1206 void _thread_dump_info(void);
1207 void _thread_init(void);
1208 void _thread_kern_sched(ucontext_t *);
1209 void _thread_kern_scheduler(void);
1210 void _thread_kern_sched_frame(struct pthread_signal_frame *psf);
1211 void _thread_kern_sched_sig(void);
1212 void _thread_kern_sched_state(enum pthread_state, char *fname, int lineno);
1213 void _thread_kern_sched_state_unlock(enum pthread_state state,
1214 spinlock_t *lock, char *fname, int lineno);
1215 void _thread_kern_set_timeout(const struct timespec *);
1216 void _thread_kern_sig_defer(void);
1217 void _thread_kern_sig_undefer(void);
1218 void _thread_mksigpipe(void);
1219 void _thread_sig_handler(int, siginfo_t *, ucontext_t *);
1220 void _thread_sig_check_pending(struct pthread *pthread);
1221 void _thread_sig_handle_pending(void);
1222 void _thread_sig_send(struct pthread *pthread, int sig);
1223 void _thread_sig_wrapper(void);
1224 void _thread_sigframe_restore(struct pthread *thread,
1225 struct pthread_signal_frame *psf);
1226 void _thread_start(void);
1227 void _thread_seterrno(pthread_t, int);
1228 pthread_addr_t _thread_gc(pthread_addr_t);
1229 void _thread_enter_cancellation_point(void);
1230 void _thread_leave_cancellation_point(void);
1231 void _thread_cancellation_point(void);
1233 /* #include <aio.h> */
1235 int __sys_aio_suspend(const struct aiocb * const[], int, const struct timespec *);
1238 /* #include <sys/event.h> */
1239 #ifdef _SYS_EVENT_H_
1240 int __sys_kevent(int, const struct kevent *, int, struct kevent *,
1241 int, const struct timespec *);
1244 /* #include <sys/ioctl.h> */
1245 #ifdef _SYS_IOCTL_H_
1246 int __sys_ioctl(int, unsigned long, ...);
1249 /* #include <sys/mman.h> */
1251 int __sys_msync(void *, size_t, int);
1254 /* #include <sys/mount.h> */
1255 #ifdef _SYS_MOUNT_H_
1256 int __sys_fstatfs(int, struct statfs *);
1259 /* #include <sys/socket.h> */
1260 #ifdef _SYS_SOCKET_H_
1261 int __sys_accept(int, struct sockaddr *, socklen_t *);
1262 int __sys_bind(int, const struct sockaddr *, socklen_t);
1263 int __sys_connect(int, const struct sockaddr *, socklen_t);
1264 int __sys_getpeername(int, struct sockaddr *, socklen_t *);
1265 int __sys_getsockname(int, struct sockaddr *, socklen_t *);
1266 int __sys_getsockopt(int, int, int, void *, socklen_t *);
1267 int __sys_listen(int, int);
1268 ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
1269 ssize_t __sys_recvmsg(int, struct msghdr *, int);
1270 int __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *, off_t *, int);
1271 ssize_t __sys_sendmsg(int, const struct msghdr *, int);
1272 ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
1273 int __sys_setsockopt(int, int, int, const void *, socklen_t);
1274 int __sys_shutdown(int, int);
1275 int __sys_socket(int, int, int);
1276 int __sys_socketpair(int, int, int, int *);
1279 /* #include <sys/stat.h> */
1281 int __sys_fchflags(int, u_long);
1282 int __sys_fchmod(int, mode_t);
1283 int __sys_fstat(int, struct stat *);
1286 /* #include <sys/uio.h> */
1288 ssize_t __sys_readv(int, const struct iovec *, int);
1289 ssize_t __sys_writev(int, const struct iovec *, int);
1292 /* #include <sys/wait.h> */
1294 pid_t __sys_wait4(pid_t, int *, int, struct rusage *);
1297 /* #include <dirent.h> */
1299 int __sys_getdirentries(int, char *, int, long *);
1302 /* #include <fcntl.h> */
1303 #ifdef _SYS_FCNTL_H_
1304 int __sys_fcntl(int, int, ...);
1305 int __sys_flock(int, int);
1306 int __sys_open(const char *, int, ...);
1309 /* #include <poll.h> */
1311 int __sys_poll(struct pollfd *, unsigned, int);
1314 /* #include <signal.h> */
1316 int __sys_sigaction(int, const struct sigaction *, struct sigaction *);
1317 int __sys_sigprocmask(int, const sigset_t *, sigset_t *);
1318 int __sys_sigreturn(ucontext_t *);
1321 /* #include <unistd.h> */
1323 void __sys__exit(int);
1324 int __sys_close(int);
1326 int __sys_dup2(int, int);
1327 int __sys_execve(const char *, char * const *, char * const *);
1328 int __sys_fchown(int, uid_t, gid_t);
1329 pid_t __sys_fork(void);
1330 long __sys_fpathconf(int, int);
1331 int __sys_fsync(int);
1332 int __sys_pipe(int *);
1333 ssize_t __sys_read(int, void *, size_t);
1334 ssize_t __sys_write(int, const void *, size_t);
1337 /* #include <setjmp.h> */
1339 extern void __siglongjmp(sigjmp_buf, int) __dead2;
1340 extern void __longjmp(jmp_buf, int) __dead2;
1341 extern void ___longjmp(jmp_buf, int) __dead2;
1345 #endif /* !_PTHREAD_PRIVATE_H */