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