xref: /freebsd/lib/libthr/thread/thr_private.h (revision 7cc1fde083b1fe9d55388045ba3df7d770d4f3bf)
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		32
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 struct pthread_mutex {
139 	/*
140 	 * Lock for accesses to this structure.
141 	 */
142 	struct umutex			m_lock;
143 	enum pthread_mutextype		m_type;
144 	struct pthread			*m_owner;
145 	int				m_count;
146 	int				m_refcount;
147 	int				m_spinloops;
148 	int				m_yieldloops;
149 	int				m_private;
150 	/*
151 	 * Link for all mutexes a thread currently owns.
152 	 */
153 	TAILQ_ENTRY(pthread_mutex)	m_qe;
154 };
155 
156 struct pthread_mutex_attr {
157 	enum pthread_mutextype	m_type;
158 	int			m_protocol;
159 	int			m_ceiling;
160 };
161 
162 #define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \
163 	{ PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE }
164 
165 struct pthread_cond {
166 	struct umutex	c_lock;
167 	struct ucond	c_kerncv;
168 	int		c_pshared;
169 	int		c_clockid;
170 };
171 
172 struct pthread_cond_attr {
173 	int		c_pshared;
174 	int		c_clockid;
175 };
176 
177 struct pthread_barrier {
178 	struct umutex		b_lock;
179 	struct ucond		b_cv;
180 	volatile int64_t	b_cycle;
181 	volatile int		b_count;
182 	volatile int		b_waiters;
183 };
184 
185 struct pthread_barrierattr {
186 	int		pshared;
187 };
188 
189 struct pthread_spinlock {
190 	struct umutex	s_lock;
191 };
192 
193 /*
194  * Flags for condition variables.
195  */
196 #define COND_FLAGS_PRIVATE	0x01
197 #define COND_FLAGS_INITED	0x02
198 #define COND_FLAGS_BUSY		0x04
199 
200 /*
201  * Cleanup definitions.
202  */
203 struct pthread_cleanup {
204 	struct pthread_cleanup	*prev;
205 	void			(*routine)(void *);
206 	void			*routine_arg;
207 	int			onheap;
208 };
209 
210 #define	THR_CLEANUP_PUSH(td, func, arg) {		\
211 	struct pthread_cleanup __cup;			\
212 							\
213 	__cup.routine = func;				\
214 	__cup.routine_arg = arg;			\
215 	__cup.onheap = 0;				\
216 	__cup.prev = (td)->cleanup;			\
217 	(td)->cleanup = &__cup;
218 
219 #define	THR_CLEANUP_POP(td, exec)			\
220 	(td)->cleanup = __cup.prev;			\
221 	if ((exec) != 0)				\
222 		__cup.routine(__cup.routine_arg);	\
223 }
224 
225 struct pthread_atfork {
226 	TAILQ_ENTRY(pthread_atfork) qe;
227 	void (*prepare)(void);
228 	void (*parent)(void);
229 	void (*child)(void);
230 };
231 
232 struct pthread_attr {
233 	int	sched_policy;
234 	int	sched_inherit;
235 	int	prio;
236 	int	suspend;
237 #define	THR_STACK_USER		0x100	/* 0xFF reserved for <pthread.h> */
238 	int	flags;
239 	void	*stackaddr_attr;
240 	size_t	stacksize_attr;
241 	size_t	guardsize_attr;
242 	cpuset_t	*cpuset;
243 	size_t	cpusetsize;
244 };
245 
246 /*
247  * Thread creation state attributes.
248  */
249 #define THR_CREATE_RUNNING		0
250 #define THR_CREATE_SUSPENDED		1
251 
252 /*
253  * Miscellaneous definitions.
254  */
255 #define THR_STACK_DEFAULT		(sizeof(void *) / 4 * 1024 * 1024)
256 
257 /*
258  * Maximum size of initial thread's stack.  This perhaps deserves to be larger
259  * than the stacks of other threads, since many applications are likely to run
260  * almost entirely on this stack.
261  */
262 #define THR_STACK_INITIAL		(THR_STACK_DEFAULT * 2)
263 
264 /*
265  * Define priorities returned by kernel.
266  */
267 #define THR_MIN_PRIORITY		(_thr_priorities[SCHED_OTHER-1].pri_min)
268 #define THR_MAX_PRIORITY		(_thr_priorities[SCHED_OTHER-1].pri_max)
269 #define THR_DEF_PRIORITY		(_thr_priorities[SCHED_OTHER-1].pri_default)
270 
271 #define THR_MIN_RR_PRIORITY		(_thr_priorities[SCHED_RR-1].pri_min)
272 #define THR_MAX_RR_PRIORITY		(_thr_priorities[SCHED_RR-1].pri_max)
273 #define THR_DEF_RR_PRIORITY		(_thr_priorities[SCHED_RR-1].pri_default)
274 
275 /* XXX The SCHED_FIFO should have same priority range as SCHED_RR */
276 #define THR_MIN_FIFO_PRIORITY		(_thr_priorities[SCHED_FIFO_1].pri_min)
277 #define THR_MAX_FIFO_PRIORITY		(_thr_priorities[SCHED_FIFO-1].pri_max)
278 #define THR_DEF_FIFO_PRIORITY		(_thr_priorities[SCHED_FIFO-1].pri_default)
279 
280 struct pthread_prio {
281 	int	pri_min;
282 	int	pri_max;
283 	int	pri_default;
284 };
285 
286 struct pthread_rwlockattr {
287 	int		pshared;
288 	int		kind;
289 };
290 
291 struct pthread_rwlock {
292 	struct urwlock 	lock;
293 	struct pthread	*owner;
294 	int	recurse;
295 	int	kind;
296 };
297 
298 /*
299  * Thread states.
300  */
301 enum pthread_state {
302 	PS_RUNNING,
303 	PS_DEAD
304 };
305 
306 struct pthread_specific_elem {
307 	const void	*data;
308 	int		seqno;
309 };
310 
311 struct pthread_key {
312 	volatile int	allocated;
313 	int		seqno;
314 	void            (*destructor)(void *);
315 };
316 
317 /*
318  * lwpid_t is 32bit but kernel thr API exports tid as long type
319  * in very earily date.
320  */
321 #define TID(thread)	((uint32_t) ((thread)->tid))
322 
323 /*
324  * Thread structure.
325  */
326 struct pthread {
327 	/* Kernel thread id. */
328 	long			tid;
329 #define	TID_TERMINATED		1
330 
331 	/*
332 	 * Lock for accesses to this thread structure.
333 	 */
334 	struct umutex		lock;
335 
336 	/* Internal condition variable cycle number. */
337 	uint32_t		cycle;
338 
339 	/* How many low level locks the thread held. */
340 	int			locklevel;
341 
342 	/*
343 	 * Set to non-zero when this thread has entered a critical
344 	 * region.  We allow for recursive entries into critical regions.
345 	 */
346 	int			critical_count;
347 
348 	/* Signal blocked counter. */
349 	int			sigblock;
350 
351 	/* Queue entry for list of all threads. */
352 	TAILQ_ENTRY(pthread)	tle;	/* link for all threads in process */
353 
354 	/* Queue entry for GC lists. */
355 	TAILQ_ENTRY(pthread)	gcle;
356 
357 	/* Hash queue entry. */
358 	LIST_ENTRY(pthread)	hle;
359 
360 	/* Threads reference count. */
361 	int			refcount;
362 
363 	/*
364 	 * Thread start routine, argument, stack pointer and thread
365 	 * attributes.
366 	 */
367 	void			*(*start_routine)(void *);
368 	void			*arg;
369 	struct pthread_attr	attr;
370 
371 #define	SHOULD_CANCEL(thr)					\
372 	((thr)->cancel_pending && (thr)->cancel_enable &&	\
373 	 (thr)->no_cancel == 0)
374 
375 	/* Cancellation is enabled */
376 	int			cancel_enable;
377 
378 	/* Cancellation request is pending */
379 	int			cancel_pending;
380 
381 	/* Thread is at cancellation point */
382 	int			cancel_point;
383 
384 	/* Cancellation is temporarily disabled */
385 	int			no_cancel;
386 
387 	/* Asynchronouse cancellation is enabled */
388 	int			cancel_async;
389 
390 	/* Cancellation is in progress */
391 	int			cancelling;
392 
393 	/* Thread temporary signal mask. */
394 	sigset_t		sigmask;
395 
396 	/* Thread should unblock SIGCANCEL. */
397 	int			unblock_sigcancel;
398 
399 	/* In sigsuspend state */
400 	int			in_sigsuspend;
401 
402 	/* deferred signal info	*/
403 	siginfo_t		deferred_siginfo;
404 
405 	/* signal mask to restore. */
406 	sigset_t		deferred_sigmask;
407 
408 	/* the sigaction should be used for deferred signal. */
409 	struct sigaction	deferred_sigact;
410 
411 	/* Force new thread to exit. */
412 	int			force_exit;
413 
414 	/* Thread state: */
415 	enum pthread_state 	state;
416 
417 	/*
418 	 * Error variable used instead of errno. The function __error()
419 	 * returns a pointer to this.
420 	 */
421 	int			error;
422 
423 	/*
424 	 * The joiner is the thread that is joining to this thread.  The
425 	 * join status keeps track of a join operation to another thread.
426 	 */
427 	struct pthread		*joiner;
428 
429 	/* Miscellaneous flags; only set with scheduling lock held. */
430 	int			flags;
431 #define THR_FLAGS_PRIVATE	0x0001
432 #define	THR_FLAGS_NEED_SUSPEND	0x0002	/* thread should be suspended */
433 #define	THR_FLAGS_SUSPENDED	0x0004	/* thread is suspended */
434 #define	THR_FLAGS_DETACHED	0x0008	/* thread is detached */
435 
436 	/* Thread list flags; only set with thread list lock held. */
437 	int			tlflags;
438 #define	TLFLAGS_GC_SAFE		0x0001	/* thread safe for cleaning */
439 #define	TLFLAGS_IN_TDLIST	0x0002	/* thread in all thread list */
440 #define	TLFLAGS_IN_GCLIST	0x0004	/* thread in gc list */
441 
442 	/* Queue of currently owned NORMAL or PRIO_INHERIT type mutexes. */
443 	struct mutex_queue	mutexq;
444 
445 	/* Queue of all owned PRIO_PROTECT mutexes. */
446 	struct mutex_queue	pp_mutexq;
447 
448 	void				*ret;
449 	struct pthread_specific_elem	*specific;
450 	int				specific_data_count;
451 
452 	/* Number rwlocks rdlocks held. */
453 	int			rdlock_count;
454 
455 	/*
456 	 * Current locks bitmap for rtld. */
457 	int			rtld_bits;
458 
459 	/* Thread control block */
460 	struct tcb		*tcb;
461 
462 	/* Cleanup handlers Link List */
463 	struct pthread_cleanup	*cleanup;
464 
465 #ifdef _PTHREAD_FORCED_UNWIND
466 	struct _Unwind_Exception	ex;
467 	void			*unwind_stackend;
468 	int			unwind_disabled;
469 #endif
470 
471 	/*
472 	 * Magic value to help recognize a valid thread structure
473 	 * from an invalid one:
474 	 */
475 #define	THR_MAGIC		((u_int32_t) 0xd09ba115)
476 	u_int32_t		magic;
477 
478 	/* Enable event reporting */
479 	int			report_events;
480 
481 	/* Event mask */
482 	int			event_mask;
483 
484 	/* Event */
485 	td_event_msg_t		event_buf;
486 };
487 
488 #define THR_SHOULD_GC(thrd) 						\
489 	((thrd)->refcount == 0 && (thrd)->state == PS_DEAD &&		\
490 	 ((thrd)->flags & THR_FLAGS_DETACHED) != 0)
491 
492 #define	THR_IN_CRITICAL(thrd)				\
493 	(((thrd)->locklevel > 0) ||			\
494 	((thrd)->critical_count > 0))
495 
496 #define	THR_CRITICAL_ENTER(thrd)			\
497 	(thrd)->critical_count++
498 
499 #define	THR_CRITICAL_LEAVE(thrd)			\
500 	do {						\
501 		(thrd)->critical_count--;		\
502 		_thr_ast(thrd);				\
503 	} while (0)
504 
505 #define THR_UMUTEX_TRYLOCK(thrd, lck)			\
506 	_thr_umutex_trylock((lck), TID(thrd))
507 
508 #define	THR_UMUTEX_LOCK(thrd, lck)			\
509 	_thr_umutex_lock((lck), TID(thrd))
510 
511 #define	THR_UMUTEX_TIMEDLOCK(thrd, lck, timo)		\
512 	_thr_umutex_timedlock((lck), TID(thrd), (timo))
513 
514 #define	THR_UMUTEX_UNLOCK(thrd, lck)			\
515 	_thr_umutex_unlock((lck), TID(thrd))
516 
517 #define	THR_LOCK_ACQUIRE(thrd, lck)			\
518 do {							\
519 	(thrd)->locklevel++;				\
520 	_thr_umutex_lock(lck, TID(thrd));		\
521 } while (0)
522 
523 #ifdef	_PTHREADS_INVARIANTS
524 #define	THR_ASSERT_LOCKLEVEL(thrd)			\
525 do {							\
526 	if (__predict_false((thrd)->locklevel <= 0))	\
527 		_thr_assert_lock_level();		\
528 } while (0)
529 #else
530 #define THR_ASSERT_LOCKLEVEL(thrd)
531 #endif
532 
533 #define	THR_LOCK_RELEASE(thrd, lck)			\
534 do {							\
535 	THR_ASSERT_LOCKLEVEL(thrd);			\
536 	_thr_umutex_unlock((lck), TID(thrd));		\
537 	(thrd)->locklevel--;				\
538 	_thr_ast(thrd);					\
539 } while (0)
540 
541 #define	THR_LOCK(curthrd)		THR_LOCK_ACQUIRE(curthrd, &(curthrd)->lock)
542 #define	THR_UNLOCK(curthrd)		THR_LOCK_RELEASE(curthrd, &(curthrd)->lock)
543 #define	THR_THREAD_LOCK(curthrd, thr)	THR_LOCK_ACQUIRE(curthrd, &(thr)->lock)
544 #define	THR_THREAD_UNLOCK(curthrd, thr)	THR_LOCK_RELEASE(curthrd, &(thr)->lock)
545 
546 #define	THREAD_LIST_RDLOCK(curthrd)				\
547 do {								\
548 	(curthrd)->locklevel++;					\
549 	_thr_rwl_rdlock(&_thr_list_lock);			\
550 } while (0)
551 
552 #define	THREAD_LIST_WRLOCK(curthrd)				\
553 do {								\
554 	(curthrd)->locklevel++;					\
555 	_thr_rwl_wrlock(&_thr_list_lock);			\
556 } while (0)
557 
558 #define	THREAD_LIST_UNLOCK(curthrd)				\
559 do {								\
560 	_thr_rwl_unlock(&_thr_list_lock);			\
561 	(curthrd)->locklevel--;					\
562 	_thr_ast(curthrd);					\
563 } while (0)
564 
565 /*
566  * Macros to insert/remove threads to the all thread list and
567  * the gc list.
568  */
569 #define	THR_LIST_ADD(thrd) do {					\
570 	if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) == 0) {	\
571 		TAILQ_INSERT_HEAD(&_thread_list, thrd, tle);	\
572 		_thr_hash_add(thrd);				\
573 		(thrd)->tlflags |= TLFLAGS_IN_TDLIST;		\
574 	}							\
575 } while (0)
576 #define	THR_LIST_REMOVE(thrd) do {				\
577 	if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) != 0) {	\
578 		TAILQ_REMOVE(&_thread_list, thrd, tle);		\
579 		_thr_hash_remove(thrd);				\
580 		(thrd)->tlflags &= ~TLFLAGS_IN_TDLIST;		\
581 	}							\
582 } while (0)
583 #define	THR_GCLIST_ADD(thrd) do {				\
584 	if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) == 0) {	\
585 		TAILQ_INSERT_HEAD(&_thread_gc_list, thrd, gcle);\
586 		(thrd)->tlflags |= TLFLAGS_IN_GCLIST;		\
587 		_gc_count++;					\
588 	}							\
589 } while (0)
590 #define	THR_GCLIST_REMOVE(thrd) do {				\
591 	if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) != 0) {	\
592 		TAILQ_REMOVE(&_thread_gc_list, thrd, gcle);	\
593 		(thrd)->tlflags &= ~TLFLAGS_IN_GCLIST;		\
594 		_gc_count--;					\
595 	}							\
596 } while (0)
597 
598 #define THR_REF_ADD(curthread, pthread) {			\
599 	THR_CRITICAL_ENTER(curthread);				\
600 	pthread->refcount++;					\
601 } while (0)
602 
603 #define THR_REF_DEL(curthread, pthread) {			\
604 	pthread->refcount--;					\
605 	THR_CRITICAL_LEAVE(curthread);				\
606 } while (0)
607 
608 #define GC_NEEDED()	(_gc_count >= 5)
609 
610 #define SHOULD_REPORT_EVENT(curthr, e)			\
611 	(curthr->report_events && 			\
612 	 (((curthr)->event_mask | _thread_event_mask ) & e) != 0)
613 
614 extern int __isthreaded;
615 
616 /*
617  * Global variables for the pthread kernel.
618  */
619 
620 extern char		*_usrstack __hidden;
621 extern struct pthread	*_thr_initial __hidden;
622 
623 /* For debugger */
624 extern int		_libthr_debug;
625 extern int		_thread_event_mask;
626 extern struct pthread	*_thread_last_event;
627 
628 /* List of all threads: */
629 extern pthreadlist	_thread_list;
630 
631 /* List of threads needing GC: */
632 extern pthreadlist	_thread_gc_list __hidden;
633 
634 extern int		_thread_active_threads;
635 extern atfork_head	_thr_atfork_list __hidden;
636 extern struct urwlock	_thr_atfork_lock __hidden;
637 
638 /* Default thread attributes: */
639 extern struct pthread_attr _pthread_attr_default __hidden;
640 
641 /* Default mutex attributes: */
642 extern struct pthread_mutex_attr _pthread_mutexattr_default __hidden;
643 extern struct pthread_mutex_attr _pthread_mutexattr_adaptive_default __hidden;
644 
645 /* Default condition variable attributes: */
646 extern struct pthread_cond_attr _pthread_condattr_default __hidden;
647 
648 extern struct pthread_prio _thr_priorities[] __hidden;
649 
650 extern pid_t	_thr_pid __hidden;
651 extern int	_thr_is_smp __hidden;
652 
653 extern size_t	_thr_guard_default __hidden;
654 extern size_t	_thr_stack_default __hidden;
655 extern size_t	_thr_stack_initial __hidden;
656 extern int	_thr_page_size __hidden;
657 extern int	_thr_spinloops __hidden;
658 extern int	_thr_yieldloops __hidden;
659 
660 /* Garbage thread count. */
661 extern int	_gc_count __hidden;
662 
663 extern struct umutex	_mutex_static_lock __hidden;
664 extern struct umutex	_cond_static_lock __hidden;
665 extern struct umutex	_rwlock_static_lock __hidden;
666 extern struct umutex	_keytable_lock __hidden;
667 extern struct urwlock	_thr_list_lock __hidden;
668 extern struct umutex	_thr_event_lock __hidden;
669 
670 /*
671  * Function prototype definitions.
672  */
673 __BEGIN_DECLS
674 int	_thr_setthreaded(int) __hidden;
675 int	_mutex_cv_lock(pthread_mutex_t *, int count) __hidden;
676 int	_mutex_cv_unlock(pthread_mutex_t *, int *count) __hidden;
677 int	_mutex_reinit(pthread_mutex_t *) __hidden;
678 void	_mutex_fork(struct pthread *curthread) __hidden;
679 void	_libpthread_init(struct pthread *) __hidden;
680 struct pthread *_thr_alloc(struct pthread *) __hidden;
681 void	_thread_exit(const char *, int, const char *) __hidden __dead2;
682 int	_thr_ref_add(struct pthread *, struct pthread *, int) __hidden;
683 void	_thr_ref_delete(struct pthread *, struct pthread *) __hidden;
684 void	_thr_ref_delete_unlocked(struct pthread *, struct pthread *) __hidden;
685 int	_thr_find_thread(struct pthread *, struct pthread *, int) __hidden;
686 void	_thr_rtld_init(void) __hidden;
687 void	_thr_rtld_fini(void) __hidden;
688 void	_thr_rtld_postfork_child(void) __hidden;
689 int	_thr_stack_alloc(struct pthread_attr *) __hidden;
690 void	_thr_stack_free(struct pthread_attr *) __hidden;
691 void	_thr_free(struct pthread *, struct pthread *) __hidden;
692 void	_thr_gc(struct pthread *) __hidden;
693 void    _thread_cleanupspecific(void) __hidden;
694 void	_thread_printf(int, const char *, ...) __hidden;
695 void	_thr_spinlock_init(void) __hidden;
696 void	_thr_cancel_enter(struct pthread *) __hidden;
697 void	_thr_cancel_enter2(struct pthread *, int) __hidden;
698 void	_thr_cancel_leave(struct pthread *, int) __hidden;
699 void	_thr_testcancel(struct pthread *) __hidden;
700 void	_thr_signal_block(struct pthread *) __hidden;
701 void	_thr_signal_unblock(struct pthread *) __hidden;
702 void	_thr_signal_init(void) __hidden;
703 void	_thr_signal_deinit(void) __hidden;
704 int	_thr_send_sig(struct pthread *, int sig) __hidden;
705 void	_thr_list_init(void) __hidden;
706 void	_thr_hash_add(struct pthread *) __hidden;
707 void	_thr_hash_remove(struct pthread *) __hidden;
708 struct pthread *_thr_hash_find(struct pthread *) __hidden;
709 void	_thr_link(struct pthread *, struct pthread *) __hidden;
710 void	_thr_unlink(struct pthread *, struct pthread *) __hidden;
711 void	_thr_assert_lock_level(void) __hidden __dead2;
712 void	_thr_ast(struct pthread *) __hidden;
713 void	_thr_once_init(void) __hidden;
714 void	_thr_report_creation(struct pthread *curthread,
715 	    struct pthread *newthread) __hidden;
716 void	_thr_report_death(struct pthread *curthread) __hidden;
717 int	_thr_getscheduler(lwpid_t, int *, struct sched_param *) __hidden;
718 int	_thr_setscheduler(lwpid_t, int, const struct sched_param *) __hidden;
719 void	_thr_signal_prefork(void) __hidden;
720 void	_thr_signal_postfork(void) __hidden;
721 void	_thr_signal_postfork_child(void) __hidden;
722 void	_thr_try_gc(struct pthread *, struct pthread *) __hidden;
723 int	_rtp_to_schedparam(const struct rtprio *rtp, int *policy,
724 		struct sched_param *param) __hidden;
725 int	_schedparam_to_rtp(int policy, const struct sched_param *param,
726 		struct rtprio *rtp) __hidden;
727 void	_thread_bp_create(void);
728 void	_thread_bp_death(void);
729 int	_sched_yield(void);
730 
731 void	_pthread_cleanup_push(void (*)(void *), void *);
732 void	_pthread_cleanup_pop(int);
733 void	_pthread_exit_mask(void *status, sigset_t *mask) __dead2 __hidden;
734 void	_pthread_cancel_enter(int maycancel);
735 void 	_pthread_cancel_leave(int maycancel);
736 
737 /* #include <fcntl.h> */
738 #ifdef  _SYS_FCNTL_H_
739 int     __sys_fcntl(int, int, ...);
740 int     __sys_open(const char *, int, ...);
741 int     __sys_openat(int, const char *, int, ...);
742 #endif
743 
744 /* #include <signal.h> */
745 #ifdef _SIGNAL_H_
746 int	__sys_kill(pid_t, int);
747 int     __sys_sigaction(int, const struct sigaction *, struct sigaction *);
748 int     __sys_sigpending(sigset_t *);
749 int     __sys_sigprocmask(int, const sigset_t *, sigset_t *);
750 int     __sys_sigsuspend(const sigset_t *);
751 int     __sys_sigreturn(const ucontext_t *);
752 int     __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *);
753 int	__sys_sigwait(const sigset_t *, int *);
754 int	__sys_sigtimedwait(const sigset_t *, siginfo_t *,
755 		const struct timespec *);
756 int	__sys_sigwaitinfo(const sigset_t *set, siginfo_t *info);
757 #endif
758 
759 /* #include <time.h> */
760 #ifdef	_TIME_H_
761 int	__sys_nanosleep(const struct timespec *, struct timespec *);
762 #endif
763 
764 /* #include <sys/ucontext.h> */
765 #ifdef _SYS_UCONTEXT_H_
766 int	__sys_setcontext(const ucontext_t *ucp);
767 int	__sys_swapcontext(ucontext_t *oucp, const ucontext_t *ucp);
768 #endif
769 
770 /* #include <unistd.h> */
771 #ifdef  _UNISTD_H_
772 int     __sys_close(int);
773 int	__sys_fork(void);
774 pid_t	__sys_getpid(void);
775 ssize_t __sys_read(int, void *, size_t);
776 ssize_t __sys_write(int, const void *, size_t);
777 void	__sys_exit(int);
778 #endif
779 
780 int	_umtx_op_err(void *, int op, u_long, void *, void *) __hidden;
781 
782 static inline int
783 _thr_isthreaded(void)
784 {
785 	return (__isthreaded != 0);
786 }
787 
788 static inline int
789 _thr_is_inited(void)
790 {
791 	return (_thr_initial != NULL);
792 }
793 
794 static inline void
795 _thr_check_init(void)
796 {
797 	if (_thr_initial == NULL)
798 		_libpthread_init(NULL);
799 }
800 
801 struct dl_phdr_info;
802 void __pthread_cxa_finalize(struct dl_phdr_info *phdr_info);
803 void _thr_tsd_unload(struct dl_phdr_info *phdr_info) __hidden;
804 void _thr_sigact_unload(struct dl_phdr_info *phdr_info) __hidden;
805 
806 __END_DECLS
807 
808 #endif  /* !_THR_PRIVATE_H */
809