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