171b3fa15SDavid Xu /*
2d3b15642Szrj * Copyright (C) 2005 Daniel M. Eischen <deischen@freebsd.org>
3d3b15642Szrj * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
471b3fa15SDavid Xu * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>.
5d3b15642Szrj *
671b3fa15SDavid Xu * All rights reserved.
771b3fa15SDavid Xu *
871b3fa15SDavid Xu * Redistribution and use in source and binary forms, with or without
971b3fa15SDavid Xu * modification, are permitted provided that the following conditions
1071b3fa15SDavid Xu * are met:
1171b3fa15SDavid Xu * 1. Redistributions of source code must retain the above copyright
1271b3fa15SDavid Xu * notice, this list of conditions and the following disclaimer.
1371b3fa15SDavid Xu * 2. Redistributions in binary form must reproduce the above copyright
1471b3fa15SDavid Xu * notice, this list of conditions and the following disclaimer in the
1571b3fa15SDavid Xu * documentation and/or other materials provided with the distribution.
1671b3fa15SDavid Xu *
17d3b15642Szrj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18d3b15642Szrj * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19d3b15642Szrj * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20d3b15642Szrj * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21d3b15642Szrj * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22d3b15642Szrj * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23d3b15642Szrj * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24d3b15642Szrj * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25d3b15642Szrj * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26d3b15642Szrj * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2771b3fa15SDavid Xu *
28fcf53d9bSJohn Marino * $FreeBSD: head/lib/libthr/thread/thr_private.h 217706 2010-08-23 $
2971b3fa15SDavid Xu */
3071b3fa15SDavid Xu
31d3b15642Szrj /*
32d3b15642Szrj * Private thread definitions for the uthread kernel.
33d3b15642Szrj */
34d3b15642Szrj
3571b3fa15SDavid Xu #ifndef _THR_PRIVATE_H
3671b3fa15SDavid Xu #define _THR_PRIVATE_H
3771b3fa15SDavid Xu
3871b3fa15SDavid Xu /*
3971b3fa15SDavid Xu * Include files.
4071b3fa15SDavid Xu */
4171b3fa15SDavid Xu #include <sys/types.h>
4271b3fa15SDavid Xu #include <sys/time.h>
4371b3fa15SDavid Xu #include <sys/cdefs.h>
4471b3fa15SDavid Xu #include <sys/queue.h>
45d1cf13b9SSascha Wildner #include <sys/rtprio.h>
468356845aSMatthew Dillon #include <sys/mman.h>
4771b3fa15SDavid Xu #include <machine/atomic.h>
4871b3fa15SDavid Xu #include <errno.h>
4971b3fa15SDavid Xu #include <limits.h>
5071b3fa15SDavid Xu #include <signal.h>
51da82a65aSzrj #include <sys/cpumask.h>
52c4dd4100SJoe Talbott #include <sys/sched.h>
534a9b8501Szrj #include <stdarg.h>
5471b3fa15SDavid Xu #include <unistd.h>
5571b3fa15SDavid Xu #include <pthread.h>
5671b3fa15SDavid Xu #include <pthread_np.h>
5771b3fa15SDavid Xu
58fcaa7a3aSMatthew Dillon #if defined(_PTHREADS_DEBUGGING) || defined(_PTHREADS_DEBUGGING2)
59fcaa7a3aSMatthew Dillon void _thr_log(const char *buf, size_t bytes);
60fcaa7a3aSMatthew Dillon #endif
61fcaa7a3aSMatthew Dillon
6271b3fa15SDavid Xu #include "pthread_md.h"
6371b3fa15SDavid Xu #include "thr_umtx.h"
643cd47da3SDavid Xu #include "thread_db.h"
6571b3fa15SDavid Xu
6671b3fa15SDavid Xu /* Signal to do cancellation */
6771b3fa15SDavid Xu #define SIGCANCEL 32
6871b3fa15SDavid Xu
6971b3fa15SDavid Xu /*
7071b3fa15SDavid Xu * Kernel fatal error handler macro.
7171b3fa15SDavid Xu */
724a9b8501Szrj #define PANIC(args...) _thread_exitf(__FILE__, __LINE__, ##args)
7371b3fa15SDavid Xu
7471b3fa15SDavid Xu /* Output debug messages like this: */
7571b3fa15SDavid Xu #define stdout_debug(args...) _thread_printf(STDOUT_FILENO, ##args)
76f3e607ffSHasso Tepper #define stderr_debug(args...) _thread_printf(STDERR_FILENO, ##args)
7771b3fa15SDavid Xu
7871b3fa15SDavid Xu #ifdef _PTHREADS_INVARIANTS
7971b3fa15SDavid Xu #define THR_ASSERT(cond, msg) do { \
8071b3fa15SDavid Xu if (__predict_false(!(cond))) \
8171b3fa15SDavid Xu PANIC(msg); \
8271b3fa15SDavid Xu } while (0)
8371b3fa15SDavid Xu #else
8471b3fa15SDavid Xu #define THR_ASSERT(cond, msg)
8571b3fa15SDavid Xu #endif
8671b3fa15SDavid Xu
8722d6e608SDavid Xu #ifdef PIC
8822d6e608SDavid Xu #define STATIC_LIB_REQUIRE(name)
8922d6e608SDavid Xu #else
9022d6e608SDavid Xu #define STATIC_LIB_REQUIRE(name) __asm(".globl " #name)
9122d6e608SDavid Xu #endif
9222d6e608SDavid Xu
93cf8046a9Szrj typedef TAILQ_HEAD(thread_head, __pthread_s) thread_head;
941ff88af1SSascha Wildner typedef TAILQ_HEAD(atfork_head, pthread_atfork) atfork_head;
95e8382b15SDavid Xu
96cf8046a9Szrj struct __pthread_mutex_s {
9771b3fa15SDavid Xu /*
9871b3fa15SDavid Xu * Lock for accesses to this structure.
9971b3fa15SDavid Xu */
10071b3fa15SDavid Xu volatile umtx_t m_lock;
101fcaa7a3aSMatthew Dillon #ifdef _PTHREADS_DEBUGGING2
102fcaa7a3aSMatthew Dillon int m_lastop[32];
103fcaa7a3aSMatthew Dillon #endif
10471b3fa15SDavid Xu enum pthread_mutextype m_type;
10571b3fa15SDavid Xu int m_protocol;
106cf8046a9Szrj TAILQ_HEAD(mutex_head, __pthread_s) m_queue;
107cf8046a9Szrj struct __pthread_s *m_owner;
10871b3fa15SDavid Xu long m_flags;
10971b3fa15SDavid Xu int m_count;
11071b3fa15SDavid Xu int m_refcount;
11171b3fa15SDavid Xu
11271b3fa15SDavid Xu /*
1136a8bb22dSSascha Wildner * Used for priority inheritance and protection.
11471b3fa15SDavid Xu *
1156a8bb22dSSascha Wildner * m_prio - For priority inheritance, the highest active
11671b3fa15SDavid Xu * priority (threads locking the mutex inherit
11771b3fa15SDavid Xu * this priority). For priority protection, the
11871b3fa15SDavid Xu * ceiling priority of this mutex.
11971b3fa15SDavid Xu * m_saved_prio - mutex owners inherited priority before
12071b3fa15SDavid Xu * taking the mutex, restored when the owner
12171b3fa15SDavid Xu * unlocks the mutex.
12271b3fa15SDavid Xu */
12371b3fa15SDavid Xu int m_prio;
12471b3fa15SDavid Xu int m_saved_prio;
12571b3fa15SDavid Xu
12671b3fa15SDavid Xu /*
12771b3fa15SDavid Xu * Link for list of all mutexes a thread currently owns.
12871b3fa15SDavid Xu */
129cf8046a9Szrj TAILQ_ENTRY(__pthread_mutex_s) m_qe;
13071b3fa15SDavid Xu };
13171b3fa15SDavid Xu
13271b3fa15SDavid Xu #define TAILQ_INITIALIZER { NULL, NULL }
13371b3fa15SDavid Xu
13471b3fa15SDavid Xu #define PTHREAD_MUTEX_STATIC_INITIALIZER \
135fcaa7a3aSMatthew Dillon { .m_lock = 0, \
136fcaa7a3aSMatthew Dillon .m_type = PTHREAD_MUTEX_DEFAULT, \
137fcaa7a3aSMatthew Dillon .m_protocol = PTHREAD_PRIO_NONE, \
138fcaa7a3aSMatthew Dillon .m_queue = TAILQ_INITIALIZER, \
139fcaa7a3aSMatthew Dillon .m_flags = MUTEX_FLAGS_PRIVATE \
140fcaa7a3aSMatthew Dillon }
14171b3fa15SDavid Xu /*
14271b3fa15SDavid Xu * Flags for mutexes.
14371b3fa15SDavid Xu */
14471b3fa15SDavid Xu #define MUTEX_FLAGS_PRIVATE 0x01
14571b3fa15SDavid Xu #define MUTEX_FLAGS_INITED 0x02
14671b3fa15SDavid Xu
147cf8046a9Szrj struct __pthread_mutexattr_s {
14871b3fa15SDavid Xu enum pthread_mutextype m_type;
14971b3fa15SDavid Xu int m_protocol;
15071b3fa15SDavid Xu int m_ceiling;
1517ab4b9f8SDavid Xu int m_flags;
15271b3fa15SDavid Xu };
15371b3fa15SDavid Xu
15471b3fa15SDavid Xu #define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \
15571b3fa15SDavid Xu { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE }
15671b3fa15SDavid Xu
157fcaa7a3aSMatthew Dillon struct cond_cancel_info;
158fcaa7a3aSMatthew Dillon
159cf8046a9Szrj struct __pthread_cond_s {
16071b3fa15SDavid Xu /*
16171b3fa15SDavid Xu * Lock for accesses to this structure.
16271b3fa15SDavid Xu */
16371b3fa15SDavid Xu volatile umtx_t c_lock;
164fcaa7a3aSMatthew Dillon volatile int c_unused01;
16571b3fa15SDavid Xu int c_pshared;
16671b3fa15SDavid Xu int c_clockid;
167fcaa7a3aSMatthew Dillon TAILQ_HEAD(, cond_cancel_info) c_waitlist;
16871b3fa15SDavid Xu };
16971b3fa15SDavid Xu
170cf8046a9Szrj struct __pthread_condattr_s {
17171b3fa15SDavid Xu int c_pshared;
17271b3fa15SDavid Xu int c_clockid;
17371b3fa15SDavid Xu };
17471b3fa15SDavid Xu
1757ab4b9f8SDavid Xu /*
1767ab4b9f8SDavid Xu * Flags for condition variables.
1777ab4b9f8SDavid Xu */
1787ab4b9f8SDavid Xu #define COND_FLAGS_PRIVATE 0x01
1797ab4b9f8SDavid Xu #define COND_FLAGS_INITED 0x02
1807ab4b9f8SDavid Xu
181cf8046a9Szrj struct __pthread_barrier_s {
18271b3fa15SDavid Xu volatile umtx_t b_lock;
18371b3fa15SDavid Xu volatile umtx_t b_cycle;
18471b3fa15SDavid Xu volatile int b_count;
18571b3fa15SDavid Xu volatile int b_waiters;
18671b3fa15SDavid Xu };
18771b3fa15SDavid Xu
188cf8046a9Szrj struct __pthread_barrierattr_s {
18971b3fa15SDavid Xu int pshared;
19071b3fa15SDavid Xu };
19171b3fa15SDavid Xu
192cf8046a9Szrj struct __pthread_spinlock_s {
19371b3fa15SDavid Xu volatile umtx_t s_lock;
19471b3fa15SDavid Xu };
19571b3fa15SDavid Xu
19671b3fa15SDavid Xu /*
19771b3fa15SDavid Xu * Cleanup definitions.
19871b3fa15SDavid Xu */
19971b3fa15SDavid Xu struct pthread_cleanup {
20071b3fa15SDavid Xu struct pthread_cleanup *next;
201fc71f871SDavid Xu void (*routine)(void *);
20271b3fa15SDavid Xu void *routine_arg;
20371b3fa15SDavid Xu int onstack;
20471b3fa15SDavid Xu };
20571b3fa15SDavid Xu
20671b3fa15SDavid Xu #define THR_CLEANUP_PUSH(td, func, arg) { \
20771b3fa15SDavid Xu struct pthread_cleanup __cup; \
20871b3fa15SDavid Xu \
20971b3fa15SDavid Xu __cup.routine = func; \
21071b3fa15SDavid Xu __cup.routine_arg = arg; \
21171b3fa15SDavid Xu __cup.onstack = 1; \
21271b3fa15SDavid Xu __cup.next = (td)->cleanup; \
21371b3fa15SDavid Xu (td)->cleanup = &__cup;
21471b3fa15SDavid Xu
21571b3fa15SDavid Xu #define THR_CLEANUP_POP(td, exec) \
21671b3fa15SDavid Xu (td)->cleanup = __cup.next; \
21771b3fa15SDavid Xu if ((exec) != 0) \
21871b3fa15SDavid Xu __cup.routine(__cup.routine_arg); \
21971b3fa15SDavid Xu }
22071b3fa15SDavid Xu
22171b3fa15SDavid Xu struct pthread_atfork {
22271b3fa15SDavid Xu TAILQ_ENTRY(pthread_atfork) qe;
22371b3fa15SDavid Xu void (*prepare)(void);
22471b3fa15SDavid Xu void (*parent)(void);
22571b3fa15SDavid Xu void (*child)(void);
22671b3fa15SDavid Xu };
22771b3fa15SDavid Xu
228cf8046a9Szrj struct __pthread_attr_s {
22971b3fa15SDavid Xu int sched_policy;
23071b3fa15SDavid Xu int sched_inherit;
23171b3fa15SDavid Xu int prio;
23271b3fa15SDavid Xu int suspend;
23371b3fa15SDavid Xu #define THR_STACK_USER 0x100 /* 0xFF reserved for <pthread.h> */
2343ae362f2SSepherosa Ziehau #define THR_CPUMASK 0x200 /* cpumask is valid */
23571b3fa15SDavid Xu int flags;
23671b3fa15SDavid Xu void *stackaddr_attr;
23771b3fa15SDavid Xu size_t stacksize_attr;
23871b3fa15SDavid Xu size_t guardsize_attr;
2393ae362f2SSepherosa Ziehau cpumask_t cpumask;
24071b3fa15SDavid Xu };
24171b3fa15SDavid Xu
24271b3fa15SDavid Xu /*
24371b3fa15SDavid Xu * Thread creation state attributes.
24471b3fa15SDavid Xu */
24571b3fa15SDavid Xu #define THR_CREATE_RUNNING 0
24671b3fa15SDavid Xu #define THR_CREATE_SUSPENDED 1
24771b3fa15SDavid Xu
24871b3fa15SDavid Xu /*
24971b3fa15SDavid Xu * Miscellaneous definitions.
25071b3fa15SDavid Xu */
251eddabc99SDavid Xu #define THR_STACK_DEFAULT (sizeof(void *) / 4 * 1024 * 1024)
25271b3fa15SDavid Xu
25371b3fa15SDavid Xu /*
25471b3fa15SDavid Xu * Maximum size of initial thread's stack. This perhaps deserves to be larger
25571b3fa15SDavid Xu * than the stacks of other threads, since many applications are likely to run
25671b3fa15SDavid Xu * almost entirely on this stack.
25771b3fa15SDavid Xu */
258eddabc99SDavid Xu #define THR_STACK_INITIAL (THR_STACK_DEFAULT * 2)
25971b3fa15SDavid Xu
26071b3fa15SDavid Xu /*
26171b3fa15SDavid Xu * Define the different priority ranges. All applications have thread
26271b3fa15SDavid Xu * priorities constrained within 0-31. The threads library raises the
26371b3fa15SDavid Xu * priority when delivering signals in order to ensure that signal
26471b3fa15SDavid Xu * delivery happens (from the POSIX spec) "as soon as possible".
26571b3fa15SDavid Xu * In the future, the threads library will also be able to map specific
26671b3fa15SDavid Xu * threads into real-time (cooperating) processes or kernel threads.
26771b3fa15SDavid Xu * The RT and SIGNAL priorities will be used internally and added to
26871b3fa15SDavid Xu * thread base priorities so that the scheduling queue can handle both
26971b3fa15SDavid Xu * normal and RT priority threads with and without signal handling.
27071b3fa15SDavid Xu *
27171b3fa15SDavid Xu * The approach taken is that, within each class, signal delivery
27271b3fa15SDavid Xu * always has priority over thread execution.
27371b3fa15SDavid Xu */
27464524f1cSMatthew Dillon #define THR_DEFAULT_PRIORITY 0
27564524f1cSMatthew Dillon #define THR_MUTEX_CEIL_PRIORITY 31 /* dummy */
27671b3fa15SDavid Xu
27771b3fa15SDavid Xu /*
27871b3fa15SDavid Xu * Time slice period in microseconds.
27971b3fa15SDavid Xu */
28071b3fa15SDavid Xu #define TIMESLICE_USEC 20000
28171b3fa15SDavid Xu
282cf8046a9Szrj struct __pthread_rwlockattr_s {
28371b3fa15SDavid Xu int pshared;
28471b3fa15SDavid Xu };
28571b3fa15SDavid Xu
286cf8046a9Szrj struct __pthread_rwlock_s {
28771b3fa15SDavid Xu pthread_mutex_t lock; /* monitor lock */
28871b3fa15SDavid Xu pthread_cond_t read_signal;
28971b3fa15SDavid Xu pthread_cond_t write_signal;
29071b3fa15SDavid Xu int state; /* 0 = idle >0 = # of readers -1 = writer */
29171b3fa15SDavid Xu int blocked_writers;
29271b3fa15SDavid Xu };
29371b3fa15SDavid Xu
29471b3fa15SDavid Xu /*
29571b3fa15SDavid Xu * Thread states.
29671b3fa15SDavid Xu */
29771b3fa15SDavid Xu enum pthread_state {
29871b3fa15SDavid Xu PS_RUNNING,
29971b3fa15SDavid Xu PS_DEAD
30071b3fa15SDavid Xu };
30171b3fa15SDavid Xu
30271b3fa15SDavid Xu struct pthread_specific_elem {
30371b3fa15SDavid Xu const void *data;
30471b3fa15SDavid Xu int seqno;
30571b3fa15SDavid Xu };
30671b3fa15SDavid Xu
30771b3fa15SDavid Xu struct pthread_key {
30871b3fa15SDavid Xu volatile int allocated;
30971b3fa15SDavid Xu volatile int count;
31071b3fa15SDavid Xu int seqno;
31171b3fa15SDavid Xu void (*destructor)(void *);
31271b3fa15SDavid Xu };
31371b3fa15SDavid Xu
31471b3fa15SDavid Xu /*
31571b3fa15SDavid Xu * Thread structure.
31671b3fa15SDavid Xu */
317cf8046a9Szrj struct __pthread_s {
31871b3fa15SDavid Xu /*
31971b3fa15SDavid Xu * Magic value to help recognize a valid thread structure
32071b3fa15SDavid Xu * from an invalid one:
32171b3fa15SDavid Xu */
32271b3fa15SDavid Xu #define THR_MAGIC ((u_int32_t) 0xd09ba115)
32371b3fa15SDavid Xu u_int32_t magic;
32471b3fa15SDavid Xu char *name;
32571b3fa15SDavid Xu u_int64_t uniqueid; /* for gdb */
32671b3fa15SDavid Xu
32771b3fa15SDavid Xu /*
32871b3fa15SDavid Xu * Lock for accesses to this thread structure.
32971b3fa15SDavid Xu */
33071b3fa15SDavid Xu umtx_t lock;
33171b3fa15SDavid Xu
33271b3fa15SDavid Xu /* Thread is terminated in kernel, written by kernel. */
33371b3fa15SDavid Xu long terminated;
33471b3fa15SDavid Xu
33571b3fa15SDavid Xu /* Kernel thread id. */
336a276dc6bSMatthew Dillon lwpid_t tid;
33771b3fa15SDavid Xu
33871b3fa15SDavid Xu /* Internal condition variable cycle number. */
33971b3fa15SDavid Xu umtx_t cycle;
34071b3fa15SDavid Xu
34171b3fa15SDavid Xu /* How many low level locks the thread held. */
34271b3fa15SDavid Xu int locklevel;
34371b3fa15SDavid Xu
344fd4288bdSDavid Xu /*
345fd4288bdSDavid Xu * Set to non-zero when this thread has entered a critical
346fd4288bdSDavid Xu * region. We allow for recursive entries into critical regions.
347fd4288bdSDavid Xu */
348fd4288bdSDavid Xu int critical_count;
349fd4288bdSDavid Xu
35071b3fa15SDavid Xu /* Signal blocked counter. */
35171b3fa15SDavid Xu int sigblock;
35271b3fa15SDavid Xu
35371b3fa15SDavid Xu /* Queue entry for list of all threads. */
354cf8046a9Szrj TAILQ_ENTRY(__pthread_s) tle; /* link for all threads in process */
35571b3fa15SDavid Xu
35671b3fa15SDavid Xu /* Queue entry for GC lists. */
357cf8046a9Szrj TAILQ_ENTRY(__pthread_s) gcle;
35871b3fa15SDavid Xu
35971b3fa15SDavid Xu /* Hash queue entry. */
360cf8046a9Szrj LIST_ENTRY(__pthread_s) hle;
36171b3fa15SDavid Xu
36271b3fa15SDavid Xu /* Threads reference count. */
36371b3fa15SDavid Xu int refcount;
36471b3fa15SDavid Xu
36571b3fa15SDavid Xu /*
36671b3fa15SDavid Xu * Thread start routine, argument, stack pointer and thread
36771b3fa15SDavid Xu * attributes.
36871b3fa15SDavid Xu */
36971b3fa15SDavid Xu void *(*start_routine)(void *);
37071b3fa15SDavid Xu void *arg;
371cf8046a9Szrj struct __pthread_attr_s attr;
37271b3fa15SDavid Xu
37371b3fa15SDavid Xu /*
37471b3fa15SDavid Xu * Cancelability flags
37571b3fa15SDavid Xu */
37671b3fa15SDavid Xu #define THR_CANCEL_DISABLE 0x0001
37771b3fa15SDavid Xu #define THR_CANCEL_EXITING 0x0002
37871b3fa15SDavid Xu #define THR_CANCEL_AT_POINT 0x0004
37971b3fa15SDavid Xu #define THR_CANCEL_NEEDED 0x0008
38071b3fa15SDavid Xu #define SHOULD_CANCEL(val) \
38171b3fa15SDavid Xu (((val) & (THR_CANCEL_DISABLE | THR_CANCEL_EXITING | \
38271b3fa15SDavid Xu THR_CANCEL_NEEDED)) == THR_CANCEL_NEEDED)
38371b3fa15SDavid Xu
38471b3fa15SDavid Xu #define SHOULD_ASYNC_CANCEL(val) \
38571b3fa15SDavid Xu (((val) & (THR_CANCEL_DISABLE | THR_CANCEL_EXITING | \
38671b3fa15SDavid Xu THR_CANCEL_NEEDED | THR_CANCEL_AT_POINT)) == \
38771b3fa15SDavid Xu (THR_CANCEL_NEEDED | THR_CANCEL_AT_POINT))
38871b3fa15SDavid Xu int cancelflags;
38971b3fa15SDavid Xu
39071b3fa15SDavid Xu /* Thread temporary signal mask. */
39171b3fa15SDavid Xu sigset_t sigmask;
39271b3fa15SDavid Xu
39371b3fa15SDavid Xu /* Thread state: */
39471b3fa15SDavid Xu umtx_t state;
39571b3fa15SDavid Xu
39671b3fa15SDavid Xu /*
39767837f3aSDavid Xu * Error variable used instead of errno, used for internal.
39871b3fa15SDavid Xu */
39971b3fa15SDavid Xu int error;
40071b3fa15SDavid Xu
40171b3fa15SDavid Xu /*
40271b3fa15SDavid Xu * The joiner is the thread that is joining to this thread. The
40371b3fa15SDavid Xu * join status keeps track of a join operation to another thread.
40471b3fa15SDavid Xu */
405cf8046a9Szrj struct __pthread_s *joiner;
40671b3fa15SDavid Xu
40771b3fa15SDavid Xu /*
40871b3fa15SDavid Xu * The current thread can belong to a priority mutex queue.
40971b3fa15SDavid Xu * This is the synchronization queue link.
41071b3fa15SDavid Xu */
411cf8046a9Szrj TAILQ_ENTRY(__pthread_s) sqe;
41271b3fa15SDavid Xu
41371b3fa15SDavid Xu /* Miscellaneous flags; only set with scheduling lock held. */
41471b3fa15SDavid Xu int flags;
41571b3fa15SDavid Xu #define THR_FLAGS_PRIVATE 0x0001
41671b3fa15SDavid Xu #define THR_FLAGS_NEED_SUSPEND 0x0002 /* thread should be suspended */
41771b3fa15SDavid Xu #define THR_FLAGS_SUSPENDED 0x0004 /* thread is suspended */
41871b3fa15SDavid Xu
41971b3fa15SDavid Xu /* Thread list flags; only set with thread list lock held. */
42071b3fa15SDavid Xu int tlflags;
42171b3fa15SDavid Xu #define TLFLAGS_GC_SAFE 0x0001 /* thread safe for cleaning */
42271b3fa15SDavid Xu #define TLFLAGS_IN_TDLIST 0x0002 /* thread in all thread list */
42371b3fa15SDavid Xu #define TLFLAGS_IN_GCLIST 0x0004 /* thread in gc list */
42471b3fa15SDavid Xu #define TLFLAGS_DETACHED 0x0008 /* thread is detached */
42571b3fa15SDavid Xu
42671b3fa15SDavid Xu /*
42771b3fa15SDavid Xu * Base priority is the user setable and retrievable priority
42871b3fa15SDavid Xu * of the thread. It is only affected by explicit calls to
42971b3fa15SDavid Xu * set thread priority and upon thread creation via a thread
43071b3fa15SDavid Xu * attribute or default priority.
43171b3fa15SDavid Xu */
43271b3fa15SDavid Xu char base_priority;
43371b3fa15SDavid Xu
43471b3fa15SDavid Xu /*
43571b3fa15SDavid Xu * Inherited priority is the priority a thread inherits by
4366a8bb22dSSascha Wildner * taking a priority inheritance or protection mutex. It
43771b3fa15SDavid Xu * is not affected by base priority changes. Inherited
43871b3fa15SDavid Xu * priority defaults to and remains 0 until a mutex is taken
43971b3fa15SDavid Xu * that is being waited on by any other thread whose priority
44071b3fa15SDavid Xu * is non-zero.
44171b3fa15SDavid Xu */
44271b3fa15SDavid Xu char inherited_priority;
44371b3fa15SDavid Xu
44471b3fa15SDavid Xu /*
44571b3fa15SDavid Xu * Active priority is always the maximum of the threads base
44671b3fa15SDavid Xu * priority and inherited priority. When there is a change
44771b3fa15SDavid Xu * in either the base or inherited priority, the active
44871b3fa15SDavid Xu * priority must be recalculated.
44971b3fa15SDavid Xu */
45071b3fa15SDavid Xu char active_priority;
45171b3fa15SDavid Xu
45271b3fa15SDavid Xu /* Number of priority ceiling or protection mutexes owned. */
45371b3fa15SDavid Xu int priority_mutex_count;
45471b3fa15SDavid Xu
45571b3fa15SDavid Xu /* Queue of currently owned simple type mutexes. */
456cf8046a9Szrj TAILQ_HEAD(, __pthread_mutex_s) mutexq;
45771b3fa15SDavid Xu
45871b3fa15SDavid Xu void *ret;
45971b3fa15SDavid Xu struct pthread_specific_elem *specific;
46071b3fa15SDavid Xu int specific_data_count;
46171b3fa15SDavid Xu
46271b3fa15SDavid Xu /* Number rwlocks rdlocks held. */
46371b3fa15SDavid Xu int rdlock_count;
46471b3fa15SDavid Xu
46571b3fa15SDavid Xu /*
46671b3fa15SDavid Xu * Current locks bitmap for rtld. */
46771b3fa15SDavid Xu int rtld_bits;
46871b3fa15SDavid Xu
46971b3fa15SDavid Xu /* Thread control block */
4709e2ee207SJoerg Sonnenberger struct tls_tcb *tcb;
47171b3fa15SDavid Xu
47271b3fa15SDavid Xu /* Cleanup handlers Link List */
47371b3fa15SDavid Xu struct pthread_cleanup *cleanup;
4743cd47da3SDavid Xu
4753cd47da3SDavid Xu /* Enable event reporting */
4763cd47da3SDavid Xu int report_events;
4773cd47da3SDavid Xu
4783cd47da3SDavid Xu /* Event mask */
4793cd47da3SDavid Xu td_thr_events_t event_mask;
4803cd47da3SDavid Xu
4813cd47da3SDavid Xu /* Event */
4823cd47da3SDavid Xu td_event_msg_t event_buf;
48371b3fa15SDavid Xu };
48471b3fa15SDavid Xu
485fd4288bdSDavid Xu #define THR_IN_CRITICAL(thrd) \
486fd4288bdSDavid Xu (((thrd)->locklevel > 0) || \
487fd4288bdSDavid Xu ((thrd)->critical_count > 0))
488fd4288bdSDavid Xu
489721505deSMatthew Dillon /*
490721505deSMatthew Dillon * Internal temporary locks without suspend check
491721505deSMatthew Dillon */
49271b3fa15SDavid Xu #define THR_UMTX_TRYLOCK(thrd, lck) \
493721505deSMatthew Dillon _thr_umtx_trylock((lck), (thrd)->tid, 1)
49471b3fa15SDavid Xu
49571b3fa15SDavid Xu #define THR_UMTX_LOCK(thrd, lck) \
496721505deSMatthew Dillon _thr_umtx_lock((lck), (thrd)->tid, 1)
49771b3fa15SDavid Xu
49871b3fa15SDavid Xu #define THR_UMTX_TIMEDLOCK(thrd, lck, timo) \
499721505deSMatthew Dillon _thr_umtx_timedlock((lck), (thrd)->tid, (timo), 1)
50071b3fa15SDavid Xu
50171b3fa15SDavid Xu #define THR_UMTX_UNLOCK(thrd, lck) \
502721505deSMatthew Dillon _thr_umtx_unlock((lck), (thrd)->tid, 1)
50371b3fa15SDavid Xu
504721505deSMatthew Dillon /*
505721505deSMatthew Dillon * Interal locks without suspend check, used when the lock
506721505deSMatthew Dillon * state needs to persist (i.e. to help implement things
507721505deSMatthew Dillon * like pthread_mutex_lock()). Non-temporary.
508721505deSMatthew Dillon */
509721505deSMatthew Dillon #define THR_UMTX_TRYLOCK_PERSIST(thrd, lck) \
510721505deSMatthew Dillon _thr_umtx_trylock((lck), (thrd)->tid, 0)
511721505deSMatthew Dillon
512721505deSMatthew Dillon #define THR_UMTX_LOCK_PERSIST(thrd, lck) \
513721505deSMatthew Dillon _thr_umtx_lock((lck), (thrd)->tid, 0)
514721505deSMatthew Dillon
515721505deSMatthew Dillon #define THR_UMTX_TIMEDLOCK_PERSIST(thrd, lck, timo) \
516721505deSMatthew Dillon _thr_umtx_timedlock((lck), (thrd)->tid, (timo), 0)
517721505deSMatthew Dillon
518721505deSMatthew Dillon #define THR_UMTX_UNLOCK_PERSIST(thrd, lck) \
519721505deSMatthew Dillon _thr_umtx_unlock((lck), (thrd)->tid, 0)
520721505deSMatthew Dillon
521721505deSMatthew Dillon /*
522721505deSMatthew Dillon * Internal temporary locks with suspend check
523721505deSMatthew Dillon */
52471b3fa15SDavid Xu #define THR_LOCK_ACQUIRE(thrd, lck) \
52571b3fa15SDavid Xu do { \
52671b3fa15SDavid Xu (thrd)->locklevel++; \
527721505deSMatthew Dillon _thr_umtx_lock((lck), (thrd)->tid, 1); \
52871b3fa15SDavid Xu } while (0)
52971b3fa15SDavid Xu
530fd4288bdSDavid Xu #ifdef _PTHREADS_INVARIANTS
531fd4288bdSDavid Xu #define THR_ASSERT_LOCKLEVEL(thrd) \
532fd4288bdSDavid Xu do { \
533fd4288bdSDavid Xu if (__predict_false((thrd)->locklevel <= 0)) \
534fd4288bdSDavid Xu _thr_assert_lock_level(); \
535fd4288bdSDavid Xu } while (0)
536fd4288bdSDavid Xu #else
537fd4288bdSDavid Xu #define THR_ASSERT_LOCKLEVEL(thrd)
538fd4288bdSDavid Xu #endif
539fd4288bdSDavid Xu
54071b3fa15SDavid Xu #define THR_LOCK_RELEASE(thrd, lck) \
54171b3fa15SDavid Xu do { \
542fd4288bdSDavid Xu THR_ASSERT_LOCKLEVEL(thrd); \
543721505deSMatthew Dillon _thr_umtx_unlock((lck), (thrd)->tid, 1); \
54471b3fa15SDavid Xu (thrd)->locklevel--; \
545fd4288bdSDavid Xu _thr_ast(thrd); \
54671b3fa15SDavid Xu } while (0)
54771b3fa15SDavid Xu
54871b3fa15SDavid Xu #define THR_LOCK(curthrd) THR_LOCK_ACQUIRE(curthrd, &(curthrd)->lock)
54971b3fa15SDavid Xu #define THR_UNLOCK(curthrd) THR_LOCK_RELEASE(curthrd, &(curthrd)->lock)
55071b3fa15SDavid Xu #define THR_THREAD_LOCK(curthrd, thr) THR_LOCK_ACQUIRE(curthrd, &(thr)->lock)
55171b3fa15SDavid Xu #define THR_THREAD_UNLOCK(curthrd, thr) THR_LOCK_RELEASE(curthrd, &(thr)->lock)
55271b3fa15SDavid Xu
55371b3fa15SDavid Xu #define THREAD_LIST_LOCK(curthrd) \
55471b3fa15SDavid Xu do { \
55571b3fa15SDavid Xu THR_LOCK_ACQUIRE((curthrd), &_thr_list_lock); \
55671b3fa15SDavid Xu } while (0)
55771b3fa15SDavid Xu
55871b3fa15SDavid Xu #define THREAD_LIST_UNLOCK(curthrd) \
55971b3fa15SDavid Xu do { \
56071b3fa15SDavid Xu THR_LOCK_RELEASE((curthrd), &_thr_list_lock); \
56171b3fa15SDavid Xu } while (0)
56271b3fa15SDavid Xu
56371b3fa15SDavid Xu /*
56471b3fa15SDavid Xu * Macros to insert/remove threads to the all thread list and
56571b3fa15SDavid Xu * the gc list.
56671b3fa15SDavid Xu */
56771b3fa15SDavid Xu #define THR_LIST_ADD(thrd) do { \
56871b3fa15SDavid Xu if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) == 0) { \
56971b3fa15SDavid Xu TAILQ_INSERT_HEAD(&_thread_list, thrd, tle); \
57071b3fa15SDavid Xu _thr_hash_add(thrd); \
57171b3fa15SDavid Xu (thrd)->tlflags |= TLFLAGS_IN_TDLIST; \
57271b3fa15SDavid Xu } \
57371b3fa15SDavid Xu } while (0)
57471b3fa15SDavid Xu #define THR_LIST_REMOVE(thrd) do { \
57571b3fa15SDavid Xu if (((thrd)->tlflags & TLFLAGS_IN_TDLIST) != 0) { \
57671b3fa15SDavid Xu TAILQ_REMOVE(&_thread_list, thrd, tle); \
57771b3fa15SDavid Xu _thr_hash_remove(thrd); \
57871b3fa15SDavid Xu (thrd)->tlflags &= ~TLFLAGS_IN_TDLIST; \
57971b3fa15SDavid Xu } \
58071b3fa15SDavid Xu } while (0)
58171b3fa15SDavid Xu #define THR_GCLIST_ADD(thrd) do { \
58271b3fa15SDavid Xu if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) == 0) { \
58371b3fa15SDavid Xu TAILQ_INSERT_HEAD(&_thread_gc_list, thrd, gcle);\
58471b3fa15SDavid Xu (thrd)->tlflags |= TLFLAGS_IN_GCLIST; \
585e8382b15SDavid Xu _thr_gc_count++; \
58671b3fa15SDavid Xu } \
58771b3fa15SDavid Xu } while (0)
58871b3fa15SDavid Xu #define THR_GCLIST_REMOVE(thrd) do { \
58971b3fa15SDavid Xu if (((thrd)->tlflags & TLFLAGS_IN_GCLIST) != 0) { \
59071b3fa15SDavid Xu TAILQ_REMOVE(&_thread_gc_list, thrd, gcle); \
59171b3fa15SDavid Xu (thrd)->tlflags &= ~TLFLAGS_IN_GCLIST; \
592e8382b15SDavid Xu _thr_gc_count--; \
59371b3fa15SDavid Xu } \
59471b3fa15SDavid Xu } while (0)
59571b3fa15SDavid Xu
596e8382b15SDavid Xu #define GC_NEEDED() (_thr_gc_count >= 5)
59771b3fa15SDavid Xu
59871b3fa15SDavid Xu #define THR_IN_SYNCQ(thrd) (((thrd)->sflags & THR_FLAGS_IN_SYNCQ) != 0)
59971b3fa15SDavid Xu
6003cd47da3SDavid Xu #define SHOULD_REPORT_EVENT(curthr, e) \
6013cd47da3SDavid Xu (curthr->report_events && \
6023cd47da3SDavid Xu (((curthr)->event_mask | _thread_event_mask ) & e) != 0)
6033cd47da3SDavid Xu
604ceae3ef4SSascha Wildner #ifndef __LIBC_ISTHREADED_DECLARED
605ceae3ef4SSascha Wildner #define __LIBC_ISTHREADED_DECLARED
60671b3fa15SDavid Xu extern int __isthreaded;
6076be5672cSzrj #endif
60871b3fa15SDavid Xu
60971b3fa15SDavid Xu /*
610e8382b15SDavid Xu * Global variables for the pthread library.
61171b3fa15SDavid Xu */
612fc71f871SDavid Xu extern char *_usrstack;
613940be950Szrj extern pthread_t _thr_initial;
6143cd47da3SDavid Xu
61571b3fa15SDavid Xu /* For debugger */
616e8382b15SDavid Xu extern int _libthread_xu_debug;
617e8382b15SDavid Xu extern int _thread_event_mask;
618940be950Szrj extern pthread_t _thread_last_event;
61971b3fa15SDavid Xu
620e8382b15SDavid Xu /* List of all threads */
621e8382b15SDavid Xu extern struct thread_head _thread_list;
62271b3fa15SDavid Xu
623e8382b15SDavid Xu /* List of threads needing GC */
624e8382b15SDavid Xu extern struct thread_head _thread_gc_list;
62571b3fa15SDavid Xu
626e8382b15SDavid Xu extern int _thread_active_threads;
62771b3fa15SDavid Xu
628e8382b15SDavid Xu extern struct atfork_head _thr_atfork_list;
629fcaa7a3aSMatthew Dillon extern struct atfork_head _thr_atfork_kern_list;
630e8382b15SDavid Xu extern umtx_t _thr_atfork_lock;
63171b3fa15SDavid Xu
632e8382b15SDavid Xu /* Default thread attributes */
633cf8046a9Szrj extern struct __pthread_attr_s _pthread_attr_default;
63471b3fa15SDavid Xu
635e8382b15SDavid Xu /* Default mutex attributes */
636cf8046a9Szrj extern struct __pthread_mutexattr_s _pthread_mutexattr_default;
63771b3fa15SDavid Xu
638e8382b15SDavid Xu /* Default condition variable attributes */
639cf8046a9Szrj extern struct __pthread_condattr_s _pthread_condattr_default;
64071b3fa15SDavid Xu
641e8382b15SDavid Xu extern pid_t _thr_pid;
642fc71f871SDavid Xu extern size_t _thr_guard_default;
643fc71f871SDavid Xu extern size_t _thr_stack_default;
644fc71f871SDavid Xu extern size_t _thr_stack_initial;
645e8382b15SDavid Xu extern int _thr_page_size;
646e8382b15SDavid Xu extern int _thr_gc_count;
64771b3fa15SDavid Xu
648e8382b15SDavid Xu extern umtx_t _mutex_static_lock;
649e8382b15SDavid Xu extern umtx_t _cond_static_lock;
650e8382b15SDavid Xu extern umtx_t _rwlock_static_lock;
651e8382b15SDavid Xu extern umtx_t _keytable_lock;
652e8382b15SDavid Xu extern umtx_t _thr_list_lock;
653e8382b15SDavid Xu extern umtx_t _thr_event_lock;
65471b3fa15SDavid Xu
65571b3fa15SDavid Xu /*
65671b3fa15SDavid Xu * Function prototype definitions.
65771b3fa15SDavid Xu */
65871b3fa15SDavid Xu __BEGIN_DECLS
65971b3fa15SDavid Xu int _thr_setthreaded(int);
660a8851a0fSDavid Xu int _mutex_cv_lock(pthread_mutex_t *, int count);
661a8851a0fSDavid Xu int _mutex_cv_unlock(pthread_mutex_t *, int *count);
662940be950Szrj void _mutex_notify_priochange(pthread_t, pthread_t, int);
663940be950Szrj void _mutex_fork(pthread_t, lwpid_t tid);
664940be950Szrj void _mutex_unlock_private(pthread_t);
665fcaa7a3aSMatthew Dillon
666fcaa7a3aSMatthew Dillon #if 0
667fcaa7a3aSMatthew Dillon int _mutex_reinit(pthread_mutex_t *);
668fcaa7a3aSMatthew Dillon void _cond_reinit(pthread_cond_t pcond);
669fcaa7a3aSMatthew Dillon void _rwlock_reinit(pthread_rwlock_t prwlock);
670fcaa7a3aSMatthew Dillon #endif
671fcaa7a3aSMatthew Dillon
672940be950Szrj void _libpthread_init(pthread_t);
673940be950Szrj pthread_t _thr_alloc(pthread_t);
674fc71f871SDavid Xu void _thread_exit(const char *, int, const char *) __dead2;
6754a9b8501Szrj void _thread_exitf(const char *, int, const char *, ...) __dead2
6764a9b8501Szrj __printflike(3, 4);
67771b3fa15SDavid Xu void _thr_exit_cleanup(void);
678fcaa7a3aSMatthew Dillon void _thr_atfork_kern(void (*prepare)(void), void (*parent)(void),
679fcaa7a3aSMatthew Dillon void (*child)(void));
680940be950Szrj int _thr_ref_add(pthread_t, pthread_t, int);
681940be950Szrj void _thr_ref_delete(pthread_t, pthread_t);
682940be950Szrj void _thr_ref_delete_unlocked(pthread_t, pthread_t);
683940be950Szrj int _thr_find_thread(pthread_t, pthread_t, int);
684e2caf0e7SMatthew Dillon void _thr_malloc_init(void);
68550caca1aSMatthew Dillon void _rtld_setthreaded(int);
68671b3fa15SDavid Xu void _thr_rtld_init(void);
68771b3fa15SDavid Xu void _thr_rtld_fini(void);
688940be950Szrj int _thr_stack_alloc(pthread_attr_t);
689940be950Szrj void _thr_stack_free(pthread_attr_t);
6900dc5e56dSMatthew Dillon void _thr_stack_cleanup(void);
69198247283SMatthew Dillon void _thr_sem_init(void);
692940be950Szrj void _thr_free(pthread_t, pthread_t);
693940be950Szrj void _thr_gc(pthread_t);
69471b3fa15SDavid Xu void _thread_cleanupspecific(void);
69571b3fa15SDavid Xu void _thread_dump_info(void);
6964a9b8501Szrj void _thread_printf(int, const char *, ...) __printflike(2, 3);
6974a9b8501Szrj void _thread_vprintf(int, const char *, va_list);
69871b3fa15SDavid Xu void _thr_spinlock_init(void);
699940be950Szrj int _thr_cancel_enter(pthread_t);
700940be950Szrj void _thr_cancel_leave(pthread_t, int);
701940be950Szrj void _thr_signal_block(pthread_t);
702940be950Szrj void _thr_signal_unblock(pthread_t);
70371b3fa15SDavid Xu void _thr_signal_init(void);
70471b3fa15SDavid Xu void _thr_signal_deinit(void);
705940be950Szrj int _thr_send_sig(pthread_t, int sig);
706fc71f871SDavid Xu void _thr_list_init(void);
707940be950Szrj void _thr_hash_add(pthread_t);
708940be950Szrj void _thr_hash_remove(pthread_t);
709940be950Szrj pthread_t _thr_hash_find(pthread_t);
710940be950Szrj void _thr_link(pthread_t, pthread_t);
711940be950Szrj void _thr_unlink(pthread_t, pthread_t);
712940be950Szrj void _thr_suspend_check(pthread_t);
713fc71f871SDavid Xu void _thr_assert_lock_level(void) __dead2;
714940be950Szrj void _thr_ast(pthread_t);
71571b3fa15SDavid Xu int _thr_get_tid(void);
716940be950Szrj void _thr_report_creation(pthread_t, pthread_t);
717940be950Szrj void _thr_report_death(pthread_t);
7183cd47da3SDavid Xu void _thread_bp_create(void);
7193cd47da3SDavid Xu void _thread_bp_death(void);
720a31d0705SMatthew Dillon int _thr_getscheduler(lwpid_t, int *, struct sched_param *);
721a31d0705SMatthew Dillon int _thr_setscheduler(lwpid_t, int, const struct sched_param *);
722940be950Szrj int _thr_set_sched_other_prio(pthread_t, int);
723d1cf13b9SSascha Wildner int _rtp_to_schedparam(const struct rtprio *rtp, int *policy,
724d1cf13b9SSascha Wildner struct sched_param *param);
725d1cf13b9SSascha Wildner int _schedparam_to_rtp(int policy, const struct sched_param *param,
726d1cf13b9SSascha Wildner struct rtprio *rtp);
7270a215286SMatthew Dillon int _umtx_sleep_err(volatile const int *, int, int);
7280a215286SMatthew Dillon int _umtx_wakeup_err(volatile const int *, int);
72971b3fa15SDavid Xu
73071b3fa15SDavid Xu /* #include <fcntl.h> */
73171b3fa15SDavid Xu #ifdef _SYS_FCNTL_H_
73271b3fa15SDavid Xu int __sys_fcntl(int, int, ...);
73371b3fa15SDavid Xu int __sys_open(const char *, int, ...);
734d1cf13b9SSascha Wildner int __sys_openat(int, const char *, int, ...);
73571b3fa15SDavid Xu #endif
73671b3fa15SDavid Xu
73771b3fa15SDavid Xu /* #include <sys/ioctl.h> */
73871b3fa15SDavid Xu #ifdef _SYS_IOCTL_H_
73971b3fa15SDavid Xu int __sys_ioctl(int, unsigned long, ...);
74071b3fa15SDavid Xu #endif
74171b3fa15SDavid Xu
74244c4a02dSSascha Wildner /* #include <sched.h> */
743*1f2cfeb1SSascha Wildner #ifdef _SYS_SCHED_H_
74471b3fa15SDavid Xu int __sys_sched_yield(void);
74571b3fa15SDavid Xu #endif
74671b3fa15SDavid Xu
74771b3fa15SDavid Xu /* #include <signal.h> */
74871b3fa15SDavid Xu #ifdef _SIGNAL_H_
74971b3fa15SDavid Xu int __sys_sigaction(int, const struct sigaction *, struct sigaction *);
75071b3fa15SDavid Xu int __sys_sigprocmask(int, const sigset_t *, sigset_t *);
75171b3fa15SDavid Xu int __sys_sigsuspend(const sigset_t *);
75271b3fa15SDavid Xu #endif
75371b3fa15SDavid Xu
75471b3fa15SDavid Xu /* #include <time.h> */
75571b3fa15SDavid Xu #ifdef _TIME_H_
75671b3fa15SDavid Xu int __sys_nanosleep(const struct timespec *, struct timespec *);
75786ccdacbSAaron LI int __sys_clock_nanosleep(clockid_t, int, const struct timespec *,
75886ccdacbSAaron LI struct timespec *);
75971b3fa15SDavid Xu #endif
76071b3fa15SDavid Xu
76171b3fa15SDavid Xu /* #include <unistd.h> */
76271b3fa15SDavid Xu #ifdef _UNISTD_H_
76371b3fa15SDavid Xu int __sys_close(int);
76471b3fa15SDavid Xu pid_t __sys_getpid(void);
76571b3fa15SDavid Xu ssize_t __sys_read(int, void *, size_t);
76671b3fa15SDavid Xu ssize_t __sys_write(int, const void *, size_t);
76771b3fa15SDavid Xu int __sys_sigtimedwait(const sigset_t *, siginfo_t *,
76871b3fa15SDavid Xu const struct timespec *);
76971b3fa15SDavid Xu int __sys_sigwaitinfo(const sigset_t *set, siginfo_t *info);
77071b3fa15SDavid Xu #endif
77171b3fa15SDavid Xu
77271b3fa15SDavid Xu static inline int
_thr_isthreaded(void)77371b3fa15SDavid Xu _thr_isthreaded(void)
77471b3fa15SDavid Xu {
77571b3fa15SDavid Xu return (__isthreaded != 0);
77671b3fa15SDavid Xu }
77771b3fa15SDavid Xu
77871b3fa15SDavid Xu static inline int
_thr_is_inited(void)77971b3fa15SDavid Xu _thr_is_inited(void)
78071b3fa15SDavid Xu {
781678e8cc6SSascha Wildner return (_thr_initial != NULL);
78271b3fa15SDavid Xu }
78371b3fa15SDavid Xu
78471b3fa15SDavid Xu static inline void
_thr_check_init(void)78571b3fa15SDavid Xu _thr_check_init(void)
78671b3fa15SDavid Xu {
787678e8cc6SSascha Wildner if (_thr_initial == NULL)
78819451dc5Szrj _libpthread_init(NULL);
78971b3fa15SDavid Xu }
79071b3fa15SDavid Xu
791fcf53d9bSJohn Marino struct dl_phdr_info;
792fcf53d9bSJohn Marino void __pthread_cxa_finalize(struct dl_phdr_info *phdr_info);
793fcf53d9bSJohn Marino
794e7bf3f77SMatthew Dillon /*
795e7bf3f77SMatthew Dillon * Used in low-level init to directly call libc's malloc implementation
796e7bf3f77SMatthew Dillon * instead of a potentially third-party malloc implementation. Required
797e7bf3f77SMatthew Dillon * for bootstrapping pthreads.
798e7bf3f77SMatthew Dillon */
799e7bf3f77SMatthew Dillon void *__malloc(size_t bytes);
800e7bf3f77SMatthew Dillon void __free(void *ptr);
801e7bf3f77SMatthew Dillon
80271b3fa15SDavid Xu __END_DECLS
80371b3fa15SDavid Xu
80471b3fa15SDavid Xu #endif /* !_THR_PRIVATE_H */
805