xref: /freebsd/lib/libthr/thread/thr_private.h (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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 	struct pthread			*m_owner;
119 	int				m_flags;
120 	int				m_count;
121 	int				m_refcount;
122 
123 	/*
124 	 * Used for priority inheritence and protection.
125 	 *
126 	 *   m_prio       - For priority inheritence, the highest active
127 	 *                  priority (threads locking the mutex inherit
128 	 *                  this priority).  For priority protection, the
129 	 *                  ceiling priority of this mutex.
130 	 *   m_saved_prio - mutex owners inherited priority before
131 	 *                  taking the mutex, restored when the owner
132 	 *                  unlocks the mutex.
133 	 */
134 	int				m_prio;
135 	int				m_saved_prio;
136 
137 	/*
138 	 * Link for list of all mutexes a thread currently owns.
139 	 */
140 	TAILQ_ENTRY(pthread_mutex)	m_qe;
141 };
142 
143 /*
144  * Flags for mutexes.
145  */
146 #define MUTEX_FLAGS_PRIVATE	0x01
147 #define MUTEX_FLAGS_INITED	0x02
148 #define MUTEX_FLAGS_BUSY	0x04
149 
150 struct pthread_mutex_attr {
151 	enum pthread_mutextype	m_type;
152 	int			m_protocol;
153 	int			m_ceiling;
154 	int			m_flags;
155 };
156 
157 #define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \
158 	{ PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE }
159 
160 struct pthread_cond {
161 	/*
162 	 * Lock for accesses to this structure.
163 	 */
164 	volatile umtx_t	c_lock;
165 	volatile umtx_t	c_seqno;
166 	volatile int	c_waiters;
167 	volatile int	c_wakeups;
168 	int		c_pshared;
169 	int		c_clockid;
170 };
171 
172 struct pthread_cond_attr {
173 	int		c_pshared;
174 	int		c_clockid;
175 };
176 
177 struct pthread_barrier {
178 	volatile umtx_t	b_lock;
179 	volatile umtx_t	b_cycle;
180 	volatile int	b_count;
181 	volatile int	b_waiters;
182 };
183 
184 struct pthread_barrierattr {
185 	int		pshared;
186 };
187 
188 struct pthread_spinlock {
189 	volatile umtx_t	s_lock;
190 };
191 
192 /*
193  * Flags for condition variables.
194  */
195 #define COND_FLAGS_PRIVATE	0x01
196 #define COND_FLAGS_INITED	0x02
197 #define COND_FLAGS_BUSY		0x04
198 
199 /*
200  * Cleanup definitions.
201  */
202 struct pthread_cleanup {
203 	struct pthread_cleanup	*next;
204 	void			(*routine)(void *args);
205 	void			*routine_arg;
206 	int			onstack;
207 };
208 
209 #define	THR_CLEANUP_PUSH(td, func, arg) {		\
210 	struct pthread_cleanup __cup;			\
211 							\
212 	__cup.routine = func;				\
213 	__cup.routine_arg = arg;			\
214 	__cup.onstack = 1;				\
215 	__cup.next = (td)->cleanup;			\
216 	(td)->cleanup = &__cup;
217 
218 #define	THR_CLEANUP_POP(td, exec)			\
219 	(td)->cleanup = __cup.next;			\
220 	if ((exec) != 0)				\
221 		__cup.routine(__cup.routine_arg);	\
222 }
223 
224 struct pthread_atfork {
225 	TAILQ_ENTRY(pthread_atfork) qe;
226 	void (*prepare)(void);
227 	void (*parent)(void);
228 	void (*child)(void);
229 };
230 
231 struct pthread_attr {
232 	int	sched_policy;
233 	int	sched_inherit;
234 	int	prio;
235 	int	suspend;
236 #define	THR_STACK_USER		0x100	/* 0xFF reserved for <pthread.h> */
237 	int	flags;
238 	void	*stackaddr_attr;
239 	size_t	stacksize_attr;
240 	size_t	guardsize_attr;
241 };
242 
243 /*
244  * Thread creation state attributes.
245  */
246 #define THR_CREATE_RUNNING		0
247 #define THR_CREATE_SUSPENDED		1
248 
249 /*
250  * Miscellaneous definitions.
251  */
252 #define THR_STACK_DEFAULT		(sizeof(void *) / 4 * 1024 * 1024)
253 
254 /*
255  * Maximum size of initial thread's stack.  This perhaps deserves to be larger
256  * than the stacks of other threads, since many applications are likely to run
257  * almost entirely on this stack.
258  */
259 #define THR_STACK_INITIAL		(THR_STACK_DEFAULT * 2)
260 
261 /*
262  * Define priorities returned by kernel.
263  */
264 #define THR_MIN_PRIORITY		(_thr_priorities[SCHED_OTHER-1].pri_min)
265 #define THR_MAX_PRIORITY		(_thr_priorities[SCHED_OTHER-1].pri_max)
266 #define THR_DEF_PRIORITY		(_thr_priorities[SCHED_OTHER-1].pri_default)
267 
268 #define THR_MIN_RR_PRIORITY		(_thr_priorities[SCHED_RR-1].pri_min)
269 #define THR_MAX_RR_PRIORITY		(_thr_priorities[SCHED_RR-1].pri_max)
270 #define THR_DEF_RR_PRIORITY		(_thr_priorities[SCHED_RR-1].pri_default)
271 
272 /* XXX The SCHED_FIFO should have same priority range as SCHED_RR */
273 #define THR_MIN_FIFO_PRIORITY		(_thr_priorities[SCHED_FIFO_1].pri_min)
274 #define THR_MAX_FIFO_PRIORITY		(_thr_priorities[SCHED_FIFO-1].pri_max)
275 #define THR_DEF_FIFO_PRIORITY		(_thr_priorities[SCHED_FIFO-1].pri_default)
276 
277 struct pthread_prio {
278 	int	pri_min;
279 	int	pri_max;
280 	int	pri_default;
281 };
282 
283 struct pthread_rwlockattr {
284 	int		pshared;
285 };
286 
287 struct pthread_rwlock {
288 	pthread_mutex_t	lock;	/* monitor lock */
289 	pthread_cond_t	read_signal;
290 	pthread_cond_t	write_signal;
291 	int		state;	/* 0 = idle  >0 = # of readers  -1 = writer */
292 	int		blocked_writers;
293 };
294 
295 /*
296  * Thread states.
297  */
298 enum pthread_state {
299 	PS_RUNNING,
300 	PS_DEAD
301 };
302 
303 struct pthread_specific_elem {
304 	const void	*data;
305 	int		seqno;
306 };
307 
308 struct pthread_key {
309 	volatile int	allocated;
310 	int		seqno;
311 	void            (*destructor)(void *);
312 };
313 
314 /*
315  * Thread structure.
316  */
317 struct pthread {
318 	/* Kernel thread id. */
319 	long			tid;
320 #define	TID_TERMINATED		1
321 
322 	/*
323 	 * Lock for accesses to this thread structure.
324 	 */
325 	umtx_t			lock;
326 
327 	/* Internal condition variable cycle number. */
328 	umtx_t			cycle;
329 
330 	/* How many low level locks the thread held. */
331 	int			locklevel;
332 
333 	/*
334 	 * Set to non-zero when this thread has entered a critical
335 	 * region.  We allow for recursive entries into critical regions.
336 	 */
337 	int			critical_count;
338 
339 	/* Signal blocked counter. */
340 	int			sigblock;
341 
342 	/* Queue entry for list of all threads. */
343 	TAILQ_ENTRY(pthread)	tle;	/* link for all threads in process */
344 
345 	/* Queue entry for GC lists. */
346 	TAILQ_ENTRY(pthread)	gcle;
347 
348 	/* Hash queue entry. */
349 	LIST_ENTRY(pthread)	hle;
350 
351 	/* Threads reference count. */
352 	int			refcount;
353 
354 	/*
355 	 * Thread start routine, argument, stack pointer and thread
356 	 * attributes.
357 	 */
358 	void			*(*start_routine)(void *);
359 	void			*arg;
360 	struct pthread_attr	attr;
361 
362  	/*
363 	 * Cancelability flags
364 	 */
365 #define	THR_CANCEL_DISABLE		0x0001
366 #define	THR_CANCEL_EXITING		0x0002
367 #define THR_CANCEL_AT_POINT		0x0004
368 #define THR_CANCEL_NEEDED		0x0008
369 #define	SHOULD_CANCEL(val)					\
370 	(((val) & (THR_CANCEL_DISABLE | THR_CANCEL_EXITING |	\
371 		 THR_CANCEL_NEEDED)) == THR_CANCEL_NEEDED)
372 
373 #define	SHOULD_ASYNC_CANCEL(val)				\
374 	(((val) & (THR_CANCEL_DISABLE | THR_CANCEL_EXITING |	\
375 		 THR_CANCEL_NEEDED | THR_CANCEL_AT_POINT)) ==	\
376 		 (THR_CANCEL_NEEDED | THR_CANCEL_AT_POINT))
377 	int			cancelflags;
378 
379 	/* Thread temporary signal mask. */
380 	sigset_t		sigmask;
381 
382 	/* Thread state: */
383 	umtx_t			state;
384 
385 	/*
386 	 * Error variable used instead of errno. The function __error()
387 	 * returns a pointer to this.
388 	 */
389 	int			error;
390 
391 	/*
392 	 * The joiner is the thread that is joining to this thread.  The
393 	 * join status keeps track of a join operation to another thread.
394 	 */
395 	struct pthread		*joiner;
396 
397 	/*
398 	 * The current thread can belong to a priority mutex queue.
399 	 * This is the synchronization queue link.
400 	 */
401 	TAILQ_ENTRY(pthread)	sqe;
402 
403 	/* Miscellaneous flags; only set with scheduling lock held. */
404 	int			flags;
405 #define THR_FLAGS_PRIVATE	0x0001
406 #define	THR_FLAGS_NEED_SUSPEND	0x0002	/* thread should be suspended */
407 #define	THR_FLAGS_SUSPENDED	0x0004	/* thread is suspended */
408 
409 	/* Thread list flags; only set with thread list lock held. */
410 	int			tlflags;
411 #define	TLFLAGS_GC_SAFE		0x0001	/* thread safe for cleaning */
412 #define	TLFLAGS_IN_TDLIST	0x0002	/* thread in all thread list */
413 #define	TLFLAGS_IN_GCLIST	0x0004	/* thread in gc list */
414 #define	TLFLAGS_DETACHED	0x0008	/* thread is detached */
415 
416 	/* Queue of currently owned simple type mutexes. */
417 	TAILQ_HEAD(, pthread_mutex)	mutexq;
418 
419 	void				*ret;
420 	struct pthread_specific_elem	*specific;
421 	int				specific_data_count;
422 
423 	/* Number rwlocks rdlocks held. */
424 	int			rdlock_count;
425 
426 	/*
427 	 * Current locks bitmap for rtld. */
428 	int			rtld_bits;
429 
430 	/* Thread control block */
431 	struct tcb		*tcb;
432 
433 	/* Cleanup handlers Link List */
434 	struct pthread_cleanup	*cleanup;
435 
436 	/*
437 	 * Magic value to help recognize a valid thread structure
438 	 * from an invalid one:
439 	 */
440 #define	THR_MAGIC		((u_int32_t) 0xd09ba115)
441 	u_int32_t		magic;
442 
443 	/* Enable event reporting */
444 	int			report_events;
445 
446 	/* Event mask */
447 	int			event_mask;
448 
449 	/* Event */
450 	td_event_msg_t		event_buf;
451 };
452 
453 #define	THR_IN_CRITICAL(thrd)				\
454 	(((thrd)->locklevel > 0) ||			\
455 	((thrd)->critical_count > 0))
456 
457 #define	THR_CRITICAL_ENTER(thrd)			\
458 	(thrd)->critical_count++
459 
460 #define	THR_CRITICAL_LEAVE(thrd)			\
461 	(thrd)->critical_count--;			\
462 	_thr_ast(thrd);
463 
464 #define THR_UMTX_TRYLOCK(thrd, lck)			\
465 	_thr_umtx_trylock((lck), (thrd)->tid)
466 
467 #define	THR_UMTX_LOCK(thrd, lck)			\
468 	_thr_umtx_lock((lck), (thrd)->tid)
469 
470 #define	THR_UMTX_TIMEDLOCK(thrd, lck, timo)		\
471 	_thr_umtx_timedlock((lck), (thrd)->tid, (timo))
472 
473 #define	THR_UMTX_UNLOCK(thrd, lck)			\
474 	_thr_umtx_unlock((lck), (thrd)->tid)
475 
476 #define	THR_LOCK_ACQUIRE(thrd, lck)			\
477 do {							\
478 	(thrd)->locklevel++;				\
479 	_thr_umtx_lock(lck, (thrd)->tid);		\
480 } while (0)
481 
482 #ifdef	_PTHREADS_INVARIANTS
483 #define	THR_ASSERT_LOCKLEVEL(thrd)			\
484 do {							\
485 	if (__predict_false((thrd)->locklevel <= 0))	\
486 		_thr_assert_lock_level();		\
487 } while (0)
488 #else
489 #define THR_ASSERT_LOCKLEVEL(thrd)
490 #endif
491 
492 #define	THR_LOCK_RELEASE(thrd, lck)			\
493 do {							\
494 	THR_ASSERT_LOCKLEVEL(thrd);			\
495 	_thr_umtx_unlock((lck), (thrd)->tid);		\
496 	(thrd)->locklevel--;				\
497 	_thr_ast(thrd);					\
498 } while (0)
499 
500 #define	THR_LOCK(curthrd)		THR_LOCK_ACQUIRE(curthrd, &(curthrd)->lock)
501 #define	THR_UNLOCK(curthrd)		THR_LOCK_RELEASE(curthrd, &(curthrd)->lock)
502 #define	THR_THREAD_LOCK(curthrd, thr)	THR_LOCK_ACQUIRE(curthrd, &(thr)->lock)
503 #define	THR_THREAD_UNLOCK(curthrd, thr)	THR_LOCK_RELEASE(curthrd, &(thr)->lock)
504 
505 #define	THREAD_LIST_LOCK(curthrd)				\
506 do {								\
507 	THR_LOCK_ACQUIRE((curthrd), &_thr_list_lock);		\
508 } while (0)
509 
510 #define	THREAD_LIST_UNLOCK(curthrd)				\
511 do {								\
512 	THR_LOCK_RELEASE((curthrd), &_thr_list_lock);		\
513 } while (0)
514 
515 /*
516  * Macros to insert/remove threads to the all thread list and
517  * the gc list.
518  */
519 #define	THR_LIST_ADD(thrd) do {					\
520 	if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) == 0) {	\
521 		TAILQ_INSERT_HEAD(&_thread_list, thrd, tle);	\
522 		_thr_hash_add(thrd);				\
523 		(thrd)->tlflags |= TLFLAGS_IN_TDLIST;		\
524 	}							\
525 } while (0)
526 #define	THR_LIST_REMOVE(thrd) do {				\
527 	if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) != 0) {	\
528 		TAILQ_REMOVE(&_thread_list, thrd, tle);		\
529 		_thr_hash_remove(thrd);				\
530 		(thrd)->tlflags &= ~TLFLAGS_IN_TDLIST;		\
531 	}							\
532 } while (0)
533 #define	THR_GCLIST_ADD(thrd) do {				\
534 	if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) == 0) {	\
535 		TAILQ_INSERT_HEAD(&_thread_gc_list, thrd, gcle);\
536 		(thrd)->tlflags |= TLFLAGS_IN_GCLIST;		\
537 		_gc_count++;					\
538 	}							\
539 } while (0)
540 #define	THR_GCLIST_REMOVE(thrd) do {				\
541 	if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) != 0) {	\
542 		TAILQ_REMOVE(&_thread_gc_list, thrd, gcle);	\
543 		(thrd)->tlflags &= ~TLFLAGS_IN_GCLIST;		\
544 		_gc_count--;					\
545 	}							\
546 } while (0)
547 
548 #define GC_NEEDED()	(_gc_count >= 5)
549 
550 #define SHOULD_REPORT_EVENT(curthr, e)			\
551 	(curthr->report_events && 			\
552 	 (((curthr)->event_mask | _thread_event_mask ) & e) != 0)
553 
554 extern int __isthreaded;
555 
556 /*
557  * Global variables for the pthread kernel.
558  */
559 
560 extern char		*_usrstack __hidden;
561 extern struct pthread	*_thr_initial __hidden;
562 extern int		_thr_scope_system __hidden;
563 
564 /* For debugger */
565 extern int		_libthr_debug;
566 extern int		_thread_event_mask;
567 extern struct pthread	*_thread_last_event;
568 
569 /* List of all threads: */
570 extern pthreadlist	_thread_list;
571 
572 /* List of threads needing GC: */
573 extern pthreadlist	_thread_gc_list __hidden;
574 
575 extern int		_thread_active_threads;
576 extern atfork_head	_thr_atfork_list __hidden;
577 extern umtx_t		_thr_atfork_lock __hidden;
578 
579 /* Default thread attributes: */
580 extern struct pthread_attr _pthread_attr_default __hidden;
581 
582 /* Default mutex attributes: */
583 extern struct pthread_mutex_attr _pthread_mutexattr_default __hidden;
584 
585 /* Default condition variable attributes: */
586 extern struct pthread_cond_attr _pthread_condattr_default __hidden;
587 
588 extern struct pthread_prio _thr_priorities[] __hidden;
589 
590 extern pid_t	_thr_pid __hidden;
591 extern size_t	_thr_guard_default __hidden;
592 extern size_t	_thr_stack_default __hidden;
593 extern size_t	_thr_stack_initial __hidden;
594 extern int	_thr_page_size __hidden;
595 /* Garbage thread count. */
596 extern int	_gc_count __hidden;
597 
598 extern umtx_t	_mutex_static_lock __hidden;
599 extern umtx_t	_cond_static_lock __hidden;
600 extern umtx_t	_rwlock_static_lock __hidden;
601 extern umtx_t	_keytable_lock __hidden;
602 extern umtx_t	_thr_list_lock __hidden;
603 extern umtx_t	_thr_event_lock __hidden;
604 
605 /*
606  * Function prototype definitions.
607  */
608 __BEGIN_DECLS
609 int	_thr_setthreaded(int) __hidden;
610 int	_mutex_cv_lock(pthread_mutex_t *, int count) __hidden;
611 int	_mutex_cv_unlock(pthread_mutex_t *, int *count) __hidden;
612 int	_mutex_reinit(pthread_mutex_t *) __hidden;
613 void	_mutex_fork(struct pthread *curthread) __hidden;
614 void	_mutex_unlock_private(struct pthread *) __hidden;
615 void	_libpthread_init(struct pthread *) __hidden;
616 struct pthread *_thr_alloc(struct pthread *) __hidden;
617 void	_thread_exit(const char *, int, const char *) __hidden __dead2;
618 void	_thr_exit_cleanup(void) __hidden;
619 int	_thr_ref_add(struct pthread *, struct pthread *, int) __hidden;
620 void	_thr_ref_delete(struct pthread *, struct pthread *) __hidden;
621 void	_thr_ref_delete_unlocked(struct pthread *, struct pthread *) __hidden;
622 int	_thr_find_thread(struct pthread *, struct pthread *, int) __hidden;
623 void	_thr_rtld_init(void) __hidden;
624 void	_thr_rtld_fini(void) __hidden;
625 int	_thr_stack_alloc(struct pthread_attr *) __hidden;
626 void	_thr_stack_free(struct pthread_attr *) __hidden;
627 void	_thr_free(struct pthread *, struct pthread *) __hidden;
628 void	_thr_gc(struct pthread *) __hidden;
629 void    _thread_cleanupspecific(void) __hidden;
630 void    _thread_dump_info(void) __hidden;
631 void	_thread_printf(int, const char *, ...) __hidden;
632 void	_thr_spinlock_init(void) __hidden;
633 int	_thr_cancel_enter(struct pthread *) __hidden;
634 void	_thr_cancel_leave(struct pthread *, int) __hidden;
635 void	_thr_signal_block(struct pthread *) __hidden;
636 void	_thr_signal_unblock(struct pthread *) __hidden;
637 void	_thr_signal_init(void) __hidden;
638 void	_thr_signal_deinit(void) __hidden;
639 int	_thr_send_sig(struct pthread *, int sig) __hidden;
640 void	_thr_list_init(void) __hidden;
641 void	_thr_hash_add(struct pthread *) __hidden;
642 void	_thr_hash_remove(struct pthread *) __hidden;
643 struct pthread *_thr_hash_find(struct pthread *) __hidden;
644 void	_thr_link(struct pthread *, struct pthread *) __hidden;
645 void	_thr_unlink(struct pthread *, struct pthread *) __hidden;
646 void	_thr_suspend_check(struct pthread *) __hidden;
647 void	_thr_assert_lock_level(void) __hidden __dead2;
648 void	_thr_ast(struct pthread *) __hidden;
649 void	_thr_once_init(void) __hidden;
650 void	_thr_report_creation(struct pthread *curthread,
651 	    struct pthread *newthread) __hidden;
652 void	_thr_report_death(struct pthread *curthread) __hidden;
653 void	_thread_bp_create(void);
654 void	_thread_bp_death(void);
655 
656 /* #include <fcntl.h> */
657 #ifdef  _SYS_FCNTL_H_
658 int     __sys_fcntl(int, int, ...);
659 int     __sys_open(const char *, int, ...);
660 #endif
661 
662 /* #include <signal.h> */
663 #ifdef _SIGNAL_H_
664 int	__sys_kill(pid_t, int);
665 int     __sys_sigaction(int, const struct sigaction *, struct sigaction *);
666 int     __sys_sigpending(sigset_t *);
667 int     __sys_sigprocmask(int, const sigset_t *, sigset_t *);
668 int     __sys_sigsuspend(const sigset_t *);
669 int     __sys_sigreturn(ucontext_t *);
670 int     __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *);
671 int	__sys_sigwait(const sigset_t *, int *);
672 int	__sys_sigtimedwait(const sigset_t *, siginfo_t *,
673 		const struct timespec *);
674 int	__sys_sigwaitinfo(const sigset_t *set, siginfo_t *info);
675 #endif
676 
677 /* #include <time.h> */
678 #ifdef	_TIME_H_
679 int	__sys_nanosleep(const struct timespec *, struct timespec *);
680 #endif
681 
682 /* #include <unistd.h> */
683 #ifdef  _UNISTD_H_
684 int     __sys_close(int);
685 int	__sys_fork(void);
686 pid_t	__sys_getpid(void);
687 ssize_t __sys_read(int, void *, size_t);
688 ssize_t __sys_write(int, const void *, size_t);
689 void	__sys_exit(int);
690 #endif
691 
692 static inline int
693 _thr_isthreaded(void)
694 {
695 	return (__isthreaded != 0);
696 }
697 
698 static inline int
699 _thr_is_inited(void)
700 {
701 	return (_thr_initial != NULL);
702 }
703 
704 static inline void
705 _thr_check_init(void)
706 {
707 	if (_thr_initial == NULL)
708 		_libpthread_init(NULL);
709 }
710 
711 __END_DECLS
712 
713 #endif  /* !_THR_PRIVATE_H */
714