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