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.
37 #ifndef _PTHREAD_PRIVATE_H
38 #define _PTHREAD_PRIVATE_H
41 * Evaluate the storage class specifier.
43 #ifdef GLOBAL_PTHREAD_PRIVATE
55 #include <sys/param.h>
56 #include <sys/queue.h>
57 #include <sys/types.h>
59 #include <sys/cdefs.h>
63 #include <pthread_np.h>
65 #include <vm/vm_param.h>
67 #include <vm/vm_map.h>
70 * Define machine dependent macros to get and set the stack pointer
71 * from the supported contexts. Also define a macro to set the return
72 * address in a jmp_buf context.
74 * XXX - These need to be moved into architecture dependent support files.
75 * XXX - These need to be documented so porters know what's required.
78 #define GET_STACK_JB(jb) ((unsigned long)((jb)[0]._jb[2]))
79 #define GET_STACK_SJB(sjb) ((unsigned long)((sjb)[0]._sjb[2]))
80 #define GET_STACK_UC(ucp) ((unsigned long)((ucp)->uc_mcontext.mc_esp))
81 #define SET_STACK_JB(jb, stk) (jb)[0]._jb[2] = (int)(stk)
82 #define SET_STACK_SJB(sjb, stk) (sjb)[0]._sjb[2] = (int)(stk)
83 #define SET_STACK_UC(ucp, stk) (ucp)->uc_mcontext.mc_esp = (int)(stk)
84 #define FP_SAVE_UC(ucp) do { \
86 fdata = (char *) (ucp)->uc_mcontext.mc_fpstate; \
87 __asm__("fnsave %0": :"m"(*fdata)); \
89 #define FP_RESTORE_UC(ucp) do { \
91 fdata = (char *) (ucp)->uc_mcontext.mc_fpstate; \
92 __asm__("frstor %0": :"m"(*fdata)); \
94 #define SET_RETURN_ADDR_JB(jb, ra) do { \
95 (jb)[0]._jb[0] = (int)(ra); \
98 #elif defined(__amd64__)
99 #define GET_STACK_JB(jb) ((unsigned long)((jb)[0]._jb[2]))
100 #define GET_STACK_SJB(sjb) ((unsigned long)((sjb)[0]._sjb[2]))
101 #define GET_STACK_UC(ucp) ((unsigned long)((ucp)->uc_mcontext.mc_rsp))
102 #define SET_STACK_JB(jb, stk) (jb)[0]._jb[2] = (long)(stk)
103 #define SET_STACK_SJB(sjb, stk) (sjb)[0]._sjb[2] = (long)(stk)
104 #define SET_STACK_UC(ucp, stk) (ucp)->uc_mcontext.mc_rsp = (long)(stk)
105 #define FP_SAVE_UC(ucp) do { \
107 fdata = (char *) (ucp)->uc_mcontext.mc_fpstate; \
108 __asm__("fxsave %0": :"m"(*fdata)); \
110 #define FP_RESTORE_UC(ucp) do { \
112 fdata = (char *) (ucp)->uc_mcontext.mc_fpstate; \
113 __asm__("fxrstor %0": :"m"(*fdata)); \
115 #define SET_RETURN_ADDR_JB(jb, ra) (jb)[0]._jb[0] = (long)(ra)
116 #elif defined(__alpha__)
117 #include <machine/reg.h>
118 #define GET_STACK_JB(jb) ((unsigned long)((jb)[0]._jb[R_SP + 4]))
119 #define GET_STACK_SJB(sjb) ((unsigned long)((sjb)[0]._sjb[R_SP + 4]))
120 #define GET_STACK_UC(ucp) ((ucp)->uc_mcontext.mc_regs[R_SP])
121 #define SET_STACK_JB(jb, stk) (jb)[0]._jb[R_SP + 4] = (long)(stk)
122 #define SET_STACK_SJB(sjb, stk) (sjb)[0]._sjb[R_SP + 4] = (long)(stk)
123 #define SET_STACK_UC(ucp, stk) (ucp)->uc_mcontext.mc_regs[R_SP] = (unsigned long)(stk)
124 #define FP_SAVE_UC(ucp)
125 #define FP_RESTORE_UC(ucp)
126 #define SET_RETURN_ADDR_JB(jb, ra) do { \
127 (jb)[0]._jb[2] = (long)(ra); \
128 (jb)[0]._jb[R_RA + 4] = (long)(ra); \
129 (jb)[0]._jb[R_T12 + 4] = (long)(ra); \
131 #elif defined(__ia64__)
132 #define GET_BSP_JB(jb) (*((unsigned long*)JMPBUF_ADDR_OF(jb,J_BSP)))
133 #define GET_STACK_JB(jb) (*((unsigned long*)JMPBUF_ADDR_OF(jb,J_SP)))
134 #define GET_STACK_SJB(sjb) GET_STACK_JB(sjb)
135 #define SET_RETURN_ADDR_JB(jb, ra) \
137 *((unsigned long*)JMPBUF_ADDR_OF(jb,J_B0)) = ((long*)(ra))[0]; \
138 *((unsigned long*)JMPBUF_ADDR_OF(jb,J_GP)) = ((long*)(ra))[1]; \
139 *((unsigned long*)JMPBUF_ADDR_OF(jb,J_PFS)) &= ~0x1FFFFFFFFFUL; \
141 #define SET_STACK_JB(jb, stk, sz) \
143 UPD_STACK_JB(jb, stk + sz - 16); \
144 GET_BSP_JB(jb) = (long)(stk); \
146 #define UPD_STACK_JB(jb, stk) GET_STACK_JB(jb) = (long)(stk)
147 #elif defined(__sparc64__)
148 #include <machine/frame.h>
150 #define CCFSZ sizeof (struct frame)
152 #define GET_STACK_JB(jb) \
153 ((unsigned long)((jb)[0]._jb[_JB_SP]) + SPOFF)
154 #define GET_STACK_SJB(sjb) \
155 ((unsigned long)((sjb)[0]._sjb[_JB_SP]) + SPOFF)
156 #define GET_STACK_UC(ucp) \
157 ((ucp)->uc_mcontext.mc_sp + SPOFF)
159 * XXX: sparc64 _longjmp() expects a register window on the stack
160 * at the given position, so we must make sure that the space where
161 * it is expected is readable. Subtracting the frame size here works
162 * because the SET_STACK macros are only used to set up new stacks
163 * or signal stacks, but it is a bit dirty.
165 #define SET_STACK_JB(jb, stk) \
166 (jb)[0]._jb[_JB_SP] = (long)(stk) - SPOFF - CCFSZ
167 #define SET_STACK_SJB(sjb, stk) \
168 (sjb)[0]._sjb[_JB_SP] = (long)(stk) - SPOFF - CCFSZ
169 #define SET_STACK_UC(ucp, stk) \
170 (ucp)->uc_mcontext.mc_sp = (unsigned long)(stk) - SPOFF - CCFSZ
171 #define FP_SAVE_UC(ucp) /* XXX */
172 #define FP_RESTORE_UC(ucp) /* XXX */
173 #define SET_RETURN_ADDR_JB(jb, ra) \
174 (jb)[0]._jb[_JB_PC] = (long)(ra) - 8
176 #error "Don't recognize this architecture!"
180 * Kernel fatal error handler macro.
182 #define PANIC(string) _thread_exit(__FILE__,__LINE__,string)
185 /* Output debug messages like this: */
186 #define stdout_debug(args...) do { \
188 snprintf(buf, sizeof(buf), ##args); \
189 __sys_write(1, buf, strlen(buf)); \
191 #define stderr_debug(args...) do { \
193 snprintf(buf, sizeof(buf), ##args); \
194 __sys_write(2, buf, strlen(buf)); \
200 * Priority queue manipulation macros (using pqe link):
202 #define PTHREAD_PRIOQ_INSERT_HEAD(thrd) _pq_insert_head(&_readyq,thrd)
203 #define PTHREAD_PRIOQ_INSERT_TAIL(thrd) _pq_insert_tail(&_readyq,thrd)
204 #define PTHREAD_PRIOQ_REMOVE(thrd) _pq_remove(&_readyq,thrd)
205 #define PTHREAD_PRIOQ_FIRST() _pq_first(&_readyq)
208 * Waiting queue manipulation macros (using pqe link):
210 #define PTHREAD_WAITQ_REMOVE(thrd) _waitq_remove(thrd)
211 #define PTHREAD_WAITQ_INSERT(thrd) _waitq_insert(thrd)
213 #if defined(_PTHREADS_INVARIANTS)
214 #define PTHREAD_WAITQ_CLEARACTIVE() _waitq_clearactive()
215 #define PTHREAD_WAITQ_SETACTIVE() _waitq_setactive()
217 #define PTHREAD_WAITQ_CLEARACTIVE()
218 #define PTHREAD_WAITQ_SETACTIVE()
222 * Work queue manipulation macros (using qe link):
224 #define PTHREAD_WORKQ_INSERT(thrd) do { \
225 TAILQ_INSERT_TAIL(&_workq,thrd,qe); \
226 (thrd)->flags |= PTHREAD_FLAGS_IN_WORKQ; \
228 #define PTHREAD_WORKQ_REMOVE(thrd) do { \
229 TAILQ_REMOVE(&_workq,thrd,qe); \
230 (thrd)->flags &= ~PTHREAD_FLAGS_IN_WORKQ; \
235 * State change macro without scheduling queue change:
237 #define PTHREAD_SET_STATE(thrd, newstate) do { \
238 (thrd)->state = newstate; \
239 (thrd)->fname = __FILE__; \
240 (thrd)->lineno = __LINE__; \
244 * State change macro with scheduling queue change - This must be
245 * called with preemption deferred (see thread_kern_sched_[un]defer).
247 #if defined(_PTHREADS_INVARIANTS)
249 #define PTHREAD_ASSERT(cond, msg) do { \
253 #define PTHREAD_ASSERT_NOT_IN_SYNCQ(thrd) \
254 PTHREAD_ASSERT((((thrd)->flags & PTHREAD_FLAGS_IN_SYNCQ) == 0), \
255 "Illegal call from signal handler");
256 #define PTHREAD_NEW_STATE(thrd, newstate) do { \
257 if (_thread_kern_new_state != 0) \
258 PANIC("Recursive PTHREAD_NEW_STATE"); \
259 _thread_kern_new_state = 1; \
260 if ((thrd)->state != newstate) { \
261 if ((thrd)->state == PS_RUNNING) { \
262 PTHREAD_PRIOQ_REMOVE(thrd); \
263 PTHREAD_SET_STATE(thrd, newstate); \
264 PTHREAD_WAITQ_INSERT(thrd); \
265 } else if (newstate == PS_RUNNING) { \
266 PTHREAD_WAITQ_REMOVE(thrd); \
267 PTHREAD_SET_STATE(thrd, newstate); \
268 PTHREAD_PRIOQ_INSERT_TAIL(thrd); \
271 _thread_kern_new_state = 0; \
274 #define PTHREAD_ASSERT(cond, msg)
275 #define PTHREAD_ASSERT_NOT_IN_SYNCQ(thrd)
276 #define PTHREAD_NEW_STATE(thrd, newstate) do { \
277 if ((thrd)->state != newstate) { \
278 if ((thrd)->state == PS_RUNNING) { \
279 PTHREAD_PRIOQ_REMOVE(thrd); \
280 PTHREAD_WAITQ_INSERT(thrd); \
281 } else if (newstate == PS_RUNNING) { \
282 PTHREAD_WAITQ_REMOVE(thrd); \
283 PTHREAD_PRIOQ_INSERT_TAIL(thrd); \
286 PTHREAD_SET_STATE(thrd, newstate); \
291 * Define the signals to be used for scheduling.
293 #if defined(_PTHREADS_COMPAT_SCHED)
294 #define _ITIMER_SCHED_TIMER ITIMER_VIRTUAL
295 #define _SCHED_SIGNAL SIGVTALRM
297 #define _ITIMER_SCHED_TIMER ITIMER_PROF
298 #define _SCHED_SIGNAL SIGPROF
304 * XXX It'd be nice if these were contained in uthread_priority_queue.[ch].
306 typedef struct pq_list {
307 TAILQ_HEAD(, pthread) pl_head; /* list of threads at this priority */
308 TAILQ_ENTRY(pq_list) pl_link; /* link for queue of priority lists */
309 int pl_prio; /* the priority of this list */
310 int pl_queued; /* is this in the priority queue */
313 typedef struct pq_queue {
314 TAILQ_HEAD(, pq_list) pq_queue; /* queue of priority lists */
315 pq_list_t *pq_lists; /* array of all priority lists */
316 int pq_size; /* number of priority lists */
321 * TailQ initialization values.
323 #define TAILQ_INITIALIZER { NULL, NULL }
328 union pthread_mutex_data {
333 struct pthread_mutex {
334 enum pthread_mutextype m_type;
336 TAILQ_HEAD(mutex_head, pthread) m_queue;
337 struct pthread *m_owner;
338 union pthread_mutex_data m_data;
343 * Used for priority inheritence and protection.
345 * m_prio - For priority inheritence, the highest active
346 * priority (threads locking the mutex inherit
347 * this priority). For priority protection, the
348 * ceiling priority of this mutex.
349 * m_saved_prio - mutex owners inherited priority before
350 * taking the mutex, restored when the owner
357 * Link for list of all mutexes a thread currently owns.
359 TAILQ_ENTRY(pthread_mutex) m_qe;
362 * Lock for accesses to this structure.
370 #define MUTEX_FLAGS_PRIVATE 0x01
371 #define MUTEX_FLAGS_INITED 0x02
372 #define MUTEX_FLAGS_BUSY 0x04
375 * Static mutex initialization values.
377 #define PTHREAD_MUTEX_STATIC_INITIALIZER \
378 { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, TAILQ_INITIALIZER, \
379 NULL, { NULL }, MUTEX_FLAGS_PRIVATE, 0, 0, 0, TAILQ_INITIALIZER, \
380 _SPINLOCK_INITIALIZER }
382 struct pthread_mutex_attr {
383 enum pthread_mutextype m_type;
389 #define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \
390 { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE }
393 * Condition variable definitions.
395 enum pthread_cond_type {
400 struct pthread_cond {
401 enum pthread_cond_type c_type;
402 TAILQ_HEAD(cond_head, pthread) c_queue;
403 pthread_mutex_t c_mutex;
409 * Lock for accesses to this structure.
414 struct pthread_cond_attr {
415 enum pthread_cond_type c_type;
420 * Flags for condition variables.
422 #define COND_FLAGS_PRIVATE 0x01
423 #define COND_FLAGS_INITED 0x02
424 #define COND_FLAGS_BUSY 0x04
427 * Static cond initialization values.
429 #define PTHREAD_COND_STATIC_INITIALIZER \
430 { COND_TYPE_FAST, TAILQ_INITIALIZER, NULL, NULL, \
431 0, 0, _SPINLOCK_INITIALIZER }
434 * Semaphore definitions.
437 #define SEM_MAGIC ((u_int32_t) 0x09fa4012)
439 pthread_mutex_t lock;
440 pthread_cond_t gtzero;
446 * Cleanup definitions.
448 struct pthread_cleanup {
449 struct pthread_cleanup *next;
454 struct pthread_atfork {
455 TAILQ_ENTRY(pthread_atfork) qe;
456 void (*prepare)(void);
457 void (*parent)(void);
461 struct pthread_attr {
469 void (*cleanup_attr) ();
470 void *stackaddr_attr;
471 size_t stacksize_attr;
472 size_t guardsize_attr;
476 * Thread creation state attributes.
478 #define PTHREAD_CREATE_RUNNING 0
479 #define PTHREAD_CREATE_SUSPENDED 1
482 * Miscellaneous definitions.
484 #define PTHREAD_STACK32_DEFAULT (1 * 1024 * 1024)
485 #define PTHREAD_STACK64_DEFAULT (2 * 1024 * 1024)
488 * Size of default red zone at the end of each stack. In actuality, this "red
489 * zone" is merely an unmapped region, except in the case of the initial stack.
490 * Since mmap() makes it possible to specify the maximum growth of a MAP_STACK
491 * region, an unmapped gap between thread stacks achieves the same effect as
492 * explicitly mapped red zones.
493 * This is declared and initialized in uthread_init.c.
495 extern int _pthread_guard_default;
497 extern int _pthread_page_size;
499 extern int _pthread_stack_default;
501 extern int _pthread_stack_initial;
504 * Maximum size of initial thread's stack. This perhaps deserves to be larger
505 * than the stacks of other threads, since many applications are likely to run
506 * almost entirely on this stack.
508 #define PTHREAD_STACK32_INITIAL (2 * 1024 * 1024)
509 #define PTHREAD_STACK64_INITIAL (4 * 1024 * 1024)
512 * Define the different priority ranges. All applications have thread
513 * priorities constrained within 0-31. The threads library raises the
514 * priority when delivering signals in order to ensure that signal
515 * delivery happens (from the POSIX spec) "as soon as possible".
516 * In the future, the threads library will also be able to map specific
517 * threads into real-time (cooperating) processes or kernel threads.
518 * The RT and SIGNAL priorities will be used internally and added to
519 * thread base priorities so that the scheduling queue can handle both
520 * normal and RT priority threads with and without signal handling.
522 * The approach taken is that, within each class, signal delivery
523 * always has priority over thread execution.
525 #define PTHREAD_DEFAULT_PRIORITY 15
526 #define PTHREAD_MIN_PRIORITY 0
527 #define PTHREAD_MAX_PRIORITY 31 /* 0x1F */
528 #define PTHREAD_SIGNAL_PRIORITY 32 /* 0x20 */
529 #define PTHREAD_RT_PRIORITY 64 /* 0x40 */
530 #define PTHREAD_FIRST_PRIORITY PTHREAD_MIN_PRIORITY
531 #define PTHREAD_LAST_PRIORITY \
532 (PTHREAD_MAX_PRIORITY + PTHREAD_SIGNAL_PRIORITY + PTHREAD_RT_PRIORITY)
533 #define PTHREAD_BASE_PRIORITY(prio) ((prio) & PTHREAD_MAX_PRIORITY)
536 * Clock resolution in microseconds.
538 #define CLOCK_RES_USEC 10000
539 #define CLOCK_RES_USEC_MIN 1000
542 * Time slice period in microseconds.
544 #define TIMESLICE_USEC 20000
547 * Define a thread-safe macro to get the current time of day
548 * which is updated at regular intervals by the scheduling signal
551 #define GET_CURRENT_TOD(tv) \
553 tv.tv_sec = _sched_tod.tv_sec; \
554 tv.tv_usec = _sched_tod.tv_usec; \
555 } while (tv.tv_sec != _sched_tod.tv_sec)
558 struct pthread_rwlockattr {
562 struct pthread_rwlock {
563 pthread_mutex_t lock; /* monitor lock */
564 int state; /* 0 = idle >0 = # of readers -1 = writer */
565 pthread_cond_t read_signal;
566 pthread_cond_t write_signal;
599 * File descriptor locking definitions.
603 #define FD_RDWR (FD_READ | FD_WRITE)
606 * File descriptor table structure.
608 struct fd_table_entry {
610 * Lock for accesses to this file descriptor table
611 * entry. This is passed to _spinlock() to provide atomic
612 * access to this structure. It does *not* represent the
613 * state of the lock on the file descriptor.
616 TAILQ_HEAD(, pthread) r_queue; /* Read queue. */
617 TAILQ_HEAD(, pthread) w_queue; /* Write queue. */
618 struct pthread *r_owner; /* Ptr to thread owning read lock. */
619 struct pthread *w_owner; /* Ptr to thread owning write lock. */
620 char *r_fname; /* Ptr to read lock source file name */
621 int r_lineno; /* Read lock source line number. */
622 char *w_fname; /* Ptr to write lock source file name */
623 int w_lineno; /* Write lock source line number. */
624 int r_lockcount; /* Count for FILE read locks. */
625 int w_lockcount; /* Count for FILE write locks. */
626 int flags; /* Flags used in open. */
629 struct pthread_poll_data {
634 union pthread_wait_data {
635 pthread_mutex_t mutex;
637 const sigset_t *sigwait; /* Waiting on a signal in sigwait */
639 short fd; /* Used when thread waiting on fd */
640 short branch; /* Line number, for debugging. */
641 char *fname; /* Source file name for debugging.*/
644 struct pthread_poll_data *poll_data;
645 spinlock_t *spinlock;
646 struct pthread *thread;
650 * Define a continuation routine that can be used to perform a
651 * transfer of control:
653 typedef void (*thread_continuation_t) (void *);
655 struct pthread_signal_frame;
657 struct pthread_state_data {
658 struct pthread_signal_frame *psd_curframe;
659 sigset_t psd_sigmask;
660 struct timespec psd_wakeup_time;
661 union pthread_wait_data psd_wait_data;
662 enum pthread_state psd_state;
666 int psd_sigmask_seqno;
668 int psd_sig_defer_count;
669 /* XXX - What about thread->timeout and/or thread->error? */
673 struct pthread *thread;
679 * The frame that is added to the top of a threads stack when setting up
680 * up the thread to run a signal handler.
682 struct pthread_signal_frame {
684 * This stores the threads state before the signal.
686 struct pthread_state_data saved_state;
689 * Threads return context; we use only jmp_buf's for now.
695 int signo; /* signal, arg 1 to sighandler */
696 int sig_has_args; /* use signal args if true */
701 struct pthread_specific_elem {
711 * Magic value to help recognize a valid thread structure
712 * from an invalid one:
714 #define PTHREAD_MAGIC ((u_int32_t) 0xd09ba115)
717 u_int64_t uniqueid; /* for gdb */
720 * Lock for accesses to this thread structure.
724 /* Queue entry for list of all threads: */
725 TAILQ_ENTRY(pthread) tle;
727 /* Queue entry for list of dead threads: */
728 TAILQ_ENTRY(pthread) dle;
731 * Thread start routine, argument, stack pointer and thread
734 void *(*start_routine)(void *);
737 struct pthread_attr attr;
740 * Threads return context; we use only jmp_buf's for now.
748 * Used for tracking delivery of signal handlers.
750 struct pthread_signal_frame *curframe;
753 * Cancelability flags - the lower 2 bits are used by cancel
754 * definitions in pthread.h
756 #define PTHREAD_AT_CANCEL_POINT 0x0004
757 #define PTHREAD_CANCELLING 0x0008
758 #define PTHREAD_CANCEL_NEEDED 0x0010
761 thread_continuation_t continuation;
764 * Current signal mask and pending signals.
772 enum pthread_state state;
774 /* Scheduling clock when this thread was last made active. */
777 /* Scheduling clock when this thread was last made inactive. */
781 * Number of microseconds accumulated by this thread when
782 * time slicing is active.
787 * Time to wake up thread. This is used for sleeping threads and
788 * for any operation which may time out (such as select).
790 struct timespec wakeup_time;
792 /* TRUE if operation has timed out. */
796 * Error variable used instead of errno. The function __error()
797 * returns a pointer to this.
802 * The joiner is the thread that is joining to this thread. The
803 * join status keeps track of a join operation to another thread.
805 struct pthread *joiner;
806 struct join_status join_status;
809 * The current thread can belong to only one scheduling queue at
810 * a time (ready or waiting queue). It can also belong to:
812 * o A queue of threads waiting for a mutex
813 * o A queue of threads waiting for a condition variable
814 * o A queue of threads waiting for a file descriptor lock
815 * o A queue of threads needing work done by the kernel thread
816 * (waiting for a spinlock or file I/O)
818 * A thread can also be joining a thread (the joiner field above).
820 * It must not be possible for a thread to belong to any of the
821 * above queues while it is handling a signal. Signal handlers
822 * may longjmp back to previous stack frames circumventing normal
823 * control flow. This could corrupt queue integrity if the thread
824 * retains membership in the queue. Therefore, if a thread is a
825 * member of one of these queues when a signal handler is invoked,
826 * it must remove itself from the queue before calling the signal
827 * handler and reinsert itself after normal return of the handler.
829 * Use pqe for the scheduling queue link (both ready and waiting),
830 * sqe for synchronization (mutex and condition variable) queue
831 * links, and qe for all other links.
833 TAILQ_ENTRY(pthread) pqe; /* priority queue link */
834 TAILQ_ENTRY(pthread) sqe; /* synchronization queue link */
835 TAILQ_ENTRY(pthread) qe; /* all other queues link */
838 union pthread_wait_data data;
841 * Allocated for converting select into poll.
843 struct pthread_poll_data poll_data;
846 * Set to TRUE if a blocking operation was
847 * interrupted by a signal:
851 /* Signal number when in state PS_SIGWAIT: */
855 * Set to non-zero when this thread has deferred signals.
856 * We allow for recursive deferral.
861 * Set to TRUE if this thread should yield after undeferring
864 int yield_on_sig_undefer;
866 /* Miscellaneous flags; only set with signals deferred. */
868 #define PTHREAD_FLAGS_PRIVATE 0x0001
869 #define PTHREAD_EXITING 0x0002
870 #define PTHREAD_FLAGS_IN_WAITQ 0x0004 /* in waiting queue using pqe link */
871 #define PTHREAD_FLAGS_IN_PRIOQ 0x0008 /* in priority queue using pqe link */
872 #define PTHREAD_FLAGS_IN_WORKQ 0x0010 /* in work queue using qe link */
873 #define PTHREAD_FLAGS_IN_FILEQ 0x0020 /* in file lock queue using qe link */
874 #define PTHREAD_FLAGS_IN_FDQ 0x0040 /* in fd lock queue using qe link */
875 #define PTHREAD_FLAGS_IN_CONDQ 0x0080 /* in condition queue using sqe link*/
876 #define PTHREAD_FLAGS_IN_MUTEXQ 0x0100 /* in mutex queue using sqe link */
877 #define PTHREAD_FLAGS_SUSPENDED 0x0200 /* thread is suspended */
878 #define PTHREAD_FLAGS_TRACE 0x0400 /* for debugging purposes */
879 #define PTHREAD_FLAGS_IN_SYNCQ \
880 (PTHREAD_FLAGS_IN_CONDQ | PTHREAD_FLAGS_IN_MUTEXQ)
883 * Base priority is the user setable and retrievable priority
884 * of the thread. It is only affected by explicit calls to
885 * set thread priority and upon thread creation via a thread
886 * attribute or default priority.
891 * Inherited priority is the priority a thread inherits by
892 * taking a priority inheritence or protection mutex. It
893 * is not affected by base priority changes. Inherited
894 * priority defaults to and remains 0 until a mutex is taken
895 * that is being waited on by any other thread whose priority
898 char inherited_priority;
901 * Active priority is always the maximum of the threads base
902 * priority and inherited priority. When there is a change
903 * in either the base or inherited priority, the active
904 * priority must be recalculated.
906 char active_priority;
908 /* Number of priority ceiling or protection mutexes owned. */
909 int priority_mutex_count;
911 /* Number rwlocks rdlocks held. */
915 * Queue of currently owned mutexes.
917 TAILQ_HEAD(, pthread_mutex) mutexq;
920 struct pthread_specific_elem *specific;
921 int specific_data_count;
923 /* Cleanup handlers Link List */
924 struct pthread_cleanup *cleanup;
925 char *fname; /* Ptr to source file name */
926 int lineno; /* Source line number. */
930 * Global variables for the uthread kernel.
933 SCLASS void *_usrstack
934 #ifdef GLOBAL_PTHREAD_PRIVATE
940 /* Kernel thread structure used when there are no running threads: */
941 SCLASS struct pthread _thread_kern_thread;
943 /* Ptr to the thread structure for the running thread: */
944 SCLASS struct pthread * volatile _thread_run
945 #ifdef GLOBAL_PTHREAD_PRIVATE
946 = &_thread_kern_thread;
951 /* Ptr to the thread structure for the last user thread to run: */
952 SCLASS struct pthread * volatile _last_user_thread
953 #ifdef GLOBAL_PTHREAD_PRIVATE
954 = &_thread_kern_thread;
959 /* List of all threads: */
960 SCLASS TAILQ_HEAD(, pthread) _thread_list
961 #ifdef GLOBAL_PTHREAD_PRIVATE
962 = TAILQ_HEAD_INITIALIZER(_thread_list);
968 * Array of kernel pipe file descriptors that are used to ensure that
969 * no signals are missed in calls to _select.
971 SCLASS int _thread_kern_pipe[2]
972 #ifdef GLOBAL_PTHREAD_PRIVATE
980 SCLASS int volatile _queue_signals
981 #ifdef GLOBAL_PTHREAD_PRIVATE
986 SCLASS int _thread_kern_in_sched
987 #ifdef GLOBAL_PTHREAD_PRIVATE
993 SCLASS int _sig_in_handler
994 #ifdef GLOBAL_PTHREAD_PRIVATE
1000 /* Time of day at last scheduling timer signal: */
1001 SCLASS struct timeval volatile _sched_tod
1002 #ifdef GLOBAL_PTHREAD_PRIVATE
1009 * Current scheduling timer ticks; used as resource usage.
1011 SCLASS unsigned int volatile _sched_ticks
1012 #ifdef GLOBAL_PTHREAD_PRIVATE
1019 SCLASS TAILQ_HEAD(, pthread) _dead_list
1020 #ifdef GLOBAL_PTHREAD_PRIVATE
1021 = TAILQ_HEAD_INITIALIZER(_dead_list);
1026 /* Initial thread: */
1027 SCLASS struct pthread *_thread_initial
1028 #ifdef GLOBAL_PTHREAD_PRIVATE
1034 SCLASS TAILQ_HEAD(atfork_head, pthread_atfork) _atfork_list;
1035 SCLASS pthread_mutex_t _atfork_mutex;
1037 /* Default thread attributes: */
1038 SCLASS struct pthread_attr _pthread_attr_default
1039 #ifdef GLOBAL_PTHREAD_PRIVATE
1040 = { SCHED_RR, 0, TIMESLICE_USEC, PTHREAD_DEFAULT_PRIORITY,
1041 PTHREAD_CREATE_RUNNING, PTHREAD_CREATE_JOINABLE, NULL, NULL, NULL,
1047 /* Default mutex attributes: */
1048 #define PTHREAD_MUTEXATTR_DEFAULT \
1049 { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, 0 }
1051 /* Default condition variable attributes: */
1052 #define PTHREAD_CONDATTR_DEFAULT { COND_TYPE_FAST, 0 }
1055 * Standard I/O file descriptors need special flag treatment since
1056 * setting one to non-blocking does all on *BSD. Sigh. This array
1057 * is used to store the initial flag settings.
1059 SCLASS int _pthread_stdio_flags[3];
1061 /* File table information: */
1062 SCLASS struct fd_table_entry **_thread_fd_table
1063 #ifdef GLOBAL_PTHREAD_PRIVATE
1069 /* Table for polling file descriptors: */
1070 SCLASS struct pollfd *_thread_pfd_table
1071 #ifdef GLOBAL_PTHREAD_PRIVATE
1077 SCLASS const int dtablecount
1078 #ifdef GLOBAL_PTHREAD_PRIVATE
1079 = 4096/sizeof(struct fd_table_entry);
1083 SCLASS int _thread_dtablesize /* Descriptor table size. */
1084 #ifdef GLOBAL_PTHREAD_PRIVATE
1090 SCLASS int _clock_res_usec /* Clock resolution in usec. */
1091 #ifdef GLOBAL_PTHREAD_PRIVATE
1097 /* Garbage collector mutex and condition variable. */
1098 SCLASS pthread_mutex_t _gc_mutex
1099 #ifdef GLOBAL_PTHREAD_PRIVATE
1103 SCLASS pthread_cond_t _gc_cond
1104 #ifdef GLOBAL_PTHREAD_PRIVATE
1110 * Array of signal actions for this process.
1112 SCLASS struct sigaction _thread_sigact[NSIG];
1115 * Array of counts of dummy handlers for SIG_DFL signals. This is used to
1116 * assure that there is always a dummy signal handler installed while there is a
1117 * thread sigwait()ing on the corresponding signal.
1119 SCLASS int _thread_dfl_count[NSIG];
1122 * Pending signals and mask for this process:
1124 SCLASS sigset_t _process_sigpending;
1125 SCLASS sigset_t _process_sigmask
1126 #ifdef GLOBAL_PTHREAD_PRIVATE
1132 * Scheduling queues:
1134 SCLASS pq_queue_t _readyq;
1135 SCLASS TAILQ_HEAD(, pthread) _waitingq;
1140 SCLASS TAILQ_HEAD(, pthread) _workq;
1142 /* Tracks the number of threads blocked while waiting for a spinlock. */
1143 SCLASS volatile int _spinblock_count
1144 #ifdef GLOBAL_PTHREAD_PRIVATE
1149 /* Used to maintain pending and active signals: */
1151 int pending; /* Is this a pending signal? */
1153 * A handler is currently active for
1154 * this signal; ignore subsequent
1155 * signals until the handler is done.
1157 int signo; /* arg 1 to signal handler */
1158 siginfo_t siginfo; /* arg 2 to signal handler */
1159 ucontext_t uc; /* arg 3 to signal handler */
1162 SCLASS struct sigstatus _thread_sigq[NSIG];
1164 /* Indicates that the signal queue needs to be checked. */
1165 SCLASS volatile int _sigq_check_reqd
1166 #ifdef GLOBAL_PTHREAD_PRIVATE
1171 /* Thread switch hook. */
1172 SCLASS pthread_switch_routine_t _sched_switch_hook
1173 #ifdef GLOBAL_PTHREAD_PRIVATE
1179 * Declare the kernel scheduler jump buffer and stack:
1181 SCLASS jmp_buf _thread_kern_sched_jb;
1183 SCLASS void * _thread_kern_sched_stack
1184 #ifdef GLOBAL_PTHREAD_PRIVATE
1190 /* Used for _PTHREADS_INVARIANTS checking. */
1191 SCLASS int _thread_kern_new_state
1192 #ifdef GLOBAL_PTHREAD_PRIVATE
1197 /* Undefine the storage class specifier: */
1201 #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock_debug(_fd, _type, \
1202 _ts, __FILE__, __LINE__)
1203 #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock_debug(_fd, _type, \
1206 #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock(_fd, _type, _ts)
1207 #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock(_fd, _type)
1211 * Function prototype definitions.
1214 char *__ttyname_basic(int);
1215 void _cond_wait_backout(pthread_t);
1216 void _fd_lock_backout(pthread_t);
1217 int _find_thread(pthread_t);
1218 struct pthread *_get_curthread(void);
1219 void _set_curthread(struct pthread *);
1220 void *_thread_stack_alloc(size_t, size_t);
1221 void _thread_stack_free(void *, size_t, size_t);
1222 int _thread_create(pthread_t *,const pthread_attr_t *,void *(*start_routine)(void *),void *,pthread_t);
1223 int _mutex_cv_lock(pthread_mutex_t *);
1224 int _mutex_cv_unlock(pthread_mutex_t *);
1225 void _mutex_lock_backout(pthread_t);
1226 void _mutex_notify_priochange(pthread_t);
1227 int _mutex_reinit(pthread_mutex_t *);
1228 void _mutex_unlock_private(pthread_t);
1229 int _cond_reinit(pthread_cond_t *);
1230 int _pq_alloc(struct pq_queue *, int, int);
1231 int _pq_init(struct pq_queue *);
1232 void _pq_remove(struct pq_queue *pq, struct pthread *);
1233 void _pq_insert_head(struct pq_queue *pq, struct pthread *);
1234 void _pq_insert_tail(struct pq_queue *pq, struct pthread *);
1235 struct pthread *_pq_first(struct pq_queue *pq);
1236 void *_pthread_getspecific(pthread_key_t);
1237 int _pthread_key_create(pthread_key_t *, void (*) (void *));
1238 int _pthread_key_delete(pthread_key_t);
1239 int _pthread_mutex_destroy(pthread_mutex_t *);
1240 int _pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
1241 int _pthread_mutex_lock(pthread_mutex_t *);
1242 int _pthread_mutex_trylock(pthread_mutex_t *);
1243 int _pthread_mutex_unlock(pthread_mutex_t *);
1244 int _pthread_mutexattr_init(pthread_mutexattr_t *);
1245 int _pthread_mutexattr_destroy(pthread_mutexattr_t *);
1246 int _pthread_mutexattr_settype(pthread_mutexattr_t *, int);
1247 int _pthread_once(pthread_once_t *, void (*) (void));
1248 pthread_t _pthread_self(void);
1249 int _pthread_setspecific(pthread_key_t, const void *);
1250 void _waitq_insert(pthread_t pthread);
1251 void _waitq_remove(pthread_t pthread);
1252 #if defined(_PTHREADS_INVARIANTS)
1253 void _waitq_setactive(void);
1254 void _waitq_clearactive(void);
1256 void _thread_exit(char *, int, char *) __dead2;
1257 void _thread_exit_cleanup(void);
1258 int _thread_fd_getflags(int);
1259 int _thread_fd_lock(int, int, struct timespec *);
1260 int _thread_fd_lock_debug(int, int, struct timespec *,char *fname,int lineno);
1261 void _thread_fd_setflags(int, int);
1262 int _thread_fd_table_init(int fd);
1263 void _thread_fd_unlock(int, int);
1264 void _thread_fd_unlock_debug(int, int, char *, int);
1265 void _thread_fd_unlock_owned(pthread_t);
1266 void *_thread_cleanup(pthread_t);
1267 void _thread_cleanupspecific(void);
1268 void _thread_dump_info(void);
1269 void _thread_init(void);
1270 void _thread_kern_sched(ucontext_t *);
1271 void _thread_kern_scheduler(void);
1272 void _thread_kern_sched_frame(struct pthread_signal_frame *psf);
1273 void _thread_kern_sched_sig(void);
1274 void _thread_kern_sched_state(enum pthread_state, char *fname, int lineno);
1275 void _thread_kern_sched_state_unlock(enum pthread_state state,
1276 spinlock_t *lock, char *fname, int lineno);
1277 void _thread_kern_set_timeout(const struct timespec *);
1278 void _thread_kern_sig_defer(void);
1279 void _thread_kern_sig_undefer(void);
1280 void _thread_sig_handler(int, siginfo_t *, ucontext_t *);
1281 void _thread_sig_check_pending(struct pthread *pthread);
1282 void _thread_sig_handle_pending(void);
1283 void _thread_sig_send(struct pthread *pthread, int sig);
1284 void _thread_sig_wrapper(void);
1285 void _thread_sigframe_restore(struct pthread *thread,
1286 struct pthread_signal_frame *psf);
1287 void _thread_start(void);
1288 void _thread_seterrno(pthread_t, int);
1289 pthread_addr_t _thread_gc(pthread_addr_t);
1290 void _thread_enter_cancellation_point(void);
1291 void _thread_leave_cancellation_point(void);
1292 void _thread_cancellation_point(void);
1294 /* #include <sys/acl.h> */
1296 int __sys___acl_aclcheck_fd(int, acl_type_t, struct acl *);
1297 int __sys___acl_delete_fd(int, acl_type_t);
1298 int __sys___acl_get_fd(int, acl_type_t, struct acl *);
1299 int __sys___acl_set_fd(int, acl_type_t, struct acl *);
1302 /* #include <sys/aio.h> */
1304 int __sys_aio_suspend(const struct aiocb * const[], int, const struct timespec *);
1307 /* #include <sys/event.h> */
1308 #ifdef _SYS_EVENT_H_
1309 int __sys_kevent(int, const struct kevent *, int, struct kevent *,
1310 int, const struct timespec *);
1313 /* #include <sys/ioctl.h> */
1314 #ifdef _SYS_IOCTL_H_
1315 int __sys_ioctl(int, unsigned long, ...);
1318 /* #include <sys/mman.h> */
1320 int __sys_msync(void *, size_t, int);
1323 /* #include <sys/mount.h> */
1324 #ifdef _SYS_MOUNT_H_
1325 int __sys_fstatfs(int, struct statfs *);
1328 /* #include <sys/socket.h> */
1329 #ifdef _SYS_SOCKET_H_
1330 int __sys_accept(int, struct sockaddr *, socklen_t *);
1331 int __sys_bind(int, const struct sockaddr *, socklen_t);
1332 int __sys_connect(int, const struct sockaddr *, socklen_t);
1333 int __sys_getpeername(int, struct sockaddr *, socklen_t *);
1334 int __sys_getsockname(int, struct sockaddr *, socklen_t *);
1335 int __sys_getsockopt(int, int, int, void *, socklen_t *);
1336 int __sys_listen(int, int);
1337 ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
1338 ssize_t __sys_recvmsg(int, struct msghdr *, int);
1339 int __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *, off_t *, int);
1340 ssize_t __sys_sendmsg(int, const struct msghdr *, int);
1341 ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
1342 int __sys_setsockopt(int, int, int, const void *, socklen_t);
1343 int __sys_shutdown(int, int);
1344 int __sys_socket(int, int, int);
1345 int __sys_socketpair(int, int, int, int *);
1348 /* #include <sys/stat.h> */
1350 int __sys_fchflags(int, u_long);
1351 int __sys_fchmod(int, mode_t);
1352 int __sys_fstat(int, struct stat *);
1355 /* #include <sys/uio.h> */
1357 ssize_t __sys_readv(int, const struct iovec *, int);
1358 ssize_t __sys_writev(int, const struct iovec *, int);
1361 /* #include <sys/wait.h> */
1363 pid_t __sys_wait4(pid_t, int *, int, struct rusage *);
1366 /* #include <dirent.h> */
1368 int __sys_getdirentries(int, char *, int, long *);
1371 /* #include <fcntl.h> */
1372 #ifdef _SYS_FCNTL_H_
1373 int __sys_fcntl(int, int, ...);
1374 int __sys_flock(int, int);
1375 int __sys_open(const char *, int, ...);
1378 /* #include <poll.h> */
1380 int __sys_poll(struct pollfd *, unsigned, int);
1383 /* #include <signal.h> */
1385 int __sys_sigaction(int, const struct sigaction *, struct sigaction *);
1386 int __sys_sigprocmask(int, const sigset_t *, sigset_t *);
1387 int __sys_sigreturn(ucontext_t *);
1390 /* #include <unistd.h> */
1392 int __sys_close(int);
1394 int __sys_dup2(int, int);
1395 int __sys_execve(const char *, char * const *, char * const *);
1396 void __sys_exit(int) __dead2;
1397 int __sys_fchown(int, uid_t, gid_t);
1398 pid_t __sys_fork(void);
1399 long __sys_fpathconf(int, int);
1400 int __sys_fsync(int);
1401 int __sys_pipe(int *);
1402 ssize_t __sys_read(int, void *, size_t);
1403 ssize_t __sys_write(int, const void *, size_t);
1406 /* #include <setjmp.h> */
1408 extern void __siglongjmp(sigjmp_buf, int) __dead2;
1409 extern void __longjmp(jmp_buf, int) __dead2;
1410 extern void ___longjmp(jmp_buf, int) __dead2;
1414 #endif /* !_PTHREAD_PRIVATE_H */