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