WARNS level 4 cleanup.
[dragonfly.git] / lib / libthread_xu / 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: src/lib/libpthread/thread/thr_private.h,v 1.120 2004/11/01 10:49:34 davidxu Exp $
35  * $DragonFly: src/lib/libthread_xu/thread/thr_private.h,v 1.12 2006/04/06 13:03:09 davidxu Exp $
36  */
37
38 #ifndef _THR_PRIVATE_H
39 #define _THR_PRIVATE_H
40
41 /*
42  * Include files.
43  */
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #include <sys/cdefs.h>
47 #include <sys/queue.h>
48 #include <machine/atomic.h>
49 #include <errno.h>
50 #include <limits.h>
51 #include <signal.h>
52 #include <stdio.h>
53 #include <sched.h>
54 #include <unistd.h>
55 #include <pthread.h>
56 #include <pthread_np.h>
57
58 #include "pthread_md.h"
59 #include "thr_umtx.h"
60 #include "thread_db.h"
61
62 /* Signal to do cancellation */
63 #define SIGCANCEL               32
64
65 /*
66  * Kernel fatal error handler macro.
67  */
68 #define PANIC(string)           _thread_exit(__FILE__,__LINE__,string)
69
70 /* Output debug messages like this: */
71 #define stdout_debug(args...)   _thread_printf(STDOUT_FILENO, ##args)
72 #define stderr_debug(args...)   _thread_printf(STDOUT_FILENO, ##args)
73
74 #ifdef __DragonFly__
75 #define __predict_true(exp)     (exp)
76 #define __predict_false(exp)    (exp)
77 #endif
78
79 #ifdef _PTHREADS_INVARIANTS
80 #define THR_ASSERT(cond, msg) do {      \
81         if (__predict_false(!(cond)))   \
82                 PANIC(msg);             \
83 } while (0)
84 #else
85 #define THR_ASSERT(cond, msg)
86 #endif
87
88 #ifdef PIC
89 #define STATIC_LIB_REQUIRE(name)
90 #else
91 #define STATIC_LIB_REQUIRE(name)        __asm(".globl " #name)
92 #endif
93
94 TAILQ_HEAD(thread_head, pthread)        thread_head;
95 TAILQ_HEAD(atfork_head, pthread_atfork) atfork_head;
96
97 #define TIMESPEC_ADD(dst, src, val)                             \
98         do {                                                    \
99                 (dst)->tv_sec = (src)->tv_sec + (val)->tv_sec;  \
100                 (dst)->tv_nsec = (src)->tv_nsec + (val)->tv_nsec; \
101                 if ((dst)->tv_nsec >= 1000000000) {             \
102                         (dst)->tv_sec++;                        \
103                         (dst)->tv_nsec -= 1000000000;           \
104                 }                                               \
105         } while (0)
106
107 #define TIMESPEC_SUB(dst, src, val)                             \
108         do {                                                    \
109                 (dst)->tv_sec = (src)->tv_sec - (val)->tv_sec;  \
110                 (dst)->tv_nsec = (src)->tv_nsec - (val)->tv_nsec; \
111                 if ((dst)->tv_nsec < 0) {                       \
112                         (dst)->tv_sec--;                        \
113                         (dst)->tv_nsec += 1000000000;           \
114                 }                                               \
115         } while (0)
116
117 struct pthread_mutex {
118         /*
119          * Lock for accesses to this structure.
120          */
121         volatile umtx_t                 m_lock;
122         enum pthread_mutextype          m_type;
123         int                             m_protocol;
124         TAILQ_HEAD(mutex_head, pthread) m_queue;
125         struct pthread                  *m_owner;
126         long                            m_flags;
127         int                             m_count;
128         int                             m_refcount;
129
130         /*
131          * Used for priority inheritence and protection.
132          *
133          *   m_prio       - For priority inheritence, the highest active
134          *                  priority (threads locking the mutex inherit
135          *                  this priority).  For priority protection, the
136          *                  ceiling priority of this mutex.
137          *   m_saved_prio - mutex owners inherited priority before
138          *                  taking the mutex, restored when the owner
139          *                  unlocks the mutex.
140          */
141         int                             m_prio;
142         int                             m_saved_prio;
143
144         /*
145          * Link for list of all mutexes a thread currently owns.
146          */
147         TAILQ_ENTRY(pthread_mutex)      m_qe;
148 };
149
150 #define TAILQ_INITIALIZER       { NULL, NULL }
151
152 #define PTHREAD_MUTEX_STATIC_INITIALIZER   \
153         {0, PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, TAILQ_INITIALIZER, \
154         NULL, { NULL }, MUTEX_FLAGS_PRIVATE, 0, 0, 0, TAILQ_INITIALIZER }
155 /*
156  * Flags for mutexes. 
157  */
158 #define MUTEX_FLAGS_PRIVATE     0x01
159 #define MUTEX_FLAGS_INITED      0x02
160 #define MUTEX_FLAGS_BUSY        0x04
161
162 struct pthread_mutex_attr {
163         enum pthread_mutextype  m_type;
164         int                     m_protocol;
165         int                     m_ceiling;
166         long                    m_flags;
167 };
168
169 #define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \
170         { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE }
171
172 struct pthread_cond {
173         /*
174          * Lock for accesses to this structure.
175          */
176         volatile umtx_t c_lock;
177         volatile umtx_t c_seqno;
178         volatile int    c_waiters;
179         volatile int    c_wakeups;
180         int             c_pshared;
181         int             c_clockid;
182 };
183
184 struct pthread_cond_attr {
185         int             c_pshared;
186         int             c_clockid;
187 };
188
189 struct pthread_barrier {
190         volatile umtx_t b_lock;
191         volatile umtx_t b_cycle;
192         volatile int    b_count;
193         volatile int    b_waiters;
194 };
195
196 struct pthread_barrierattr {
197         int             pshared;
198 };
199
200 struct pthread_spinlock {
201         volatile umtx_t s_lock;
202 };
203
204 /*
205  * Flags for condition variables.
206  */
207 #define COND_FLAGS_PRIVATE      0x01
208 #define COND_FLAGS_INITED       0x02
209 #define COND_FLAGS_BUSY         0x04
210
211 /*
212  * Cleanup definitions.
213  */
214 struct pthread_cleanup {
215         struct pthread_cleanup  *next;
216         void                    (*routine)(void *);
217         void                    *routine_arg;
218         int                     onstack;
219 };
220
221 #define THR_CLEANUP_PUSH(td, func, arg) {               \
222         struct pthread_cleanup __cup;                   \
223                                                         \
224         __cup.routine = func;                           \
225         __cup.routine_arg = arg;                        \
226         __cup.onstack = 1;                              \
227         __cup.next = (td)->cleanup;                     \
228         (td)->cleanup = &__cup;
229
230 #define THR_CLEANUP_POP(td, exec)                       \
231         (td)->cleanup = __cup.next;                     \
232         if ((exec) != 0)                                \
233                 __cup.routine(__cup.routine_arg);       \
234 }
235
236 struct pthread_atfork {
237         TAILQ_ENTRY(pthread_atfork) qe;
238         void (*prepare)(void);
239         void (*parent)(void);
240         void (*child)(void);
241 };
242
243 struct pthread_attr {
244         int     sched_policy;
245         int     sched_inherit;
246         int     sched_interval;
247         int     prio;
248         int     suspend;
249 #define THR_STACK_USER          0x100   /* 0xFF reserved for <pthread.h> */
250         int     flags;
251         void    *arg_attr;
252         void    *stackaddr_attr;
253         size_t  stacksize_attr;
254         size_t  guardsize_attr;
255 };
256
257 /*
258  * Thread creation state attributes.
259  */
260 #define THR_CREATE_RUNNING              0
261 #define THR_CREATE_SUSPENDED            1
262
263 /*
264  * Miscellaneous definitions.
265  */
266 #define THR_STACK_DEFAULT               (sizeof(void *) / 4 * 1024 * 1024)
267
268 /*
269  * Maximum size of initial thread's stack.  This perhaps deserves to be larger
270  * than the stacks of other threads, since many applications are likely to run
271  * almost entirely on this stack.
272  */
273 #define THR_STACK_INITIAL               (THR_STACK_DEFAULT * 2)
274
275 /*
276  * Define the different priority ranges.  All applications have thread
277  * priorities constrained within 0-31.  The threads library raises the
278  * priority when delivering signals in order to ensure that signal
279  * delivery happens (from the POSIX spec) "as soon as possible".
280  * In the future, the threads library will also be able to map specific
281  * threads into real-time (cooperating) processes or kernel threads.
282  * The RT and SIGNAL priorities will be used internally and added to
283  * thread base priorities so that the scheduling queue can handle both
284  * normal and RT priority threads with and without signal handling.
285  *
286  * The approach taken is that, within each class, signal delivery
287  * always has priority over thread execution.
288  */
289 #define THR_DEFAULT_PRIORITY                    15
290 #define THR_MIN_PRIORITY                        0
291 #define THR_MAX_PRIORITY                        31      /* 0x1F */
292 #define THR_SIGNAL_PRIORITY                     32      /* 0x20 */
293 #define THR_RT_PRIORITY                         64      /* 0x40 */
294 #define THR_FIRST_PRIORITY                      THR_MIN_PRIORITY
295 #define THR_LAST_PRIORITY       \
296         (THR_MAX_PRIORITY + THR_SIGNAL_PRIORITY + THR_RT_PRIORITY)
297 #define THR_BASE_PRIORITY(prio) ((prio) & THR_MAX_PRIORITY)
298
299 /*
300  * Time slice period in microseconds.
301  */
302 #define TIMESLICE_USEC                          20000
303
304 struct pthread_rwlockattr {
305         int             pshared;
306 };
307
308 struct pthread_rwlock {
309         pthread_mutex_t lock;   /* monitor lock */
310         pthread_cond_t  read_signal;
311         pthread_cond_t  write_signal;
312         int             state;  /* 0 = idle  >0 = # of readers  -1 = writer */
313         int             blocked_writers;
314 };
315
316 /*
317  * Thread states.
318  */
319 enum pthread_state {
320         PS_RUNNING,
321         PS_DEAD
322 };
323
324 union pthread_wait_data {
325         pthread_mutex_t mutex;
326 };
327
328 struct pthread_specific_elem {
329         const void      *data;
330         int             seqno;
331 };
332
333 struct pthread_key {
334         volatile int    allocated;
335         volatile int    count;
336         int             seqno;
337         void            (*destructor)(void *);
338 };
339
340 /*
341  * Thread structure.
342  */
343 struct pthread {
344         /*
345          * Magic value to help recognize a valid thread structure
346          * from an invalid one:
347          */
348 #define THR_MAGIC               ((u_int32_t) 0xd09ba115)
349         u_int32_t               magic;
350         char                    *name;
351         u_int64_t               uniqueid; /* for gdb */
352
353         /*
354          * Lock for accesses to this thread structure.
355          */
356         umtx_t                  lock;
357
358         /* Thread is terminated in kernel, written by kernel. */
359         long                    terminated;
360
361         /* Kernel thread id. */
362         long                    tid;
363
364         /* Internal condition variable cycle number. */
365         umtx_t                  cycle;
366
367         /* How many low level locks the thread held. */
368         int                     locklevel;
369
370         /*
371          * Set to non-zero when this thread has entered a critical
372          * region.  We allow for recursive entries into critical regions.
373          */
374         int                     critical_count;
375
376         /* Signal blocked counter. */
377         int                     sigblock;
378
379         /* Queue entry for list of all threads. */
380         TAILQ_ENTRY(pthread)    tle;    /* link for all threads in process */
381
382         /* Queue entry for GC lists. */
383         TAILQ_ENTRY(pthread)    gcle;
384
385         /* Hash queue entry. */
386         LIST_ENTRY(pthread)     hle;
387
388         /* Threads reference count. */
389         int                     refcount;
390
391         /*
392          * Thread start routine, argument, stack pointer and thread
393          * attributes.
394          */
395         void                    *(*start_routine)(void *);
396         void                    *arg;
397         struct pthread_attr     attr;
398
399         /*
400          * Cancelability flags 
401          */
402 #define THR_CANCEL_DISABLE              0x0001
403 #define THR_CANCEL_EXITING              0x0002
404 #define THR_CANCEL_AT_POINT             0x0004
405 #define THR_CANCEL_NEEDED               0x0008
406 #define SHOULD_CANCEL(val)                                      \
407         (((val) & (THR_CANCEL_DISABLE | THR_CANCEL_EXITING |    \
408                  THR_CANCEL_NEEDED)) == THR_CANCEL_NEEDED)
409
410 #define SHOULD_ASYNC_CANCEL(val)                                \
411         (((val) & (THR_CANCEL_DISABLE | THR_CANCEL_EXITING |    \
412                  THR_CANCEL_NEEDED | THR_CANCEL_AT_POINT)) ==   \
413                  (THR_CANCEL_NEEDED | THR_CANCEL_AT_POINT))
414         int                     cancelflags;
415
416         /* Thread temporary signal mask. */
417         sigset_t                sigmask;
418
419         /* Thread state: */
420         umtx_t                  state;
421
422         /*
423          * Error variable used instead of errno, used for internal.
424          */
425         int                     error;
426
427         /*
428          * The joiner is the thread that is joining to this thread.  The
429          * join status keeps track of a join operation to another thread.
430          */
431         struct pthread          *joiner;
432
433         /*
434          * The current thread can belong to a priority mutex queue.
435          * This is the synchronization queue link.
436          */
437         TAILQ_ENTRY(pthread)    sqe;
438
439         /* Wait data. */
440         union pthread_wait_data data;
441
442         /* Miscellaneous flags; only set with scheduling lock held. */
443         int                     flags;
444 #define THR_FLAGS_PRIVATE       0x0001
445 #define THR_FLAGS_NEED_SUSPEND  0x0002  /* thread should be suspended */
446 #define THR_FLAGS_SUSPENDED     0x0004  /* thread is suspended */
447
448         /* Thread list flags; only set with thread list lock held. */
449         int                     tlflags;
450 #define TLFLAGS_GC_SAFE         0x0001  /* thread safe for cleaning */
451 #define TLFLAGS_IN_TDLIST       0x0002  /* thread in all thread list */
452 #define TLFLAGS_IN_GCLIST       0x0004  /* thread in gc list */
453 #define TLFLAGS_DETACHED        0x0008  /* thread is detached */
454
455         /*
456          * Base priority is the user setable and retrievable priority
457          * of the thread.  It is only affected by explicit calls to
458          * set thread priority and upon thread creation via a thread
459          * attribute or default priority.
460          */
461         char                    base_priority;
462
463         /*
464          * Inherited priority is the priority a thread inherits by
465          * taking a priority inheritence or protection mutex.  It
466          * is not affected by base priority changes.  Inherited
467          * priority defaults to and remains 0 until a mutex is taken
468          * that is being waited on by any other thread whose priority
469          * is non-zero.
470          */
471         char                    inherited_priority;
472
473         /*
474          * Active priority is always the maximum of the threads base
475          * priority and inherited priority.  When there is a change
476          * in either the base or inherited priority, the active
477          * priority must be recalculated.
478          */
479         char                    active_priority;
480
481         /* Number of priority ceiling or protection mutexes owned. */
482         int                     priority_mutex_count;
483
484         /* Queue of currently owned simple type mutexes. */
485         TAILQ_HEAD(, pthread_mutex)     mutexq;
486
487         void                            *ret;
488         struct pthread_specific_elem    *specific;
489         int                             specific_data_count;
490
491         /* Number rwlocks rdlocks held. */
492         int                     rdlock_count;
493
494         /*
495          * Current locks bitmap for rtld. */
496         int                     rtld_bits;
497
498         /* Thread control block */
499         struct tls_tcb          *tcb;
500
501         /* Cleanup handlers Link List */
502         struct pthread_cleanup *cleanup;
503
504         /* Enable event reporting */
505         int                     report_events;
506         
507         /* Event mask */
508         td_thr_events_t         event_mask;
509         
510         /* Event */
511         td_event_msg_t          event_buf;
512 };
513
514 #define THR_IN_CRITICAL(thrd)                           \
515         (((thrd)->locklevel > 0) ||                     \
516         ((thrd)->critical_count > 0))
517
518 #define THR_UMTX_TRYLOCK(thrd, lck)                     \
519         _thr_umtx_trylock((lck), (thrd)->tid)
520
521 #define THR_UMTX_LOCK(thrd, lck)                        \
522         _thr_umtx_lock((lck), (thrd)->tid)
523
524 #define THR_UMTX_TIMEDLOCK(thrd, lck, timo)             \
525         _thr_umtx_timedlock((lck), (thrd)->tid, (timo))
526
527 #define THR_UMTX_UNLOCK(thrd, lck)                      \
528         _thr_umtx_unlock((lck), (thrd)->tid)
529
530 #define THR_LOCK_ACQUIRE(thrd, lck)                     \
531 do {                                                    \
532         (thrd)->locklevel++;                            \
533         _thr_umtx_lock((lck), (thrd)->tid);             \
534 } while (0)
535
536 #ifdef  _PTHREADS_INVARIANTS
537 #define THR_ASSERT_LOCKLEVEL(thrd)                      \
538 do {                                                    \
539         if (__predict_false((thrd)->locklevel <= 0))    \
540                 _thr_assert_lock_level();               \
541 } while (0)
542 #else
543 #define THR_ASSERT_LOCKLEVEL(thrd)
544 #endif
545
546 #define THR_LOCK_RELEASE(thrd, lck)                     \
547 do {                                                    \
548         THR_ASSERT_LOCKLEVEL(thrd);                     \
549         _thr_umtx_unlock((lck), (thrd)->tid);           \
550         (thrd)->locklevel--;                            \
551         _thr_ast(thrd);                                 \
552 } while (0)
553
554 #define THR_LOCK(curthrd)               THR_LOCK_ACQUIRE(curthrd, &(curthrd)->lock)
555 #define THR_UNLOCK(curthrd)             THR_LOCK_RELEASE(curthrd, &(curthrd)->lock)
556 #define THR_THREAD_LOCK(curthrd, thr)   THR_LOCK_ACQUIRE(curthrd, &(thr)->lock)
557 #define THR_THREAD_UNLOCK(curthrd, thr) THR_LOCK_RELEASE(curthrd, &(thr)->lock)
558
559 #define THREAD_LIST_LOCK(curthrd)                               \
560 do {                                                            \
561         THR_LOCK_ACQUIRE((curthrd), &_thr_list_lock);           \
562 } while (0)
563
564 #define THREAD_LIST_UNLOCK(curthrd)                             \
565 do {                                                            \
566         THR_LOCK_RELEASE((curthrd), &_thr_list_lock);           \
567 } while (0)
568
569 /*
570  * Macros to insert/remove threads to the all thread list and
571  * the gc list.
572  */
573 #define THR_LIST_ADD(thrd) do {                                 \
574         if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) == 0) {       \
575                 TAILQ_INSERT_HEAD(&_thread_list, thrd, tle);    \
576                 _thr_hash_add(thrd);                            \
577                 (thrd)->tlflags |= TLFLAGS_IN_TDLIST;           \
578         }                                                       \
579 } while (0)
580 #define THR_LIST_REMOVE(thrd) do {                              \
581         if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) != 0) {       \
582                 TAILQ_REMOVE(&_thread_list, thrd, tle);         \
583                 _thr_hash_remove(thrd);                         \
584                 (thrd)->tlflags &= ~TLFLAGS_IN_TDLIST;          \
585         }                                                       \
586 } while (0)
587 #define THR_GCLIST_ADD(thrd) do {                               \
588         if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) == 0) {       \
589                 TAILQ_INSERT_HEAD(&_thread_gc_list, thrd, gcle);\
590                 (thrd)->tlflags |= TLFLAGS_IN_GCLIST;           \
591                 _thr_gc_count++;                                        \
592         }                                                       \
593 } while (0)
594 #define THR_GCLIST_REMOVE(thrd) do {                            \
595         if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) != 0) {       \
596                 TAILQ_REMOVE(&_thread_gc_list, thrd, gcle);     \
597                 (thrd)->tlflags &= ~TLFLAGS_IN_GCLIST;          \
598                 _thr_gc_count--;                                        \
599         }                                                       \
600 } while (0)
601
602 #define GC_NEEDED()     (_thr_gc_count >= 5)
603
604 #define THR_IN_SYNCQ(thrd)      (((thrd)->sflags & THR_FLAGS_IN_SYNCQ) != 0)
605
606 #define SHOULD_REPORT_EVENT(curthr, e)                  \
607         (curthr->report_events &&                       \
608          (((curthr)->event_mask | _thread_event_mask ) & e) != 0)
609
610 extern int __isthreaded;
611
612 /*
613  * Global variables for the pthread library.
614  */
615 extern char             *_usrstack;
616 extern struct pthread   *_thr_initial;
617 extern int              _thread_scope_system;
618
619 /* For debugger */
620 extern int              _libthread_xu_debug;
621 extern int              _thread_event_mask;
622 extern struct pthread   *_thread_last_event;
623
624 /* List of all threads */
625 extern struct thread_head       _thread_list;
626
627 /* List of threads needing GC */
628 extern struct thread_head       _thread_gc_list;
629
630 extern int      _thread_active_threads;
631
632 extern struct   atfork_head     _thr_atfork_list;
633 extern umtx_t   _thr_atfork_lock;
634
635 /* Default thread attributes */
636 extern struct pthread_attr _pthread_attr_default;
637
638 /* Default mutex attributes */
639 extern struct pthread_mutex_attr _pthread_mutexattr_default;
640
641 /* Default condition variable attributes */
642 extern struct pthread_cond_attr _pthread_condattr_default;
643
644 extern pid_t    _thr_pid;
645 extern size_t   _thr_guard_default;
646 extern size_t   _thr_stack_default;
647 extern size_t   _thr_stack_initial;
648 extern int      _thr_page_size;
649 extern int      _thr_gc_count;
650
651 extern umtx_t   _mutex_static_lock;
652 extern umtx_t   _cond_static_lock;
653 extern umtx_t   _rwlock_static_lock;
654 extern umtx_t   _keytable_lock;
655 extern umtx_t   _thr_list_lock;
656 extern umtx_t   _thr_event_lock;
657
658 /*
659  * Function prototype definitions.
660  */
661 __BEGIN_DECLS
662 int     _thr_setthreaded(int);
663 int     _mutex_cv_lock(pthread_mutex_t *);
664 int     _mutex_cv_unlock(pthread_mutex_t *);
665 void    _mutex_notify_priochange(struct pthread *, struct pthread *, int);
666 int     _mutex_reinit(pthread_mutex_t *);
667 void    _mutex_fork(struct pthread *curthread);
668 void    _mutex_unlock_private(struct pthread *);
669 void    _libpthread_init(struct pthread *);
670 struct pthread *_thr_alloc(struct pthread *);
671 void    _thread_exit(const char *, int, const char *) __dead2;
672 void    _thr_exit_cleanup(void);
673 int     _thr_ref_add(struct pthread *, struct pthread *, int);
674 void    _thr_ref_delete(struct pthread *, struct pthread *);
675 void    _thr_ref_delete_unlocked(struct pthread *, struct pthread *);
676 int     _thr_find_thread(struct pthread *, struct pthread *, int);
677 void    _thr_rtld_init(void);
678 void    _thr_rtld_fini(void);
679 int     _thr_stack_alloc(struct pthread_attr *);
680 void    _thr_stack_free(struct pthread_attr *);
681 void    _thr_free(struct pthread *, struct pthread *);
682 void    _thr_gc(struct pthread *);
683 void    _thread_cleanupspecific(void);
684 void    _thread_dump_info(void);
685 void    _thread_printf(int, const char *, ...);
686 void    _thr_spinlock_init(void);
687 int     _thr_cancel_enter(struct pthread *);
688 void    _thr_cancel_leave(struct pthread *, int);
689 void    _thr_signal_block(struct pthread *);
690 void    _thr_signal_unblock(struct pthread *);
691 void    _thr_signal_init(void);
692 void    _thr_signal_deinit(void);
693 int     _thr_send_sig(struct pthread *, int sig);
694 void    _thr_list_init(void);
695 void    _thr_hash_add(struct pthread *);
696 void    _thr_hash_remove(struct pthread *);
697 struct pthread *_thr_hash_find(struct pthread *);
698 void    _thr_link(struct pthread *curthread, struct pthread *thread);
699 void    _thr_unlink(struct pthread *curthread, struct pthread *thread);
700 void    _thr_suspend_check(struct pthread *curthread);
701 void    _thr_assert_lock_level(void) __dead2;
702 void    _thr_ast(struct pthread *);
703 int     _thr_get_tid(void);
704 void    _thr_report_creation(struct pthread *curthread,
705                            struct pthread *newthread);
706 void    _thr_report_death(struct pthread *curthread);
707 void    _thread_bp_create(void);
708 void    _thread_bp_death(void);
709
710 /* #include <sys/aio.h> */
711 #ifdef _SYS_AIO_H_
712 int     __sys_aio_suspend(const struct aiocb * const[], int, const struct timespec *);
713 #endif
714
715 /* #include <fcntl.h> */
716 #ifdef  _SYS_FCNTL_H_
717 int     __sys_fcntl(int, int, ...);
718 int     __sys_open(const char *, int, ...);
719 #endif
720
721 /* #include <sys/ioctl.h> */
722 #ifdef _SYS_IOCTL_H_
723 int     __sys_ioctl(int, unsigned long, ...);
724 #endif
725
726 /* #inclde <sched.h> */
727 #ifdef  _SCHED_H_
728 int     __sys_sched_yield(void);
729 #endif
730
731 /* #include <signal.h> */
732 #ifdef _SIGNAL_H_
733 int     __sys_kill(pid_t, int);
734 int     __sys_sigaction(int, const struct sigaction *, struct sigaction *);
735 int     __sys_sigpending(sigset_t *);
736 int     __sys_sigprocmask(int, const sigset_t *, sigset_t *);
737 int     __sys_sigsuspend(const sigset_t *);
738 int     __sys_sigreturn(ucontext_t *);
739 int     __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *);
740 #endif
741
742 /* #include <sys/socket.h> */
743 #ifdef _SYS_SOCKET_H_
744 int     __sys_accept(int, struct sockaddr *, socklen_t *);
745 int     __sys_connect(int, const struct sockaddr *, socklen_t);
746 ssize_t __sys_recv(int, void *, size_t, int);
747 ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
748 ssize_t __sys_recvmsg(int, struct msghdr *, int);
749 int     __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *,
750             off_t *, int);
751 ssize_t __sys_sendmsg(int, const struct msghdr *, int);
752 ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
753 #endif
754
755 /* #include <sys/uio.h> */
756 #ifdef  _SYS_UIO_H_
757 ssize_t __sys_readv(int, const struct iovec *, int);
758 ssize_t __sys_writev(int, const struct iovec *, int);
759 #endif
760
761 /* #include <time.h> */
762 #ifdef  _TIME_H_
763 int     __sys_nanosleep(const struct timespec *, struct timespec *);
764 #endif
765
766 /* #include <unistd.h> */
767 #ifdef  _UNISTD_H_
768 int     __sys_close(int);
769 int     __sys_execve(const char *, char * const *, char * const *);
770 int     __sys_fork(void);
771 int     __sys_fsync(int);
772 pid_t   __sys_getpid(void);
773 int     __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
774 ssize_t __sys_read(int, void *, size_t);
775 ssize_t __sys_write(int, const void *, size_t);
776 void    __sys_exit(int);
777 int     __sys_sigwait(const sigset_t *, int *);
778 int     __sys_sigtimedwait(const sigset_t *, siginfo_t *,
779                 const struct timespec *);
780 int     __sys_sigwaitinfo(const sigset_t *set, siginfo_t *info);
781 #endif
782
783 /* #include <poll.h> */
784 #ifdef _SYS_POLL_H_
785 int     __sys_poll(struct pollfd *, unsigned, int);
786 #endif
787
788 /* #include <sys/mman.h> */
789 #ifdef _SYS_MMAN_H_
790 int     __sys_msync(void *, size_t, int);
791 #endif
792
793 static inline int
794 _thr_isthreaded(void)
795 {
796         return (__isthreaded != 0);
797 }
798
799 static inline int
800 _thr_is_inited(void)
801 {
802         return (_thr_initial != 0);
803 }
804
805 static inline void
806 _thr_check_init(void)
807 {
808         if (_thr_initial == 0)
809                 _libpthread_init(0);
810 }
811
812 __END_DECLS
813
814 #endif  /* !_THR_PRIVATE_H */