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