thr_private.h revision 154055
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 154055 2006-01-05 13:51:22Z 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#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
106struct 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
146struct 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
156struct 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
168struct pthread_cond_attr {
169	int		c_pshared;
170	int		c_clockid;
171};
172
173struct 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
180struct pthread_barrierattr {
181	int		pshared;
182};
183
184struct 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 */
198struct 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
220struct pthread_atfork {
221	TAILQ_ENTRY(pthread_atfork) qe;
222	void (*prepare)(void);
223	void (*parent)(void);
224	void (*child)(void);
225};
226
227struct 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
289struct pthread_rwlockattr {
290	int		pshared;
291};
292
293struct 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 */
304enum pthread_state {
305	PS_RUNNING,
306	PS_DEAD
307};
308
309union pthread_wait_data {
310	pthread_mutex_t	mutex;
311};
312
313struct pthread_specific_elem {
314	const void	*data;
315	int		seqno;
316};
317
318struct pthread_key {
319	volatile int	allocated;
320	volatile int	count;
321	int		seqno;
322	void            (*destructor)(void *);
323};
324
325/*
326 * Thread structure.
327 */
328struct 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	/*
353	 * Set to non-zero when this thread has entered a critical
354	 * region.  We allow for recursive entries into critical regions.
355	 */
356	int			critical_count;
357
358	/* Signal blocked counter. */
359	int			sigblock;
360
361	/* Queue entry for list of all threads. */
362	TAILQ_ENTRY(pthread)	tle;	/* link for all threads in process */
363
364	/* Queue entry for GC lists. */
365	TAILQ_ENTRY(pthread)	gcle;
366
367	/* Hash queue entry. */
368	LIST_ENTRY(pthread)	hle;
369
370	/* Threads reference count. */
371	int			refcount;
372
373	/*
374	 * Thread start routine, argument, stack pointer and thread
375	 * attributes.
376	 */
377	void			*(*start_routine)(void *);
378	void			*arg;
379	struct pthread_attr	attr;
380
381 	/*
382	 * Cancelability flags
383	 */
384#define	THR_CANCEL_DISABLE		0x0001
385#define	THR_CANCEL_EXITING		0x0002
386#define THR_CANCEL_AT_POINT		0x0004
387#define THR_CANCEL_NEEDED		0x0008
388#define	SHOULD_CANCEL(val)					\
389	(((val) & (THR_CANCEL_DISABLE | THR_CANCEL_EXITING |	\
390		 THR_CANCEL_NEEDED)) == THR_CANCEL_NEEDED)
391
392#define	SHOULD_ASYNC_CANCEL(val)				\
393	(((val) & (THR_CANCEL_DISABLE | THR_CANCEL_EXITING |	\
394		 THR_CANCEL_NEEDED | THR_CANCEL_AT_POINT)) ==	\
395		 (THR_CANCEL_NEEDED | THR_CANCEL_AT_POINT))
396	int			cancelflags;
397
398	/* Thread temporary signal mask. */
399	sigset_t		sigmask;
400
401	/* Thread state: */
402	umtx_t			state;
403
404	/*
405	 * Error variable used instead of errno. The function __error()
406	 * returns a pointer to this.
407	 */
408	int			error;
409
410	/*
411	 * The joiner is the thread that is joining to this thread.  The
412	 * join status keeps track of a join operation to another thread.
413	 */
414	struct pthread		*joiner;
415
416	/*
417	 * The current thread can belong to a priority mutex queue.
418	 * This is the synchronization queue link.
419	 */
420	TAILQ_ENTRY(pthread)	sqe;
421
422	/* Wait data. */
423	union pthread_wait_data data;
424
425	int			sflags;
426#define THR_FLAGS_IN_SYNCQ	0x0001
427
428	/* Miscellaneous flags; only set with scheduling lock held. */
429	int			flags;
430#define THR_FLAGS_PRIVATE	0x0001
431#define	THR_FLAGS_NEED_SUSPEND	0x0002	/* thread should be suspended */
432#define	THR_FLAGS_SUSPENDED	0x0004	/* thread is suspended */
433
434	/* Thread list flags; only set with thread list lock held. */
435	int			tlflags;
436#define	TLFLAGS_GC_SAFE		0x0001	/* thread safe for cleaning */
437#define	TLFLAGS_IN_TDLIST	0x0002	/* thread in all thread list */
438#define	TLFLAGS_IN_GCLIST	0x0004	/* thread in gc list */
439#define	TLFLAGS_DETACHED	0x0008	/* thread is detached */
440
441	/*
442	 * Base priority is the user setable and retrievable priority
443	 * of the thread.  It is only affected by explicit calls to
444	 * set thread priority and upon thread creation via a thread
445	 * attribute or default priority.
446	 */
447	char			base_priority;
448
449	/*
450	 * Inherited priority is the priority a thread inherits by
451	 * taking a priority inheritence or protection mutex.  It
452	 * is not affected by base priority changes.  Inherited
453	 * priority defaults to and remains 0 until a mutex is taken
454	 * that is being waited on by any other thread whose priority
455	 * is non-zero.
456	 */
457	char			inherited_priority;
458
459	/*
460	 * Active priority is always the maximum of the threads base
461	 * priority and inherited priority.  When there is a change
462	 * in either the base or inherited priority, the active
463	 * priority must be recalculated.
464	 */
465	char			active_priority;
466
467	/* Number of priority ceiling or protection mutexes owned. */
468	int			priority_mutex_count;
469
470	/* Queue of currently owned simple type mutexes. */
471	TAILQ_HEAD(, pthread_mutex)	mutexq;
472
473	/* Queue of currently owned priority type mutexs. */
474	TAILQ_HEAD(, pthread_mutex)	pri_mutexq;
475
476	void				*ret;
477	struct pthread_specific_elem	*specific;
478	int				specific_data_count;
479
480	/* Number rwlocks rdlocks held. */
481	int			rdlock_count;
482
483	/*
484	 * Current locks bitmap for rtld. */
485	int			rtld_bits;
486
487	/* Thread control block */
488	struct tcb		*tcb;
489
490	/* Cleanup handlers Link List */
491	struct pthread_cleanup	*cleanup;
492
493	/* Enable event reporting */
494	int			report_events;
495
496	/* Event mask */
497	int			event_mask;
498
499	/* Event */
500	td_event_msg_t		event_buf;
501};
502
503#define	THR_IN_CRITICAL(thrd)				\
504	(((thrd)->locklevel > 0) ||			\
505	((thrd)->critical_count > 0))
506
507#define THR_UMTX_TRYLOCK(thrd, lck)			\
508	_thr_umtx_trylock((lck), (thrd)->tid)
509
510#define	THR_UMTX_LOCK(thrd, lck)			\
511	_thr_umtx_lock((lck), (thrd)->tid)
512
513#define	THR_UMTX_TIMEDLOCK(thrd, lck, timo)		\
514	_thr_umtx_timedlock((lck), (thrd)->tid, (timo))
515
516#define	THR_UMTX_UNLOCK(thrd, lck)			\
517	_thr_umtx_unlock((lck), (thrd)->tid)
518
519#define	THR_LOCK_ACQUIRE(thrd, lck)			\
520do {							\
521	(thrd)->locklevel++;				\
522	_thr_umtx_lock(lck, (thrd)->tid);		\
523} while (0)
524
525#define	THR_LOCK_RELEASE(thrd, lck)			\
526do {							\
527	if ((thrd)->locklevel > 0) {			\
528		_thr_umtx_unlock((lck), (thrd)->tid);	\
529		(thrd)->locklevel--;			\
530		_thr_ast(thrd);				\
531	} else { 					\
532		_thr_assert_lock_level();		\
533	}						\
534} while (0)
535
536#define	THR_LOCK(curthrd)		THR_LOCK_ACQUIRE(curthrd, &(curthrd)->lock)
537#define	THR_UNLOCK(curthrd)		THR_LOCK_RELEASE(curthrd, &(curthrd)->lock)
538#define	THR_THREAD_LOCK(curthrd, thr)	THR_LOCK_ACQUIRE(curthrd, &(thr)->lock)
539#define	THR_THREAD_UNLOCK(curthrd, thr)	THR_LOCK_RELEASE(curthrd, &(thr)->lock)
540
541#define	THREAD_LIST_LOCK(curthrd)				\
542do {								\
543	THR_LOCK_ACQUIRE((curthrd), &_thr_list_lock);		\
544} while (0)
545
546#define	THREAD_LIST_UNLOCK(curthrd)				\
547do {								\
548	THR_LOCK_RELEASE((curthrd), &_thr_list_lock);		\
549} while (0)
550
551/*
552 * Macros to insert/remove threads to the all thread list and
553 * the gc list.
554 */
555#define	THR_LIST_ADD(thrd) do {					\
556	if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) == 0) {	\
557		TAILQ_INSERT_HEAD(&_thread_list, thrd, tle);	\
558		_thr_hash_add(thrd);				\
559		(thrd)->tlflags |= TLFLAGS_IN_TDLIST;		\
560	}							\
561} while (0)
562#define	THR_LIST_REMOVE(thrd) do {				\
563	if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) != 0) {	\
564		TAILQ_REMOVE(&_thread_list, thrd, tle);		\
565		_thr_hash_remove(thrd);				\
566		(thrd)->tlflags &= ~TLFLAGS_IN_TDLIST;		\
567	}							\
568} while (0)
569#define	THR_GCLIST_ADD(thrd) do {				\
570	if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) == 0) {	\
571		TAILQ_INSERT_HEAD(&_thread_gc_list, thrd, gcle);\
572		(thrd)->tlflags |= TLFLAGS_IN_GCLIST;		\
573		_gc_count++;					\
574	}							\
575} while (0)
576#define	THR_GCLIST_REMOVE(thrd) do {				\
577	if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) != 0) {	\
578		TAILQ_REMOVE(&_thread_gc_list, thrd, gcle);	\
579		(thrd)->tlflags &= ~TLFLAGS_IN_GCLIST;		\
580		_gc_count--;					\
581	}							\
582} while (0)
583
584#define GC_NEEDED()	(_gc_count >= 5)
585
586#define	THR_IN_SYNCQ(thrd)	(((thrd)->sflags & THR_FLAGS_IN_SYNCQ) != 0)
587
588#define SHOULD_REPORT_EVENT(curthr, e)			\
589	(curthr->report_events && 			\
590	 (((curthr)->event_mask | _thread_event_mask ) & e) != 0)
591
592extern int __isthreaded;
593
594/*
595 * Global variables for the pthread kernel.
596 */
597
598extern void		*_usrstack __hidden;
599extern struct pthread	*_thr_initial __hidden;
600extern int		_thr_scope_system __hidden;
601
602/* For debugger */
603extern int		_libthr_debug;
604extern int		_thread_event_mask;
605extern struct pthread	*_thread_last_event;
606
607/* List of all threads: */
608extern pthreadlist	_thread_list;
609
610/* List of threads needing GC: */
611extern pthreadlist	_thread_gc_list __hidden;
612
613extern int		_thread_active_threads;
614extern atfork_head	_thr_atfork_list __hidden;
615extern umtx_t		_thr_atfork_lock __hidden;
616
617/* Default thread attributes: */
618extern struct pthread_attr _pthread_attr_default __hidden;
619
620/* Default mutex attributes: */
621extern struct pthread_mutex_attr _pthread_mutexattr_default __hidden;
622
623/* Default condition variable attributes: */
624extern struct pthread_cond_attr _pthread_condattr_default __hidden;
625
626extern pid_t	_thr_pid __hidden;
627extern int	_thr_guard_default __hidden;
628extern int	_thr_stack_default __hidden;
629extern int	_thr_stack_initial __hidden;
630extern int	_thr_page_size __hidden;
631/* Garbage thread count. */
632extern int	_gc_count __hidden;
633
634extern umtx_t	_mutex_static_lock __hidden;
635extern umtx_t	_cond_static_lock __hidden;
636extern umtx_t	_rwlock_static_lock __hidden;
637extern umtx_t	_keytable_lock __hidden;
638extern umtx_t	_thr_list_lock __hidden;
639extern umtx_t	_thr_event_lock __hidden;
640
641/*
642 * Function prototype definitions.
643 */
644__BEGIN_DECLS
645int	_thr_setthreaded(int) __hidden;
646int	_mutex_cv_lock(pthread_mutex_t *) __hidden;
647int	_mutex_cv_unlock(pthread_mutex_t *) __hidden;
648void	_mutex_notify_priochange(struct pthread *, struct pthread *, int) __hidden;
649int	_mutex_reinit(pthread_mutex_t *) __hidden;
650void	_mutex_fork(struct pthread *curthread) __hidden;
651void	_mutex_unlock_private(struct pthread *) __hidden;
652void	_libpthread_init(struct pthread *) __hidden;
653void	*_pthread_getspecific(pthread_key_t);
654int	_pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *);
655int	_pthread_cond_destroy(pthread_cond_t *);
656int	_pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
657int	_pthread_cond_timedwait(pthread_cond_t *, pthread_mutex_t *,
658	    const struct timespec *);
659int	_pthread_cond_signal(pthread_cond_t *);
660int	_pthread_cond_broadcast(pthread_cond_t *);
661int	_pthread_create(pthread_t * thread, const pthread_attr_t * attr,
662	    void *(*start_routine) (void *), void *arg);
663int	_pthread_key_create(pthread_key_t *, void (*) (void *));
664int	_pthread_key_delete(pthread_key_t);
665int	_pthread_mutex_destroy(pthread_mutex_t *);
666int	_pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
667int	_pthread_mutex_lock(pthread_mutex_t *);
668int	_pthread_mutex_trylock(pthread_mutex_t *);
669int	_pthread_mutex_unlock(pthread_mutex_t *);
670int	_pthread_mutexattr_init(pthread_mutexattr_t *);
671int	_pthread_mutexattr_destroy(pthread_mutexattr_t *);
672int	_pthread_mutexattr_settype(pthread_mutexattr_t *, int);
673int	_pthread_once(pthread_once_t *, void (*) (void));
674int	_pthread_rwlock_init(pthread_rwlock_t *, const pthread_rwlockattr_t *);
675int	_pthread_rwlock_destroy (pthread_rwlock_t *);
676struct pthread *_pthread_self(void);
677int	_pthread_setspecific(pthread_key_t, const void *);
678void	_pthread_testcancel(void);
679void	_pthread_yield(void);
680void	_pthread_cleanup_push(void (*routine) (void *), void *routine_arg);
681void	_pthread_cleanup_pop(int execute);
682struct pthread *_thr_alloc(struct pthread *) __hidden;
683void	_thread_exit(char *, int, char *) __hidden __dead2;
684void	_thr_exit_cleanup(void) __hidden;
685int	_thr_ref_add(struct pthread *, struct pthread *, int) __hidden;
686void	_thr_ref_delete(struct pthread *, struct pthread *) __hidden;
687void	_thr_ref_delete_unlocked(struct pthread *, struct pthread *) __hidden;
688int	_thr_find_thread(struct pthread *, struct pthread *, int) __hidden;
689void	_thr_rtld_init(void) __hidden;
690void	_thr_rtld_fini(void) __hidden;
691int	_thr_stack_alloc(struct pthread_attr *) __hidden;
692void	_thr_stack_free(struct pthread_attr *) __hidden;
693void	_thr_free(struct pthread *, struct pthread *) __hidden;
694void	_thr_gc(struct pthread *) __hidden;
695void    _thread_cleanupspecific(void) __hidden;
696void    _thread_dump_info(void) __hidden;
697void	_thread_printf(int, const char *, ...) __hidden;
698void	_thr_spinlock_init(void) __hidden;
699int	_thr_cancel_enter(struct pthread *) __hidden;
700void	_thr_cancel_leave(struct pthread *, int) __hidden;
701void	_thr_signal_block(struct pthread *) __hidden;
702void	_thr_signal_unblock(struct pthread *) __hidden;
703void	_thr_signal_init(void) __hidden;
704void	_thr_signal_deinit(void) __hidden;
705int	_thr_send_sig(struct pthread *, int sig) __hidden;
706void	_thr_list_init(void) __hidden;
707void	_thr_hash_add(struct pthread *) __hidden;
708void	_thr_hash_remove(struct pthread *) __hidden;
709struct pthread *_thr_hash_find(struct pthread *) __hidden;
710void	_thr_link(struct pthread *, struct pthread *) __hidden;
711void	_thr_unlink(struct pthread *, struct pthread *) __hidden;
712void	_thr_suspend_check(struct pthread *) __hidden;
713void	_thr_assert_lock_level(void) __hidden __dead2;
714void	_thr_ast(struct pthread *) __hidden;
715void	_thr_timer_init(void) __hidden;
716void	_thr_report_creation(struct pthread *curthread,
717			   struct pthread *newthread) __hidden;
718void	_thr_report_death(struct pthread *curthread) __hidden;
719void	_thread_bp_create(void);
720void	_thread_bp_death(void);
721
722/* #include <sys/aio.h> */
723#ifdef _SYS_AIO_H_
724int	__sys_aio_suspend(const struct aiocb * const[], int, const struct timespec *);
725#endif
726
727/* #include <fcntl.h> */
728#ifdef  _SYS_FCNTL_H_
729int     __sys_fcntl(int, int, ...);
730int     __sys_open(const char *, int, ...);
731#endif
732
733/* #include <sys/ioctl.h> */
734#ifdef _SYS_IOCTL_H_
735int	__sys_ioctl(int, unsigned long, ...);
736#endif
737
738/* #inclde <sched.h> */
739#ifdef	_SCHED_H_
740int	__sys_sched_yield(void);
741#endif
742
743/* #include <signal.h> */
744#ifdef _SIGNAL_H_
745int	__sys_kill(pid_t, int);
746int     __sys_sigaction(int, const struct sigaction *, struct sigaction *);
747int     __sys_sigpending(sigset_t *);
748int     __sys_sigprocmask(int, const sigset_t *, sigset_t *);
749int     __sys_sigsuspend(const sigset_t *);
750int     __sys_sigreturn(ucontext_t *);
751int     __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *);
752#endif
753
754/* #include <sys/socket.h> */
755#ifdef _SYS_SOCKET_H_
756int	__sys_accept(int, struct sockaddr *, socklen_t *);
757int	__sys_connect(int, const struct sockaddr *, socklen_t);
758ssize_t __sys_recv(int, void *, size_t, int);
759ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
760ssize_t __sys_recvmsg(int, struct msghdr *, int);
761int	__sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *,
762	    off_t *, int);
763ssize_t __sys_sendmsg(int, const struct msghdr *, int);
764ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
765#endif
766
767/* #include <sys/uio.h> */
768#ifdef  _SYS_UIO_H_
769ssize_t __sys_readv(int, const struct iovec *, int);
770ssize_t __sys_writev(int, const struct iovec *, int);
771#endif
772
773/* #include <time.h> */
774#ifdef	_TIME_H_
775int	__sys_nanosleep(const struct timespec *, struct timespec *);
776#endif
777
778/* #include <unistd.h> */
779#ifdef  _UNISTD_H_
780int     __sys_close(int);
781int     __sys_execve(const char *, char * const *, char * const *);
782int	__sys_fork(void);
783int	__sys_fsync(int);
784pid_t	__sys_getpid(void);
785int     __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
786ssize_t __sys_read(int, void *, size_t);
787ssize_t __sys_write(int, const void *, size_t);
788void	__sys_exit(int);
789int	__sys_sigwait(const sigset_t *, int *);
790int	__sys_sigtimedwait(const sigset_t *, siginfo_t *,
791		const struct timespec *);
792int	__sys_sigwaitinfo(const sigset_t *set, siginfo_t *info);
793#endif
794
795/* #include <poll.h> */
796#ifdef _SYS_POLL_H_
797int 	__sys_poll(struct pollfd *, unsigned, int);
798#endif
799
800/* #include <sys/mman.h> */
801#ifdef _SYS_MMAN_H_
802int	__sys_msync(void *, size_t, int);
803#endif
804
805static inline int
806_thr_isthreaded(void)
807{
808	return (__isthreaded != 0);
809}
810
811static inline int
812_thr_is_inited(void)
813{
814	return (_thr_initial != NULL);
815}
816
817static inline void
818_thr_check_init(void)
819{
820	if (_thr_initial == NULL)
821		_libpthread_init(NULL);
822}
823
824__END_DECLS
825
826#endif  /* !_THR_PRIVATE_H */
827