thr_private.h revision 213153
1155192Srwatson/*
2155192Srwatson * Copyright (C) 2005 Daniel M. Eischen <deischen@freebsd.org>
3155192Srwatson * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
4155192Srwatson * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>.
5155192Srwatson *
6155192Srwatson * All rights reserved.
7155192Srwatson *
8155192Srwatson * Redistribution and use in source and binary forms, with or without
9155192Srwatson * modification, are permitted provided that the following conditions
10155192Srwatson * are met:
11155192Srwatson * 1. Redistributions of source code must retain the above copyright
12155192Srwatson *    notice unmodified, this list of conditions, and the following
13155192Srwatson *    disclaimer.
14155192Srwatson * 2. Redistributions in binary form must reproduce the above copyright
15155192Srwatson *    notice, this list of conditions and the following disclaimer in the
16155192Srwatson *    documentation and/or other materials provided with the distribution.
17155192Srwatson *
18155192Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19155192Srwatson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20155192Srwatson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21155192Srwatson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22155192Srwatson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23155192Srwatson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24155192Srwatson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25155192Srwatson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26155192Srwatson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27155192Srwatson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28155192Srwatson *
29155192Srwatson * $FreeBSD: head/lib/libthr/thread/thr_private.h 213153 2010-09-25 01:57:47Z davidxu $
30155192Srwatson */
31155192Srwatson
32155192Srwatson#ifndef _THR_PRIVATE_H
33155192Srwatson#define _THR_PRIVATE_H
34155192Srwatson
35155192Srwatson/*
36155192Srwatson * Include files.
37156882Srwatson */
38156882Srwatson#include <sys/types.h>
39155192Srwatson#include <sys/time.h>
40155192Srwatson#include <sys/cdefs.h>
41155192Srwatson#include <sys/queue.h>
42155192Srwatson#include <sys/param.h>
43155192Srwatson#include <sys/cpuset.h>
44155192Srwatson#include <machine/atomic.h>
45155192Srwatson#include <errno.h>
46155192Srwatson#include <limits.h>
47155192Srwatson#include <signal.h>
48155192Srwatson#include <stddef.h>
49155192Srwatson#include <stdio.h>
50155192Srwatson#include <unistd.h>
51155192Srwatson#include <ucontext.h>
52155192Srwatson#include <sys/thr.h>
53155192Srwatson#include <pthread.h>
54155192Srwatson
55155192Srwatson#define	SYM_FB10(sym)			__CONCAT(sym, _fb10)
56156889Srwatson#define	SYM_FBP10(sym)			__CONCAT(sym, _fbp10)
57156889Srwatson#define	WEAK_REF(sym, alias)		__weak_reference(sym, alias)
58155192Srwatson#define	SYM_COMPAT(sym, impl, ver)	__sym_compat(sym, impl, ver)
59155192Srwatson#define	SYM_DEFAULT(sym, impl, ver)	__sym_default(sym, impl, ver)
60155192Srwatson
61155192Srwatson#define	FB10_COMPAT(func, sym)				\
62155192Srwatson	WEAK_REF(func, SYM_FB10(sym));			\
63155192Srwatson	SYM_COMPAT(sym, SYM_FB10(sym), FBSD_1.0)
64161813Swsalamon
65161813Swsalamon#define	FB10_COMPAT_PRIVATE(func, sym)			\
66155192Srwatson	WEAK_REF(func, SYM_FBP10(sym));			\
67155192Srwatson	SYM_DEFAULT(sym, SYM_FBP10(sym), FBSDprivate_1.0)
68155192Srwatson
69155192Srwatson#include "pthread_md.h"
70155192Srwatson#include "thr_umtx.h"
71156889Srwatson#include "thread_db.h"
72156889Srwatson
73156889Srwatson#ifdef _PTHREAD_FORCED_UNWIND
74155192Srwatson#include <unwind-generic.h>
75155192Srwatson#endif
76159269Srwatson
77159269Srwatsontypedef TAILQ_HEAD(pthreadlist, pthread) pthreadlist;
78159269Srwatsontypedef TAILQ_HEAD(atfork_head, pthread_atfork) atfork_head;
79155192SrwatsonTAILQ_HEAD(mutex_queue, pthread_mutex);
80155192Srwatson
81155192Srwatson/* Signal to do cancellation */
82155192Srwatson#define	SIGCANCEL		32
83159269Srwatson
84159269Srwatson/*
85159269Srwatson * Kernel fatal error handler macro.
86162380Scsjp */
87162380Scsjp#define PANIC(string)		_thread_exit(__FILE__,__LINE__,string)
88162380Scsjp
89155192Srwatson/* Output debug messages like this: */
90155192Srwatson#define stdout_debug(args...)	_thread_printf(STDOUT_FILENO, ##args)
91155192Srwatson#define stderr_debug(args...)	_thread_printf(STDERR_FILENO, ##args)
92155192Srwatson
93155192Srwatson#ifdef _PTHREADS_INVARIANTS
94155192Srwatson#define THR_ASSERT(cond, msg) do {	\
95155192Srwatson	if (__predict_false(!(cond)))	\
96155192Srwatson		PANIC(msg);		\
97156889Srwatson} while (0)
98156889Srwatson#else
99156889Srwatson#define THR_ASSERT(cond, msg)
100156889Srwatson#endif
101156889Srwatson
102156889Srwatson#ifdef PIC
103156889Srwatson# define STATIC_LIB_REQUIRE(name)
104155192Srwatson#else
105155192Srwatson# define STATIC_LIB_REQUIRE(name) __asm (".globl " #name)
106155192Srwatson#endif
107155192Srwatson
108155192Srwatson#define	TIMESPEC_ADD(dst, src, val)				\
109155192Srwatson	do { 							\
110155192Srwatson		(dst)->tv_sec = (src)->tv_sec + (val)->tv_sec;	\
111155192Srwatson		(dst)->tv_nsec = (src)->tv_nsec + (val)->tv_nsec; \
112155192Srwatson		if ((dst)->tv_nsec >= 1000000000) {		\
113155192Srwatson			(dst)->tv_sec++;			\
114155192Srwatson			(dst)->tv_nsec -= 1000000000;		\
115156889Srwatson		}						\
116156889Srwatson	} while (0)
117156889Srwatson
118156889Srwatson#define	TIMESPEC_SUB(dst, src, val)				\
119155192Srwatson	do { 							\
120155192Srwatson		(dst)->tv_sec = (src)->tv_sec - (val)->tv_sec;	\
121155192Srwatson		(dst)->tv_nsec = (src)->tv_nsec - (val)->tv_nsec; \
122155192Srwatson		if ((dst)->tv_nsec < 0) {			\
123155192Srwatson			(dst)->tv_sec--;			\
124155192Srwatson			(dst)->tv_nsec += 1000000000;		\
125155192Srwatson		}						\
126155192Srwatson	} while (0)
127155192Srwatson
128155192Srwatsonstruct pthread_mutex {
129155192Srwatson	/*
130155192Srwatson	 * Lock for accesses to this structure.
131155192Srwatson	 */
132155192Srwatson	struct umutex			m_lock;
133155192Srwatson	enum pthread_mutextype		m_type;
134155192Srwatson	struct pthread			*m_owner;
135155192Srwatson	int				m_count;
136155192Srwatson	int				m_refcount;
137155192Srwatson	int				m_spinloops;
138156889Srwatson	int				m_yieldloops;
139156889Srwatson	int				m_private;
140156889Srwatson	/*
141155192Srwatson	 * Link for all mutexes a thread currently owns.
142155192Srwatson	 */
143155192Srwatson	TAILQ_ENTRY(pthread_mutex)	m_qe;
144155192Srwatson};
145155192Srwatson
146155192Srwatsonstruct pthread_mutex_attr {
147155192Srwatson	enum pthread_mutextype	m_type;
148155192Srwatson	int			m_protocol;
149155192Srwatson	int			m_ceiling;
150155192Srwatson};
151155192Srwatson
152155192Srwatson#define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \
153155192Srwatson	{ PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE }
154156889Srwatson
155156889Srwatsonstruct pthread_cond {
156156889Srwatson	struct umutex	c_lock;
157156889Srwatson	struct ucond	c_kerncv;
158156889Srwatson	int		c_pshared;
159156889Srwatson	int		c_clockid;
160156889Srwatson};
161156889Srwatson
162168688Scsjpstruct pthread_cond_attr {
163156889Srwatson	int		c_pshared;
164155192Srwatson	int		c_clockid;
165155192Srwatson};
166156889Srwatson
167156889Srwatsonstruct pthread_barrier {
168156889Srwatson	struct umutex		b_lock;
169156889Srwatson	struct ucond		b_cv;
170156889Srwatson	volatile int64_t	b_cycle;
171156889Srwatson	volatile int		b_count;
172156889Srwatson	volatile int		b_waiters;
173156889Srwatson};
174156889Srwatson
175168688Scsjpstruct pthread_barrierattr {
176156889Srwatson	int		pshared;
177156889Srwatson};
178156889Srwatson
179156889Srwatsonstruct pthread_spinlock {
180156889Srwatson	struct umutex	s_lock;
181156889Srwatson};
182156889Srwatson
183156889Srwatson/*
184156889Srwatson * Flags for condition variables.
185156889Srwatson */
186156889Srwatson#define COND_FLAGS_PRIVATE	0x01
187156889Srwatson#define COND_FLAGS_INITED	0x02
188156889Srwatson#define COND_FLAGS_BUSY		0x04
189156889Srwatson
190156889Srwatson/*
191156889Srwatson * Cleanup definitions.
192156889Srwatson */
193156889Srwatsonstruct pthread_cleanup {
194156889Srwatson	struct pthread_cleanup	*prev;
195156889Srwatson	void			(*routine)(void *);
196156889Srwatson	void			*routine_arg;
197156889Srwatson	int			onheap;
198156889Srwatson};
199156889Srwatson
200156889Srwatson#define	THR_CLEANUP_PUSH(td, func, arg) {		\
201156889Srwatson	struct pthread_cleanup __cup;			\
202156889Srwatson							\
203156889Srwatson	__cup.routine = func;				\
204156889Srwatson	__cup.routine_arg = arg;			\
205161813Swsalamon	__cup.onheap = 0;				\
206161813Swsalamon	__cup.prev = (td)->cleanup;			\
207161813Swsalamon	(td)->cleanup = &__cup;
208161813Swsalamon
209156889Srwatson#define	THR_CLEANUP_POP(td, exec)			\
210156889Srwatson	(td)->cleanup = __cup.prev;			\
211164011Scsjp	if ((exec) != 0)				\
212155192Srwatson		__cup.routine(__cup.routine_arg);	\
213155192Srwatson}
214155192Srwatson
215155192Srwatsonstruct pthread_atfork {
216155192Srwatson	TAILQ_ENTRY(pthread_atfork) qe;
217155192Srwatson	void (*prepare)(void);
218155192Srwatson	void (*parent)(void);
219155192Srwatson	void (*child)(void);
220155192Srwatson};
221155192Srwatson
222155192Srwatsonstruct pthread_attr {
223155192Srwatson	int	sched_policy;
224155192Srwatson	int	sched_inherit;
225155192Srwatson	int	prio;
226156889Srwatson	int	suspend;
227156889Srwatson#define	THR_STACK_USER		0x100	/* 0xFF reserved for <pthread.h> */
228155192Srwatson	int	flags;
229155192Srwatson	void	*stackaddr_attr;
230156889Srwatson	size_t	stacksize_attr;
231156889Srwatson	size_t	guardsize_attr;
232156889Srwatson	cpuset_t	*cpuset;
233156889Srwatson	size_t	cpusetsize;
234156889Srwatson};
235156889Srwatson
236155192Srwatson/*
237156888Srwatson * Thread creation state attributes.
238155192Srwatson */
239155192Srwatson#define THR_CREATE_RUNNING		0
240155192Srwatson#define THR_CREATE_SUSPENDED		1
241155192Srwatson
242155192Srwatson/*
243155192Srwatson * Miscellaneous definitions.
244156888Srwatson */
245155192Srwatson#define THR_STACK_DEFAULT		(sizeof(void *) / 4 * 1024 * 1024)
246155192Srwatson
247155192Srwatson/*
248155192Srwatson * Maximum size of initial thread's stack.  This perhaps deserves to be larger
249155192Srwatson * than the stacks of other threads, since many applications are likely to run
250155192Srwatson * almost entirely on this stack.
251155192Srwatson */
252156291Srwatson#define THR_STACK_INITIAL		(THR_STACK_DEFAULT * 2)
253156889Srwatson
254156889Srwatson/*
255155192Srwatson * Define priorities returned by kernel.
256155192Srwatson */
257155192Srwatson#define THR_MIN_PRIORITY		(_thr_priorities[SCHED_OTHER-1].pri_min)
258155192Srwatson#define THR_MAX_PRIORITY		(_thr_priorities[SCHED_OTHER-1].pri_max)
259156889Srwatson#define THR_DEF_PRIORITY		(_thr_priorities[SCHED_OTHER-1].pri_default)
260156889Srwatson
261155192Srwatson#define THR_MIN_RR_PRIORITY		(_thr_priorities[SCHED_RR-1].pri_min)
262155192Srwatson#define THR_MAX_RR_PRIORITY		(_thr_priorities[SCHED_RR-1].pri_max)
263155192Srwatson#define THR_DEF_RR_PRIORITY		(_thr_priorities[SCHED_RR-1].pri_default)
264155192Srwatson
265156889Srwatson/* XXX The SCHED_FIFO should have same priority range as SCHED_RR */
266156889Srwatson#define THR_MIN_FIFO_PRIORITY		(_thr_priorities[SCHED_FIFO_1].pri_min)
267156889Srwatson#define THR_MAX_FIFO_PRIORITY		(_thr_priorities[SCHED_FIFO-1].pri_max)
268155192Srwatson#define THR_DEF_FIFO_PRIORITY		(_thr_priorities[SCHED_FIFO-1].pri_default)
269156888Srwatson
270156888Srwatsonstruct pthread_prio {
271156888Srwatson	int	pri_min;
272156888Srwatson	int	pri_max;
273156889Srwatson	int	pri_default;
274159261Srwatson};
275159261Srwatson
276156889Srwatsonstruct pthread_rwlockattr {
277156889Srwatson	int		pshared;
278156889Srwatson};
279156889Srwatson
280156888Srwatsonstruct pthread_rwlock {
281156888Srwatson	struct urwlock 	lock;
282155192Srwatson	struct pthread	*owner;
283155192Srwatson};
284155192Srwatson
285155192Srwatson/*
286155192Srwatson * Thread states.
287155192Srwatson */
288155192Srwatsonenum pthread_state {
289155192Srwatson	PS_RUNNING,
290155192Srwatson	PS_DEAD
291155192Srwatson};
292155192Srwatson
293156889Srwatsonstruct pthread_specific_elem {
294170196Srwatson	const void	*data;
295155192Srwatson	int		seqno;
296155192Srwatson};
297155192Srwatson
298155192Srwatsonstruct pthread_key {
299155192Srwatson	volatile int	allocated;
300155192Srwatson	int		seqno;
301159269Srwatson	void            (*destructor)(void *);
302159269Srwatson};
303155192Srwatson
304155192Srwatson/*
305155192Srwatson * lwpid_t is 32bit but kernel thr API exports tid as long type
306155192Srwatson * in very earily date.
307155192Srwatson */
308155192Srwatson#define TID(thread)	((uint32_t) ((thread)->tid))
309176565Srwatson
310176565Srwatson/*
311176565Srwatson * Thread structure.
312155192Srwatson */
313155192Srwatsonstruct pthread {
314155192Srwatson	/* Kernel thread id. */
315155192Srwatson	long			tid;
316155192Srwatson#define	TID_TERMINATED		1
317155192Srwatson
318176686Srwatson	/*
319155192Srwatson	 * Lock for accesses to this thread structure.
320155192Srwatson	 */
321155192Srwatson	struct umutex		lock;
322155192Srwatson
323155192Srwatson	/* Internal condition variable cycle number. */
324156888Srwatson	uint32_t		cycle;
325155192Srwatson
326155192Srwatson	/* How many low level locks the thread held. */
327155192Srwatson	int			locklevel;
328156888Srwatson
329155192Srwatson	/*
330155408Srwatson	 * Set to non-zero when this thread has entered a critical
331155408Srwatson	 * region.  We allow for recursive entries into critical regions.
332155408Srwatson	 */
333159269Srwatson	int			critical_count;
334159269Srwatson
335159269Srwatson	/* Signal blocked counter. */
336159269Srwatson	int			sigblock;
337159269Srwatson
338155408Srwatson	/* Queue entry for list of all threads. */
339156882Srwatson	TAILQ_ENTRY(pthread)	tle;	/* link for all threads in process */
340
341	/* Queue entry for GC lists. */
342	TAILQ_ENTRY(pthread)	gcle;
343
344	/* Hash queue entry. */
345	LIST_ENTRY(pthread)	hle;
346
347	/* Threads reference count. */
348	int			refcount;
349
350	/*
351	 * Thread start routine, argument, stack pointer and thread
352	 * attributes.
353	 */
354	void			*(*start_routine)(void *);
355	void			*arg;
356	struct pthread_attr	attr;
357
358#define	SHOULD_CANCEL(thr)					\
359	((thr)->cancel_pending && (thr)->cancel_enable &&	\
360	 (thr)->no_cancel == 0)
361
362	/* Cancellation is enabled */
363	int			cancel_enable;
364
365	/* Cancellation request is pending */
366	int			cancel_pending;
367
368	/* Thread is at cancellation point */
369	int			cancel_point;
370
371	/* Cancellation is temporarily disabled */
372	int			no_cancel;
373
374	/* Asynchronouse cancellation is enabled */
375	int			cancel_async;
376
377	/* Cancellation is in progress */
378	int			cancelling;
379
380	/* Thread temporary signal mask. */
381	sigset_t		sigmask;
382
383	/* Thread should unblock SIGCANCEL. */
384	int			unblock_sigcancel;
385
386	/* In sigsuspend state */
387	int			in_sigsuspend;
388
389	/* deferred signal info	*/
390	siginfo_t		deferred_siginfo;
391
392	/* signal mask to restore. */
393	sigset_t		deferred_sigmask;
394
395	/* the sigaction should be used for deferred signal. */
396	struct sigaction	deferred_sigact;
397
398	/* Force new thread to exit. */
399	int			force_exit;
400
401	/* Thread state: */
402	enum pthread_state 	state;
403
404	/*
405	 * Error variable used instead of errno. The function __error()
406	 * returns a pointer to this.
407	 */
408	int			error;
409
410	/*
411	 * The joiner is the thread that is joining to this thread.  The
412	 * join status keeps track of a join operation to another thread.
413	 */
414	struct pthread		*joiner;
415
416	/* Miscellaneous flags; only set with scheduling lock held. */
417	int			flags;
418#define THR_FLAGS_PRIVATE	0x0001
419#define	THR_FLAGS_NEED_SUSPEND	0x0002	/* thread should be suspended */
420#define	THR_FLAGS_SUSPENDED	0x0004	/* thread is suspended */
421#define	THR_FLAGS_DETACHED	0x0008	/* thread is detached */
422
423	/* Thread list flags; only set with thread list lock held. */
424	int			tlflags;
425#define	TLFLAGS_GC_SAFE		0x0001	/* thread safe for cleaning */
426#define	TLFLAGS_IN_TDLIST	0x0002	/* thread in all thread list */
427#define	TLFLAGS_IN_GCLIST	0x0004	/* thread in gc list */
428
429	/* Queue of currently owned NORMAL or PRIO_INHERIT type mutexes. */
430	struct mutex_queue	mutexq;
431
432	/* Queue of all owned PRIO_PROTECT mutexes. */
433	struct mutex_queue	pp_mutexq;
434
435	void				*ret;
436	struct pthread_specific_elem	*specific;
437	int				specific_data_count;
438
439	/* Number rwlocks rdlocks held. */
440	int			rdlock_count;
441
442	/*
443	 * Current locks bitmap for rtld. */
444	int			rtld_bits;
445
446	/* Thread control block */
447	struct tcb		*tcb;
448
449	/* Cleanup handlers Link List */
450	struct pthread_cleanup	*cleanup;
451
452#ifdef _PTHREAD_FORCED_UNWIND
453	struct _Unwind_Exception	ex;
454	void			*unwind_stackend;
455#endif
456
457	/*
458	 * Magic value to help recognize a valid thread structure
459	 * from an invalid one:
460	 */
461#define	THR_MAGIC		((u_int32_t) 0xd09ba115)
462	u_int32_t		magic;
463
464	/* Enable event reporting */
465	int			report_events;
466
467	/* Event mask */
468	int			event_mask;
469
470	/* Event */
471	td_event_msg_t		event_buf;
472};
473
474#define THR_SHOULD_GC(thrd) 						\
475	((thrd)->refcount == 0 && (thrd)->state == PS_DEAD &&		\
476	 ((thrd)->flags & THR_FLAGS_DETACHED) != 0)
477
478#define	THR_IN_CRITICAL(thrd)				\
479	(((thrd)->locklevel > 0) ||			\
480	((thrd)->critical_count > 0))
481
482#define	THR_CRITICAL_ENTER(thrd)			\
483	(thrd)->critical_count++
484
485#define	THR_CRITICAL_LEAVE(thrd)			\
486	do {						\
487		(thrd)->critical_count--;		\
488		_thr_ast(thrd);				\
489	} while (0)
490
491#define THR_UMUTEX_TRYLOCK(thrd, lck)			\
492	_thr_umutex_trylock((lck), TID(thrd))
493
494#define	THR_UMUTEX_LOCK(thrd, lck)			\
495	_thr_umutex_lock((lck), TID(thrd))
496
497#define	THR_UMUTEX_TIMEDLOCK(thrd, lck, timo)		\
498	_thr_umutex_timedlock((lck), TID(thrd), (timo))
499
500#define	THR_UMUTEX_UNLOCK(thrd, lck)			\
501	_thr_umutex_unlock((lck), TID(thrd))
502
503#define	THR_LOCK_ACQUIRE(thrd, lck)			\
504do {							\
505	(thrd)->locklevel++;				\
506	_thr_umutex_lock(lck, TID(thrd));		\
507} while (0)
508
509#ifdef	_PTHREADS_INVARIANTS
510#define	THR_ASSERT_LOCKLEVEL(thrd)			\
511do {							\
512	if (__predict_false((thrd)->locklevel <= 0))	\
513		_thr_assert_lock_level();		\
514} while (0)
515#else
516#define THR_ASSERT_LOCKLEVEL(thrd)
517#endif
518
519#define	THR_LOCK_RELEASE(thrd, lck)			\
520do {							\
521	THR_ASSERT_LOCKLEVEL(thrd);			\
522	_thr_umutex_unlock((lck), TID(thrd));		\
523	(thrd)->locklevel--;				\
524	_thr_ast(thrd);					\
525} while (0)
526
527#define	THR_LOCK(curthrd)		THR_LOCK_ACQUIRE(curthrd, &(curthrd)->lock)
528#define	THR_UNLOCK(curthrd)		THR_LOCK_RELEASE(curthrd, &(curthrd)->lock)
529#define	THR_THREAD_LOCK(curthrd, thr)	THR_LOCK_ACQUIRE(curthrd, &(thr)->lock)
530#define	THR_THREAD_UNLOCK(curthrd, thr)	THR_LOCK_RELEASE(curthrd, &(thr)->lock)
531
532#define	THREAD_LIST_RDLOCK(curthrd)				\
533do {								\
534	(curthrd)->locklevel++;					\
535	_thr_rwl_rdlock(&_thr_list_lock);			\
536} while (0)
537
538#define	THREAD_LIST_WRLOCK(curthrd)				\
539do {								\
540	(curthrd)->locklevel++;					\
541	_thr_rwl_wrlock(&_thr_list_lock);			\
542} while (0)
543
544#define	THREAD_LIST_UNLOCK(curthrd)				\
545do {								\
546	_thr_rwl_unlock(&_thr_list_lock);			\
547	(curthrd)->locklevel--;					\
548	_thr_ast(curthrd);					\
549} while (0)
550
551/*
552 * Macros to insert/remove threads to the all thread list and
553 * the gc list.
554 */
555#define	THR_LIST_ADD(thrd) do {					\
556	if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) == 0) {	\
557		TAILQ_INSERT_HEAD(&_thread_list, thrd, tle);	\
558		_thr_hash_add(thrd);				\
559		(thrd)->tlflags |= TLFLAGS_IN_TDLIST;		\
560	}							\
561} while (0)
562#define	THR_LIST_REMOVE(thrd) do {				\
563	if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) != 0) {	\
564		TAILQ_REMOVE(&_thread_list, thrd, tle);		\
565		_thr_hash_remove(thrd);				\
566		(thrd)->tlflags &= ~TLFLAGS_IN_TDLIST;		\
567	}							\
568} while (0)
569#define	THR_GCLIST_ADD(thrd) do {				\
570	if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) == 0) {	\
571		TAILQ_INSERT_HEAD(&_thread_gc_list, thrd, gcle);\
572		(thrd)->tlflags |= TLFLAGS_IN_GCLIST;		\
573		_gc_count++;					\
574	}							\
575} while (0)
576#define	THR_GCLIST_REMOVE(thrd) do {				\
577	if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) != 0) {	\
578		TAILQ_REMOVE(&_thread_gc_list, thrd, gcle);	\
579		(thrd)->tlflags &= ~TLFLAGS_IN_GCLIST;		\
580		_gc_count--;					\
581	}							\
582} while (0)
583
584#define THR_REF_ADD(curthread, pthread) {			\
585	THR_CRITICAL_ENTER(curthread);				\
586	pthread->refcount++;					\
587} while (0)
588
589#define THR_REF_DEL(curthread, pthread) {			\
590	pthread->refcount--;					\
591	THR_CRITICAL_LEAVE(curthread);				\
592} while (0)
593
594#define GC_NEEDED()	(_gc_count >= 5)
595
596#define SHOULD_REPORT_EVENT(curthr, e)			\
597	(curthr->report_events && 			\
598	 (((curthr)->event_mask | _thread_event_mask ) & e) != 0)
599
600extern int __isthreaded;
601
602/*
603 * Global variables for the pthread kernel.
604 */
605
606extern char		*_usrstack __hidden;
607extern struct pthread	*_thr_initial __hidden;
608
609/* For debugger */
610extern int		_libthr_debug;
611extern int		_thread_event_mask;
612extern struct pthread	*_thread_last_event;
613
614/* List of all threads: */
615extern pthreadlist	_thread_list;
616
617/* List of threads needing GC: */
618extern pthreadlist	_thread_gc_list __hidden;
619
620extern int		_thread_active_threads;
621extern atfork_head	_thr_atfork_list __hidden;
622extern struct urwlock	_thr_atfork_lock __hidden;
623
624/* Default thread attributes: */
625extern struct pthread_attr _pthread_attr_default __hidden;
626
627/* Default mutex attributes: */
628extern struct pthread_mutex_attr _pthread_mutexattr_default __hidden;
629
630/* Default condition variable attributes: */
631extern struct pthread_cond_attr _pthread_condattr_default __hidden;
632
633extern struct pthread_prio _thr_priorities[] __hidden;
634
635extern pid_t	_thr_pid __hidden;
636extern int	_thr_is_smp __hidden;
637
638extern size_t	_thr_guard_default __hidden;
639extern size_t	_thr_stack_default __hidden;
640extern size_t	_thr_stack_initial __hidden;
641extern int	_thr_page_size __hidden;
642extern int	_thr_spinloops __hidden;
643extern int	_thr_yieldloops __hidden;
644
645/* Garbage thread count. */
646extern int	_gc_count __hidden;
647
648extern struct umutex	_mutex_static_lock __hidden;
649extern struct umutex	_cond_static_lock __hidden;
650extern struct umutex	_rwlock_static_lock __hidden;
651extern struct umutex	_keytable_lock __hidden;
652extern struct urwlock	_thr_list_lock __hidden;
653extern struct umutex	_thr_event_lock __hidden;
654
655/*
656 * Function prototype definitions.
657 */
658__BEGIN_DECLS
659int	_thr_setthreaded(int) __hidden;
660int	_mutex_cv_lock(pthread_mutex_t *, int count) __hidden;
661int	_mutex_cv_unlock(pthread_mutex_t *, int *count) __hidden;
662int	_mutex_reinit(pthread_mutex_t *) __hidden;
663void	_mutex_fork(struct pthread *curthread) __hidden;
664void	_libpthread_init(struct pthread *) __hidden;
665struct pthread *_thr_alloc(struct pthread *) __hidden;
666void	_thread_exit(const char *, int, const char *) __hidden __dead2;
667int	_thr_ref_add(struct pthread *, struct pthread *, int) __hidden;
668void	_thr_ref_delete(struct pthread *, struct pthread *) __hidden;
669void	_thr_ref_delete_unlocked(struct pthread *, struct pthread *) __hidden;
670int	_thr_find_thread(struct pthread *, struct pthread *, int) __hidden;
671void	_thr_rtld_init(void) __hidden;
672void	_thr_rtld_fini(void) __hidden;
673void	_thr_rtld_postfork_child(void) __hidden;
674int	_thr_stack_alloc(struct pthread_attr *) __hidden;
675void	_thr_stack_free(struct pthread_attr *) __hidden;
676void	_thr_free(struct pthread *, struct pthread *) __hidden;
677void	_thr_gc(struct pthread *) __hidden;
678void    _thread_cleanupspecific(void) __hidden;
679void	_thread_printf(int, const char *, ...) __hidden;
680void	_thr_spinlock_init(void) __hidden;
681void	_thr_cancel_enter(struct pthread *) __hidden;
682void	_thr_cancel_enter2(struct pthread *, int) __hidden;
683void	_thr_cancel_leave(struct pthread *, int) __hidden;
684void	_thr_testcancel(struct pthread *) __hidden;
685void	_thr_signal_block(struct pthread *) __hidden;
686void	_thr_signal_unblock(struct pthread *) __hidden;
687void	_thr_signal_init(void) __hidden;
688void	_thr_signal_deinit(void) __hidden;
689int	_thr_send_sig(struct pthread *, int sig) __hidden;
690void	_thr_list_init(void) __hidden;
691void	_thr_hash_add(struct pthread *) __hidden;
692void	_thr_hash_remove(struct pthread *) __hidden;
693struct pthread *_thr_hash_find(struct pthread *) __hidden;
694void	_thr_link(struct pthread *, struct pthread *) __hidden;
695void	_thr_unlink(struct pthread *, struct pthread *) __hidden;
696void	_thr_assert_lock_level(void) __hidden __dead2;
697void	_thr_ast(struct pthread *) __hidden;
698void	_thr_once_init(void) __hidden;
699void	_thr_report_creation(struct pthread *curthread,
700	    struct pthread *newthread) __hidden;
701void	_thr_report_death(struct pthread *curthread) __hidden;
702int	_thr_getscheduler(lwpid_t, int *, struct sched_param *) __hidden;
703int	_thr_setscheduler(lwpid_t, int, const struct sched_param *) __hidden;
704void	_thr_signal_prefork(void) __hidden;
705void	_thr_signal_postfork(void) __hidden;
706void	_thr_signal_postfork_child(void) __hidden;
707void	_thr_try_gc(struct pthread *, struct pthread *) __hidden;
708int	_rtp_to_schedparam(const struct rtprio *rtp, int *policy,
709		struct sched_param *param) __hidden;
710int	_schedparam_to_rtp(int policy, const struct sched_param *param,
711		struct rtprio *rtp) __hidden;
712void	_thread_bp_create(void);
713void	_thread_bp_death(void);
714int	_sched_yield(void);
715
716void	_pthread_cleanup_push(void (*)(void *), void *);
717void	_pthread_cleanup_pop(int);
718void	_pthread_exit_mask(void *status, sigset_t *mask) __dead2 __hidden;
719void	_pthread_cancel_enter(int maycancel);
720void 	_pthread_cancel_leave(int maycancel);
721
722/* #include <fcntl.h> */
723#ifdef  _SYS_FCNTL_H_
724int     __sys_fcntl(int, int, ...);
725int     __sys_open(const char *, int, ...);
726int     __sys_openat(int, const char *, int, ...);
727#endif
728
729/* #include <signal.h> */
730#ifdef _SIGNAL_H_
731int	__sys_kill(pid_t, int);
732int     __sys_sigaction(int, const struct sigaction *, struct sigaction *);
733int     __sys_sigpending(sigset_t *);
734int     __sys_sigprocmask(int, const sigset_t *, sigset_t *);
735int     __sys_sigsuspend(const sigset_t *);
736int     __sys_sigreturn(const ucontext_t *);
737int     __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *);
738int	__sys_sigwait(const sigset_t *, int *);
739int	__sys_sigtimedwait(const sigset_t *, siginfo_t *,
740		const struct timespec *);
741int	__sys_sigwaitinfo(const sigset_t *set, siginfo_t *info);
742#endif
743
744/* #include <time.h> */
745#ifdef	_TIME_H_
746int	__sys_nanosleep(const struct timespec *, struct timespec *);
747#endif
748
749/* #include <sys/ucontext.h> */
750#ifdef _SYS_UCONTEXT_H_
751int	__sys_setcontext(const ucontext_t *ucp);
752int	__sys_swapcontext(ucontext_t *oucp, const ucontext_t *ucp);
753#endif
754
755/* #include <unistd.h> */
756#ifdef  _UNISTD_H_
757int     __sys_close(int);
758int	__sys_fork(void);
759pid_t	__sys_getpid(void);
760ssize_t __sys_read(int, void *, size_t);
761ssize_t __sys_write(int, const void *, size_t);
762void	__sys_exit(int);
763#endif
764
765int	_umtx_op_err(void *, int op, u_long, void *, void *) __hidden;
766
767static inline int
768_thr_isthreaded(void)
769{
770	return (__isthreaded != 0);
771}
772
773static inline int
774_thr_is_inited(void)
775{
776	return (_thr_initial != NULL);
777}
778
779static inline void
780_thr_check_init(void)
781{
782	if (_thr_initial == NULL)
783		_libpthread_init(NULL);
784}
785
786struct dl_phdr_info;
787void __pthread_cxa_finalize(struct dl_phdr_info *phdr_info);
788void _thr_tsd_unload(struct dl_phdr_info *phdr_info) __hidden;
789void _thr_sigact_unload(struct dl_phdr_info *phdr_info) __hidden;
790
791__END_DECLS
792
793#endif  /* !_THR_PRIVATE_H */
794