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