thr_private.h revision 144711
1/*
2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by John Birrell.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: head/lib/libthr/thread/thr_private.h 144711 2005-04-06 13:57:31Z davidxu $
33 */
34
35#ifndef _THR_PRIVATE_H
36#define _THR_PRIVATE_H
37
38/*
39 * Include files.
40 */
41#include <sys/types.h>
42#include <sys/time.h>
43#include <sys/cdefs.h>
44#include <sys/queue.h>
45#include <machine/atomic.h>
46#include <errno.h>
47#include <limits.h>
48#include <signal.h>
49#include <stddef.h>
50#include <stdio.h>
51#include <sched.h>
52#include <unistd.h>
53#include <ucontext.h>
54#include <sys/thr.h>
55#include <pthread.h>
56
57#include "pthread_md.h"
58#include "thr_umtx.h"
59
60/*
61 * Evaluate the storage class specifier.
62 */
63#ifdef GLOBAL_PTHREAD_PRIVATE
64#define SCLASS
65#define SCLASS_PRESET(x...)	= x
66#else
67#define SCLASS			extern
68#define SCLASS_PRESET(x...)
69#endif
70
71/* Signal to do cancellation */
72#define	SIGCANCEL		32
73
74/*
75 * Kernel fatal error handler macro.
76 */
77#define PANIC(string)		_thread_exit(__FILE__,__LINE__,string)
78
79/* Output debug messages like this: */
80#define stdout_debug(args...)	_thread_printf(STDOUT_FILENO, ##args)
81#define stderr_debug(args...)	_thread_printf(STDOUT_FILENO, ##args)
82
83#ifdef _PTHREADS_INVARIANTS
84#define THR_ASSERT(cond, msg) do {	\
85	if (__predict_false(!(cond)))	\
86		PANIC(msg);		\
87} while (0)
88#else
89#define THR_ASSERT(cond, msg)
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	char			*name;
342
343	/*
344	 * Lock for accesses to this thread structure.
345	 */
346	umtx_t			lock;
347
348	/* Thread is terminated in kernel, written by kernel. */
349	long			terminated;
350
351	/* Kernel thread id. */
352	long			tid;
353
354	/* Internal condition variable cycle number. */
355	umtx_t			cycle;
356
357	/* How many low level locks the thread held. */
358	int			locklevel;
359
360	/* Signal blocked counter. */
361	int			sigblock;
362
363	/* Queue entry for list of all threads. */
364	TAILQ_ENTRY(pthread)	tle;	/* link for all threads in process */
365
366	/* Queue entry for GC lists. */
367	TAILQ_ENTRY(pthread)	gcle;
368
369	/* Hash queue entry. */
370	LIST_ENTRY(pthread)	hle;
371
372	/* Threads reference count. */
373	int			refcount;
374
375	/*
376	 * Thread start routine, argument, stack pointer and thread
377	 * attributes.
378	 */
379	void			*(*start_routine)(void *);
380	void			*arg;
381	struct pthread_attr	attr;
382
383 	/*
384	 * Cancelability flags
385	 */
386#define	THR_CANCEL_DISABLE		0x0001
387#define	THR_CANCEL_EXITING		0x0002
388#define THR_CANCEL_AT_POINT		0x0004
389#define THR_CANCEL_NEEDED		0x0008
390#define	SHOULD_CANCEL(val)					\
391	(((val) & (THR_CANCEL_DISABLE | THR_CANCEL_EXITING |	\
392		 THR_CANCEL_NEEDED)) == THR_CANCEL_NEEDED)
393
394#define	SHOULD_ASYNC_CANCEL(val)				\
395	(((val) & (THR_CANCEL_DISABLE | THR_CANCEL_EXITING |	\
396		 THR_CANCEL_NEEDED | THR_CANCEL_AT_POINT)) ==	\
397		 (THR_CANCEL_NEEDED | THR_CANCEL_AT_POINT))
398	int			cancelflags;
399
400	/* Thread temporary signal mask. */
401	sigset_t		sigmask;
402
403	/* Thread state: */
404	umtx_t			state;
405
406	/*
407	 * Error variable used instead of errno. The function __error()
408	 * returns a pointer to this.
409	 */
410	int			error;
411
412	/*
413	 * The joiner is the thread that is joining to this thread.  The
414	 * join status keeps track of a join operation to another thread.
415	 */
416	struct pthread		*joiner;
417
418	/*
419	 * The current thread can belong to a priority mutex queue.
420	 * This is the synchronization queue link.
421	 */
422	TAILQ_ENTRY(pthread)	sqe;
423
424	/* Wait data. */
425	union pthread_wait_data data;
426
427	int			sflags;
428#define THR_FLAGS_IN_SYNCQ	0x0001
429
430	/* Miscellaneous flags; only set with scheduling lock held. */
431	int			flags;
432#define THR_FLAGS_PRIVATE	0x0001
433#define	THR_FLAGS_NEED_SUSPEND	0x0002	/* thread should be suspended */
434#define	THR_FLAGS_SUSPENDED	0x0004	/* thread is suspended */
435
436	/* Thread list flags; only set with thread list lock held. */
437	int			tlflags;
438#define	TLFLAGS_GC_SAFE		0x0001	/* thread safe for cleaning */
439#define	TLFLAGS_IN_TDLIST	0x0002	/* thread in all thread list */
440#define	TLFLAGS_IN_GCLIST	0x0004	/* thread in gc list */
441#define	TLFLAGS_DETACHED	0x0008	/* thread is detached */
442
443	/*
444	 * Base priority is the user setable and retrievable priority
445	 * of the thread.  It is only affected by explicit calls to
446	 * set thread priority and upon thread creation via a thread
447	 * attribute or default priority.
448	 */
449	char			base_priority;
450
451	/*
452	 * Inherited priority is the priority a thread inherits by
453	 * taking a priority inheritence or protection mutex.  It
454	 * is not affected by base priority changes.  Inherited
455	 * priority defaults to and remains 0 until a mutex is taken
456	 * that is being waited on by any other thread whose priority
457	 * is non-zero.
458	 */
459	char			inherited_priority;
460
461	/*
462	 * Active priority is always the maximum of the threads base
463	 * priority and inherited priority.  When there is a change
464	 * in either the base or inherited priority, the active
465	 * priority must be recalculated.
466	 */
467	char			active_priority;
468
469	/* Number of priority ceiling or protection mutexes owned. */
470	int			priority_mutex_count;
471
472	/* Queue of currently owned simple type mutexes. */
473	TAILQ_HEAD(, pthread_mutex)	mutexq;
474
475	/* Queue of currently owned priority type mutexs. */
476	TAILQ_HEAD(, pthread_mutex)	pri_mutexq;
477
478	void				*ret;
479	struct pthread_specific_elem	*specific;
480	int				specific_data_count;
481
482	/* Number rwlocks rdlocks held. */
483	int			rdlock_count;
484
485	/*
486	 * Current locks bitmap for rtld. */
487	int			rtld_bits;
488
489	/* Thread control block */
490	struct tcb		*tcb;
491
492	/* Cleanup handlers Link List */
493	struct pthread_cleanup	*cleanup;
494};
495
496#define THR_UMTX_TRYLOCK(thrd, lck)			\
497	_thr_umtx_trylock((lck), (thrd)->tid)
498
499#define	THR_UMTX_LOCK(thrd, lck)			\
500	_thr_umtx_lock((lck), (thrd)->tid)
501
502#define	THR_UMTX_TIMEDLOCK(thrd, lck, timo)		\
503	_thr_umtx_timedlock((lck), (thrd)->tid, (timo))
504
505#define	THR_UMTX_UNLOCK(thrd, lck)			\
506	_thr_umtx_unlock((lck), (thrd)->tid)
507
508#define	THR_LOCK_ACQUIRE(thrd, lck)			\
509do {							\
510	(thrd)->locklevel++;				\
511	_thr_umtx_lock(lck, (thrd)->tid);		\
512} while (0)
513
514#define	THR_LOCK_RELEASE(thrd, lck)			\
515do {							\
516	if ((thrd)->locklevel > 0) {			\
517		_thr_umtx_unlock((lck), (thrd)->tid);	\
518		(thrd)->locklevel--;			\
519	} else { 					\
520		_thr_assert_lock_level();		\
521	}						\
522} while (0)
523
524#define	THR_LOCK(curthrd)		THR_LOCK_ACQUIRE(curthrd, &(curthrd)->lock)
525#define	THR_UNLOCK(curthrd)		THR_LOCK_RELEASE(curthrd, &(curthrd)->lock)
526#define	THR_THREAD_LOCK(curthrd, thr)	THR_LOCK_ACQUIRE(curthrd, &(thr)->lock)
527#define	THR_THREAD_UNLOCK(curthrd, thr)	THR_LOCK_RELEASE(curthrd, &(thr)->lock)
528
529#define	THREAD_LIST_LOCK(curthrd)				\
530do {								\
531	THR_LOCK_ACQUIRE((curthrd), &_thr_list_lock);		\
532} while (0)
533
534#define	THREAD_LIST_UNLOCK(curthrd)				\
535do {								\
536	THR_LOCK_RELEASE((curthrd), &_thr_list_lock);		\
537} while (0)
538
539/*
540 * Macros to insert/remove threads to the all thread list and
541 * the gc list.
542 */
543#define	THR_LIST_ADD(thrd) do {					\
544	if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) == 0) {	\
545		TAILQ_INSERT_HEAD(&_thread_list, thrd, tle);	\
546		_thr_hash_add(thrd);				\
547		(thrd)->tlflags |= TLFLAGS_IN_TDLIST;		\
548	}							\
549} while (0)
550#define	THR_LIST_REMOVE(thrd) do {				\
551	if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) != 0) {	\
552		TAILQ_REMOVE(&_thread_list, thrd, tle);		\
553		_thr_hash_remove(thrd);				\
554		(thrd)->tlflags &= ~TLFLAGS_IN_TDLIST;		\
555	}							\
556} while (0)
557#define	THR_GCLIST_ADD(thrd) do {				\
558	if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) == 0) {	\
559		TAILQ_INSERT_HEAD(&_thread_gc_list, thrd, gcle);\
560		(thrd)->tlflags |= TLFLAGS_IN_GCLIST;		\
561		_gc_count++;					\
562	}							\
563} while (0)
564#define	THR_GCLIST_REMOVE(thrd) do {				\
565	if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) != 0) {	\
566		TAILQ_REMOVE(&_thread_gc_list, thrd, gcle);	\
567		(thrd)->tlflags &= ~TLFLAGS_IN_GCLIST;		\
568		_gc_count--;					\
569	}							\
570} while (0)
571
572#define GC_NEEDED()	(_gc_count >= 5)
573
574#define	THR_IN_SYNCQ(thrd)	(((thrd)->sflags & THR_FLAGS_IN_SYNCQ) != 0)
575
576extern int __isthreaded;
577
578/*
579 * Global variables for the pthread kernel.
580 */
581
582SCLASS void		*_usrstack	SCLASS_PRESET(NULL);
583SCLASS struct pthread	*_thr_initial	SCLASS_PRESET(NULL);
584/* For debugger */
585SCLASS int		_libthr_debug		SCLASS_PRESET(0);
586SCLASS int		_thr_scope_system	SCLASS_PRESET(0);
587
588/* List of all threads: */
589SCLASS TAILQ_HEAD(, pthread)	_thread_list
590    SCLASS_PRESET(TAILQ_HEAD_INITIALIZER(_thread_list));
591
592/* List of threads needing GC: */
593SCLASS TAILQ_HEAD(, pthread)	_thread_gc_list
594    SCLASS_PRESET(TAILQ_HEAD_INITIALIZER(_thread_gc_list));
595
596SCLASS int	_thread_active_threads  SCLASS_PRESET(1);
597
598SCLASS TAILQ_HEAD(atfork_head, pthread_atfork) _thr_atfork_list;
599SCLASS umtx_t	_thr_atfork_lock;
600
601/* Default thread attributes: */
602SCLASS struct pthread_attr _pthread_attr_default
603    SCLASS_PRESET({
604	.sched_policy = SCHED_RR,
605	.sched_inherit = 0,
606	.sched_interval = TIMESLICE_USEC,
607	.prio = THR_DEFAULT_PRIORITY,
608	.suspend = THR_CREATE_RUNNING,
609	.flags = 0,
610	.arg_attr = NULL,
611	.cleanup_attr = NULL,
612	.stackaddr_attr = NULL,
613	.stacksize_attr = THR_STACK_DEFAULT,
614	.guardsize_attr = 0
615    });
616
617/* Default mutex attributes: */
618SCLASS struct pthread_mutex_attr _pthread_mutexattr_default
619    SCLASS_PRESET({
620	.m_type = PTHREAD_MUTEX_DEFAULT,
621	.m_protocol = PTHREAD_PRIO_NONE,
622	.m_ceiling = 0,
623	.m_flags = 0
624    });
625
626/* Default condition variable attributes: */
627SCLASS struct pthread_cond_attr _pthread_condattr_default
628    SCLASS_PRESET({
629	.c_pshared = PTHREAD_PROCESS_PRIVATE,
630	.c_clockid = CLOCK_REALTIME
631    });
632
633SCLASS pid_t		_thr_pid		SCLASS_PRESET(0);
634SCLASS int		_thr_guard_default;
635SCLASS int		_thr_stack_default	SCLASS_PRESET(THR_STACK_DEFAULT);
636SCLASS int		_thr_stack_initial	SCLASS_PRESET(THR_STACK_INITIAL);
637SCLASS int		_thr_page_size;
638/* Garbage thread count. */
639SCLASS int              _gc_count               SCLASS_PRESET(0);
640
641SCLASS umtx_t		_mutex_static_lock;
642SCLASS umtx_t		_cond_static_lock;
643SCLASS umtx_t		_rwlock_static_lock;
644SCLASS umtx_t		_keytable_lock;
645SCLASS umtx_t		_thr_list_lock;
646
647/* Undefine the storage class and preset specifiers: */
648#undef  SCLASS
649#undef	SCLASS_PRESET
650
651/*
652 * Function prototype definitions.
653 */
654__BEGIN_DECLS
655int	_thr_setthreaded(int);
656int	_mutex_cv_lock(pthread_mutex_t *);
657int	_mutex_cv_unlock(pthread_mutex_t *);
658void	_mutex_notify_priochange(struct pthread *, struct pthread *, int);
659int	_mutex_reinit(pthread_mutex_t *);
660void	_mutex_fork(struct pthread *curthread);
661void	_mutex_unlock_private(struct pthread *);
662void	_libpthread_init(struct pthread *);
663void	*_pthread_getspecific(pthread_key_t);
664int	_pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *);
665int	_pthread_cond_destroy(pthread_cond_t *);
666int	_pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
667int	_pthread_cond_timedwait(pthread_cond_t *, pthread_mutex_t *,
668	    const struct timespec *);
669int	_pthread_cond_signal(pthread_cond_t *);
670int	_pthread_cond_broadcast(pthread_cond_t *);
671int	_pthread_create(pthread_t * thread, const pthread_attr_t * attr,
672	    void *(*start_routine) (void *), void *arg);
673int	_pthread_key_create(pthread_key_t *, void (*) (void *));
674int	_pthread_key_delete(pthread_key_t);
675int	_pthread_mutex_destroy(pthread_mutex_t *);
676int	_pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
677int	_pthread_mutex_lock(pthread_mutex_t *);
678int	_pthread_mutex_trylock(pthread_mutex_t *);
679int	_pthread_mutex_unlock(pthread_mutex_t *);
680int	_pthread_mutexattr_init(pthread_mutexattr_t *);
681int	_pthread_mutexattr_destroy(pthread_mutexattr_t *);
682int	_pthread_mutexattr_settype(pthread_mutexattr_t *, int);
683int	_pthread_once(pthread_once_t *, void (*) (void));
684int	_pthread_rwlock_init(pthread_rwlock_t *, const pthread_rwlockattr_t *);
685int	_pthread_rwlock_destroy (pthread_rwlock_t *);
686struct pthread *_pthread_self(void);
687int	_pthread_setspecific(pthread_key_t, const void *);
688void	_pthread_testcancel(void);
689void	_pthread_yield(void);
690void	_pthread_cleanup_push(void (*routine) (void *), void *routine_arg);
691void	_pthread_cleanup_pop(int execute);
692struct pthread *_thr_alloc(struct pthread *);
693void	_thread_exit(char *, int, char *) __dead2;
694void	_thr_exit_cleanup(void);
695int	_thr_ref_add(struct pthread *, struct pthread *, int);
696void	_thr_ref_delete(struct pthread *, struct pthread *);
697int	_thr_find_thread(struct pthread *, struct pthread *, int);
698void	_thr_rtld_init(void);
699void	_thr_rtld_fini(void);
700int	_thr_stack_alloc(struct pthread_attr *);
701void	_thr_stack_free(struct pthread_attr *);
702void	_thr_free(struct pthread *, struct pthread *);
703void	_thr_gc(struct pthread *);
704void    _thread_cleanupspecific(void);
705void    _thread_dump_info(void);
706void	_thread_printf(int, const char *, ...);
707void	_thr_spinlock_init(void);
708int	_thr_cancel_enter(struct pthread *);
709void	_thr_cancel_leave(struct pthread *, int);
710void	_thr_signal_block(struct pthread *);
711void	_thr_signal_unblock(struct pthread *);
712void	_thr_signal_init(void);
713void	_thr_signal_deinit(void);
714int	_thr_send_sig(struct pthread *, int sig);
715void	_thr_list_init();
716void	_thr_hash_add(struct pthread *);
717void	_thr_hash_remove(struct pthread *);
718struct pthread *_thr_hash_find(struct pthread *);
719void	_thr_link(struct pthread *curthread, struct pthread *thread);
720void	_thr_unlink(struct pthread *curthread, struct pthread *thread);
721void	_thr_suspend_check(struct pthread *curthread);
722void	_thr_assert_lock_level() __dead2;
723
724/* #include <sys/aio.h> */
725#ifdef _SYS_AIO_H_
726int	__sys_aio_suspend(const struct aiocb * const[], int, const struct timespec *);
727#endif
728
729/* #include <fcntl.h> */
730#ifdef  _SYS_FCNTL_H_
731int     __sys_fcntl(int, int, ...);
732int     __sys_open(const char *, int, ...);
733#endif
734
735/* #include <sys/ioctl.h> */
736#ifdef _SYS_IOCTL_H_
737int	__sys_ioctl(int, unsigned long, ...);
738#endif
739
740/* #inclde <sched.h> */
741#ifdef	_SCHED_H_
742int	__sys_sched_yield(void);
743#endif
744
745/* #include <signal.h> */
746#ifdef _SIGNAL_H_
747int	__sys_kill(pid_t, int);
748int     __sys_sigaction(int, const struct sigaction *, struct sigaction *);
749int     __sys_sigpending(sigset_t *);
750int     __sys_sigprocmask(int, const sigset_t *, sigset_t *);
751int     __sys_sigsuspend(const sigset_t *);
752int     __sys_sigreturn(ucontext_t *);
753int     __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *);
754#endif
755
756/* #include <sys/socket.h> */
757#ifdef _SYS_SOCKET_H_
758int	__sys_accept(int, struct sockaddr *, socklen_t *);
759int	__sys_connect(int, const struct sockaddr *, socklen_t);
760ssize_t __sys_recv(int, void *, size_t, int);
761ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
762ssize_t __sys_recvmsg(int, struct msghdr *, int);
763int	__sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *,
764	    off_t *, int);
765ssize_t __sys_sendmsg(int, const struct msghdr *, int);
766ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
767#endif
768
769/* #include <sys/uio.h> */
770#ifdef  _SYS_UIO_H_
771ssize_t __sys_readv(int, const struct iovec *, int);
772ssize_t __sys_writev(int, const struct iovec *, int);
773#endif
774
775/* #include <time.h> */
776#ifdef	_TIME_H_
777int	__sys_nanosleep(const struct timespec *, struct timespec *);
778#endif
779
780/* #include <unistd.h> */
781#ifdef  _UNISTD_H_
782int     __sys_close(int);
783int     __sys_execve(const char *, char * const *, char * const *);
784int	__sys_fork(void);
785int	__sys_fsync(int);
786pid_t	__sys_getpid(void);
787int     __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
788ssize_t __sys_read(int, void *, size_t);
789ssize_t __sys_write(int, const void *, size_t);
790void	__sys_exit(int);
791int	__sys_sigwait(const sigset_t *, int *);
792int	__sys_sigtimedwait(const sigset_t *, siginfo_t *,
793		const struct timespec *);
794int	__sys_sigwaitinfo(const sigset_t *set, siginfo_t *info);
795#endif
796
797/* #include <poll.h> */
798#ifdef _SYS_POLL_H_
799int 	__sys_poll(struct pollfd *, unsigned, int);
800#endif
801
802/* #include <sys/mman.h> */
803#ifdef _SYS_MMAN_H_
804int	__sys_msync(void *, size_t, int);
805#endif
806
807static inline int
808_thr_isthreaded(void)
809{
810	return (__isthreaded != 0);
811}
812
813static inline int
814_thr_is_inited(void)
815{
816	return (_thr_initial != NULL);
817}
818
819static inline void
820_thr_check_init(void)
821{
822	if (_thr_initial == NULL)
823		_libpthread_init(NULL);
824}
825
826__END_DECLS
827
828#endif  /* !_THR_PRIVATE_H */
829