]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libkse/thread/thr_private.h
This commit was generated by cvs2svn to compensate for changes in r132943,
[FreeBSD/FreeBSD.git] / lib / libkse / thread / thr_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$
35  */
36
37 #ifndef _THR_PRIVATE_H
38 #define _THR_PRIVATE_H
39
40 /*
41  * Include files.
42  */
43 #include <setjmp.h>
44 #include <signal.h>
45 #include <stdio.h>
46 #include <sys/queue.h>
47 #include <sys/types.h>
48 #include <sys/time.h>
49 #include <sys/cdefs.h>
50 #include <sys/kse.h>
51 #include <sched.h>
52 #include <ucontext.h>
53 #include <unistd.h>
54 #include <pthread.h>
55 #include <pthread_np.h>
56
57 #ifndef LIBTHREAD_DB
58 #include "lock.h"
59 #include "pthread_md.h"
60 #endif
61
62 /*
63  * Evaluate the storage class specifier.
64  */
65 #ifdef GLOBAL_PTHREAD_PRIVATE
66 #define SCLASS
67 #define SCLASS_PRESET(x...)     = x
68 #else
69 #define SCLASS                  extern
70 #define SCLASS_PRESET(x...)
71 #endif
72
73 /*
74  * Kernel fatal error handler macro.
75  */
76 #define PANIC(string)   _thr_exit(__FILE__,__LINE__,string)
77
78
79 /* Output debug messages like this: */
80 #define stdout_debug(args...)   _thread_printf(STDOUT_FILENO, ##args)
81 #define stderr_debug(args...)   _thread_printf(STDOUT_FILENO, ##args)
82
83 #define DBG_MUTEX       0x0001
84 #define DBG_SIG         0x0002
85
86 #ifdef _PTHREADS_INVARIANTS
87 #define THR_ASSERT(cond, msg) do {      \
88         if (!(cond))                    \
89                 PANIC(msg);             \
90 } while (0)
91 #else
92 #define THR_ASSERT(cond, msg)
93 #endif
94
95 /*
96  * State change macro without scheduling queue change:
97  */
98 #define THR_SET_STATE(thrd, newstate) do {                              \
99         (thrd)->state = newstate;                                       \
100         (thrd)->fname = __FILE__;                                       \
101         (thrd)->lineno = __LINE__;                                      \
102 } while (0)
103
104
105 #define TIMESPEC_ADD(dst, src, val)                             \
106         do {                                                    \
107                 (dst)->tv_sec = (src)->tv_sec + (val)->tv_sec;  \
108                 (dst)->tv_nsec = (src)->tv_nsec + (val)->tv_nsec; \
109                 if ((dst)->tv_nsec > 1000000000) {              \
110                         (dst)->tv_sec++;                        \
111                         (dst)->tv_nsec -= 1000000000;           \
112                 }                                               \
113         } while (0)
114
115 #define TIMESPEC_SUB(dst, src, val)                             \
116         do {                                                    \
117                 (dst)->tv_sec = (src)->tv_sec - (val)->tv_sec;  \
118                 (dst)->tv_nsec = (src)->tv_nsec - (val)->tv_nsec; \
119                 if ((dst)->tv_nsec < 0) {                       \
120                         (dst)->tv_sec--;                        \
121                         (dst)->tv_nsec += 1000000000;           \
122                 }                                               \
123         } while (0)
124
125 /*
126  * Priority queues.
127  *
128  * XXX It'd be nice if these were contained in uthread_priority_queue.[ch].
129  */
130 typedef struct pq_list {
131         TAILQ_HEAD(, pthread)   pl_head; /* list of threads at this priority */
132         TAILQ_ENTRY(pq_list)    pl_link; /* link for queue of priority lists */
133         int                     pl_prio; /* the priority of this list */
134         int                     pl_queued; /* is this in the priority queue */
135 } pq_list_t;
136
137 typedef struct pq_queue {
138         TAILQ_HEAD(, pq_list)    pq_queue; /* queue of priority lists */
139         pq_list_t               *pq_lists; /* array of all priority lists */
140         int                      pq_size;  /* number of priority lists */
141 #define PQF_ACTIVE      0x0001
142         int                      pq_flags;
143         int                      pq_threads;
144 } pq_queue_t;
145
146 /*
147  * Each KSEG has a scheduling queue.  For now, threads that exist in their
148  * own KSEG (system scope) will get a full priority queue.  In the future
149  * this can be optimized for the single thread per KSEG case.
150  */
151 struct sched_queue {
152         pq_queue_t              sq_runq;
153         TAILQ_HEAD(, pthread)   sq_waitq;       /* waiting in userland */
154 };
155
156 typedef struct kse_thr_mailbox *kse_critical_t;
157
158 struct kse_group;
159
160 #define MAX_KSE_LOCKLEVEL       5       
161 struct kse {
162         /* -- location and order specific items for gdb -- */
163         struct kcb              *k_kcb;
164         struct pthread          *k_curthread;   /* current thread */
165         struct kse_group        *k_kseg;        /* parent KSEG */
166         struct sched_queue      *k_schedq;      /* scheduling queue */
167         /* -- end of location and order specific items -- */
168         TAILQ_ENTRY(kse)        k_qe;           /* KSE list link entry */
169         TAILQ_ENTRY(kse)        k_kgqe;         /* KSEG's KSE list entry */
170         /*
171          * Items that are only modified by the kse, or that otherwise
172          * don't need to be locked when accessed
173          */
174         struct lock             k_lock;
175         struct lockuser         k_lockusers[MAX_KSE_LOCKLEVEL];
176         int                     k_locklevel;
177         stack_t                 k_stack;
178         int                     k_flags;
179 #define KF_STARTED                      0x0001  /* kernel kse created */
180 #define KF_INITIALIZED                  0x0002  /* initialized on 1st upcall */
181 #define KF_TERMINATED                   0x0004  /* kse is terminated */
182 #define KF_IDLE                         0x0008  /* kse is idle */
183 #define KF_SWITCH                       0x0010  /* thread switch in UTS */
184         int                     k_error;        /* syscall errno in critical */
185         int                     k_cpu;          /* CPU ID when bound */
186         int                     k_sigseqno;     /* signal buffered count */
187 };
188
189 #define KSE_SET_IDLE(kse)       ((kse)->k_flags |= KF_IDLE)
190 #define KSE_CLEAR_IDLE(kse)     ((kse)->k_flags &= ~KF_IDLE)
191 #define KSE_IS_IDLE(kse)        (((kse)->k_flags & KF_IDLE) != 0)
192 #define KSE_SET_SWITCH(kse)     ((kse)->k_flags |= KF_SWITCH)
193 #define KSE_CLEAR_SWITCH(kse)   ((kse)->k_flags &= ~KF_SWITCH)
194 #define KSE_IS_SWITCH(kse)      (((kse)->k_flags & KF_SWITCH) != 0)
195
196 /*
197  * Each KSE group contains one or more KSEs in which threads can run.
198  * At least for now, there is one scheduling queue per KSE group; KSEs
199  * within the same KSE group compete for threads from the same scheduling
200  * queue.  A scope system thread has one KSE in one KSE group; the group
201  * does not use its scheduling queue.
202  */
203 struct kse_group {
204         TAILQ_HEAD(, kse)       kg_kseq;        /* list of KSEs in group */
205         TAILQ_HEAD(, pthread)   kg_threadq;     /* list of threads in group */
206         TAILQ_ENTRY(kse_group)  kg_qe;          /* link entry */
207         struct sched_queue      kg_schedq;      /* scheduling queue */
208         struct lock             kg_lock;
209         int                     kg_threadcount; /* # of assigned threads */
210         int                     kg_ksecount;    /* # of assigned KSEs */
211         int                     kg_idle_kses;
212         int                     kg_flags;
213 #define KGF_SINGLE_THREAD               0x0001  /* scope system kse group */
214 #define KGF_SCHEDQ_INITED               0x0002  /* has an initialized schedq */
215 };
216
217 /*
218  * Add/remove threads from a KSE's scheduling queue.
219  * For now the scheduling queue is hung off the KSEG.
220  */
221 #define KSEG_THRQ_ADD(kseg, thr)                        \
222 do {                                                    \
223         TAILQ_INSERT_TAIL(&(kseg)->kg_threadq, thr, kle);\
224         (kseg)->kg_threadcount++;                       \
225 } while (0)
226
227 #define KSEG_THRQ_REMOVE(kseg, thr)                     \
228 do {                                                    \
229         TAILQ_REMOVE(&(kseg)->kg_threadq, thr, kle);    \
230         (kseg)->kg_threadcount--;                       \
231 } while (0)
232
233
234 /*
235  * Lock acquire and release for KSEs.
236  */
237 #define KSE_LOCK_ACQUIRE(kse, lck)                                      \
238 do {                                                                    \
239         if ((kse)->k_locklevel < MAX_KSE_LOCKLEVEL) {                   \
240                 (kse)->k_locklevel++;                                   \
241                 _lock_acquire((lck),                                    \
242                     &(kse)->k_lockusers[(kse)->k_locklevel - 1], 0);    \
243         }                                                               \
244         else                                                            \
245                 PANIC("Exceeded maximum lock level");                   \
246 } while (0)
247
248 #define KSE_LOCK_RELEASE(kse, lck)                                      \
249 do {                                                                    \
250         if ((kse)->k_locklevel > 0) {                                   \
251                 _lock_release((lck),                                    \
252                     &(kse)->k_lockusers[(kse)->k_locklevel - 1]);       \
253                 (kse)->k_locklevel--;                                   \
254         }                                                               \
255 } while (0)
256
257 /*
258  * Lock our own KSEG.
259  */
260 #define KSE_LOCK(curkse)                \
261         KSE_LOCK_ACQUIRE(curkse, &(curkse)->k_kseg->kg_lock)
262 #define KSE_UNLOCK(curkse)              \
263         KSE_LOCK_RELEASE(curkse, &(curkse)->k_kseg->kg_lock)
264
265 /*
266  * Lock a potentially different KSEG.
267  */
268 #define KSE_SCHED_LOCK(curkse, kseg)    \
269         KSE_LOCK_ACQUIRE(curkse, &(kseg)->kg_lock)
270 #define KSE_SCHED_UNLOCK(curkse, kseg)  \
271         KSE_LOCK_RELEASE(curkse, &(kseg)->kg_lock)
272
273 /*
274  * Waiting queue manipulation macros (using pqe link):
275  */
276 #define KSE_WAITQ_REMOVE(kse, thrd) \
277 do { \
278         if (((thrd)->flags & THR_FLAGS_IN_WAITQ) != 0) { \
279                 TAILQ_REMOVE(&(kse)->k_schedq->sq_waitq, thrd, pqe); \
280                 (thrd)->flags &= ~THR_FLAGS_IN_WAITQ; \
281         } \
282 } while (0)
283 #define KSE_WAITQ_INSERT(kse, thrd)     kse_waitq_insert(thrd)
284 #define KSE_WAITQ_FIRST(kse)            TAILQ_FIRST(&(kse)->k_schedq->sq_waitq)
285
286 #define KSE_WAKEUP(kse)         kse_wakeup(&(kse)->k_kcb->kcb_kmbx)
287
288 /*
289  * TailQ initialization values.
290  */
291 #define TAILQ_INITIALIZER       { NULL, NULL }
292
293 /*
294  * lock initialization values.
295  */
296 #define LCK_INITIALIZER         { NULL, NULL, LCK_DEFAULT }
297
298 struct pthread_mutex {
299         /*
300          * Lock for accesses to this structure.
301          */
302         struct lock                     m_lock;
303         enum pthread_mutextype          m_type;
304         int                             m_protocol;
305         TAILQ_HEAD(mutex_head, pthread) m_queue;
306         struct pthread                  *m_owner;
307         long                            m_flags;
308         int                             m_count;
309         int                             m_refcount;
310
311         /*
312          * Used for priority inheritence and protection.
313          *
314          *   m_prio       - For priority inheritence, the highest active
315          *                  priority (threads locking the mutex inherit
316          *                  this priority).  For priority protection, the
317          *                  ceiling priority of this mutex.
318          *   m_saved_prio - mutex owners inherited priority before
319          *                  taking the mutex, restored when the owner
320          *                  unlocks the mutex.
321          */
322         int                             m_prio;
323         int                             m_saved_prio;
324
325         /*
326          * Link for list of all mutexes a thread currently owns.
327          */
328         TAILQ_ENTRY(pthread_mutex)      m_qe;
329 };
330
331 /*
332  * Flags for mutexes. 
333  */
334 #define MUTEX_FLAGS_PRIVATE     0x01
335 #define MUTEX_FLAGS_INITED      0x02
336 #define MUTEX_FLAGS_BUSY        0x04
337
338 /*
339  * Static mutex initialization values. 
340  */
341 #define PTHREAD_MUTEX_STATIC_INITIALIZER                                \
342         { LCK_INITIALIZER, PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE,    \
343         TAILQ_INITIALIZER, NULL, MUTEX_FLAGS_PRIVATE, 0, 0, 0, 0,       \
344         TAILQ_INITIALIZER }
345
346 struct pthread_mutex_attr {
347         enum pthread_mutextype  m_type;
348         int                     m_protocol;
349         int                     m_ceiling;
350         long                    m_flags;
351 };
352
353 #define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \
354         { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE }
355
356 /* 
357  * Condition variable definitions.
358  */
359 enum pthread_cond_type {
360         COND_TYPE_FAST,
361         COND_TYPE_MAX
362 };
363
364 struct pthread_cond {
365         /*
366          * Lock for accesses to this structure.
367          */
368         struct lock                     c_lock;
369         enum pthread_cond_type          c_type;
370         TAILQ_HEAD(cond_head, pthread)  c_queue;
371         struct pthread_mutex            *c_mutex;
372         long                            c_flags;
373         long                            c_seqno;
374 };
375
376 struct pthread_cond_attr {
377         enum pthread_cond_type  c_type;
378         long                    c_flags;
379 };
380
381 struct pthread_barrier {
382         pthread_mutex_t b_lock;
383         pthread_cond_t  b_cond;
384         int             b_count;
385         int             b_waiters;
386         int             b_generation;
387 };
388
389 struct pthread_barrierattr {
390         int             pshared;
391 };
392
393 struct pthread_spinlock {
394         volatile int    s_lock;
395         pthread_t       s_owner;
396 };
397
398 /*
399  * Flags for condition variables.
400  */
401 #define COND_FLAGS_PRIVATE      0x01
402 #define COND_FLAGS_INITED       0x02
403 #define COND_FLAGS_BUSY         0x04
404
405 /*
406  * Static cond initialization values. 
407  */
408 #define PTHREAD_COND_STATIC_INITIALIZER                         \
409         { LCK_INITIALIZER, COND_TYPE_FAST, TAILQ_INITIALIZER,   \
410         NULL, NULL, 0, 0 }
411
412 /*
413  * Cleanup definitions.
414  */
415 struct pthread_cleanup {
416         struct pthread_cleanup  *next;
417         void                    (*routine) ();
418         void                    *routine_arg;
419 };
420
421 struct pthread_atfork {
422         TAILQ_ENTRY(pthread_atfork) qe;
423         void (*prepare)(void);
424         void (*parent)(void);
425         void (*child)(void);
426 };
427
428 struct pthread_attr {
429         int     sched_policy;
430         int     sched_inherit;
431         int     sched_interval;
432         int     prio;
433         int     suspend;
434 #define THR_STACK_USER          0x100   /* 0xFF reserved for <pthread.h> */
435 #define THR_SIGNAL_THREAD       0x200   /* This is a signal thread */
436         int     flags;
437         void    *arg_attr;
438         void    (*cleanup_attr) ();
439         void    *stackaddr_attr;
440         size_t  stacksize_attr;
441         size_t  guardsize_attr;
442 };
443
444 /*
445  * Thread creation state attributes.
446  */
447 #define THR_CREATE_RUNNING              0
448 #define THR_CREATE_SUSPENDED            1
449
450 /*
451  * Miscellaneous definitions.
452  */
453 #define THR_STACK_DEFAULT                       65536
454
455 /*
456  * Maximum size of initial thread's stack.  This perhaps deserves to be larger
457  * than the stacks of other threads, since many applications are likely to run
458  * almost entirely on this stack.
459  */
460 #define THR_STACK_INITIAL                       0x100000
461
462 /*
463  * Define the different priority ranges.  All applications have thread
464  * priorities constrained within 0-31.  The threads library raises the
465  * priority when delivering signals in order to ensure that signal
466  * delivery happens (from the POSIX spec) "as soon as possible".
467  * In the future, the threads library will also be able to map specific
468  * threads into real-time (cooperating) processes or kernel threads.
469  * The RT and SIGNAL priorities will be used internally and added to
470  * thread base priorities so that the scheduling queue can handle both
471  * normal and RT priority threads with and without signal handling.
472  *
473  * The approach taken is that, within each class, signal delivery
474  * always has priority over thread execution.
475  */
476 #define THR_DEFAULT_PRIORITY                    15
477 #define THR_MIN_PRIORITY                        0
478 #define THR_MAX_PRIORITY                        31      /* 0x1F */
479 #define THR_SIGNAL_PRIORITY                     32      /* 0x20 */
480 #define THR_RT_PRIORITY                         64      /* 0x40 */
481 #define THR_FIRST_PRIORITY                      THR_MIN_PRIORITY
482 #define THR_LAST_PRIORITY       \
483         (THR_MAX_PRIORITY + THR_SIGNAL_PRIORITY + THR_RT_PRIORITY)
484 #define THR_BASE_PRIORITY(prio) ((prio) & THR_MAX_PRIORITY)
485
486 /*
487  * Clock resolution in microseconds.
488  */
489 #define CLOCK_RES_USEC                          10000
490
491 /*
492  * Time slice period in microseconds.
493  */
494 #define TIMESLICE_USEC                          20000
495
496 /*
497  * XXX - Define a thread-safe macro to get the current time of day
498  *       which is updated at regular intervals by something.
499  *
500  * For now, we just make the system call to get the time.
501  */
502 #define KSE_GET_TOD(curkse, tsp) \
503 do {                                                    \
504         *tsp = (curkse)->k_kcb->kcb_kmbx.km_timeofday;  \
505         if ((tsp)->tv_sec == 0)                         \
506                 clock_gettime(CLOCK_REALTIME, tsp);     \
507 } while (0)
508
509 struct pthread_rwlockattr {
510         int             pshared;
511 };
512
513 struct pthread_rwlock {
514         pthread_mutex_t lock;   /* monitor lock */
515         pthread_cond_t  read_signal;
516         pthread_cond_t  write_signal;
517         int             state;  /* 0 = idle  >0 = # of readers  -1 = writer */
518         int             blocked_writers;
519 };
520
521 /*
522  * Thread states.
523  */
524 enum pthread_state {
525         PS_RUNNING,
526         PS_LOCKWAIT,
527         PS_MUTEX_WAIT,
528         PS_COND_WAIT,
529         PS_SLEEP_WAIT,
530         PS_SIGSUSPEND,
531         PS_SIGWAIT,
532         PS_JOIN,
533         PS_SUSPENDED,
534         PS_DEAD,
535         PS_DEADLOCK,
536         PS_STATE_MAX
537 };
538
539 struct sigwait_data {
540         sigset_t        *waitset;
541         siginfo_t       *siginfo;       /* used to save siginfo for sigwaitinfo() */
542 };
543
544 union pthread_wait_data {
545         pthread_mutex_t mutex;
546         pthread_cond_t  cond;
547         struct lock     *lock;
548         struct sigwait_data *sigwait;
549 };
550
551 /*
552  * Define a continuation routine that can be used to perform a
553  * transfer of control:
554  */
555 typedef void    (*thread_continuation_t) (void *);
556
557 /*
558  * This stores a thread's state prior to running a signal handler.
559  * It is used when a signal is delivered to a thread blocked in
560  * userland.  If the signal handler returns normally, the thread's
561  * state is restored from here.
562  */
563 struct pthread_sigframe {
564         int                     psf_valid;
565         int                     psf_flags;
566         int                     psf_interrupted;
567         int                     psf_timeout;
568         int                     psf_signo;
569         enum pthread_state      psf_state;
570         union pthread_wait_data psf_wait_data;
571         struct timespec         psf_wakeup_time;
572         sigset_t                psf_sigset;
573         sigset_t                psf_sigmask;
574         int                     psf_seqno;
575 };
576
577 struct join_status {
578         struct pthread  *thread;
579         void            *ret;
580         int             error;
581 };
582
583 struct pthread_specific_elem {
584         const void      *data;
585         int             seqno;
586 };
587
588 struct pthread_key {
589         volatile int    allocated;
590         volatile int    count;
591         int             seqno;
592         void            (*destructor) (void *);
593 };
594
595 #define MAX_THR_LOCKLEVEL       5       
596 /*
597  * Thread structure.
598  */
599 struct pthread {
600         /* Thread control block */
601         struct tcb              *tcb;
602
603         /*
604          * Magic value to help recognize a valid thread structure
605          * from an invalid one:
606          */
607 #define THR_MAGIC               ((u_int32_t) 0xd09ba115)
608         u_int32_t               magic;
609         char                    *name;
610         u_int64_t               uniqueid; /* for gdb */
611
612         /* Queue entry for list of all threads: */
613         TAILQ_ENTRY(pthread)    tle;    /* link for all threads in process */
614         TAILQ_ENTRY(pthread)    kle;    /* link for all threads in KSE/KSEG */
615
616         /* Queue entry for GC lists: */
617         TAILQ_ENTRY(pthread)    gcle;
618
619         /* Hash queue entry */
620         LIST_ENTRY(pthread)     hle;
621
622         /*
623          * Lock for accesses to this thread structure.
624          */
625         struct lock             lock;
626         struct lockuser         lockusers[MAX_THR_LOCKLEVEL];
627         int                     locklevel;
628         kse_critical_t          critical[MAX_KSE_LOCKLEVEL];
629         struct kse              *kse;
630         struct kse_group        *kseg;
631
632         /*
633          * Thread start routine, argument, stack pointer and thread
634          * attributes.
635          */
636         void                    *(*start_routine)(void *);
637         void                    *arg;
638         struct pthread_attr     attr;
639
640         int                     active;         /* thread running */
641         int                     blocked;        /* thread blocked in kernel */
642         int                     need_switchout;
643
644         /*
645          * Used for tracking delivery of signal handlers.
646          */
647         struct pthread_sigframe *curframe;
648         siginfo_t               *siginfo;
649
650         /*
651          * Cancelability flags - the lower 2 bits are used by cancel
652          * definitions in pthread.h
653          */
654 #define THR_AT_CANCEL_POINT             0x0004
655 #define THR_CANCELLING                  0x0008
656 #define THR_CANCEL_NEEDED               0x0010
657         int                     cancelflags;
658
659         thread_continuation_t   continuation;
660
661         /*
662          * The thread's base and pending signal masks.  The active
663          * signal mask is stored in the thread's context (in mailbox).
664          */
665         sigset_t                sigmask;
666         sigset_t                sigpend;
667         sigset_t                *oldsigmask;
668         volatile int            check_pending;
669         int                     refcount;
670
671         /* Thread state: */
672         enum pthread_state      state;
673         volatile int            lock_switch;
674
675         /*
676          * Number of microseconds accumulated by this thread when
677          * time slicing is active.
678          */
679         long                    slice_usec;
680
681         /*
682          * Time to wake up thread. This is used for sleeping threads and
683          * for any operation which may time out (such as select).
684          */
685         struct timespec         wakeup_time;
686
687         /* TRUE if operation has timed out. */
688         int                     timeout;
689
690         /*
691          * Error variable used instead of errno. The function __error()
692          * returns a pointer to this. 
693          */
694         int                     error;
695
696         /*
697          * The joiner is the thread that is joining to this thread.  The
698          * join status keeps track of a join operation to another thread.
699          */
700         struct pthread          *joiner;
701         struct join_status      join_status;
702
703         /*
704          * The current thread can belong to only one scheduling queue at
705          * a time (ready or waiting queue).  It can also belong to:
706          *
707          *   o A queue of threads waiting for a mutex
708          *   o A queue of threads waiting for a condition variable
709          *
710          * It is possible for a thread to belong to more than one of the
711          * above queues if it is handling a signal.  A thread may only
712          * enter a mutex or condition variable queue when it is not
713          * being called from a signal handler.  If a thread is a member
714          * of one of these queues when a signal handler is invoked, it
715          * must be removed from the queue before invoking the handler
716          * and then added back to the queue after return from the handler.
717          *
718          * Use pqe for the scheduling queue link (both ready and waiting),
719          * sqe for synchronization (mutex, condition variable, and join)
720          * queue links, and qe for all other links.
721          */
722         TAILQ_ENTRY(pthread)    pqe;    /* priority, wait queues link */
723         TAILQ_ENTRY(pthread)    sqe;    /* synchronization queue link */
724
725         /* Wait data. */
726         union pthread_wait_data data;
727
728         /*
729          * Set to TRUE if a blocking operation was
730          * interrupted by a signal:
731          */
732         int                     interrupted;
733
734         /*
735          * Set to non-zero when this thread has entered a critical
736          * region.  We allow for recursive entries into critical regions.
737          */
738         int                     critical_count;
739
740         /*
741          * Set to TRUE if this thread should yield after leaving a
742          * critical region to check for signals, messages, etc.
743          */
744         int                     critical_yield;
745
746         int                     sflags;
747 #define THR_FLAGS_IN_SYNCQ      0x0001
748
749         /* Miscellaneous flags; only set with scheduling lock held. */
750         int                     flags;
751 #define THR_FLAGS_PRIVATE       0x0001
752 #define THR_FLAGS_IN_WAITQ      0x0002  /* in waiting queue using pqe link */
753 #define THR_FLAGS_IN_RUNQ       0x0004  /* in run queue using pqe link */
754 #define THR_FLAGS_EXITING       0x0008  /* thread is exiting */
755 #define THR_FLAGS_SUSPENDED     0x0010  /* thread is suspended */
756 #define THR_FLAGS_GC_SAFE       0x0020  /* thread safe for cleaning */
757 #define THR_FLAGS_IN_TDLIST     0x0040  /* thread in all thread list */
758 #define THR_FLAGS_IN_GCLIST     0x0080  /* thread in gc list */
759         /*
760          * Base priority is the user setable and retrievable priority
761          * of the thread.  It is only affected by explicit calls to
762          * set thread priority and upon thread creation via a thread
763          * attribute or default priority.
764          */
765         char                    base_priority;
766
767         /*
768          * Inherited priority is the priority a thread inherits by
769          * taking a priority inheritence or protection mutex.  It
770          * is not affected by base priority changes.  Inherited
771          * priority defaults to and remains 0 until a mutex is taken
772          * that is being waited on by any other thread whose priority
773          * is non-zero.
774          */
775         char                    inherited_priority;
776
777         /*
778          * Active priority is always the maximum of the threads base
779          * priority and inherited priority.  When there is a change
780          * in either the base or inherited priority, the active
781          * priority must be recalculated.
782          */
783         char                    active_priority;
784
785         /* Number of priority ceiling or protection mutexes owned. */
786         int                     priority_mutex_count;
787
788         /* Number rwlocks rdlocks held. */
789         int                     rdlock_count;
790
791         /*
792          * Queue of currently owned mutexes.
793          */
794         TAILQ_HEAD(, pthread_mutex)     mutexq;
795
796         void                            *ret;
797         struct pthread_specific_elem    *specific;
798         int                             specific_data_count;
799
800         /* Alternative stack for sigaltstack() */
801         stack_t                         sigstk;
802
803         /*
804          * Current locks bitmap for rtld.
805          */
806         int     rtld_bits;
807
808         /* Cleanup handlers Link List */
809         struct pthread_cleanup *cleanup;
810         char                    *fname; /* Ptr to source file name  */
811         int                     lineno; /* Source line number.      */
812 };
813
814 /*
815  * Critical regions can also be detected by looking at the threads
816  * current lock level.  Ensure these macros increment and decrement
817  * the lock levels such that locks can not be held with a lock level
818  * of 0.
819  */
820 #define THR_IN_CRITICAL(thrd)                                   \
821         (((thrd)->locklevel > 0) ||                             \
822         ((thrd)->critical_count > 0))
823
824 #define THR_YIELD_CHECK(thrd)                                   \
825 do {                                                            \
826         if (!THR_IN_CRITICAL(thrd)) {                           \
827                 if (__predict_false(_libkse_debug))             \
828                         _thr_debug_check_yield(thrd);           \
829                 if ((thrd)->critical_yield != 0)                \
830                         _thr_sched_switch(thrd);                \
831                 if ((thrd)->check_pending != 0)                 \
832                         _thr_sig_check_pending(thrd);           \
833         }                                                       \
834 } while (0)
835
836 #define THR_LOCK_ACQUIRE(thrd, lck)                             \
837 do {                                                            \
838         if ((thrd)->locklevel < MAX_THR_LOCKLEVEL) {            \
839                 THR_DEACTIVATE_LAST_LOCK(thrd);                 \
840                 (thrd)->locklevel++;                            \
841                 _lock_acquire((lck),                            \
842                     &(thrd)->lockusers[(thrd)->locklevel - 1],  \
843                     (thrd)->active_priority);                   \
844         } else                                                  \
845                 PANIC("Exceeded maximum lock level");           \
846 } while (0)
847
848 #define THR_LOCK_RELEASE(thrd, lck)                             \
849 do {                                                            \
850         if ((thrd)->locklevel > 0) {                            \
851                 _lock_release((lck),                            \
852                     &(thrd)->lockusers[(thrd)->locklevel - 1]); \
853                 (thrd)->locklevel--;                            \
854                 THR_ACTIVATE_LAST_LOCK(thrd);                   \
855                 if ((thrd)->locklevel == 0)                     \
856                         THR_YIELD_CHECK(thrd);                  \
857         }                                                       \
858 } while (0)
859
860 #define THR_ACTIVATE_LAST_LOCK(thrd)                                    \
861 do {                                                                    \
862         if ((thrd)->locklevel > 0)                                      \
863                 _lockuser_setactive(                                    \
864                     &(thrd)->lockusers[(thrd)->locklevel - 1], 1);      \
865 } while (0)
866
867 #define THR_DEACTIVATE_LAST_LOCK(thrd)                                  \
868 do {                                                                    \
869         if ((thrd)->locklevel > 0)                                      \
870                 _lockuser_setactive(                                    \
871                     &(thrd)->lockusers[(thrd)->locklevel - 1], 0);      \
872 } while (0)
873
874 /*
875  * For now, threads will have their own lock separate from their
876  * KSE scheduling lock.
877  */
878 #define THR_LOCK(thr)                   THR_LOCK_ACQUIRE(thr, &(thr)->lock)
879 #define THR_UNLOCK(thr)                 THR_LOCK_RELEASE(thr, &(thr)->lock)
880 #define THR_THREAD_LOCK(curthrd, thr)   THR_LOCK_ACQUIRE(curthrd, &(thr)->lock)
881 #define THR_THREAD_UNLOCK(curthrd, thr) THR_LOCK_RELEASE(curthrd, &(thr)->lock)
882
883 /*
884  * Priority queue manipulation macros (using pqe link).  We use
885  * the thread's kseg link instead of the kse link because a thread
886  * does not (currently) have a statically assigned kse.
887  */
888 #define THR_RUNQ_INSERT_HEAD(thrd)      \
889         _pq_insert_head(&(thrd)->kseg->kg_schedq.sq_runq, thrd)
890 #define THR_RUNQ_INSERT_TAIL(thrd)      \
891         _pq_insert_tail(&(thrd)->kseg->kg_schedq.sq_runq, thrd)
892 #define THR_RUNQ_REMOVE(thrd)           \
893         _pq_remove(&(thrd)->kseg->kg_schedq.sq_runq, thrd)
894
895 /*
896  * Macros to insert/remove threads to the all thread list and
897  * the gc list.
898  */
899 #define THR_LIST_ADD(thrd) do {                                 \
900         if (((thrd)->flags & THR_FLAGS_IN_TDLIST) == 0) {       \
901                 TAILQ_INSERT_HEAD(&_thread_list, thrd, tle);    \
902                 _thr_hash_add(thrd);                            \
903                 (thrd)->flags |= THR_FLAGS_IN_TDLIST;           \
904         }                                                       \
905 } while (0)
906 #define THR_LIST_REMOVE(thrd) do {                              \
907         if (((thrd)->flags & THR_FLAGS_IN_TDLIST) != 0) {       \
908                 TAILQ_REMOVE(&_thread_list, thrd, tle);         \
909                 _thr_hash_remove(thrd);                         \
910                 (thrd)->flags &= ~THR_FLAGS_IN_TDLIST;          \
911         }                                                       \
912 } while (0)
913 #define THR_GCLIST_ADD(thrd) do {                               \
914         if (((thrd)->flags & THR_FLAGS_IN_GCLIST) == 0) {       \
915                 TAILQ_INSERT_HEAD(&_thread_gc_list, thrd, gcle);\
916                 (thrd)->flags |= THR_FLAGS_IN_GCLIST;           \
917                 _gc_count++;                                    \
918         }                                                       \
919 } while (0)
920 #define THR_GCLIST_REMOVE(thrd) do {                            \
921         if (((thrd)->flags & THR_FLAGS_IN_GCLIST) != 0) {       \
922                 TAILQ_REMOVE(&_thread_gc_list, thrd, gcle);     \
923                 (thrd)->flags &= ~THR_FLAGS_IN_GCLIST;          \
924                 _gc_count--;                                    \
925         }                                                       \
926 } while (0)
927
928 #define GC_NEEDED()     (atomic_load_acq_int(&_gc_count) >= 5)
929
930 /*
931  * Locking the scheduling queue for another thread uses that thread's
932  * KSEG lock.
933  */
934 #define THR_SCHED_LOCK(curthr, thr) do {                \
935         (curthr)->critical[(curthr)->locklevel] = _kse_critical_enter(); \
936         (curthr)->locklevel++;                          \
937         KSE_SCHED_LOCK((curthr)->kse, (thr)->kseg);     \
938 } while (0)
939
940 #define THR_SCHED_UNLOCK(curthr, thr) do {              \
941         KSE_SCHED_UNLOCK((curthr)->kse, (thr)->kseg);   \
942         (curthr)->locklevel--;                          \
943         _kse_critical_leave((curthr)->critical[(curthr)->locklevel]); \
944 } while (0)
945
946 /* Take the scheduling lock with the intent to call the scheduler. */
947 #define THR_LOCK_SWITCH(curthr) do {                    \
948         (void)_kse_critical_enter();                    \
949         KSE_SCHED_LOCK((curthr)->kse, (curthr)->kseg);  \
950 } while (0)
951 #define THR_UNLOCK_SWITCH(curthr) do {                  \
952         KSE_SCHED_UNLOCK((curthr)->kse, (curthr)->kseg);\
953 } while (0)
954
955 #define THR_CRITICAL_ENTER(thr)         (thr)->critical_count++
956 #define THR_CRITICAL_LEAVE(thr) do {            \
957         (thr)->critical_count--;                \
958         if (((thr)->critical_yield != 0) &&     \
959             ((thr)->critical_count == 0)) {     \
960                 (thr)->critical_yield = 0;      \
961                 _thr_sched_switch(thr);         \
962         }                                       \
963 } while (0)
964
965 #define THR_IS_ACTIVE(thrd) \
966         ((thrd)->kse != NULL) && ((thrd)->kse->k_curthread == (thrd))
967
968 #define THR_IN_SYNCQ(thrd)      (((thrd)->sflags & THR_FLAGS_IN_SYNCQ) != 0)
969
970 #define THR_IS_SUSPENDED(thrd) \
971         (((thrd)->state == PS_SUSPENDED) || \
972         (((thrd)->flags & THR_FLAGS_SUSPENDED) != 0))
973 #define THR_IS_EXITING(thrd)    (((thrd)->flags & THR_FLAGS_EXITING) != 0)
974 #define DBG_CAN_RUN(thrd) (((thrd)->tcb->tcb_tmbx.tm_dflags & \
975         TMDF_DONOTRUNUSER) == 0)
976
977 extern int __isthreaded;
978
979 static inline int
980 _kse_isthreaded(void)
981 {
982         return (__isthreaded != 0);
983 }
984
985 /*
986  * Global variables for the pthread kernel.
987  */
988
989 SCLASS void             *_usrstack      SCLASS_PRESET(NULL);
990 SCLASS struct kse       *_kse_initial   SCLASS_PRESET(NULL);
991 SCLASS struct pthread   *_thr_initial   SCLASS_PRESET(NULL);
992 /* For debugger */
993 SCLASS int              _libkse_debug           SCLASS_PRESET(0);
994 SCLASS int              _thread_activated       SCLASS_PRESET(0);
995
996 /* List of all threads: */
997 SCLASS TAILQ_HEAD(, pthread)    _thread_list
998     SCLASS_PRESET(TAILQ_HEAD_INITIALIZER(_thread_list));
999
1000 /* List of threads needing GC: */
1001 SCLASS TAILQ_HEAD(, pthread)    _thread_gc_list
1002     SCLASS_PRESET(TAILQ_HEAD_INITIALIZER(_thread_gc_list));
1003
1004 SCLASS int      _thread_active_threads  SCLASS_PRESET(1);
1005
1006 SCLASS TAILQ_HEAD(atfork_head, pthread_atfork) _thr_atfork_list;
1007 SCLASS pthread_mutex_t          _thr_atfork_mutex;
1008
1009 /* Default thread attributes: */
1010 SCLASS struct pthread_attr _pthread_attr_default
1011     SCLASS_PRESET({
1012         SCHED_RR, 0, TIMESLICE_USEC, THR_DEFAULT_PRIORITY,
1013         THR_CREATE_RUNNING,     PTHREAD_CREATE_JOINABLE, NULL,
1014         NULL, NULL, THR_STACK_DEFAULT, /* guardsize */0
1015     });
1016
1017 /* Default mutex attributes: */
1018 SCLASS struct pthread_mutex_attr _pthread_mutexattr_default
1019     SCLASS_PRESET({PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, 0 });
1020
1021 /* Default condition variable attributes: */
1022 SCLASS struct pthread_cond_attr _pthread_condattr_default
1023     SCLASS_PRESET({COND_TYPE_FAST, 0});
1024
1025 /* Clock resolution in usec.    */
1026 SCLASS int              _clock_res_usec         SCLASS_PRESET(CLOCK_RES_USEC);
1027
1028 /* Array of signal actions for this process: */
1029 SCLASS struct sigaction _thread_sigact[_SIG_MAXSIG];
1030
1031 /*
1032  * Lock for above count of dummy handlers and for the process signal
1033  * mask and pending signal sets.
1034  */
1035 SCLASS struct lock      _thread_signal_lock;
1036
1037 /* Pending signals and mask for this process: */
1038 SCLASS sigset_t         _thr_proc_sigpending;
1039 SCLASS siginfo_t        _thr_proc_siginfo[_SIG_MAXSIG];
1040
1041 SCLASS pid_t            _thr_pid                SCLASS_PRESET(0);
1042
1043 /* Garbage collector lock. */
1044 SCLASS struct lock      _gc_lock;
1045 SCLASS int              _gc_check               SCLASS_PRESET(0);
1046 SCLASS int              _gc_count               SCLASS_PRESET(0);
1047
1048 SCLASS struct lock      _mutex_static_lock;
1049 SCLASS struct lock      _rwlock_static_lock;
1050 SCLASS struct lock      _keytable_lock;
1051 SCLASS struct lock      _thread_list_lock;
1052 SCLASS int              _thr_guard_default;
1053 SCLASS int              _thr_page_size;
1054 SCLASS pthread_t        _thr_sig_daemon;
1055 SCLASS int              _thr_debug_flags        SCLASS_PRESET(0);
1056
1057 /* Undefine the storage class and preset specifiers: */
1058 #undef  SCLASS
1059 #undef  SCLASS_PRESET
1060
1061
1062 /*
1063  * Function prototype definitions.
1064  */
1065 __BEGIN_DECLS
1066 int     _cond_reinit(pthread_cond_t *);
1067 void    _cond_wait_backout(struct pthread *);
1068 struct kse *_kse_alloc(struct pthread *, int sys_scope);
1069 kse_critical_t _kse_critical_enter(void);
1070 void    _kse_critical_leave(kse_critical_t);
1071 int     _kse_in_critical(void);
1072 void    _kse_free(struct pthread *, struct kse *);
1073 void    _kse_init();
1074 struct kse_group *_kseg_alloc(struct pthread *);
1075 void    _kse_lock_wait(struct lock *, struct lockuser *lu);
1076 void    _kse_lock_wakeup(struct lock *, struct lockuser *lu);
1077 void    _kse_single_thread(struct pthread *);
1078 int     _kse_setthreaded(int);
1079 void    _kseg_free(struct kse_group *);
1080 int     _mutex_cv_lock(pthread_mutex_t *);
1081 int     _mutex_cv_unlock(pthread_mutex_t *);
1082 void    _mutex_lock_backout(struct pthread *);
1083 void    _mutex_notify_priochange(struct pthread *, struct pthread *, int);
1084 int     _mutex_reinit(struct pthread_mutex *);
1085 void    _mutex_unlock_private(struct pthread *);
1086 void    _libpthread_init(struct pthread *);
1087 int     _pq_alloc(struct pq_queue *, int, int);
1088 void    _pq_free(struct pq_queue *);
1089 int     _pq_init(struct pq_queue *);
1090 void    _pq_remove(struct pq_queue *pq, struct pthread *);
1091 void    _pq_insert_head(struct pq_queue *pq, struct pthread *);
1092 void    _pq_insert_tail(struct pq_queue *pq, struct pthread *);
1093 struct pthread *_pq_first(struct pq_queue *pq);
1094 struct pthread *_pq_first_debug(struct pq_queue *pq);
1095 void    *_pthread_getspecific(pthread_key_t);
1096 int     _pthread_key_create(pthread_key_t *, void (*) (void *));
1097 int     _pthread_key_delete(pthread_key_t);
1098 int     _pthread_mutex_destroy(pthread_mutex_t *);
1099 int     _pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
1100 int     _pthread_mutex_lock(pthread_mutex_t *);
1101 int     _pthread_mutex_trylock(pthread_mutex_t *);
1102 int     _pthread_mutex_unlock(pthread_mutex_t *);
1103 int     _pthread_mutexattr_init(pthread_mutexattr_t *);
1104 int     _pthread_mutexattr_destroy(pthread_mutexattr_t *);
1105 int     _pthread_mutexattr_settype(pthread_mutexattr_t *, int);
1106 int     _pthread_once(pthread_once_t *, void (*) (void));
1107 int     _pthread_rwlock_init(pthread_rwlock_t *, const pthread_rwlockattr_t *);
1108 int     _pthread_rwlock_destroy (pthread_rwlock_t *);
1109 struct pthread *_pthread_self(void);
1110 int     _pthread_setspecific(pthread_key_t, const void *);
1111 void    _pthread_yield(void);
1112 void    _pthread_cleanup_push(void (*routine) (void *), void *routine_arg);
1113 void    _pthread_cleanup_pop(int execute);
1114 struct pthread *_thr_alloc(struct pthread *);
1115 void    _thr_exit(char *, int, char *);
1116 void    _thr_exit_cleanup(void);
1117 void    _thr_lock_wait(struct lock *lock, struct lockuser *lu);
1118 void    _thr_lock_wakeup(struct lock *lock, struct lockuser *lu);
1119 void    _thr_mutex_reinit(pthread_mutex_t *);
1120 int     _thr_ref_add(struct pthread *, struct pthread *, int);
1121 void    _thr_ref_delete(struct pthread *, struct pthread *);
1122 void    _thr_rtld_init(void);
1123 void    _thr_rtld_fini(void);
1124 int     _thr_schedule_add(struct pthread *, struct pthread *);
1125 void    _thr_schedule_remove(struct pthread *, struct pthread *);
1126 void    _thr_setrunnable(struct pthread *curthread, struct pthread *thread);
1127 struct kse_mailbox *_thr_setrunnable_unlocked(struct pthread *thread);
1128 struct kse_mailbox *_thr_sig_add(struct pthread *, int, siginfo_t *);
1129 void    _thr_sig_dispatch(struct kse *, int, siginfo_t *);
1130 int     _thr_stack_alloc(struct pthread_attr *);
1131 void    _thr_stack_free(struct pthread_attr *);
1132 void    _thr_exit_cleanup(void);
1133 void    _thr_free(struct pthread *, struct pthread *);
1134 void    _thr_gc(struct pthread *);
1135 void    _thr_panic_exit(char *, int, char *);
1136 void    _thread_cleanupspecific(void);
1137 void    _thread_dump_info(void);
1138 void    _thread_printf(int, const char *, ...);
1139 void    _thr_sched_switch(struct pthread *);
1140 void    _thr_sched_switch_unlocked(struct pthread *);
1141 void    _thr_set_timeout(const struct timespec *);
1142 void    _thr_seterrno(struct pthread *, int);
1143 void    _thr_sig_handler(int, siginfo_t *, ucontext_t *);
1144 void    _thr_sig_check_pending(struct pthread *);
1145 void    _thr_sig_rundown(struct pthread *, ucontext_t *,
1146             struct pthread_sigframe *);
1147 void    _thr_sig_send(struct pthread *pthread, int sig);
1148 void    _thr_sigframe_restore(struct pthread *thread, struct pthread_sigframe *psf);
1149 void    _thr_spinlock_init(void);
1150 void    _thr_cancel_enter(struct pthread *);
1151 void    _thr_cancel_leave(struct pthread *, int);
1152 int     _thr_setconcurrency(int new_level);
1153 int     _thr_setmaxconcurrency(void);
1154 void    _thr_critical_enter(struct pthread *);
1155 void    _thr_critical_leave(struct pthread *);
1156 int     _thr_start_sig_daemon(void);
1157 int     _thr_getprocsig(int sig, siginfo_t *siginfo);
1158 int     _thr_getprocsig_unlocked(int sig, siginfo_t *siginfo);
1159 void    _thr_signal_init(void);
1160 void    _thr_signal_deinit(void);
1161 void    _thr_hash_add(struct pthread *);
1162 void    _thr_hash_remove(struct pthread *);
1163 struct pthread *_thr_hash_find(struct pthread *);
1164 void    _thr_finish_cancellation(void *arg);
1165 int     _thr_sigonstack(void *sp);
1166 void    _thr_debug_check_yield(struct pthread *);
1167
1168 /*
1169  * Aliases for _pthread functions. Should be called instead of
1170  * originals if PLT replocation is unwanted at runtme.
1171  */
1172 int     _thr_cond_broadcast(pthread_cond_t *);
1173 int     _thr_cond_signal(pthread_cond_t *);
1174 int     _thr_cond_wait(pthread_cond_t *, pthread_mutex_t *);
1175 int     _thr_mutex_lock(pthread_mutex_t *);
1176 int     _thr_mutex_unlock(pthread_mutex_t *);
1177 int     _thr_rwlock_rdlock (pthread_rwlock_t *);
1178 int     _thr_rwlock_wrlock (pthread_rwlock_t *);
1179 int     _thr_rwlock_unlock (pthread_rwlock_t *);
1180
1181 /* #include <sys/aio.h> */
1182 #ifdef _SYS_AIO_H_
1183 int     __sys_aio_suspend(const struct aiocb * const[], int, const struct timespec *);
1184 #endif
1185
1186 /* #include <fcntl.h> */
1187 #ifdef  _SYS_FCNTL_H_
1188 int     __sys_fcntl(int, int, ...);
1189 int     __sys_open(const char *, int, ...);
1190 #endif
1191
1192 /* #include <sys/ioctl.h> */
1193 #ifdef _SYS_IOCTL_H_
1194 int     __sys_ioctl(int, unsigned long, ...);
1195 #endif
1196
1197 /* #inclde <sched.h> */
1198 #ifdef  _SCHED_H_
1199 int     __sys_sched_yield(void);
1200 #endif
1201
1202 /* #include <signal.h> */
1203 #ifdef _SIGNAL_H_
1204 int     __sys_kill(pid_t, int);
1205 int     __sys_sigaction(int, const struct sigaction *, struct sigaction *);
1206 int     __sys_sigpending(sigset_t *);
1207 int     __sys_sigprocmask(int, const sigset_t *, sigset_t *);
1208 int     __sys_sigsuspend(const sigset_t *);
1209 int     __sys_sigreturn(ucontext_t *);
1210 int     __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *);
1211 #endif
1212
1213 /* #include <sys/socket.h> */
1214 #ifdef _SYS_SOCKET_H_
1215 int     __sys_accept(int, struct sockaddr *, socklen_t *);
1216 int     __sys_connect(int, const struct sockaddr *, socklen_t);
1217 int     __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *,
1218             off_t *, int);
1219 #endif
1220
1221 /* #include <sys/uio.h> */
1222 #ifdef  _SYS_UIO_H_
1223 ssize_t __sys_readv(int, const struct iovec *, int);
1224 ssize_t __sys_writev(int, const struct iovec *, int);
1225 #endif
1226
1227 /* #include <time.h> */
1228 #ifdef  _TIME_H_
1229 int     __sys_nanosleep(const struct timespec *, struct timespec *);
1230 #endif
1231
1232 /* #include <unistd.h> */
1233 #ifdef  _UNISTD_H_
1234 int     __sys_close(int);
1235 int     __sys_execve(const char *, char * const *, char * const *);
1236 int     __sys_fork(void);
1237 int     __sys_fsync(int);
1238 pid_t   __sys_getpid(void);
1239 int     __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
1240 ssize_t __sys_read(int, void *, size_t);
1241 ssize_t __sys_write(int, const void *, size_t);
1242 void    __sys_exit(int);
1243 int     __sys_sigwait(const sigset_t *, int *);
1244 int     __sys_sigtimedwait(sigset_t *, siginfo_t *, struct timespec *);
1245 #endif
1246
1247 /* #include <poll.h> */
1248 #ifdef _SYS_POLL_H_
1249 int     __sys_poll(struct pollfd *, unsigned, int);
1250 #endif
1251
1252 /* #include <sys/mman.h> */
1253 #ifdef _SYS_MMAN_H_
1254 int     __sys_msync(void *, size_t, int);
1255 #endif
1256
1257 #endif  /* !_THR_PRIVATE_H */