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