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