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