Merge from vendor branch NTPD:
[dragonfly.git] / lib / libc_r / uthread / pthread_private.h
1 /*
2  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  * Private thread definitions for the uthread kernel.
33  *
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.5 2005/03/13 15:10:03 swildner Exp $
36  */
37
38 #ifndef _PTHREAD_PRIVATE_H
39 #define _PTHREAD_PRIVATE_H
40
41 /*
42  * Evaluate the storage class specifier.
43  */
44 #ifdef GLOBAL_PTHREAD_PRIVATE
45 #define SCLASS
46 #else
47 #define SCLASS extern
48 #endif
49
50 /*
51  * Include files.
52  */
53 #include <setjmp.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <sys/queue.h>
57 #include <sys/types.h>
58 #include <sys/time.h>
59 #include <sys/cdefs.h>
60 #include <sched.h>
61 #include <spinlock.h>
62 #include <pthread_np.h>
63
64 /*
65  * Define machine dependent macros to get and set the stack pointer
66  * from the supported contexts.  Also define a macro to set the return
67  * address in a jmp_buf context.
68  *
69  * XXX - These need to be moved into architecture dependent support files.
70  */
71 #if     defined(__i386__)
72 #define GET_STACK_JB(jb)        ((unsigned long)((jb)[0]._jb[2]))
73 #define GET_STACK_SJB(sjb)      ((unsigned long)((sjb)[0]._sjb[2]))
74 #define GET_STACK_UC(ucp)       ((unsigned long)((ucp)->uc_mcontext.mc_esp))
75 #define SET_STACK_JB(jb, stk)   (jb)[0]._jb[2] = (int)(stk)
76 #define SET_STACK_SJB(sjb, stk) (sjb)[0]._sjb[2] = (int)(stk)
77 #define SET_STACK_UC(ucp, stk)  (ucp)->uc_mcontext.mc_esp = (int)(stk)
78 #define FP_SAVE_UC(ucp)         do {                    \
79         char    *fdata;                                 \
80         fdata = (char *) (ucp)->uc_mcontext.mc_fpregs;  \
81         __asm__("fnsave %0": :"m"(*fdata));             \
82 } while (0)
83 #define FP_RESTORE_UC(ucp)      do {                    \
84         char    *fdata;                                 \
85         fdata = (char *) (ucp)->uc_mcontext.mc_fpregs;  \
86         __asm__("frstor %0": :"m"(*fdata));             \
87 } while (0)
88 #define SET_RETURN_ADDR_JB(jb, ra)      (jb)[0]._jb[0] = (int)(ra)
89 #else
90 #error "Don't recognize this architecture!"
91 #endif
92
93 /*
94  * Kernel fatal error handler macro.
95  */
96 #define PANIC(string)   _thread_exit(__FILE__,__LINE__,string)
97
98
99 /* Output debug messages like this: */
100 #define stdout_debug(args...)   do {            \
101         char buf[128];                          \
102         snprintf(buf, sizeof(buf), ##args);     \
103         __sys_write(1, buf, strlen(buf));       \
104 } while (0)
105 #define stderr_debug(args...)   do {            \
106         char buf[128];                          \
107         snprintf(buf, sizeof(buf), ##args);     \
108         __sys_write(2, buf, strlen(buf));       \
109 } while (0)
110
111
112
113 /*
114  * Priority queue manipulation macros (using pqe link):
115  */
116 #define PTHREAD_PRIOQ_INSERT_HEAD(thrd) _pq_insert_head(&_readyq,thrd)
117 #define PTHREAD_PRIOQ_INSERT_TAIL(thrd) _pq_insert_tail(&_readyq,thrd)
118 #define PTHREAD_PRIOQ_REMOVE(thrd)      _pq_remove(&_readyq,thrd)
119 #define PTHREAD_PRIOQ_FIRST()           _pq_first(&_readyq)
120
121 /*
122  * Waiting queue manipulation macros (using pqe link):
123  */
124 #define PTHREAD_WAITQ_REMOVE(thrd)      _waitq_remove(thrd)
125 #define PTHREAD_WAITQ_INSERT(thrd)      _waitq_insert(thrd)
126
127 #if defined(_PTHREADS_INVARIANTS)
128 #define PTHREAD_WAITQ_CLEARACTIVE()     _waitq_clearactive()
129 #define PTHREAD_WAITQ_SETACTIVE()       _waitq_setactive()
130 #else
131 #define PTHREAD_WAITQ_CLEARACTIVE()
132 #define PTHREAD_WAITQ_SETACTIVE()
133 #endif
134
135 /*
136  * Work queue manipulation macros (using qe link):
137  */
138 #define PTHREAD_WORKQ_INSERT(thrd) do {                                 \
139         TAILQ_INSERT_TAIL(&_workq,thrd,qe);                             \
140         (thrd)->flags |= PTHREAD_FLAGS_IN_WORKQ;                        \
141 } while (0)
142 #define PTHREAD_WORKQ_REMOVE(thrd) do {                                 \
143         TAILQ_REMOVE(&_workq,thrd,qe);                                  \
144         (thrd)->flags &= ~PTHREAD_FLAGS_IN_WORKQ;                       \
145 } while (0)
146
147
148 /*
149  * State change macro without scheduling queue change:
150  */
151 #define PTHREAD_SET_STATE(thrd, newstate) do {                          \
152         (thrd)->state = newstate;                                       \
153         (thrd)->fname = __FILE__;                                       \
154         (thrd)->lineno = __LINE__;                                      \
155 } while (0)
156
157 /*
158  * State change macro with scheduling queue change - This must be
159  * called with preemption deferred (see thread_kern_sched_[un]defer).
160  */
161 #if defined(_PTHREADS_INVARIANTS)
162 #include <assert.h>
163 #define PTHREAD_ASSERT(cond, msg) do {  \
164         if (!(cond))                    \
165                 PANIC(msg);             \
166 } while (0)
167 #define PTHREAD_ASSERT_NOT_IN_SYNCQ(thrd) \
168         PTHREAD_ASSERT((((thrd)->flags & PTHREAD_FLAGS_IN_SYNCQ) == 0), \
169             "Illegal call from signal handler");
170 #define PTHREAD_NEW_STATE(thrd, newstate) do {                          \
171         if (_thread_kern_new_state != 0)                                \
172                 PANIC("Recursive PTHREAD_NEW_STATE");                   \
173         _thread_kern_new_state = 1;                                     \
174         if ((thrd)->state != newstate) {                                \
175                 if ((thrd)->state == PS_RUNNING) {                      \
176                         PTHREAD_PRIOQ_REMOVE(thrd);                     \
177                         PTHREAD_SET_STATE(thrd, newstate);              \
178                         PTHREAD_WAITQ_INSERT(thrd);                     \
179                 } else if (newstate == PS_RUNNING) {                    \
180                         PTHREAD_WAITQ_REMOVE(thrd);                     \
181                         PTHREAD_SET_STATE(thrd, newstate);              \
182                         PTHREAD_PRIOQ_INSERT_TAIL(thrd);                \
183                 }                                                       \
184         }                                                               \
185         _thread_kern_new_state = 0;                                     \
186 } while (0)
187 #else
188 #define PTHREAD_ASSERT(cond, msg)
189 #define PTHREAD_ASSERT_NOT_IN_SYNCQ(thrd)
190 #define PTHREAD_NEW_STATE(thrd, newstate) do {                          \
191         if ((thrd)->state != newstate) {                                \
192                 if ((thrd)->state == PS_RUNNING) {                      \
193                         PTHREAD_PRIOQ_REMOVE(thrd);                     \
194                         PTHREAD_WAITQ_INSERT(thrd);                     \
195                 } else if (newstate == PS_RUNNING) {                    \
196                         PTHREAD_WAITQ_REMOVE(thrd);                     \
197                         PTHREAD_PRIOQ_INSERT_TAIL(thrd);                \
198                 }                                                       \
199         }                                                               \
200         PTHREAD_SET_STATE(thrd, newstate);                              \
201 } while (0)
202 #endif
203
204 /*
205  * Define the signals to be used for scheduling.
206  */
207 #if defined(_PTHREADS_COMPAT_SCHED)
208 #define _ITIMER_SCHED_TIMER     ITIMER_VIRTUAL
209 #define _SCHED_SIGNAL           SIGVTALRM
210 #else
211 #define _ITIMER_SCHED_TIMER     ITIMER_PROF
212 #define _SCHED_SIGNAL           SIGPROF
213 #endif
214
215 /*
216  * Priority queues.
217  *
218  * XXX It'd be nice if these were contained in uthread_priority_queue.[ch].
219  */
220 typedef struct pq_list {
221         TAILQ_HEAD(, pthread)   pl_head; /* list of threads at this priority */
222         TAILQ_ENTRY(pq_list)    pl_link; /* link for queue of priority lists */
223         int                     pl_prio; /* the priority of this list */
224         int                     pl_queued; /* is this in the priority queue */
225 } pq_list_t;
226
227 typedef struct pq_queue {
228         TAILQ_HEAD(, pq_list)    pq_queue; /* queue of priority lists */
229         pq_list_t               *pq_lists; /* array of all priority lists */
230         int                      pq_size;  /* number of priority lists */
231 } pq_queue_t;
232
233
234 /*
235  * TailQ initialization values.
236  */
237 #define TAILQ_INITIALIZER       { NULL, NULL }
238
239 /* 
240  * Mutex definitions.
241  */
242 union pthread_mutex_data {
243         void    *m_ptr;
244         int     m_count;
245 };
246
247 struct pthread_mutex {
248         enum pthread_mutextype          m_type;
249         int                             m_protocol;
250         TAILQ_HEAD(mutex_head, pthread) m_queue;
251         struct pthread                  *m_owner;
252         union pthread_mutex_data        m_data;
253         long                            m_flags;
254         int                             m_refcount;
255
256         /*
257          * Used for priority inheritence and protection.
258          *
259          *   m_prio       - For priority inheritence, the highest active
260          *                  priority (threads locking the mutex inherit
261          *                  this priority).  For priority protection, the
262          *                  ceiling priority of this mutex.
263          *   m_saved_prio - mutex owners inherited priority before
264          *                  taking the mutex, restored when the owner
265          *                  unlocks the mutex.
266          */
267         int                             m_prio;
268         int                             m_saved_prio;
269
270         /*
271          * Link for list of all mutexes a thread currently owns.
272          */
273         TAILQ_ENTRY(pthread_mutex)      m_qe;
274
275         /*
276          * Lock for accesses to this structure.
277          */
278         spinlock_t                      lock;
279 };
280
281 /*
282  * Flags for mutexes. 
283  */
284 #define MUTEX_FLAGS_PRIVATE     0x01
285 #define MUTEX_FLAGS_INITED      0x02
286 #define MUTEX_FLAGS_BUSY        0x04
287
288 /*
289  * Static mutex initialization values. 
290  */
291 #define PTHREAD_MUTEX_STATIC_INITIALIZER   \
292         { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, TAILQ_INITIALIZER, \
293         NULL, { NULL }, MUTEX_FLAGS_PRIVATE, 0, 0, 0, TAILQ_INITIALIZER, \
294         _SPINLOCK_INITIALIZER }
295
296 struct pthread_mutex_attr {
297         enum pthread_mutextype  m_type;
298         int                     m_protocol;
299         int                     m_ceiling;
300         long                    m_flags;
301 };
302
303 /* 
304  * Condition variable definitions.
305  */
306 enum pthread_cond_type {
307         COND_TYPE_FAST,
308         COND_TYPE_MAX
309 };
310
311 struct pthread_cond {
312         enum pthread_cond_type          c_type;
313         TAILQ_HEAD(cond_head, pthread)  c_queue;
314         pthread_mutex_t                 c_mutex;
315         void                            *c_data;
316         long                            c_flags;
317         int                             c_seqno;
318
319         /*
320          * Lock for accesses to this structure.
321          */
322         spinlock_t                      lock;
323 };
324
325 struct pthread_cond_attr {
326         enum pthread_cond_type  c_type;
327         long                    c_flags;
328 };
329
330 /*
331  * Flags for condition variables.
332  */
333 #define COND_FLAGS_PRIVATE      0x01
334 #define COND_FLAGS_INITED       0x02
335 #define COND_FLAGS_BUSY         0x04
336
337 /*
338  * Static cond initialization values. 
339  */
340 #define PTHREAD_COND_STATIC_INITIALIZER    \
341         { COND_TYPE_FAST, TAILQ_INITIALIZER, NULL, NULL, \
342         0, 0, _SPINLOCK_INITIALIZER }
343
344 /*
345  * Semaphore definitions.
346  */
347 struct sem {
348 #define SEM_MAGIC       ((u_int32_t) 0x09fa4012)
349         u_int32_t       magic;
350         pthread_mutex_t lock;
351         pthread_cond_t  gtzero;
352         u_int32_t       count;
353         u_int32_t       nwaiters;
354 };
355
356 /*
357  * Cleanup definitions.
358  */
359 struct pthread_cleanup {
360         struct pthread_cleanup  *next;
361         void                    (*routine)(void *);
362         void                    *routine_arg;
363 };
364
365 struct pthread_attr {
366         int     sched_policy;
367         int     sched_inherit;
368         int     sched_interval;
369         int     prio;
370         int     suspend;
371         int     flags;
372         void    *arg_attr;
373         void    (*cleanup_attr)(void *);
374         void    *stackaddr_attr;
375         size_t  stacksize_attr;
376 };
377
378 /*
379  * Thread creation state attributes.
380  */
381 #define PTHREAD_CREATE_RUNNING                  0
382 #define PTHREAD_CREATE_SUSPENDED                1
383
384 /*
385  * Miscellaneous definitions.
386  */
387 #define PTHREAD_STACK_DEFAULT                   65536
388 /*
389  * Size of red zone at the end of each stack.  In actuality, this "red zone" is
390  * merely an unmapped region, except in the case of the initial stack.  Since
391  * mmap() makes it possible to specify the maximum growth of a MAP_STACK region,
392  * an unmapped gap between thread stacks achieves the same effect as explicitly
393  * mapped red zones.
394  */
395 #define PTHREAD_STACK_GUARD                     PAGE_SIZE
396
397 /*
398  * Maximum size of initial thread's stack.  This perhaps deserves to be larger
399  * than the stacks of other threads, since many applications are likely to run
400  * almost entirely on this stack.
401  */
402 #define PTHREAD_STACK_INITIAL                   0x100000
403
404 /* Size of the scheduler stack: */
405 #define SCHED_STACK_SIZE                        PAGE_SIZE
406
407 /*
408  * Define the different priority ranges.  All applications have thread
409  * priorities constrained within 0-31.  The threads library raises the
410  * priority when delivering signals in order to ensure that signal
411  * delivery happens (from the POSIX spec) "as soon as possible".
412  * In the future, the threads library will also be able to map specific
413  * threads into real-time (cooperating) processes or kernel threads.
414  * The RT and SIGNAL priorities will be used internally and added to
415  * thread base priorities so that the scheduling queue can handle both
416  * normal and RT priority threads with and without signal handling.
417  *
418  * The approach taken is that, within each class, signal delivery
419  * always has priority over thread execution.
420  */
421 #define PTHREAD_DEFAULT_PRIORITY                15
422 #define PTHREAD_MIN_PRIORITY                    0
423 #define PTHREAD_MAX_PRIORITY                    31      /* 0x1F */
424 #define PTHREAD_SIGNAL_PRIORITY                 32      /* 0x20 */
425 #define PTHREAD_RT_PRIORITY                     64      /* 0x40 */
426 #define PTHREAD_FIRST_PRIORITY                  PTHREAD_MIN_PRIORITY
427 #define PTHREAD_LAST_PRIORITY   \
428         (PTHREAD_MAX_PRIORITY + PTHREAD_SIGNAL_PRIORITY + PTHREAD_RT_PRIORITY)
429 #define PTHREAD_BASE_PRIORITY(prio)     ((prio) & PTHREAD_MAX_PRIORITY)
430
431 /*
432  * Clock resolution in microseconds.
433  */
434 #define CLOCK_RES_USEC                          10000
435 #define CLOCK_RES_USEC_MIN                      1000
436
437 /*
438  * Time slice period in microseconds.
439  */
440 #define TIMESLICE_USEC                          20000
441
442 /*
443  * Define a thread-safe macro to get the current time of day
444  * which is updated at regular intervals by the scheduling signal
445  * handler.
446  */
447 #define GET_CURRENT_TOD(tv)                             \
448         do {                                            \
449                 tv.tv_sec = _sched_tod.tv_sec;          \
450                 tv.tv_usec = _sched_tod.tv_usec;        \
451         } while (tv.tv_sec != _sched_tod.tv_sec)
452
453
454 struct pthread_key {
455         spinlock_t      lock;
456         volatile int    allocated;
457         volatile int    count;
458         void            (*destructor)(void *);
459 };
460
461 struct pthread_rwlockattr {
462         int             pshared;
463 };
464
465 struct pthread_rwlock {
466         pthread_mutex_t lock;   /* monitor lock */
467         int             state;  /* 0 = idle  >0 = # of readers  -1 = writer */
468         pthread_cond_t  read_signal;
469         pthread_cond_t  write_signal;
470         int             blocked_writers;
471 };
472
473 /*
474  * Thread states.
475  */
476 enum pthread_state {
477         PS_RUNNING,
478         PS_SIGTHREAD,
479         PS_MUTEX_WAIT,
480         PS_COND_WAIT,
481         PS_FDLR_WAIT,
482         PS_FDLW_WAIT,
483         PS_FDR_WAIT,
484         PS_FDW_WAIT,
485         PS_FILE_WAIT,
486         PS_POLL_WAIT,
487         PS_SELECT_WAIT,
488         PS_SLEEP_WAIT,
489         PS_WAIT_WAIT,
490         PS_SIGSUSPEND,
491         PS_SIGWAIT,
492         PS_SPINBLOCK,
493         PS_JOIN,
494         PS_SUSPENDED,
495         PS_DEAD,
496         PS_DEADLOCK,
497         PS_STATE_MAX
498 };
499
500
501 /*
502  * File descriptor locking definitions.
503  */
504 #define FD_READ             0x1
505 #define FD_WRITE            0x2
506 #define FD_RDWR             (FD_READ | FD_WRITE)
507
508 /*
509  * File descriptor table structure.
510  */
511 struct fd_table_entry {
512         /*
513          * Lock for accesses to this file descriptor table
514          * entry. This is passed to _spinlock() to provide atomic
515          * access to this structure. It does *not* represent the
516          * state of the lock on the file descriptor.
517          */
518         spinlock_t              lock;
519         TAILQ_HEAD(, pthread)   r_queue;        /* Read queue.                        */
520         TAILQ_HEAD(, pthread)   w_queue;        /* Write queue.                       */
521         struct pthread          *r_owner;       /* Ptr to thread owning read lock.    */
522         struct pthread          *w_owner;       /* Ptr to thread owning write lock.   */
523         char                    *r_fname;       /* Ptr to read lock source file name  */
524         int                     r_lineno;       /* Read lock source line number.      */
525         char                    *w_fname;       /* Ptr to write lock source file name */
526         int                     w_lineno;       /* Write lock source line number.     */
527         int                     r_lockcount;    /* Count for FILE read locks.         */
528         int                     w_lockcount;    /* Count for FILE write locks.        */
529         int                     flags;          /* Flags used in open.                */
530 };
531
532 struct pthread_poll_data {
533         int     nfds;
534         struct pollfd *fds;
535 };
536
537 union pthread_wait_data {
538         pthread_mutex_t mutex;
539         pthread_cond_t  cond;
540         const sigset_t  *sigwait;       /* Waiting on a signal in sigwait */
541         struct {
542                 short   fd;             /* Used when thread waiting on fd */
543                 short   branch;         /* Line number, for debugging.    */
544                 char    *fname;         /* Source file name for debugging.*/
545         } fd;
546         FILE            *fp;
547         struct pthread_poll_data *poll_data;
548         spinlock_t      *spinlock;
549         struct pthread  *thread;
550 };
551
552 /*
553  * Define a continuation routine that can be used to perform a
554  * transfer of control:
555  */
556 typedef void    (*thread_continuation_t) (void *);
557
558 struct pthread_signal_frame;
559
560 struct pthread_state_data {
561         struct pthread_signal_frame *psd_curframe;
562         sigset_t                psd_sigmask;
563         struct timespec         psd_wakeup_time;
564         union pthread_wait_data psd_wait_data;
565         enum pthread_state      psd_state;
566         int                     psd_flags;
567         int                     psd_interrupted;
568         int                     psd_longjmp_val;
569         int                     psd_sigmask_seqno;
570         int                     psd_signo;
571         int                     psd_sig_defer_count;
572         /* XXX - What about thread->timeout and/or thread->error? */
573 };
574
575 struct join_status {
576         struct pthread  *thread;
577         void            *ret;
578         int             error;
579 };
580
581 /*
582  * The frame that is added to the top of a threads stack when setting up
583  * up the thread to run a signal handler.
584  */
585 struct pthread_signal_frame {
586         /*
587          * This stores the threads state before the signal.
588          */
589         struct pthread_state_data saved_state;
590
591         /*
592          * Threads return context; we use only jmp_buf's for now.
593          */
594         union {
595                 jmp_buf         jb;
596                 ucontext_t      uc;
597         } ctx;
598         int                     signo;  /* signal, arg 1 to sighandler */
599         int                     sig_has_args;   /* use signal args if true */
600         ucontext_t              uc;
601         siginfo_t               siginfo;
602 };
603
604 /*
605  * Thread structure.
606  */
607 struct pthread {
608         /*
609          * Magic value to help recognize a valid thread structure
610          * from an invalid one:
611          */
612 #define PTHREAD_MAGIC           ((u_int32_t) 0xd09ba115)
613         u_int32_t               magic;
614         char                    *name;
615         u_int64_t               uniqueid; /* for gdb */
616
617         /*
618          * Lock for accesses to this thread structure.
619          */
620         spinlock_t              lock;
621
622         /* Queue entry for list of all threads: */
623         TAILQ_ENTRY(pthread)    tle;
624
625         /* Queue entry for list of dead threads: */
626         TAILQ_ENTRY(pthread)    dle;
627
628         /*
629          * Thread start routine, argument, stack pointer and thread
630          * attributes.
631          */
632         void                    *(*start_routine)(void *);
633         void                    *arg;
634         void                    *stack;
635         struct pthread_attr     attr;
636
637         /*
638          * Threads return context; we use only jmp_buf's for now.
639          */
640         union {
641                 jmp_buf         jb;
642                 ucontext_t      uc;
643         } ctx;
644
645         /*
646          * Used for tracking delivery of signal handlers.
647          */
648         struct pthread_signal_frame     *curframe;
649
650         /*
651          * Cancelability flags - the lower 2 bits are used by cancel
652          * definitions in pthread.h
653          */
654 #define PTHREAD_AT_CANCEL_POINT         0x0004
655 #define PTHREAD_CANCELLING              0x0008
656 #define PTHREAD_CANCEL_NEEDED           0x0010
657         int     cancelflags;
658
659         thread_continuation_t   continuation;
660
661         /*
662          * Current signal mask and pending signals.
663          */
664         sigset_t        sigmask;
665         sigset_t        sigpend;
666         int             sigmask_seqno;
667         int             check_pending;
668
669         /* Thread state: */
670         enum pthread_state      state;
671
672         /* Scheduling clock when this thread was last made active. */
673         long    last_active;
674
675         /* Scheduling clock when this thread was last made inactive. */
676         long    last_inactive;
677
678         /*
679          * Number of microseconds accumulated by this thread when
680          * time slicing is active.
681          */
682         long    slice_usec;
683
684         /*
685          * Time to wake up thread. This is used for sleeping threads and
686          * for any operation which may time out (such as select).
687          */
688         struct timespec wakeup_time;
689
690         /* TRUE if operation has timed out. */
691         int     timeout;
692
693         /*
694          * Error variable used instead of errno. The function __error()
695          * returns a pointer to this. 
696          */
697         int     error;
698
699         /*
700          * The joiner is the thread that is joining to this thread.  The
701          * join status keeps track of a join operation to another thread.
702          */
703         struct pthread          *joiner;
704         struct join_status      join_status;
705
706         /*
707          * The current thread can belong to only one scheduling queue at
708          * a time (ready or waiting queue).  It can also belong to:
709          *
710          *   o A queue of threads waiting for a mutex
711          *   o A queue of threads waiting for a condition variable
712          *   o A queue of threads waiting for a file descriptor lock
713          *   o A queue of threads needing work done by the kernel thread
714          *     (waiting for a spinlock or file I/O)
715          *
716          * A thread can also be joining a thread (the joiner field above).
717          *
718          * It must not be possible for a thread to belong to any of the
719          * above queues while it is handling a signal.  Signal handlers
720          * may longjmp back to previous stack frames circumventing normal
721          * control flow.  This could corrupt queue integrity if the thread
722          * retains membership in the queue.  Therefore, if a thread is a
723          * member of one of these queues when a signal handler is invoked,
724          * it must remove itself from the queue before calling the signal
725          * handler and reinsert itself after normal return of the handler.
726          *
727          * Use pqe for the scheduling queue link (both ready and waiting),
728          * sqe for synchronization (mutex and condition variable) queue
729          * links, and qe for all other links.
730          */
731         TAILQ_ENTRY(pthread)    pqe;    /* priority queue link */
732         TAILQ_ENTRY(pthread)    sqe;    /* synchronization queue link */
733         TAILQ_ENTRY(pthread)    qe;     /* all other queues link */
734
735         /* Wait data. */
736         union pthread_wait_data data;
737
738         /*
739          * Allocated for converting select into poll.
740          */
741         struct pthread_poll_data poll_data;
742
743         /*
744          * Set to TRUE if a blocking operation was
745          * interrupted by a signal:
746          */
747         int             interrupted;
748
749         /* Signal number when in state PS_SIGWAIT: */
750         int             signo;
751
752         /*
753          * Set to non-zero when this thread has deferred signals.
754          * We allow for recursive deferral.
755          */
756         int             sig_defer_count;
757
758         /*
759          * Set to TRUE if this thread should yield after undeferring
760          * signals.
761          */
762         int             yield_on_sig_undefer;
763
764         /* Miscellaneous flags; only set with signals deferred. */
765         int             flags;
766 #define PTHREAD_FLAGS_PRIVATE   0x0001
767 #define PTHREAD_EXITING         0x0002
768 #define PTHREAD_FLAGS_IN_WAITQ  0x0004  /* in waiting queue using pqe link */
769 #define PTHREAD_FLAGS_IN_PRIOQ  0x0008  /* in priority queue using pqe link */
770 #define PTHREAD_FLAGS_IN_WORKQ  0x0010  /* in work queue using qe link */
771 #define PTHREAD_FLAGS_IN_FILEQ  0x0020  /* in file lock queue using qe link */
772 #define PTHREAD_FLAGS_IN_FDQ    0x0040  /* in fd lock queue using qe link */
773 #define PTHREAD_FLAGS_IN_CONDQ  0x0080  /* in condition queue using sqe link*/
774 #define PTHREAD_FLAGS_IN_MUTEXQ 0x0100  /* in mutex queue using sqe link */
775 #define PTHREAD_FLAGS_SUSPENDED 0x0200  /* thread is suspended */
776 #define PTHREAD_FLAGS_TRACE     0x0400  /* for debugging purposes */
777 #define PTHREAD_FLAGS_IN_SYNCQ  \
778     (PTHREAD_FLAGS_IN_CONDQ | PTHREAD_FLAGS_IN_MUTEXQ)
779
780         /*
781          * Base priority is the user setable and retrievable priority
782          * of the thread.  It is only affected by explicit calls to
783          * set thread priority and upon thread creation via a thread
784          * attribute or default priority.
785          */
786         char            base_priority;
787
788         /*
789          * Inherited priority is the priority a thread inherits by
790          * taking a priority inheritence or protection mutex.  It
791          * is not affected by base priority changes.  Inherited
792          * priority defaults to and remains 0 until a mutex is taken
793          * that is being waited on by any other thread whose priority
794          * is non-zero.
795          */
796         char            inherited_priority;
797
798         /*
799          * Active priority is always the maximum of the threads base
800          * priority and inherited priority.  When there is a change
801          * in either the base or inherited priority, the active
802          * priority must be recalculated.
803          */
804         char            active_priority;
805
806         /* Number of priority ceiling or protection mutexes owned. */
807         int             priority_mutex_count;
808
809         /*
810          * Queue of currently owned mutexes.
811          */
812         TAILQ_HEAD(, pthread_mutex)     mutexq;
813
814         void            *ret;
815         const void      **specific_data;
816         int             specific_data_count;
817
818         /* Cleanup handlers Link List */
819         struct pthread_cleanup *cleanup;
820         char                    *fname; /* Ptr to source file name  */
821         int                     lineno; /* Source line number.      */
822 };
823
824 /* Spare thread stack. */
825 struct stack {
826         SLIST_ENTRY(stack)      qe; /* Queue entry for this stack. */
827 };
828
829 /*
830  * Global variables for the uthread kernel.
831  */
832
833 SCLASS void *_usrstack
834 #ifdef GLOBAL_PTHREAD_PRIVATE
835 = (void *) USRSTACK;
836 #else
837 ;
838 #endif
839
840 /* Kernel thread structure used when there are no running threads: */
841 SCLASS struct pthread   _thread_kern_thread;
842
843 /* Ptr to the thread structure for the running thread: */
844 SCLASS struct pthread   * volatile _thread_run
845 #ifdef GLOBAL_PTHREAD_PRIVATE
846 = &_thread_kern_thread;
847 #else
848 ;
849 #endif
850
851 /* Ptr to the thread structure for the last user thread to run: */
852 SCLASS struct pthread   * volatile _last_user_thread
853 #ifdef GLOBAL_PTHREAD_PRIVATE
854 = &_thread_kern_thread;
855 #else
856 ;
857 #endif
858
859 /* List of all threads: */
860 SCLASS TAILQ_HEAD(, pthread)    _thread_list
861 #ifdef GLOBAL_PTHREAD_PRIVATE
862 = TAILQ_HEAD_INITIALIZER(_thread_list);
863 #else
864 ;
865 #endif
866
867 /*
868  * Array of kernel pipe file descriptors that are used to ensure that
869  * no signals are missed in calls to _select.
870  */
871 SCLASS int              _thread_kern_pipe[2]
872 #ifdef GLOBAL_PTHREAD_PRIVATE
873 = {
874         -1,
875         -1
876 };
877 #else
878 ;
879 #endif
880 SCLASS int              volatile _queue_signals
881 #ifdef GLOBAL_PTHREAD_PRIVATE
882 = 0;
883 #else
884 ;
885 #endif
886 SCLASS int              _thread_kern_in_sched
887 #ifdef GLOBAL_PTHREAD_PRIVATE
888 = 0;
889 #else
890 ;
891 #endif
892
893 SCLASS int              _sig_in_handler
894 #ifdef GLOBAL_PTHREAD_PRIVATE
895 = 0;
896 #else
897 ;
898 #endif
899
900 /* Time of day at last scheduling timer signal: */
901 SCLASS struct timeval volatile  _sched_tod
902 #ifdef GLOBAL_PTHREAD_PRIVATE
903 = { 0, 0 };
904 #else
905 ;
906 #endif
907
908 /*
909  * Current scheduling timer ticks; used as resource usage.
910  */
911 SCLASS unsigned int volatile    _sched_ticks
912 #ifdef GLOBAL_PTHREAD_PRIVATE
913 = 0;
914 #else
915 ;
916 #endif
917
918 /* Dead threads: */
919 SCLASS TAILQ_HEAD(, pthread) _dead_list
920 #ifdef GLOBAL_PTHREAD_PRIVATE
921 = TAILQ_HEAD_INITIALIZER(_dead_list);
922 #else
923 ;
924 #endif
925
926 /* Initial thread: */
927 SCLASS struct pthread *_thread_initial
928 #ifdef GLOBAL_PTHREAD_PRIVATE
929 = NULL;
930 #else
931 ;
932 #endif
933
934 /* Default thread attributes: */
935 SCLASS struct pthread_attr pthread_attr_default
936 #ifdef GLOBAL_PTHREAD_PRIVATE
937 = { SCHED_RR, 0, TIMESLICE_USEC, PTHREAD_DEFAULT_PRIORITY, PTHREAD_CREATE_RUNNING,
938         PTHREAD_CREATE_JOINABLE, NULL, NULL, NULL, PTHREAD_STACK_DEFAULT };
939 #else
940 ;
941 #endif
942
943 /* Default mutex attributes: */
944 SCLASS struct pthread_mutex_attr pthread_mutexattr_default
945 #ifdef GLOBAL_PTHREAD_PRIVATE
946 = { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, 0 };
947 #else
948 ;
949 #endif
950
951 /* Default condition variable attributes: */
952 SCLASS struct pthread_cond_attr pthread_condattr_default
953 #ifdef GLOBAL_PTHREAD_PRIVATE
954 = { COND_TYPE_FAST, 0 };
955 #else
956 ;
957 #endif
958
959 /*
960  * Standard I/O file descriptors need special flag treatment since
961  * setting one to non-blocking does all on *BSD. Sigh. This array
962  * is used to store the initial flag settings.
963  */
964 SCLASS int      _pthread_stdio_flags[3];
965
966 /* File table information: */
967 SCLASS struct fd_table_entry **_thread_fd_table
968 #ifdef GLOBAL_PTHREAD_PRIVATE
969 = NULL;
970 #else
971 ;
972 #endif
973
974 /* Table for polling file descriptors: */
975 SCLASS struct pollfd *_thread_pfd_table
976 #ifdef GLOBAL_PTHREAD_PRIVATE
977 = NULL;
978 #else
979 ;
980 #endif
981
982 SCLASS const int dtablecount
983 #ifdef GLOBAL_PTHREAD_PRIVATE
984 = 4096/sizeof(struct fd_table_entry);
985 #else
986 ;
987 #endif
988 SCLASS int    _thread_dtablesize        /* Descriptor table size.           */
989 #ifdef GLOBAL_PTHREAD_PRIVATE
990 = 0;
991 #else
992 ;
993 #endif
994
995 SCLASS int    _clock_res_usec           /* Clock resolution in usec.    */
996 #ifdef GLOBAL_PTHREAD_PRIVATE
997 = CLOCK_RES_USEC;
998 #else
999 ;
1000 #endif
1001
1002 /* Garbage collector mutex and condition variable. */
1003 SCLASS  pthread_mutex_t _gc_mutex
1004 #ifdef GLOBAL_PTHREAD_PRIVATE
1005 = NULL
1006 #endif
1007 ;
1008 SCLASS  pthread_cond_t  _gc_cond
1009 #ifdef GLOBAL_PTHREAD_PRIVATE
1010 = NULL
1011 #endif
1012 ;
1013
1014 /*
1015  * Array of signal actions for this process.
1016  */
1017 SCLASS struct  sigaction _thread_sigact[NSIG];
1018
1019 /*
1020  * Array of counts of dummy handlers for SIG_DFL signals.  This is used to
1021  * assure that there is always a dummy signal handler installed while there is a
1022  * thread sigwait()ing on the corresponding signal.
1023  */
1024 SCLASS int      _thread_dfl_count[NSIG];
1025
1026 /*
1027  * Pending signals and mask for this process:
1028  */
1029 SCLASS sigset_t _process_sigpending;
1030 SCLASS sigset_t _process_sigmask
1031 #ifdef GLOBAL_PTHREAD_PRIVATE
1032 = { {0, 0, 0, 0} }
1033 #endif
1034 ;
1035
1036 /*
1037  * Scheduling queues:
1038  */
1039 SCLASS pq_queue_t               _readyq;
1040 SCLASS TAILQ_HEAD(, pthread)    _waitingq;
1041
1042 /*
1043  * Work queue:
1044  */
1045 SCLASS TAILQ_HEAD(, pthread)    _workq;
1046
1047 /* Tracks the number of threads blocked while waiting for a spinlock. */
1048 SCLASS  volatile int    _spinblock_count
1049 #ifdef GLOBAL_PTHREAD_PRIVATE
1050 = 0
1051 #endif
1052 ;
1053
1054 /* Used to maintain pending and active signals: */
1055 struct sigstatus {
1056         int             pending;        /* Is this a pending signal? */
1057         int             blocked;        /*
1058                                          * A handler is currently active for
1059                                          * this signal; ignore subsequent
1060                                          * signals until the handler is done.
1061                                          */
1062         int             signo;          /* arg 1 to signal handler */
1063         siginfo_t       siginfo;        /* arg 2 to signal handler */
1064         ucontext_t      uc;             /* arg 3 to signal handler */
1065 };
1066
1067 SCLASS struct sigstatus _thread_sigq[NSIG];
1068
1069 /* Indicates that the signal queue needs to be checked. */
1070 SCLASS  volatile int    _sigq_check_reqd
1071 #ifdef GLOBAL_PTHREAD_PRIVATE
1072 = 0
1073 #endif
1074 ;
1075
1076 /* Thread switch hook. */
1077 SCLASS pthread_switch_routine_t _sched_switch_hook
1078 #ifdef GLOBAL_PTHREAD_PRIVATE
1079 = NULL
1080 #endif
1081 ;
1082
1083 /*
1084  * Spare stack queue.  Stacks of default size are cached in order to reduce
1085  * thread creation time.  Spare stacks are used in LIFO order to increase cache
1086  * locality.
1087  */
1088 SCLASS SLIST_HEAD(, stack)      _stackq;
1089
1090 /*
1091  * Base address of next unallocated default-size {stack, red zone}.  Stacks are
1092  * allocated contiguously, starting below the bottom of the main stack.  When a
1093  * new stack is created, a red zone is created (actually, the red zone is simply
1094  * left unmapped) below the bottom of the stack, such that the stack will not be
1095  * able to grow all the way to the top of the next stack.  This isn't
1096  * fool-proof.  It is possible for a stack to grow by a large amount, such that
1097  * it grows into the next stack, and as long as the memory within the red zone
1098  * is never accessed, nothing will prevent one thread stack from trouncing all
1099  * over the next.
1100  */
1101 SCLASS void *   _next_stack
1102 #ifdef GLOBAL_PTHREAD_PRIVATE
1103 /* main stack top   - main stack size       - stack size            - (red zone + main stack red zone) */
1104 = (void *) USRSTACK - PTHREAD_STACK_INITIAL - PTHREAD_STACK_DEFAULT - (2 * PTHREAD_STACK_GUARD)
1105 #endif
1106 ;
1107
1108 /*
1109  * Declare the kernel scheduler jump buffer and stack:
1110  */
1111 SCLASS jmp_buf  _thread_kern_sched_jb;
1112
1113 SCLASS void *   _thread_kern_sched_stack
1114 #ifdef GLOBAL_PTHREAD_PRIVATE
1115 = NULL
1116 #endif
1117 ;
1118
1119
1120 /* Used for _PTHREADS_INVARIANTS checking. */
1121 SCLASS int      _thread_kern_new_state
1122 #ifdef GLOBAL_PTHREAD_PRIVATE
1123 = 0
1124 #endif
1125 ;
1126
1127 /* Undefine the storage class specifier: */
1128 #undef  SCLASS
1129
1130 #ifdef  _LOCK_DEBUG
1131 #define _FD_LOCK(_fd,_type,_ts)         _thread_fd_lock_debug(_fd, _type, \
1132                                                 _ts, __FILE__, __LINE__)
1133 #define _FD_UNLOCK(_fd,_type)           _thread_fd_unlock_debug(_fd, _type, \
1134                                                 __FILE__, __LINE__)
1135 #else
1136 #define _FD_LOCK(_fd,_type,_ts)         _thread_fd_lock(_fd, _type, _ts)
1137 #define _FD_UNLOCK(_fd,_type)           _thread_fd_unlock(_fd, _type)
1138 #endif
1139
1140 /*
1141  * Function prototype definitions.
1142  */
1143 __BEGIN_DECLS
1144 char    *__ttyname_basic(int);
1145 char    *__ttyname_r_basic(int, char *, size_t);
1146 char    *ttyname_r(int, char *, size_t);
1147 void    _cond_wait_backout(pthread_t);
1148 void    _fd_lock_backout(pthread_t);
1149 int     _find_thread(pthread_t);
1150 struct pthread *_get_curthread(void);
1151 void    _set_curthread(struct pthread *);
1152 void    _flockfile_backout(struct pthread *);
1153 void    _funlock_owned(struct pthread *);
1154 int     _thread_create(pthread_t *,const pthread_attr_t *,void *(*start_routine)(void *),void *,pthread_t);
1155 int     _mutex_cv_lock(pthread_mutex_t *);
1156 int     _mutex_cv_unlock(pthread_mutex_t *);
1157 void    _mutex_lock_backout(pthread_t);
1158 void    _mutex_notify_priochange(pthread_t);
1159 int     _mutex_reinit(pthread_mutex_t *);
1160 void    _mutex_unlock_private(pthread_t);
1161 int     _cond_reinit(pthread_cond_t *);
1162 int     _pq_alloc(struct pq_queue *, int, int);
1163 int     _pq_init(struct pq_queue *);
1164 void    _pq_remove(struct pq_queue *pq, struct pthread *);
1165 void    _pq_insert_head(struct pq_queue *pq, struct pthread *);
1166 void    _pq_insert_tail(struct pq_queue *pq, struct pthread *);
1167 struct pthread *_pq_first(struct pq_queue *pq);
1168 void    *_pthread_getspecific(pthread_key_t);
1169 int     _pthread_key_create(pthread_key_t *, void (*) (void *));
1170 int     _pthread_key_delete(pthread_key_t);
1171 int     _pthread_mutex_destroy(pthread_mutex_t *);
1172 int     _pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
1173 int     _pthread_mutex_lock(pthread_mutex_t *);
1174 int     _pthread_mutex_trylock(pthread_mutex_t *);
1175 int     _pthread_mutex_unlock(pthread_mutex_t *);
1176 int     _pthread_once(pthread_once_t *, void (*) (void));
1177 int     _pthread_setspecific(pthread_key_t, const void *);
1178 int     _pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *);
1179 int     _pthread_cond_destroy(pthread_cond_t *);
1180 int     _pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
1181 int     _pthread_cond_timedwait(pthread_cond_t *, pthread_mutex_t *,
1182             const struct timespec *);
1183 int     _pthread_cond_signal(pthread_cond_t *);
1184 int     _pthread_cond_broadcast(pthread_cond_t *);
1185 void    _waitq_insert(pthread_t pthread);
1186 void    _waitq_remove(pthread_t pthread);
1187 #if defined(_PTHREADS_INVARIANTS)
1188 void    _waitq_setactive(void);
1189 void    _waitq_clearactive(void);
1190 #endif
1191 void    _thread_exit(char *, int, char *);
1192 void    _thread_exit_cleanup(void);
1193 int     _thread_fd_getflags(int);
1194 int     _thread_fd_lock(int, int, struct timespec *);
1195 int     _thread_fd_lock_debug(int, int, struct timespec *,char *fname,int lineno);
1196 void    _thread_fd_setflags(int, int);
1197 int     _thread_fd_table_init(int fd);
1198 void    _thread_fd_unlock(int, int);
1199 void    _thread_fd_unlock_debug(int, int, char *, int);
1200 void    _thread_fd_unlock_owned(pthread_t);
1201 void    *_thread_cleanup(pthread_t);
1202 void    _thread_cleanupspecific(void);
1203 void    _thread_dump_info(void);
1204 void    _thread_init(void);
1205 void    _thread_kern_sched(ucontext_t *);
1206 void    _thread_kern_scheduler(void);
1207 void    _thread_kern_sched_frame(struct pthread_signal_frame *psf);
1208 void    _thread_kern_sched_sig(void);
1209 void    _thread_kern_sched_state(enum pthread_state, char *fname, int lineno);
1210 void    _thread_kern_sched_state_unlock(enum pthread_state state,
1211             spinlock_t *lock, char *fname, int lineno);
1212 void    _thread_kern_set_timeout(const struct timespec *);
1213 void    _thread_kern_sig_defer(void);
1214 void    _thread_kern_sig_undefer(void);
1215 void    _thread_sig_handler(int, siginfo_t *, ucontext_t *);
1216 void    _thread_sig_check_pending(struct pthread *pthread);
1217 void    _thread_sig_handle_pending(void);
1218 void    _thread_sig_send(struct pthread *pthread, int sig);
1219 void    _thread_sig_wrapper(void);
1220 void    _thread_sigframe_restore(struct pthread *thread,
1221             struct pthread_signal_frame *psf);
1222 void    _thread_start(void);
1223 void    _thread_seterrno(pthread_t, int);
1224 pthread_addr_t _thread_gc(pthread_addr_t);
1225 void    _thread_enter_cancellation_point(void);
1226 void    _thread_leave_cancellation_point(void);
1227 void    _thread_cancellation_point(void);
1228
1229 /* #include <aio.h> */
1230 #ifdef _SYS_AIO_H_
1231 int     __sys_aio_suspend(const struct aiocb * const[], int, const struct timespec *);
1232 #endif
1233
1234 /* #include <sys/event.h> */
1235 #ifdef _SYS_EVENT_H_
1236 int     __sys_kevent(int, const struct kevent *, int, struct kevent *,
1237             int, const struct timespec *);
1238 #endif
1239
1240 /* #include <sys/ioctl.h> */
1241 #ifdef _SYS_IOCTL_H_
1242 int     __sys_ioctl(int, unsigned long, ...);
1243 #endif
1244
1245 /* #include <sys/mman.h> */
1246 #ifdef _SYS_MMAN_H_
1247 int     __sys_msync(void *, size_t, int);
1248 #endif
1249
1250 /* #include <sys/mount.h> */
1251 #ifdef _SYS_MOUNT_H_
1252 int     __sys_fstatfs(int, struct statfs *);
1253 #endif
1254
1255 /* #include <sys/socket.h> */
1256 #ifdef _SYS_SOCKET_H_
1257 int     __sys_accept(int, struct sockaddr *, socklen_t *);
1258 int     __sys_bind(int, const struct sockaddr *, socklen_t);
1259 int     __sys_connect(int, const struct sockaddr *, socklen_t);
1260 int     __sys_getpeername(int, struct sockaddr *, socklen_t *);
1261 int     __sys_getsockname(int, struct sockaddr *, socklen_t *);
1262 int     __sys_getsockopt(int, int, int, void *, socklen_t *);
1263 int     __sys_listen(int, int);
1264 ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
1265 ssize_t __sys_recvmsg(int, struct msghdr *, int);
1266 int     __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *, off_t *, int);
1267 ssize_t __sys_sendmsg(int, const struct msghdr *, int);
1268 ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
1269 int     __sys_setsockopt(int, int, int, const void *, socklen_t);
1270 int     __sys_shutdown(int, int);
1271 int     __sys_socket(int, int, int);
1272 int     __sys_socketpair(int, int, int, int *);
1273 #endif
1274
1275 /* #include <sys/stat.h> */
1276 #ifdef _SYS_STAT_H_
1277 int     __sys_fchflags(int, u_long);
1278 int     __sys_fchmod(int, mode_t);
1279 int     __sys_fstat(int, struct stat *);
1280 #endif
1281
1282 /* #include <sys/uio.h> */
1283 #ifdef _SYS_UIO_H_
1284 ssize_t __sys_readv(int, const struct iovec *, int);
1285 ssize_t __sys_writev(int, const struct iovec *, int);
1286 #endif
1287
1288 /* #include <sys/wait.h> */
1289 #ifdef WNOHANG
1290 pid_t   __sys_wait4(pid_t, int *, int, struct rusage *);
1291 #endif
1292
1293 /* #include <dirent.h> */
1294 #ifdef _DIRENT_H_
1295 int     __sys_getdirentries(int, char *, int, long *);
1296 #endif
1297
1298 /* #include <fcntl.h> */
1299 #ifdef _SYS_FCNTL_H_
1300 int     __sys_fcntl(int, int, ...);
1301 int     __sys_flock(int, int);
1302 int     __sys_open(const char *, int, ...);
1303 #endif
1304
1305 /* #include <poll.h> */
1306 #ifdef _SYS_POLL_H_
1307 int     __sys_poll(struct pollfd *, unsigned, int);
1308 #endif
1309
1310 /* #include <signal.h> */
1311 #ifdef _SIGNAL_H_
1312 int     __sys_sigaction(int, const struct sigaction *, struct sigaction *);
1313 int     __sys_sigprocmask(int, const sigset_t *, sigset_t *);
1314 int     __sys_sigreturn(ucontext_t *);
1315 #endif
1316
1317 /* #include <unistd.h> */
1318 #ifdef _UNISTD_H_
1319 void    __sys__exit(int);
1320 int     __sys_close(int);
1321 int     __sys_dup(int);
1322 int     __sys_dup2(int, int);
1323 int     __sys_execve(const char *, char * const *, char * const *);
1324 int     __sys_fchown(int, uid_t, gid_t);
1325 pid_t   __sys_fork(void);
1326 long    __sys_fpathconf(int, int);
1327 int     __sys_fsync(int);
1328 int     __sys_pipe(int *);
1329 ssize_t __sys_read(int, void *, size_t);
1330 ssize_t __sys_write(int, const void *, size_t);
1331 #endif
1332
1333 /* #include <setjmp.h> */
1334 #ifdef _SETJMP_H_
1335 extern void     __siglongjmp(sigjmp_buf, int) __dead2;
1336 extern void     __longjmp(jmp_buf, int) __dead2;
1337 extern void     ___longjmp(jmp_buf, int) __dead2;
1338 #endif
1339 __END_DECLS
1340
1341 #endif  /* !_PTHREAD_PRIVATE_H */