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.12 2007/01/08 21:41:53 dillon 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)
91 #elif defined(__amd64__)
92 #define GET_STACK_JB(jb) ((unsigned long)((jb)[0]._jb[2]))
93 #define GET_STACK_SJB(sjb) ((unsigned long)((sjb)[0]._sjb[2]))
94 #define GET_STACK_UC(ucp) ((unsigned long)((ucp)->uc_mcontext.mc_rsp))
95 #define SET_STACK_JB(jb, stk) (jb)[0]._jb[2] = (long)(stk)
96 #define SET_STACK_SJB(sjb, stk) (sjb)[0]._sjb[2] = (long)(stk)
97 #define SET_STACK_UC(ucp, stk) (ucp)->uc_mcontext.mc_rsp = (long)(stk)
98 #define FP_SAVE_UC(ucp) do { \
100 fdata = (char *) (ucp)->uc_mcontext.mc_fpstate; \
101 __asm__("fxsave %0": :"m"(*fdata)); \
103 #define FP_RESTORE_UC(ucp) do { \
105 fdata = (char *) (ucp)->uc_mcontext.mc_fpstate; \
106 __asm__("fxrstor %0": :"m"(*fdata)); \
108 #define SET_RETURN_ADDR_JB(jb, ra) (jb)[0]._jb[0] = (long)(ra)
110 #error "Don't recognize this architecture!"
114 * Kernel fatal error handler macro.
116 #define PANIC(string) _thread_exit(__FILE__,__LINE__,string)
119 /* Output debug messages like this: */
120 #define stdout_debug(args...) do { \
122 snprintf(buf, sizeof(buf), ##args); \
123 __sys_extpwrite(1, buf, strlen(buf), O_FBLOCKING, -1); \
125 #define stderr_debug(args...) do { \
127 snprintf(buf, sizeof(buf), ##args); \
128 __sys_extpwrite(2, buf, strlen(buf), O_FBLOCKING, -1); \
134 * Priority queue manipulation macros (using pqe link):
136 #define PTHREAD_PRIOQ_INSERT_HEAD(thrd) _pq_insert_head(&_readyq,thrd)
137 #define PTHREAD_PRIOQ_INSERT_TAIL(thrd) _pq_insert_tail(&_readyq,thrd)
138 #define PTHREAD_PRIOQ_REMOVE(thrd) _pq_remove(&_readyq,thrd)
139 #define PTHREAD_PRIOQ_FIRST() _pq_first(&_readyq)
142 * Waiting queue manipulation macros (using pqe link):
144 #define PTHREAD_WAITQ_REMOVE(thrd) _waitq_remove(thrd)
145 #define PTHREAD_WAITQ_INSERT(thrd) _waitq_insert(thrd)
147 #if defined(_PTHREADS_INVARIANTS)
148 #define PTHREAD_WAITQ_CLEARACTIVE() _waitq_clearactive()
149 #define PTHREAD_WAITQ_SETACTIVE() _waitq_setactive()
151 #define PTHREAD_WAITQ_CLEARACTIVE()
152 #define PTHREAD_WAITQ_SETACTIVE()
156 * Work queue manipulation macros (using qe link):
158 #define PTHREAD_WORKQ_INSERT(thrd) do { \
159 TAILQ_INSERT_TAIL(&_workq,thrd,qe); \
160 (thrd)->flags |= PTHREAD_FLAGS_IN_WORKQ; \
162 #define PTHREAD_WORKQ_REMOVE(thrd) do { \
163 TAILQ_REMOVE(&_workq,thrd,qe); \
164 (thrd)->flags &= ~PTHREAD_FLAGS_IN_WORKQ; \
169 * State change macro without scheduling queue change:
171 #define PTHREAD_SET_STATE(thrd, newstate) do { \
172 (thrd)->state = newstate; \
173 (thrd)->fname = __FILE__; \
174 (thrd)->lineno = __LINE__; \
178 * State change macro with scheduling queue change - This must be
179 * called with preemption deferred (see thread_kern_sched_[un]defer).
181 #if defined(_PTHREADS_INVARIANTS)
183 #define PTHREAD_ASSERT(cond, msg) do { \
187 #define PTHREAD_ASSERT_NOT_IN_SYNCQ(thrd) \
188 PTHREAD_ASSERT((((thrd)->flags & PTHREAD_FLAGS_IN_SYNCQ) == 0), \
189 "Illegal call from signal handler");
190 #define PTHREAD_NEW_STATE(thrd, newstate) do { \
191 if (_thread_kern_new_state != 0) \
192 PANIC("Recursive PTHREAD_NEW_STATE"); \
193 _thread_kern_new_state = 1; \
194 if ((thrd)->state != newstate) { \
195 if ((thrd)->state == PS_RUNNING) { \
196 PTHREAD_PRIOQ_REMOVE(thrd); \
197 PTHREAD_SET_STATE(thrd, newstate); \
198 PTHREAD_WAITQ_INSERT(thrd); \
199 } else if (newstate == PS_RUNNING) { \
200 PTHREAD_WAITQ_REMOVE(thrd); \
201 PTHREAD_SET_STATE(thrd, newstate); \
202 PTHREAD_PRIOQ_INSERT_TAIL(thrd); \
205 _thread_kern_new_state = 0; \
208 #define PTHREAD_ASSERT(cond, msg)
209 #define PTHREAD_ASSERT_NOT_IN_SYNCQ(thrd)
210 #define PTHREAD_NEW_STATE(thrd, newstate) do { \
211 if ((thrd)->state != newstate) { \
212 if ((thrd)->state == PS_RUNNING) { \
213 PTHREAD_PRIOQ_REMOVE(thrd); \
214 PTHREAD_WAITQ_INSERT(thrd); \
215 } else if (newstate == PS_RUNNING) { \
216 PTHREAD_WAITQ_REMOVE(thrd); \
217 PTHREAD_PRIOQ_INSERT_TAIL(thrd); \
220 PTHREAD_SET_STATE(thrd, newstate); \
225 * Define the signals to be used for scheduling.
227 #if defined(_PTHREADS_COMPAT_SCHED)
228 #define _ITIMER_SCHED_TIMER ITIMER_VIRTUAL
229 #define _SCHED_SIGNAL SIGVTALRM
231 #define _ITIMER_SCHED_TIMER ITIMER_PROF
232 #define _SCHED_SIGNAL SIGPROF
238 * XXX It'd be nice if these were contained in uthread_priority_queue.[ch].
240 typedef struct pq_list {
241 TAILQ_HEAD(, pthread) pl_head; /* list of threads at this priority */
242 TAILQ_ENTRY(pq_list) pl_link; /* link for queue of priority lists */
243 int pl_prio; /* the priority of this list */
244 int pl_queued; /* is this in the priority queue */
247 typedef struct pq_queue {
248 TAILQ_HEAD(, pq_list) pq_queue; /* queue of priority lists */
249 pq_list_t *pq_lists; /* array of all priority lists */
250 int pq_size; /* number of priority lists */
255 * TailQ initialization values.
257 #define TAILQ_INITIALIZER { NULL, NULL }
262 union pthread_mutex_data {
267 struct pthread_mutex {
268 enum pthread_mutextype m_type;
270 TAILQ_HEAD(mutex_head, pthread) m_queue;
271 struct pthread *m_owner;
272 union pthread_mutex_data m_data;
277 * Used for priority inheritence and protection.
279 * m_prio - For priority inheritence, the highest active
280 * priority (threads locking the mutex inherit
281 * this priority). For priority protection, the
282 * ceiling priority of this mutex.
283 * m_saved_prio - mutex owners inherited priority before
284 * taking the mutex, restored when the owner
291 * Link for list of all mutexes a thread currently owns.
293 TAILQ_ENTRY(pthread_mutex) m_qe;
296 * Lock for accesses to this structure.
304 #define MUTEX_FLAGS_PRIVATE 0x01
305 #define MUTEX_FLAGS_INITED 0x02
306 #define MUTEX_FLAGS_BUSY 0x04
309 * Static mutex initialization values.
311 #define PTHREAD_MUTEX_STATIC_INITIALIZER \
312 { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, TAILQ_INITIALIZER, \
313 NULL, { NULL }, MUTEX_FLAGS_PRIVATE, 0, 0, 0, TAILQ_INITIALIZER, \
314 _SPINLOCK_INITIALIZER }
316 struct pthread_mutex_attr {
317 enum pthread_mutextype m_type;
324 * Condition variable definitions.
326 enum pthread_cond_type {
331 struct pthread_cond {
332 enum pthread_cond_type c_type;
333 TAILQ_HEAD(cond_head, pthread) c_queue;
334 pthread_mutex_t c_mutex;
340 * Lock for accesses to this structure.
345 struct pthread_cond_attr {
346 enum pthread_cond_type c_type;
351 * Flags for condition variables.
353 #define COND_FLAGS_PRIVATE 0x01
354 #define COND_FLAGS_INITED 0x02
355 #define COND_FLAGS_BUSY 0x04
358 * Static cond initialization values.
360 #define PTHREAD_COND_STATIC_INITIALIZER \
361 { COND_TYPE_FAST, TAILQ_INITIALIZER, NULL, NULL, \
362 0, 0, _SPINLOCK_INITIALIZER }
365 * Semaphore definitions.
368 #define SEM_MAGIC ((u_int32_t) 0x09fa4012)
370 pthread_mutex_t lock;
371 pthread_cond_t gtzero;
377 * Cleanup definitions.
379 struct pthread_cleanup {
380 struct pthread_cleanup *next;
381 void (*routine)(void *);
385 struct pthread_attr {
393 void (*cleanup_attr)(void *);
394 void *stackaddr_attr;
395 size_t stacksize_attr;
399 * Thread creation state attributes.
401 #define PTHREAD_CREATE_RUNNING 0
402 #define PTHREAD_CREATE_SUSPENDED 1
405 * Miscellaneous definitions.
407 #define PTHREAD_STACK_DEFAULT 65536
409 * Size of red zone at the end of each stack. In actuality, this "red zone" is
410 * merely an unmapped region, except in the case of the initial stack. Since
411 * mmap() makes it possible to specify the maximum growth of a MAP_STACK region,
412 * an unmapped gap between thread stacks achieves the same effect as explicitly
415 #define PTHREAD_STACK_GUARD PAGE_SIZE
418 * Maximum size of initial thread's stack. This perhaps deserves to be larger
419 * than the stacks of other threads, since many applications are likely to run
420 * almost entirely on this stack.
422 #define PTHREAD_STACK_INITIAL 0x100000
424 /* Size of the scheduler stack: */
425 #define SCHED_STACK_SIZE PAGE_SIZE
428 * Define the different priority ranges. All applications have thread
429 * priorities constrained within 0-31. The threads library raises the
430 * priority when delivering signals in order to ensure that signal
431 * delivery happens (from the POSIX spec) "as soon as possible".
432 * In the future, the threads library will also be able to map specific
433 * threads into real-time (cooperating) processes or kernel threads.
434 * The RT and SIGNAL priorities will be used internally and added to
435 * thread base priorities so that the scheduling queue can handle both
436 * normal and RT priority threads with and without signal handling.
438 * The approach taken is that, within each class, signal delivery
439 * always has priority over thread execution.
441 #define PTHREAD_DEFAULT_PRIORITY 15
442 #define PTHREAD_MIN_PRIORITY 0
443 #define PTHREAD_MAX_PRIORITY 31 /* 0x1F */
444 #define PTHREAD_SIGNAL_PRIORITY 32 /* 0x20 */
445 #define PTHREAD_RT_PRIORITY 64 /* 0x40 */
446 #define PTHREAD_FIRST_PRIORITY PTHREAD_MIN_PRIORITY
447 #define PTHREAD_LAST_PRIORITY \
448 (PTHREAD_MAX_PRIORITY + PTHREAD_SIGNAL_PRIORITY + PTHREAD_RT_PRIORITY)
449 #define PTHREAD_BASE_PRIORITY(prio) ((prio) & PTHREAD_MAX_PRIORITY)
452 * Clock resolution in microseconds.
454 #define CLOCK_RES_USEC 10000
455 #define CLOCK_RES_USEC_MIN 1000
458 * Time slice period in microseconds.
460 #define TIMESLICE_USEC 20000
463 * Define a thread-safe macro to get the current time of day
464 * which is updated at regular intervals by the scheduling signal
467 #define GET_CURRENT_TOD(tv) \
469 tv.tv_sec = _sched_tod.tv_sec; \
470 tv.tv_usec = _sched_tod.tv_usec; \
471 } while (tv.tv_sec != _sched_tod.tv_sec)
476 volatile int allocated;
478 void (*destructor)(void *);
481 struct pthread_rwlockattr {
485 struct pthread_rwlock {
486 pthread_mutex_t lock; /* monitor lock */
487 int state; /* 0 = idle >0 = # of readers -1 = writer */
488 pthread_cond_t read_signal;
489 pthread_cond_t write_signal;
521 * File descriptor locking definitions.
525 #define FD_RDWR (FD_READ | FD_WRITE)
528 * File descriptor table structure.
530 struct fd_table_entry {
532 * Lock for accesses to this file descriptor table
533 * entry. This is passed to _spinlock() to provide atomic
534 * access to this structure. It does *not* represent the
535 * state of the lock on the file descriptor.
538 TAILQ_HEAD(, pthread) r_queue; /* Read queue. */
539 TAILQ_HEAD(, pthread) w_queue; /* Write queue. */
540 struct pthread *r_owner; /* Ptr to thread owning read lock. */
541 struct pthread *w_owner; /* Ptr to thread owning write lock. */
542 char *r_fname; /* Ptr to read lock source file name */
543 int r_lineno; /* Read lock source line number. */
544 char *w_fname; /* Ptr to write lock source file name */
545 int w_lineno; /* Write lock source line number. */
546 int r_lockcount; /* Count for FILE read locks. */
547 int w_lockcount; /* Count for FILE write locks. */
548 int flags; /* Flags used in open. */
551 struct pthread_poll_data {
556 union pthread_wait_data {
557 pthread_mutex_t mutex;
559 const sigset_t *sigwait; /* Waiting on a signal in sigwait */
561 short fd; /* Used when thread waiting on fd */
562 short branch; /* Line number, for debugging. */
563 char *fname; /* Source file name for debugging.*/
566 struct pthread_poll_data *poll_data;
567 spinlock_t *spinlock;
568 struct pthread *thread;
572 * Define a continuation routine that can be used to perform a
573 * transfer of control:
575 typedef void (*thread_continuation_t) (void *);
577 struct pthread_signal_frame;
579 struct pthread_state_data {
580 struct pthread_signal_frame *psd_curframe;
581 sigset_t psd_sigmask;
582 struct timespec psd_wakeup_time;
583 union pthread_wait_data psd_wait_data;
584 enum pthread_state psd_state;
588 int psd_sigmask_seqno;
590 int psd_sig_defer_count;
591 /* XXX - What about thread->timeout and/or thread->error? */
595 struct pthread *thread;
601 * The frame that is added to the top of a threads stack when setting up
602 * up the thread to run a signal handler.
604 struct pthread_signal_frame {
606 * This stores the threads state before the signal.
608 struct pthread_state_data saved_state;
611 * Threads return context; we use only jmp_buf's for now.
617 int signo; /* signal, arg 1 to sighandler */
618 int sig_has_args; /* use signal args if true */
628 * Magic value to help recognize a valid thread structure
629 * from an invalid one:
631 #define PTHREAD_MAGIC ((u_int32_t) 0xd09ba115)
634 u_int64_t uniqueid; /* for gdb */
638 * Lock for accesses to this thread structure.
642 /* Queue entry for list of all threads: */
643 TAILQ_ENTRY(pthread) tle;
645 /* Queue entry for list of dead threads: */
646 TAILQ_ENTRY(pthread) dle;
649 * Thread start routine, argument, stack pointer and thread
652 void *(*start_routine)(void *);
655 struct pthread_attr attr;
658 * Threads return context; we use only jmp_buf's for now.
666 * Used for tracking delivery of signal handlers.
668 struct pthread_signal_frame *curframe;
671 * Cancelability flags - the lower 2 bits are used by cancel
672 * definitions in pthread.h
674 #define PTHREAD_AT_CANCEL_POINT 0x0004
675 #define PTHREAD_CANCELLING 0x0008
676 #define PTHREAD_CANCEL_NEEDED 0x0010
679 thread_continuation_t continuation;
682 * Current signal mask and pending signals.
690 enum pthread_state state;
692 /* Scheduling clock when this thread was last made active. */
695 /* Scheduling clock when this thread was last made inactive. */
699 * Number of microseconds accumulated by this thread when
700 * time slicing is active.
705 * Time to wake up thread. This is used for sleeping threads and
706 * for any operation which may time out (such as select).
708 struct timespec wakeup_time;
710 /* TRUE if operation has timed out. */
714 * The joiner is the thread that is joining to this thread. The
715 * join status keeps track of a join operation to another thread.
717 struct pthread *joiner;
718 struct join_status join_status;
721 * The current thread can belong to only one scheduling queue at
722 * a time (ready or waiting queue). It can also belong to:
724 * o A queue of threads waiting for a mutex
725 * o A queue of threads waiting for a condition variable
726 * o A queue of threads waiting for a file descriptor lock
727 * o A queue of threads needing work done by the kernel thread
728 * (waiting for a spinlock or file I/O)
730 * A thread can also be joining a thread (the joiner field above).
732 * It must not be possible for a thread to belong to any of the
733 * above queues while it is handling a signal. Signal handlers
734 * may longjmp back to previous stack frames circumventing normal
735 * control flow. This could corrupt queue integrity if the thread
736 * retains membership in the queue. Therefore, if a thread is a
737 * member of one of these queues when a signal handler is invoked,
738 * it must remove itself from the queue before calling the signal
739 * handler and reinsert itself after normal return of the handler.
741 * Use pqe for the scheduling queue link (both ready and waiting),
742 * sqe for synchronization (mutex and condition variable) queue
743 * links, and qe for all other links.
745 TAILQ_ENTRY(pthread) pqe; /* priority queue link */
746 TAILQ_ENTRY(pthread) sqe; /* synchronization queue link */
747 TAILQ_ENTRY(pthread) qe; /* all other queues link */
750 union pthread_wait_data data;
753 * Allocated for converting select into poll.
755 struct pthread_poll_data poll_data;
758 * Set to TRUE if a blocking operation was
759 * interrupted by a signal:
763 /* Signal number when in state PS_SIGWAIT: */
767 * Set to non-zero when this thread has deferred signals.
768 * We allow for recursive deferral.
773 * Set to TRUE if this thread should yield after undeferring
776 int yield_on_sig_undefer;
778 /* Miscellaneous flags; only set with signals deferred. */
780 #define PTHREAD_FLAGS_PRIVATE 0x0001
781 #define PTHREAD_EXITING 0x0002
782 #define PTHREAD_FLAGS_IN_WAITQ 0x0004 /* in waiting queue using pqe link */
783 #define PTHREAD_FLAGS_IN_PRIOQ 0x0008 /* in priority queue using pqe link */
784 #define PTHREAD_FLAGS_IN_WORKQ 0x0010 /* in work queue using qe link */
785 #define PTHREAD_FLAGS_IN_FILEQ 0x0020 /* in file lock queue using qe link */
786 #define PTHREAD_FLAGS_IN_FDQ 0x0040 /* in fd lock queue using qe link */
787 #define PTHREAD_FLAGS_IN_CONDQ 0x0080 /* in condition queue using sqe link*/
788 #define PTHREAD_FLAGS_IN_MUTEXQ 0x0100 /* in mutex queue using sqe link */
789 #define PTHREAD_FLAGS_SUSPENDED 0x0200 /* thread is suspended */
790 #define PTHREAD_FLAGS_TRACE 0x0400 /* for debugging purposes */
791 #define PTHREAD_FLAGS_IN_SYNCQ \
792 (PTHREAD_FLAGS_IN_CONDQ | PTHREAD_FLAGS_IN_MUTEXQ)
795 * Base priority is the user setable and retrievable priority
796 * of the thread. It is only affected by explicit calls to
797 * set thread priority and upon thread creation via a thread
798 * attribute or default priority.
803 * Inherited priority is the priority a thread inherits by
804 * taking a priority inheritence or protection mutex. It
805 * is not affected by base priority changes. Inherited
806 * priority defaults to and remains 0 until a mutex is taken
807 * that is being waited on by any other thread whose priority
810 char inherited_priority;
813 * Active priority is always the maximum of the threads base
814 * priority and inherited priority. When there is a change
815 * in either the base or inherited priority, the active
816 * priority must be recalculated.
818 char active_priority;
820 /* Number of priority ceiling or protection mutexes owned. */
821 int priority_mutex_count;
824 * Queue of currently owned mutexes.
826 TAILQ_HEAD(, pthread_mutex) mutexq;
829 const void **specific_data;
830 int specific_data_count;
832 /* Cleanup handlers Link List */
833 struct pthread_cleanup *cleanup;
834 char *fname; /* Ptr to source file name */
835 int lineno; /* Source line number. */
838 /* Spare thread stack. */
840 SLIST_ENTRY(stack) qe; /* Queue entry for this stack. */
844 * Global variables for the uthread kernel.
847 SCLASS void *_usrstack
848 #ifdef GLOBAL_PTHREAD_PRIVATE
854 /* Kernel thread structure used when there are no running threads: */
855 SCLASS struct pthread _thread_kern_thread;
857 /* Ptr to the thread structure for the running thread: */
858 SCLASS struct pthread * volatile _thread_run
859 #ifdef GLOBAL_PTHREAD_PRIVATE
860 = &_thread_kern_thread;
865 /* Ptr to the thread structure for the last user thread to run: */
866 SCLASS struct pthread * volatile _last_user_thread
867 #ifdef GLOBAL_PTHREAD_PRIVATE
868 = &_thread_kern_thread;
873 /* List of all threads: */
874 SCLASS TAILQ_HEAD(, pthread) _thread_list
875 #ifdef GLOBAL_PTHREAD_PRIVATE
876 = TAILQ_HEAD_INITIALIZER(_thread_list);
882 * Array of kernel pipe file descriptors that are used to ensure that
883 * no signals are missed in calls to _select.
885 SCLASS int _thread_kern_pipe[2]
886 #ifdef GLOBAL_PTHREAD_PRIVATE
894 SCLASS int volatile _queue_signals
895 #ifdef GLOBAL_PTHREAD_PRIVATE
900 SCLASS int _thread_kern_in_sched
901 #ifdef GLOBAL_PTHREAD_PRIVATE
907 SCLASS int _sig_in_handler
908 #ifdef GLOBAL_PTHREAD_PRIVATE
914 /* Time of day at last scheduling timer signal: */
915 SCLASS struct timeval volatile _sched_tod
916 #ifdef GLOBAL_PTHREAD_PRIVATE
923 * Current scheduling timer ticks; used as resource usage.
925 SCLASS unsigned int volatile _sched_ticks
926 #ifdef GLOBAL_PTHREAD_PRIVATE
933 SCLASS TAILQ_HEAD(, pthread) _dead_list
934 #ifdef GLOBAL_PTHREAD_PRIVATE
935 = TAILQ_HEAD_INITIALIZER(_dead_list);
940 /* Initial thread: */
941 SCLASS struct pthread *_thread_initial
942 #ifdef GLOBAL_PTHREAD_PRIVATE
948 /* Default thread attributes: */
949 SCLASS struct pthread_attr pthread_attr_default
950 #ifdef GLOBAL_PTHREAD_PRIVATE
951 = { SCHED_RR, 0, TIMESLICE_USEC, PTHREAD_DEFAULT_PRIORITY, PTHREAD_CREATE_RUNNING,
952 PTHREAD_CREATE_JOINABLE, NULL, NULL, NULL, PTHREAD_STACK_DEFAULT };
957 /* Default mutex attributes: */
958 SCLASS struct pthread_mutex_attr pthread_mutexattr_default
959 #ifdef GLOBAL_PTHREAD_PRIVATE
960 = { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, 0 };
965 /* Default condition variable attributes: */
966 SCLASS struct pthread_cond_attr pthread_condattr_default
967 #ifdef GLOBAL_PTHREAD_PRIVATE
968 = { COND_TYPE_FAST, 0 };
974 * Standard I/O file descriptors need special flag treatment since
975 * setting one to non-blocking does all on *BSD. Sigh. This array
976 * is used to store the initial flag settings.
978 SCLASS int _pthread_stdio_flags[3];
980 /* File table information: */
981 SCLASS struct fd_table_entry **_thread_fd_table
982 #ifdef GLOBAL_PTHREAD_PRIVATE
988 /* Table for polling file descriptors: */
989 SCLASS struct pollfd *_thread_pfd_table
990 #ifdef GLOBAL_PTHREAD_PRIVATE
996 SCLASS const int dtablecount
997 #ifdef GLOBAL_PTHREAD_PRIVATE
998 = 4096/sizeof(struct fd_table_entry);
1002 SCLASS int _thread_dtablesize /* Descriptor table size. */
1003 #ifdef GLOBAL_PTHREAD_PRIVATE
1009 SCLASS int _clock_res_usec /* Clock resolution in usec. */
1010 #ifdef GLOBAL_PTHREAD_PRIVATE
1016 /* Garbage collector mutex and condition variable. */
1017 SCLASS pthread_mutex_t _gc_mutex
1018 #ifdef GLOBAL_PTHREAD_PRIVATE
1022 SCLASS pthread_cond_t _gc_cond
1023 #ifdef GLOBAL_PTHREAD_PRIVATE
1029 * Array of signal actions for this process.
1031 SCLASS struct sigaction _thread_sigact[NSIG];
1034 * Array of counts of dummy handlers for SIG_DFL signals. This is used to
1035 * assure that there is always a dummy signal handler installed while there is a
1036 * thread sigwait()ing on the corresponding signal.
1038 SCLASS int _thread_dfl_count[NSIG];
1041 * Pending signals and mask for this process:
1043 SCLASS sigset_t _process_sigpending;
1044 SCLASS sigset_t _process_sigmask
1045 #ifdef GLOBAL_PTHREAD_PRIVATE
1051 * Scheduling queues:
1053 SCLASS pq_queue_t _readyq;
1054 SCLASS TAILQ_HEAD(, pthread) _waitingq;
1059 SCLASS TAILQ_HEAD(, pthread) _workq;
1061 /* Tracks the number of threads blocked while waiting for a spinlock. */
1062 SCLASS volatile int _spinblock_count
1063 #ifdef GLOBAL_PTHREAD_PRIVATE
1068 /* Used to maintain pending and active signals: */
1070 int pending; /* Is this a pending signal? */
1072 * A handler is currently active for
1073 * this signal; ignore subsequent
1074 * signals until the handler is done.
1076 int signo; /* arg 1 to signal handler */
1077 siginfo_t siginfo; /* arg 2 to signal handler */
1078 ucontext_t uc; /* arg 3 to signal handler */
1081 SCLASS struct sigstatus _thread_sigq[NSIG];
1083 /* Indicates that the signal queue needs to be checked. */
1084 SCLASS volatile int _sigq_check_reqd
1085 #ifdef GLOBAL_PTHREAD_PRIVATE
1090 /* Thread switch hook. */
1091 SCLASS pthread_switch_routine_t _sched_switch_hook
1092 #ifdef GLOBAL_PTHREAD_PRIVATE
1098 * Spare stack queue. Stacks of default size are cached in order to reduce
1099 * thread creation time. Spare stacks are used in LIFO order to increase cache
1102 SCLASS SLIST_HEAD(, stack) _stackq;
1105 * Base address of next unallocated default-size {stack, red zone}. Stacks are
1106 * allocated contiguously, starting below the bottom of the main stack. When a
1107 * new stack is created, a red zone is created (actually, the red zone is simply
1108 * left unmapped) below the bottom of the stack, such that the stack will not be
1109 * able to grow all the way to the top of the next stack. This isn't
1110 * fool-proof. It is possible for a stack to grow by a large amount, such that
1111 * it grows into the next stack, and as long as the memory within the red zone
1112 * is never accessed, nothing will prevent one thread stack from trouncing all
1115 SCLASS void * _next_stack
1116 #ifdef GLOBAL_PTHREAD_PRIVATE
1117 /* main stack top - main stack size - stack size - (red zone + main stack red zone) */
1118 = (void *) USRSTACK - PTHREAD_STACK_INITIAL - PTHREAD_STACK_DEFAULT - (2 * PTHREAD_STACK_GUARD)
1123 * Declare the kernel scheduler jump buffer and stack:
1125 SCLASS jmp_buf _thread_kern_sched_jb;
1127 SCLASS void * _thread_kern_sched_stack
1128 #ifdef GLOBAL_PTHREAD_PRIVATE
1134 /* Used for _PTHREADS_INVARIANTS checking. */
1135 SCLASS int _thread_kern_new_state
1136 #ifdef GLOBAL_PTHREAD_PRIVATE
1141 /* Undefine the storage class specifier: */
1145 #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock_debug(_fd, _type, \
1146 _ts, __FILE__, __LINE__)
1147 #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock_debug(_fd, _type, \
1150 #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock(_fd, _type, _ts)
1151 #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock(_fd, _type)
1155 * Function prototype definitions.
1158 char *__ttyname_basic(int);
1159 char *__ttyname_r_basic(int, char *, size_t);
1160 char *ttyname_r(int, char *, size_t);
1161 void _cond_wait_backout(pthread_t);
1162 void _fd_lock_backout(pthread_t);
1163 int _find_thread(pthread_t);
1164 struct pthread *_get_curthread(void);
1165 void _set_curthread(struct pthread *);
1166 void _flockfile_backout(struct pthread *);
1167 void _funlock_owned(struct pthread *);
1168 int _thread_create(pthread_t *,const pthread_attr_t *,void *(*start_routine)(void *),void *,pthread_t);
1169 int _mutex_cv_lock(pthread_mutex_t *);
1170 int _mutex_cv_unlock(pthread_mutex_t *);
1171 void _mutex_lock_backout(pthread_t);
1172 void _mutex_notify_priochange(pthread_t);
1173 int _mutex_reinit(pthread_mutex_t *);
1174 void _mutex_unlock_private(pthread_t);
1175 int _cond_reinit(pthread_cond_t *);
1176 int _pq_alloc(struct pq_queue *, int, int);
1177 int _pq_init(struct pq_queue *);
1178 void _pq_remove(struct pq_queue *pq, struct pthread *);
1179 void _pq_insert_head(struct pq_queue *pq, struct pthread *);
1180 void _pq_insert_tail(struct pq_queue *pq, struct pthread *);
1181 struct pthread *_pq_first(struct pq_queue *pq);
1182 void *_pthread_getspecific(pthread_key_t);
1183 int _pthread_key_create(pthread_key_t *, void (*) (void *));
1184 int _pthread_key_delete(pthread_key_t);
1185 int _pthread_mutex_destroy(pthread_mutex_t *);
1186 int _pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
1187 int _pthread_mutex_lock(pthread_mutex_t *);
1188 int _pthread_mutex_trylock(pthread_mutex_t *);
1189 int _pthread_mutex_unlock(pthread_mutex_t *);
1190 int _pthread_once(pthread_once_t *, void (*) (void));
1191 int _pthread_setspecific(pthread_key_t, const void *);
1192 int _pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *);
1193 int _pthread_cond_destroy(pthread_cond_t *);
1194 int _pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
1195 int _pthread_cond_timedwait(pthread_cond_t *, pthread_mutex_t *,
1196 const struct timespec *);
1197 int _pthread_cond_signal(pthread_cond_t *);
1198 int _pthread_cond_broadcast(pthread_cond_t *);
1199 void _waitq_insert(pthread_t pthread);
1200 void _waitq_remove(pthread_t pthread);
1201 #if defined(_PTHREADS_INVARIANTS)
1202 void _waitq_setactive(void);
1203 void _waitq_clearactive(void);
1205 void _thread_exit(char *, int, char *);
1206 void _thread_exit_cleanup(void);
1207 int _thread_fd_getflags(int);
1208 int _thread_fd_lock(int, int, struct timespec *);
1209 int _thread_fd_lock_debug(int, int, struct timespec *,char *fname,int lineno);
1210 void _thread_fd_setflags(int, int);
1211 int _thread_fd_table_init(int fd);
1212 void _thread_fd_unlock(int, int);
1213 void _thread_fd_unlock_debug(int, int, char *, int);
1214 void _thread_fd_unlock_owned(pthread_t);
1215 void *_thread_cleanup(pthread_t);
1216 void _thread_cleanupspecific(void);
1217 void _thread_dump_info(void);
1218 void _thread_init(void);
1219 void _thread_kern_sched(ucontext_t *);
1220 void _thread_kern_scheduler(void);
1221 void _thread_kern_sched_frame(struct pthread_signal_frame *psf);
1222 void _thread_kern_sched_sig(void);
1223 void _thread_kern_sched_state(enum pthread_state, char *fname, int lineno);
1224 void _thread_kern_sched_state_unlock(enum pthread_state state,
1225 spinlock_t *lock, char *fname, int lineno);
1226 void _thread_kern_set_timeout(const struct timespec *);
1227 void _thread_kern_sig_defer(void);
1228 void _thread_kern_sig_undefer(void);
1229 void _thread_mksigpipe(void);
1230 void _thread_sig_handler(int, siginfo_t *, ucontext_t *);
1231 void _thread_sig_check_pending(struct pthread *pthread);
1232 void _thread_sig_handle_pending(void);
1233 void _thread_sig_send(struct pthread *pthread, int sig);
1234 void _thread_sig_wrapper(void);
1235 void _thread_sigframe_restore(struct pthread *thread,
1236 struct pthread_signal_frame *psf);
1237 void _thread_start(void);
1238 pthread_addr_t _thread_gc(pthread_addr_t);
1239 void _thread_enter_cancellation_point(void);
1240 void _thread_leave_cancellation_point(void);
1241 void _thread_cancellation_point(void);
1243 /* #include <aio.h> */
1245 int __sys_aio_suspend(const struct aiocb * const[], int, const struct timespec *);
1248 /* #include <sys/event.h> */
1249 #ifdef _SYS_EVENT_H_
1250 int __sys_kevent(int, const struct kevent *, int, struct kevent *,
1251 int, const struct timespec *);
1254 /* #include <sys/ioctl.h> */
1255 #ifdef _SYS_IOCTL_H_
1256 int __sys_ioctl(int, unsigned long, ...);
1259 /* #include <sys/mman.h> */
1261 int __sys_msync(void *, size_t, int);
1264 /* #include <sys/mount.h> */
1265 #ifdef _SYS_MOUNT_H_
1266 int __sys_fstatfs(int, struct statfs *);
1269 /* #include <sys/socket.h> */
1270 #ifdef _SYS_SOCKET_H_
1271 int __sys_accept(int, struct sockaddr *, socklen_t *);
1272 int __sys_extaccept(int, int, struct sockaddr *, socklen_t *);
1273 int __sys_bind(int, const struct sockaddr *, socklen_t);
1274 int __sys_connect(int, const struct sockaddr *, socklen_t);
1275 int __sys_extconnect(int, int, const struct sockaddr *, socklen_t);
1276 int __sys_getpeername(int, struct sockaddr *, socklen_t *);
1277 int __sys_getsockname(int, struct sockaddr *, socklen_t *);
1278 int __sys_getsockopt(int, int, int, void *, socklen_t *);
1279 int __sys_listen(int, int);
1280 ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
1281 ssize_t __sys_recvmsg(int, struct msghdr *, int);
1282 int __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *, off_t *, int);
1283 ssize_t __sys_sendmsg(int, const struct msghdr *, int);
1284 ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
1285 int __sys_setsockopt(int, int, int, const void *, socklen_t);
1286 int __sys_shutdown(int, int);
1287 int __sys_socket(int, int, int);
1288 int __sys_socketpair(int, int, int, int *);
1291 /* #include <sys/stat.h> */
1293 int __sys_fchflags(int, u_long);
1294 int __sys_fchmod(int, mode_t);
1295 int __sys_fstat(int, struct stat *);
1298 /* #include <sys/uio.h> */
1300 ssize_t __sys_readv(int, const struct iovec *, int);
1301 ssize_t __sys_writev(int, const struct iovec *, int);
1302 ssize_t __sys_extpreadv(int, const struct iovec *, int, int, off_t);
1303 ssize_t __sys_extpwritev(int, const struct iovec *, int, int, off_t);
1306 /* #include <sys/wait.h> */
1308 pid_t __sys_wait4(pid_t, int *, int, struct rusage *);
1311 /* #include <dirent.h> */
1313 int __sys_getdirentries(int, char *, int, long *);
1316 /* #include <fcntl.h> */
1317 #ifdef _SYS_FCNTL_H_
1318 int __sys_fcntl(int, int, ...);
1319 int __sys_flock(int, int);
1320 int __sys_open(const char *, int, ...);
1323 /* #include <poll.h> */
1325 int __sys_poll(struct pollfd *, unsigned, int);
1328 /* #include <signal.h> */
1330 int __sys_sigaction(int, const struct sigaction *, struct sigaction *);
1331 int __sys_sigprocmask(int, const sigset_t *, sigset_t *);
1332 int __sys_sigreturn(ucontext_t *);
1335 /* #include <unistd.h> */
1337 void __sys__exit(int);
1338 int __sys_close(int);
1339 int __sys_closefrom(int);
1341 int __sys_dup2(int, int);
1342 int __sys_execve(const char *, char * const *, char * const *);
1343 int __sys_fchown(int, uid_t, gid_t);
1344 pid_t __sys_fork(void);
1345 long __sys_fpathconf(int, int);
1346 int __sys_fsync(int);
1347 int __sys_pipe(int *);
1348 ssize_t __sys_read(int, void *, size_t);
1349 ssize_t __sys_extpread(int, void *, size_t, int, off_t);
1350 ssize_t __sys_write(int, const void *, size_t);
1351 ssize_t __sys_extpwrite(int, const void *, size_t, int, off_t);
1354 /* #include <setjmp.h> */
1356 extern void __siglongjmp(sigjmp_buf, int) __dead2;
1357 extern void __longjmp(jmp_buf, int) __dead2;
1358 extern void ___longjmp(jmp_buf, int) __dead2;
1362 #endif /* !_PTHREAD_PRIVATE_H */