xref: /freebsd/lib/libthr/thread/thr_private.h (revision 730cecb05aaf016ac52ef7cfc691ccec3a0408cd)
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 <sys/param.h>
43 #include <sys/cpuset.h>
44 #include <machine/atomic.h>
45 #include <errno.h>
46 #include <limits.h>
47 #include <signal.h>
48 #include <stddef.h>
49 #include <stdio.h>
50 #include <unistd.h>
51 #include <ucontext.h>
52 #include <sys/thr.h>
53 #include <pthread.h>
54 
55 #define	SYM_FB10(sym)			__CONCAT(sym, _fb10)
56 #define	SYM_FBP10(sym)			__CONCAT(sym, _fbp10)
57 #define	WEAK_REF(sym, alias)		__weak_reference(sym, alias)
58 #define	SYM_COMPAT(sym, impl, ver)	__sym_compat(sym, impl, ver)
59 #define	SYM_DEFAULT(sym, impl, ver)	__sym_default(sym, impl, ver)
60 
61 #define	FB10_COMPAT(func, sym)				\
62 	WEAK_REF(func, SYM_FB10(sym));			\
63 	SYM_COMPAT(sym, SYM_FB10(sym), FBSD_1.0)
64 
65 #define	FB10_COMPAT_PRIVATE(func, sym)			\
66 	WEAK_REF(func, SYM_FBP10(sym));			\
67 	SYM_DEFAULT(sym, SYM_FBP10(sym), FBSDprivate_1.0)
68 
69 #include "pthread_md.h"
70 #include "thr_umtx.h"
71 #include "thread_db.h"
72 
73 #ifdef _PTHREAD_FORCED_UNWIND
74 #define _BSD_SOURCE
75 #include <unwind.h>
76 #endif
77 
78 typedef TAILQ_HEAD(pthreadlist, pthread) pthreadlist;
79 typedef TAILQ_HEAD(atfork_head, pthread_atfork) atfork_head;
80 TAILQ_HEAD(mutex_queue, pthread_mutex);
81 
82 /* Signal to do cancellation */
83 #define	SIGCANCEL		SIGTHR
84 
85 /*
86  * Kernel fatal error handler macro.
87  */
88 #define PANIC(string)		_thread_exit(__FILE__,__LINE__,string)
89 
90 /* Output debug messages like this: */
91 #define stdout_debug(args...)	_thread_printf(STDOUT_FILENO, ##args)
92 #define stderr_debug(args...)	_thread_printf(STDERR_FILENO, ##args)
93 
94 #ifdef _PTHREADS_INVARIANTS
95 #define THR_ASSERT(cond, msg) do {	\
96 	if (__predict_false(!(cond)))	\
97 		PANIC(msg);		\
98 } while (0)
99 #else
100 #define THR_ASSERT(cond, msg)
101 #endif
102 
103 #ifdef PIC
104 # define STATIC_LIB_REQUIRE(name)
105 #else
106 # define STATIC_LIB_REQUIRE(name) __asm (".globl " #name)
107 #endif
108 
109 #define	TIMESPEC_ADD(dst, src, val)				\
110 	do { 							\
111 		(dst)->tv_sec = (src)->tv_sec + (val)->tv_sec;	\
112 		(dst)->tv_nsec = (src)->tv_nsec + (val)->tv_nsec; \
113 		if ((dst)->tv_nsec >= 1000000000) {		\
114 			(dst)->tv_sec++;			\
115 			(dst)->tv_nsec -= 1000000000;		\
116 		}						\
117 	} while (0)
118 
119 #define	TIMESPEC_SUB(dst, src, val)				\
120 	do { 							\
121 		(dst)->tv_sec = (src)->tv_sec - (val)->tv_sec;	\
122 		(dst)->tv_nsec = (src)->tv_nsec - (val)->tv_nsec; \
123 		if ((dst)->tv_nsec < 0) {			\
124 			(dst)->tv_sec--;			\
125 			(dst)->tv_nsec += 1000000000;		\
126 		}						\
127 	} while (0)
128 
129 /* XXX These values should be same as those defined in pthread.h */
130 #define	THR_MUTEX_INITIALIZER		((struct pthread_mutex *)NULL)
131 #define	THR_ADAPTIVE_MUTEX_INITIALIZER	((struct pthread_mutex *)1)
132 #define	THR_MUTEX_DESTROYED		((struct pthread_mutex *)2)
133 #define	THR_COND_INITIALIZER		((struct pthread_cond *)NULL)
134 #define	THR_COND_DESTROYED		((struct pthread_cond *)1)
135 #define	THR_RWLOCK_INITIALIZER		((struct pthread_rwlock *)NULL)
136 #define	THR_RWLOCK_DESTROYED		((struct pthread_rwlock *)1)
137 
138 #define PMUTEX_FLAG_TYPE_MASK	0x0ff
139 #define PMUTEX_FLAG_PRIVATE	0x100
140 #define PMUTEX_FLAG_DEFERED	0x200
141 #define PMUTEX_TYPE(mtxflags)	((mtxflags) & PMUTEX_FLAG_TYPE_MASK)
142 
143 #define MAX_DEFER_WAITERS       50
144 
145 struct pthread_mutex {
146 	/*
147 	 * Lock for accesses to this structure.
148 	 */
149 	struct umutex			m_lock;
150 	int				m_flags;
151 	struct pthread			*m_owner;
152 	int				m_count;
153 	int				m_spinloops;
154 	int				m_yieldloops;
155 	/*
156 	 * Link for all mutexes a thread currently owns.
157 	 */
158 	TAILQ_ENTRY(pthread_mutex)	m_qe;
159 };
160 
161 struct pthread_mutex_attr {
162 	enum pthread_mutextype	m_type;
163 	int			m_protocol;
164 	int			m_ceiling;
165 };
166 
167 #define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \
168 	{ PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE }
169 
170 struct pthread_cond {
171 	__uint32_t	__has_user_waiters;
172 	__uint32_t	__has_kern_waiters;
173 	__uint32_t	__flags;
174 	__uint32_t	__clock_id;
175 };
176 
177 struct pthread_cond_attr {
178 	int		c_pshared;
179 	int		c_clockid;
180 };
181 
182 struct pthread_barrier {
183 	struct umutex		b_lock;
184 	struct ucond		b_cv;
185 	int64_t			b_cycle;
186 	int			b_count;
187 	int			b_waiters;
188 	int			b_refcount;
189 	int			b_destroying;
190 };
191 
192 struct pthread_barrierattr {
193 	int		pshared;
194 };
195 
196 struct pthread_spinlock {
197 	struct umutex	s_lock;
198 };
199 
200 /*
201  * Flags for condition variables.
202  */
203 #define COND_FLAGS_PRIVATE	0x01
204 #define COND_FLAGS_INITED	0x02
205 #define COND_FLAGS_BUSY		0x04
206 
207 /*
208  * Cleanup definitions.
209  */
210 struct pthread_cleanup {
211 	struct pthread_cleanup	*prev;
212 	void			(*routine)(void *);
213 	void			*routine_arg;
214 	int			onheap;
215 };
216 
217 #define	THR_CLEANUP_PUSH(td, func, arg) {		\
218 	struct pthread_cleanup __cup;			\
219 							\
220 	__cup.routine = func;				\
221 	__cup.routine_arg = arg;			\
222 	__cup.onheap = 0;				\
223 	__cup.prev = (td)->cleanup;			\
224 	(td)->cleanup = &__cup;
225 
226 #define	THR_CLEANUP_POP(td, exec)			\
227 	(td)->cleanup = __cup.prev;			\
228 	if ((exec) != 0)				\
229 		__cup.routine(__cup.routine_arg);	\
230 }
231 
232 struct pthread_atfork {
233 	TAILQ_ENTRY(pthread_atfork) qe;
234 	void (*prepare)(void);
235 	void (*parent)(void);
236 	void (*child)(void);
237 };
238 
239 struct pthread_attr {
240 #define pthread_attr_start_copy	sched_policy
241 	int	sched_policy;
242 	int	sched_inherit;
243 	int	prio;
244 	int	suspend;
245 #define	THR_STACK_USER		0x100	/* 0xFF reserved for <pthread.h> */
246 	int	flags;
247 	void	*stackaddr_attr;
248 	size_t	stacksize_attr;
249 	size_t	guardsize_attr;
250 #define pthread_attr_end_copy	cpuset
251 	cpuset_t	*cpuset;
252 	size_t	cpusetsize;
253 };
254 
255 struct wake_addr {
256 	struct wake_addr *link;
257 	unsigned int	value;
258 	char		pad[12];
259 };
260 
261 struct sleepqueue {
262 	TAILQ_HEAD(, pthread)    sq_blocked;
263 	SLIST_HEAD(, sleepqueue) sq_freeq;
264 	LIST_ENTRY(sleepqueue)   sq_hash;
265 	SLIST_ENTRY(sleepqueue)  sq_flink;
266 	void			 *sq_wchan;
267 	int			 sq_type;
268 };
269 
270 /*
271  * Thread creation state attributes.
272  */
273 #define THR_CREATE_RUNNING		0
274 #define THR_CREATE_SUSPENDED		1
275 
276 /*
277  * Miscellaneous definitions.
278  */
279 #define THR_STACK_DEFAULT		(sizeof(void *) / 4 * 1024 * 1024)
280 
281 /*
282  * Maximum size of initial thread's stack.  This perhaps deserves to be larger
283  * than the stacks of other threads, since many applications are likely to run
284  * almost entirely on this stack.
285  */
286 #define THR_STACK_INITIAL		(THR_STACK_DEFAULT * 2)
287 
288 /*
289  * Define priorities returned by kernel.
290  */
291 #define THR_MIN_PRIORITY		(_thr_priorities[SCHED_OTHER-1].pri_min)
292 #define THR_MAX_PRIORITY		(_thr_priorities[SCHED_OTHER-1].pri_max)
293 #define THR_DEF_PRIORITY		(_thr_priorities[SCHED_OTHER-1].pri_default)
294 
295 #define THR_MIN_RR_PRIORITY		(_thr_priorities[SCHED_RR-1].pri_min)
296 #define THR_MAX_RR_PRIORITY		(_thr_priorities[SCHED_RR-1].pri_max)
297 #define THR_DEF_RR_PRIORITY		(_thr_priorities[SCHED_RR-1].pri_default)
298 
299 /* XXX The SCHED_FIFO should have same priority range as SCHED_RR */
300 #define THR_MIN_FIFO_PRIORITY		(_thr_priorities[SCHED_FIFO_1].pri_min)
301 #define THR_MAX_FIFO_PRIORITY		(_thr_priorities[SCHED_FIFO-1].pri_max)
302 #define THR_DEF_FIFO_PRIORITY		(_thr_priorities[SCHED_FIFO-1].pri_default)
303 
304 struct pthread_prio {
305 	int	pri_min;
306 	int	pri_max;
307 	int	pri_default;
308 };
309 
310 struct pthread_rwlockattr {
311 	int		pshared;
312 };
313 
314 struct pthread_rwlock {
315 	struct urwlock 	lock;
316 	struct pthread	*owner;
317 };
318 
319 /*
320  * Thread states.
321  */
322 enum pthread_state {
323 	PS_RUNNING,
324 	PS_DEAD
325 };
326 
327 struct pthread_specific_elem {
328 	const void	*data;
329 	int		seqno;
330 };
331 
332 struct pthread_key {
333 	volatile int	allocated;
334 	int		seqno;
335 	void            (*destructor)(void *);
336 };
337 
338 /*
339  * lwpid_t is 32bit but kernel thr API exports tid as long type
340  * in very earily date.
341  */
342 #define TID(thread)	((uint32_t) ((thread)->tid))
343 
344 /*
345  * Thread structure.
346  */
347 struct pthread {
348 #define _pthread_startzero	tid
349 	/* Kernel thread id. */
350 	long			tid;
351 #define	TID_TERMINATED		1
352 
353 	/*
354 	 * Lock for accesses to this thread structure.
355 	 */
356 	struct umutex		lock;
357 
358 	/* Internal condition variable cycle number. */
359 	uint32_t		cycle;
360 
361 	/* How many low level locks the thread held. */
362 	int			locklevel;
363 
364 	/*
365 	 * Set to non-zero when this thread has entered a critical
366 	 * region.  We allow for recursive entries into critical regions.
367 	 */
368 	int			critical_count;
369 
370 	/* Signal blocked counter. */
371 	int			sigblock;
372 
373 	/* Queue entry for list of all threads. */
374 	TAILQ_ENTRY(pthread)	tle;	/* link for all threads in process */
375 
376 	/* Queue entry for GC lists. */
377 	TAILQ_ENTRY(pthread)	gcle;
378 
379 	/* Hash queue entry. */
380 	LIST_ENTRY(pthread)	hle;
381 
382 	/* Sleep queue entry */
383 	TAILQ_ENTRY(pthread)    wle;
384 
385 	/* Threads reference count. */
386 	int			refcount;
387 
388 	/*
389 	 * Thread start routine, argument, stack pointer and thread
390 	 * attributes.
391 	 */
392 	void			*(*start_routine)(void *);
393 	void			*arg;
394 	struct pthread_attr	attr;
395 
396 #define	SHOULD_CANCEL(thr)					\
397 	((thr)->cancel_pending && (thr)->cancel_enable &&	\
398 	 (thr)->no_cancel == 0)
399 
400 	/* Cancellation is enabled */
401 	int			cancel_enable;
402 
403 	/* Cancellation request is pending */
404 	int			cancel_pending;
405 
406 	/* Thread is at cancellation point */
407 	int			cancel_point;
408 
409 	/* Cancellation is temporarily disabled */
410 	int			no_cancel;
411 
412 	/* Asynchronouse cancellation is enabled */
413 	int			cancel_async;
414 
415 	/* Cancellation is in progress */
416 	int			cancelling;
417 
418 	/* Thread temporary signal mask. */
419 	sigset_t		sigmask;
420 
421 	/* Thread should unblock SIGCANCEL. */
422 	int			unblock_sigcancel;
423 
424 	/* In sigsuspend state */
425 	int			in_sigsuspend;
426 
427 	/* deferred signal info	*/
428 	siginfo_t		deferred_siginfo;
429 
430 	/* signal mask to restore. */
431 	sigset_t		deferred_sigmask;
432 
433 	/* the sigaction should be used for deferred signal. */
434 	struct sigaction	deferred_sigact;
435 
436 	/* Force new thread to exit. */
437 	int			force_exit;
438 
439 	/* Thread state: */
440 	enum pthread_state 	state;
441 
442 	/*
443 	 * Error variable used instead of errno. The function __error()
444 	 * returns a pointer to this.
445 	 */
446 	int			error;
447 
448 	/*
449 	 * The joiner is the thread that is joining to this thread.  The
450 	 * join status keeps track of a join operation to another thread.
451 	 */
452 	struct pthread		*joiner;
453 
454 	/* Miscellaneous flags; only set with scheduling lock held. */
455 	int			flags;
456 #define THR_FLAGS_PRIVATE	0x0001
457 #define	THR_FLAGS_NEED_SUSPEND	0x0002	/* thread should be suspended */
458 #define	THR_FLAGS_SUSPENDED	0x0004	/* thread is suspended */
459 #define	THR_FLAGS_DETACHED	0x0008	/* thread is detached */
460 
461 	/* Thread list flags; only set with thread list lock held. */
462 	int			tlflags;
463 #define	TLFLAGS_GC_SAFE		0x0001	/* thread safe for cleaning */
464 #define	TLFLAGS_IN_TDLIST	0x0002	/* thread in all thread list */
465 #define	TLFLAGS_IN_GCLIST	0x0004	/* thread in gc list */
466 
467 	/* Queue of currently owned NORMAL or PRIO_INHERIT type mutexes. */
468 	struct mutex_queue	mutexq;
469 
470 	/* Queue of all owned PRIO_PROTECT mutexes. */
471 	struct mutex_queue	pp_mutexq;
472 
473 	void				*ret;
474 	struct pthread_specific_elem	*specific;
475 	int				specific_data_count;
476 
477 	/* Number rwlocks rdlocks held. */
478 	int			rdlock_count;
479 
480 	/*
481 	 * Current locks bitmap for rtld. */
482 	int			rtld_bits;
483 
484 	/* Thread control block */
485 	struct tcb		*tcb;
486 
487 	/* Cleanup handlers Link List */
488 	struct pthread_cleanup	*cleanup;
489 
490 #ifdef _PTHREAD_FORCED_UNWIND
491 	struct _Unwind_Exception	ex;
492 	void			*unwind_stackend;
493 	int			unwind_disabled;
494 #endif
495 
496 	/*
497 	 * Magic value to help recognize a valid thread structure
498 	 * from an invalid one:
499 	 */
500 #define	THR_MAGIC		((u_int32_t) 0xd09ba115)
501 	u_int32_t		magic;
502 
503 	/* Enable event reporting */
504 	int			report_events;
505 
506 	/* Event mask */
507 	int			event_mask;
508 
509 	/* Event */
510 	td_event_msg_t		event_buf;
511 
512 	/* Wait channel */
513 	void			*wchan;
514 
515 	/* Referenced mutex. */
516 	struct pthread_mutex	*mutex_obj;
517 
518 	/* Thread will sleep. */
519 	int			will_sleep;
520 
521 	/* Number of threads deferred. */
522 	int			nwaiter_defer;
523 
524 	/* Deferred threads from pthread_cond_signal. */
525 	unsigned int 		*defer_waiters[MAX_DEFER_WAITERS];
526 #define _pthread_endzero	wake_addr
527 
528 	struct wake_addr	*wake_addr;
529 #define WAKE_ADDR(td)           ((td)->wake_addr)
530 
531 	/* Sleep queue */
532 	struct	sleepqueue	*sleepqueue;
533 
534 };
535 
536 #define THR_SHOULD_GC(thrd) 						\
537 	((thrd)->refcount == 0 && (thrd)->state == PS_DEAD &&		\
538 	 ((thrd)->flags & THR_FLAGS_DETACHED) != 0)
539 
540 #define	THR_IN_CRITICAL(thrd)				\
541 	(((thrd)->locklevel > 0) ||			\
542 	((thrd)->critical_count > 0))
543 
544 #define	THR_CRITICAL_ENTER(thrd)			\
545 	(thrd)->critical_count++
546 
547 #define	THR_CRITICAL_LEAVE(thrd)			\
548 	do {						\
549 		(thrd)->critical_count--;		\
550 		_thr_ast(thrd);				\
551 	} while (0)
552 
553 #define THR_UMUTEX_TRYLOCK(thrd, lck)			\
554 	_thr_umutex_trylock((lck), TID(thrd))
555 
556 #define	THR_UMUTEX_LOCK(thrd, lck)			\
557 	_thr_umutex_lock((lck), TID(thrd))
558 
559 #define	THR_UMUTEX_TIMEDLOCK(thrd, lck, timo)		\
560 	_thr_umutex_timedlock((lck), TID(thrd), (timo))
561 
562 #define	THR_UMUTEX_UNLOCK(thrd, lck)			\
563 	_thr_umutex_unlock((lck), TID(thrd))
564 
565 #define	THR_LOCK_ACQUIRE(thrd, lck)			\
566 do {							\
567 	(thrd)->locklevel++;				\
568 	_thr_umutex_lock(lck, TID(thrd));		\
569 } while (0)
570 
571 #define	THR_LOCK_ACQUIRE_SPIN(thrd, lck)		\
572 do {							\
573 	(thrd)->locklevel++;				\
574 	_thr_umutex_lock_spin(lck, TID(thrd));		\
575 } while (0)
576 
577 #ifdef	_PTHREADS_INVARIANTS
578 #define	THR_ASSERT_LOCKLEVEL(thrd)			\
579 do {							\
580 	if (__predict_false((thrd)->locklevel <= 0))	\
581 		_thr_assert_lock_level();		\
582 } while (0)
583 #else
584 #define THR_ASSERT_LOCKLEVEL(thrd)
585 #endif
586 
587 #define	THR_LOCK_RELEASE(thrd, lck)			\
588 do {							\
589 	THR_ASSERT_LOCKLEVEL(thrd);			\
590 	_thr_umutex_unlock((lck), TID(thrd));		\
591 	(thrd)->locklevel--;				\
592 	_thr_ast(thrd);					\
593 } while (0)
594 
595 #define	THR_LOCK(curthrd)		THR_LOCK_ACQUIRE(curthrd, &(curthrd)->lock)
596 #define	THR_UNLOCK(curthrd)		THR_LOCK_RELEASE(curthrd, &(curthrd)->lock)
597 #define	THR_THREAD_LOCK(curthrd, thr)	THR_LOCK_ACQUIRE(curthrd, &(thr)->lock)
598 #define	THR_THREAD_UNLOCK(curthrd, thr)	THR_LOCK_RELEASE(curthrd, &(thr)->lock)
599 
600 #define	THREAD_LIST_RDLOCK(curthrd)				\
601 do {								\
602 	(curthrd)->locklevel++;					\
603 	_thr_rwl_rdlock(&_thr_list_lock);			\
604 } while (0)
605 
606 #define	THREAD_LIST_WRLOCK(curthrd)				\
607 do {								\
608 	(curthrd)->locklevel++;					\
609 	_thr_rwl_wrlock(&_thr_list_lock);			\
610 } while (0)
611 
612 #define	THREAD_LIST_UNLOCK(curthrd)				\
613 do {								\
614 	_thr_rwl_unlock(&_thr_list_lock);			\
615 	(curthrd)->locklevel--;					\
616 	_thr_ast(curthrd);					\
617 } while (0)
618 
619 /*
620  * Macros to insert/remove threads to the all thread list and
621  * the gc list.
622  */
623 #define	THR_LIST_ADD(thrd) do {					\
624 	if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) == 0) {	\
625 		TAILQ_INSERT_HEAD(&_thread_list, thrd, tle);	\
626 		_thr_hash_add(thrd);				\
627 		(thrd)->tlflags |= TLFLAGS_IN_TDLIST;		\
628 	}							\
629 } while (0)
630 #define	THR_LIST_REMOVE(thrd) do {				\
631 	if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) != 0) {	\
632 		TAILQ_REMOVE(&_thread_list, thrd, tle);		\
633 		_thr_hash_remove(thrd);				\
634 		(thrd)->tlflags &= ~TLFLAGS_IN_TDLIST;		\
635 	}							\
636 } while (0)
637 #define	THR_GCLIST_ADD(thrd) do {				\
638 	if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) == 0) {	\
639 		TAILQ_INSERT_HEAD(&_thread_gc_list, thrd, gcle);\
640 		(thrd)->tlflags |= TLFLAGS_IN_GCLIST;		\
641 		_gc_count++;					\
642 	}							\
643 } while (0)
644 #define	THR_GCLIST_REMOVE(thrd) do {				\
645 	if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) != 0) {	\
646 		TAILQ_REMOVE(&_thread_gc_list, thrd, gcle);	\
647 		(thrd)->tlflags &= ~TLFLAGS_IN_GCLIST;		\
648 		_gc_count--;					\
649 	}							\
650 } while (0)
651 
652 #define THR_REF_ADD(curthread, pthread) {			\
653 	THR_CRITICAL_ENTER(curthread);				\
654 	pthread->refcount++;					\
655 } while (0)
656 
657 #define THR_REF_DEL(curthread, pthread) {			\
658 	pthread->refcount--;					\
659 	THR_CRITICAL_LEAVE(curthread);				\
660 } while (0)
661 
662 #define GC_NEEDED()	(_gc_count >= 5)
663 
664 #define SHOULD_REPORT_EVENT(curthr, e)			\
665 	(curthr->report_events && 			\
666 	 (((curthr)->event_mask | _thread_event_mask ) & e) != 0)
667 
668 extern int __isthreaded;
669 
670 /*
671  * Global variables for the pthread kernel.
672  */
673 
674 extern char		*_usrstack __hidden;
675 extern struct pthread	*_thr_initial __hidden;
676 
677 /* For debugger */
678 extern int		_libthr_debug;
679 extern int		_thread_event_mask;
680 extern struct pthread	*_thread_last_event;
681 
682 /* List of all threads: */
683 extern pthreadlist	_thread_list;
684 
685 /* List of threads needing GC: */
686 extern pthreadlist	_thread_gc_list __hidden;
687 
688 extern int		_thread_active_threads;
689 extern atfork_head	_thr_atfork_list __hidden;
690 extern struct urwlock	_thr_atfork_lock __hidden;
691 
692 /* Default thread attributes: */
693 extern struct pthread_attr _pthread_attr_default __hidden;
694 
695 /* Default mutex attributes: */
696 extern struct pthread_mutex_attr _pthread_mutexattr_default __hidden;
697 extern struct pthread_mutex_attr _pthread_mutexattr_adaptive_default __hidden;
698 
699 /* Default condition variable attributes: */
700 extern struct pthread_cond_attr _pthread_condattr_default __hidden;
701 
702 extern struct pthread_prio _thr_priorities[] __hidden;
703 
704 extern pid_t	_thr_pid __hidden;
705 extern int	_thr_is_smp __hidden;
706 
707 extern size_t	_thr_guard_default __hidden;
708 extern size_t	_thr_stack_default __hidden;
709 extern size_t	_thr_stack_initial __hidden;
710 extern int	_thr_page_size __hidden;
711 extern int	_thr_spinloops __hidden;
712 extern int	_thr_yieldloops __hidden;
713 extern int	_thr_queuefifo __hidden;
714 
715 /* Garbage thread count. */
716 extern int	_gc_count __hidden;
717 
718 extern struct umutex	_mutex_static_lock __hidden;
719 extern struct umutex	_cond_static_lock __hidden;
720 extern struct umutex	_rwlock_static_lock __hidden;
721 extern struct umutex	_keytable_lock __hidden;
722 extern struct urwlock	_thr_list_lock __hidden;
723 extern struct umutex	_thr_event_lock __hidden;
724 extern struct umutex	_suspend_all_lock __hidden;
725 extern int		_suspend_all_waiters __hidden;
726 extern int		_suspend_all_cycle __hidden;
727 extern struct pthread	*_single_thread __hidden;
728 
729 /*
730  * Function prototype definitions.
731  */
732 __BEGIN_DECLS
733 int	_thr_setthreaded(int) __hidden;
734 int	_mutex_cv_lock(struct pthread_mutex *, int) __hidden;
735 int	_mutex_cv_unlock(struct pthread_mutex *, int *, int *) __hidden;
736 int     _mutex_cv_attach(struct pthread_mutex *, int) __hidden;
737 int     _mutex_cv_detach(struct pthread_mutex *, int *) __hidden;
738 int     _mutex_owned(struct pthread *, const struct pthread_mutex *) __hidden;
739 int	_mutex_reinit(pthread_mutex_t *) __hidden;
740 void	_mutex_fork(struct pthread *curthread) __hidden;
741 void	_libpthread_init(struct pthread *) __hidden;
742 struct pthread *_thr_alloc(struct pthread *) __hidden;
743 void	_thread_exit(const char *, int, const char *) __hidden __dead2;
744 int	_thr_ref_add(struct pthread *, struct pthread *, int) __hidden;
745 void	_thr_ref_delete(struct pthread *, struct pthread *) __hidden;
746 void	_thr_ref_delete_unlocked(struct pthread *, struct pthread *) __hidden;
747 int	_thr_find_thread(struct pthread *, struct pthread *, int) __hidden;
748 void	_thr_rtld_init(void) __hidden;
749 void	_thr_rtld_fini(void) __hidden;
750 void	_thr_rtld_postfork_child(void) __hidden;
751 int	_thr_stack_alloc(struct pthread_attr *) __hidden;
752 void	_thr_stack_free(struct pthread_attr *) __hidden;
753 void	_thr_free(struct pthread *, struct pthread *) __hidden;
754 void	_thr_gc(struct pthread *) __hidden;
755 void    _thread_cleanupspecific(void) __hidden;
756 void	_thread_printf(int, const char *, ...) __hidden;
757 void	_thr_spinlock_init(void) __hidden;
758 void	_thr_cancel_enter(struct pthread *) __hidden;
759 void	_thr_cancel_enter2(struct pthread *, int) __hidden;
760 void	_thr_cancel_leave(struct pthread *, int) __hidden;
761 void	_thr_testcancel(struct pthread *) __hidden;
762 void	_thr_signal_block(struct pthread *) __hidden;
763 void	_thr_signal_unblock(struct pthread *) __hidden;
764 void	_thr_signal_init(void) __hidden;
765 void	_thr_signal_deinit(void) __hidden;
766 int	_thr_send_sig(struct pthread *, int sig) __hidden;
767 void	_thr_list_init(void) __hidden;
768 void	_thr_hash_add(struct pthread *) __hidden;
769 void	_thr_hash_remove(struct pthread *) __hidden;
770 struct pthread *_thr_hash_find(struct pthread *) __hidden;
771 void	_thr_link(struct pthread *, struct pthread *) __hidden;
772 void	_thr_unlink(struct pthread *, struct pthread *) __hidden;
773 void	_thr_assert_lock_level(void) __hidden __dead2;
774 void	_thr_ast(struct pthread *) __hidden;
775 void	_thr_once_init(void) __hidden;
776 void	_thr_report_creation(struct pthread *curthread,
777 	    struct pthread *newthread) __hidden;
778 void	_thr_report_death(struct pthread *curthread) __hidden;
779 int	_thr_getscheduler(lwpid_t, int *, struct sched_param *) __hidden;
780 int	_thr_setscheduler(lwpid_t, int, const struct sched_param *) __hidden;
781 void	_thr_signal_prefork(void) __hidden;
782 void	_thr_signal_postfork(void) __hidden;
783 void	_thr_signal_postfork_child(void) __hidden;
784 void	_thr_suspend_all_lock(struct pthread *) __hidden;
785 void	_thr_suspend_all_unlock(struct pthread *) __hidden;
786 void	_thr_try_gc(struct pthread *, struct pthread *) __hidden;
787 int	_rtp_to_schedparam(const struct rtprio *rtp, int *policy,
788 		struct sched_param *param) __hidden;
789 int	_schedparam_to_rtp(int policy, const struct sched_param *param,
790 		struct rtprio *rtp) __hidden;
791 void	_thread_bp_create(void);
792 void	_thread_bp_death(void);
793 int	_sched_yield(void);
794 
795 void	_pthread_cleanup_push(void (*)(void *), void *);
796 void	_pthread_cleanup_pop(int);
797 void	_pthread_exit_mask(void *status, sigset_t *mask) __dead2 __hidden;
798 void	_pthread_cancel_enter(int maycancel);
799 void 	_pthread_cancel_leave(int maycancel);
800 
801 /* #include <fcntl.h> */
802 #ifdef  _SYS_FCNTL_H_
803 int     __sys_fcntl(int, int, ...);
804 int     __sys_open(const char *, int, ...);
805 int     __sys_openat(int, const char *, int, ...);
806 #endif
807 
808 /* #include <signal.h> */
809 #ifdef _SIGNAL_H_
810 int	__sys_kill(pid_t, int);
811 int     __sys_sigaction(int, const struct sigaction *, struct sigaction *);
812 int     __sys_sigpending(sigset_t *);
813 int     __sys_sigprocmask(int, const sigset_t *, sigset_t *);
814 int     __sys_sigsuspend(const sigset_t *);
815 int     __sys_sigreturn(const ucontext_t *);
816 int     __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *);
817 int	__sys_sigwait(const sigset_t *, int *);
818 int	__sys_sigtimedwait(const sigset_t *, siginfo_t *,
819 		const struct timespec *);
820 int	__sys_sigwaitinfo(const sigset_t *set, siginfo_t *info);
821 #endif
822 
823 /* #include <time.h> */
824 #ifdef	_TIME_H_
825 int	__sys_nanosleep(const struct timespec *, struct timespec *);
826 #endif
827 
828 /* #include <sys/ucontext.h> */
829 #ifdef _SYS_UCONTEXT_H_
830 int	__sys_setcontext(const ucontext_t *ucp);
831 int	__sys_swapcontext(ucontext_t *oucp, const ucontext_t *ucp);
832 #endif
833 
834 /* #include <unistd.h> */
835 #ifdef  _UNISTD_H_
836 int     __sys_close(int);
837 int	__sys_fork(void);
838 pid_t	__sys_getpid(void);
839 ssize_t __sys_read(int, void *, size_t);
840 ssize_t __sys_write(int, const void *, size_t);
841 void	__sys_exit(int);
842 #endif
843 
844 static inline int
845 _thr_isthreaded(void)
846 {
847 	return (__isthreaded != 0);
848 }
849 
850 static inline int
851 _thr_is_inited(void)
852 {
853 	return (_thr_initial != NULL);
854 }
855 
856 static inline void
857 _thr_check_init(void)
858 {
859 	if (_thr_initial == NULL)
860 		_libpthread_init(NULL);
861 }
862 
863 struct wake_addr *_thr_alloc_wake_addr(void);
864 void	_thr_release_wake_addr(struct wake_addr *);
865 int	_thr_sleep(struct pthread *, int, const struct timespec *);
866 
867 void _thr_wake_addr_init(void) __hidden;
868 
869 static inline void
870 _thr_clear_wake(struct pthread *td)
871 {
872 	td->wake_addr->value = 0;
873 }
874 
875 static inline int
876 _thr_is_woken(struct pthread *td)
877 {
878 	return td->wake_addr->value != 0;
879 }
880 
881 static inline void
882 _thr_set_wake(unsigned int *waddr)
883 {
884 	*waddr = 1;
885 	_thr_umtx_wake(waddr, INT_MAX, 0);
886 }
887 
888 void _thr_wake_all(unsigned int *waddrs[], int) __hidden;
889 
890 static inline struct pthread *
891 _sleepq_first(struct sleepqueue *sq)
892 {
893 	return TAILQ_FIRST(&sq->sq_blocked);
894 }
895 
896 void	_sleepq_init(void) __hidden;
897 struct sleepqueue *_sleepq_alloc(void) __hidden;
898 void	_sleepq_free(struct sleepqueue *) __hidden;
899 void	_sleepq_lock(void *) __hidden;
900 void	_sleepq_unlock(void *) __hidden;
901 struct sleepqueue *_sleepq_lookup(void *) __hidden;
902 void	_sleepq_add(void *, struct pthread *) __hidden;
903 int	_sleepq_remove(struct sleepqueue *, struct pthread *) __hidden;
904 void	_sleepq_drop(struct sleepqueue *,
905 		void (*cb)(struct pthread *, void *arg), void *) __hidden;
906 
907 struct dl_phdr_info;
908 void __pthread_cxa_finalize(struct dl_phdr_info *phdr_info);
909 void _thr_tsd_unload(struct dl_phdr_info *phdr_info) __hidden;
910 void _thr_sigact_unload(struct dl_phdr_info *phdr_info) __hidden;
911 void _thr_stack_fix_protection(struct pthread *thrd);
912 
913 __END_DECLS
914 
915 #endif  /* !_THR_PRIVATE_H */
916