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