1 #ifndef _PTHREAD_IMPL_H
2 #define _PTHREAD_IMPL_H
3
4 #include <pthread.h>
5 #include <signal.h>
6 #include <errno.h>
7 #include <limits.h>
8 #include <sys/mman.h>
9 #include "libc.h"
10 #include "syscall.h"
11 #include "atomic.h"
12 #include "futex.h"
13
14 #include "pthread_arch.h"
15
16 #define pthread __pthread
17
18 struct pthread {
19 /* Part 1 -- these fields may be external or
20 * internal (accessed via asm) ABI. Do not change. */
21 struct pthread *self;
22 #ifndef TLS_ABOVE_TP
23 uintptr_t *dtv;
24 #endif
25 struct pthread *prev, *next; /* non-ABI */
26 uintptr_t sysinfo;
27 #ifndef TLS_ABOVE_TP
28 #ifdef CANARY_PAD
29 uintptr_t canary_pad;
30 #endif
31 uintptr_t canary;
32 #endif
33
34 /* Part 2 -- implementation details, non-ABI. */
35 int tid;
36 int errno_val;
37 volatile int detach_state;
38 volatile int cancel;
39 volatile unsigned char canceldisable, cancelasync;
40 unsigned char tsd_used:1;
41 unsigned char dlerror_flag:1;
42 unsigned char *map_base;
43 size_t map_size;
44 void *stack;
45 size_t stack_size;
46 size_t guard_size;
47 void *result;
48 struct __ptcb *cancelbuf;
49 void **tsd;
50 struct {
51 volatile void *volatile head;
52 long off;
53 volatile void *volatile pending;
54 } robust_list;
55 int h_errno_val;
56 volatile int timer_id;
57 locale_t locale;
58 volatile int killlock[1];
59 char *dlerror_buf;
60 void *stdio_locks;
61
62 /* Part 3 -- the positions of these fields relative to
63 * the end of the structure is external and internal ABI. */
64 #ifdef TLS_ABOVE_TP
65 uintptr_t canary;
66 uintptr_t *dtv;
67 #endif
68 };
69
70 enum {
71 DT_EXITED = 0,
72 DT_EXITING,
73 DT_JOINABLE,
74 DT_DETACHED,
75 };
76
77 #define __SU (sizeof(size_t)/sizeof(int))
78
79 #define _a_stacksize __u.__s[0]
80 #define _a_guardsize __u.__s[1]
81 #define _a_stackaddr __u.__s[2]
82 #define _a_detach __u.__i[3*__SU+0]
83 #define _a_sched __u.__i[3*__SU+1]
84 #define _a_policy __u.__i[3*__SU+2]
85 #define _a_prio __u.__i[3*__SU+3]
86 #define _a_runtime __u.__i[3*__SU+3]
87 #define _a_deadline __u.__i[3*__SU+4]
88 #define _a_period __u.__i[3*__SU+5]
89 #define _m_type __u.__i[0]
90 #define _m_lock __u.__vi[1]
91 #define _m_waiters __u.__vi[2]
92 #define _m_prev __u.__p[3]
93 #define _m_next __u.__p[4]
94 #define _m_count __u.__i[5]
95 #define _c_shared __u.__p[0]
96 #define _c_seq __u.__vi[2]
97 #define _c_waiters __u.__vi[3]
98 #define _c_clock __u.__i[4]
99 #define _c_lock __u.__vi[8]
100 #define _c_head __u.__p[1]
101 #define _c_tail __u.__p[5]
102 #define _rw_lock __u.__vi[0]
103 #define _rw_waiters __u.__vi[1]
104 #define _rw_shared __u.__i[2]
105 #define _b_lock __u.__vi[0]
106 #define _b_waiters __u.__vi[1]
107 #define _b_limit __u.__i[2]
108 #define _b_count __u.__vi[3]
109 #define _b_waiters2 __u.__vi[4]
110 #define _b_inst __u.__p[3]
111
112 #ifndef TP_OFFSET
113 #define TP_OFFSET 0
114 #endif
115
116 #ifndef DTP_OFFSET
117 #define DTP_OFFSET 0
118 #endif
119
120 #ifdef TLS_ABOVE_TP
121 #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + TP_OFFSET)
122 #else
123 #define TP_ADJ(p) (p)
124 #endif
125
126 #ifndef tls_mod_off_t
127 #define tls_mod_off_t size_t
128 #endif
129
130 #define SIGTIMER 32
131 #define SIGCANCEL 33
132 #define SIGSYNCCALL 34
133
134 #define SIGALL_SET ((sigset_t *)(const unsigned long long [2]){ -1,-1 })
135 #define SIGPT_SET \
136 ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \
137 [sizeof(long)==4] = 3UL<<(32*(sizeof(long)>4)) })
138 #define SIGTIMER_SET \
139 ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \
140 0x80000000 })
141
142 void *__tls_get_addr(tls_mod_off_t *);
143 hidden int __init_tp(void *);
144 hidden void *__copy_tls(unsigned char *);
145 hidden void __reset_tls();
146
147 hidden void __membarrier_init(void);
148 hidden void __dl_thread_cleanup(void);
149 hidden void __testcancel();
150 hidden void __do_cleanup_push(struct __ptcb *);
151 hidden void __do_cleanup_pop(struct __ptcb *);
152 hidden void __pthread_tsd_run_dtors();
153
154 hidden void __pthread_key_delete_synccall(void (*)(void *), void *);
155 hidden int __pthread_key_delete_impl(pthread_key_t);
156
157 extern hidden volatile size_t __pthread_tsd_size;
158 extern hidden void *__pthread_tsd_main[];
159 extern hidden volatile int __eintr_valid_flag;
160
161 hidden int __clone(int (*)(void *), void *, int, void *, ...);
162 hidden int __thread_clone(int (*func)(void *), int flags, struct pthread *thread, unsigned char *sp);
163 hidden int __set_thread_area(void *);
164 hidden int __libc_sigaction(int, const struct sigaction *, struct sigaction *);
165 hidden void __unmapself(void *, size_t);
166
167 hidden int __timedwait(volatile int *, int, clockid_t, const struct timespec *, int);
168 hidden int __timedwait_cp(volatile int *, int, clockid_t, const struct timespec *, int);
169 hidden void __wait(volatile int *, volatile int *, int, int);
__wake(volatile void * addr,int cnt,int priv)170 static inline void __wake(volatile void *addr, int cnt, int priv)
171 {
172 if (priv) priv = FUTEX_PRIVATE;
173 if (cnt<0) cnt = INT_MAX;
174 __syscall(SYS_futex, addr, FUTEX_WAKE|priv, cnt) != -ENOSYS ||
175 __syscall(SYS_futex, addr, FUTEX_WAKE, cnt);
176 }
__futexwait(volatile void * addr,int val,int priv)177 static inline void __futexwait(volatile void *addr, int val, int priv)
178 {
179 if (priv) priv = FUTEX_PRIVATE;
180 __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0xffffffffu) != -ENOSYS ||
181 __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0xffffffffu);
182 }
183
184 hidden void __acquire_ptc(void);
185 hidden void __release_ptc(void);
186 hidden void __inhibit_ptc(void);
187
188 hidden void __tl_lock(void);
189 hidden void __tl_unlock(void);
190 hidden void __tl_sync(pthread_t);
191
192 extern hidden volatile int __thread_list_lock;
193
194 extern hidden volatile int __abort_lock[1];
195
196 extern hidden unsigned __default_stacksize;
197 extern hidden unsigned __default_guardsize;
198
199 #define DEFAULT_STACK_SIZE 131072
200 #define DEFAULT_GUARD_SIZE 8192
201
202 #define DEFAULT_STACK_MAX (8<<20)
203 #define DEFAULT_GUARD_MAX (1<<20)
204
205 #define __ATTRP_C11_THREAD ((void*)(uintptr_t)-1)
206
207 #define MUSL_TYPE_THREAD (-1)
208 #define MUSL_TYPE_PROCESS (0)
209
210 #define PTHREAD_MUTEX_TYPE_MASK 3
211 #define PTHREAD_PRIORITY_LOWEST 31
212
213 #endif
214