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