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