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