• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 _m_type __u.__i[0]
87 #define _m_lock __u.__vi[1]
88 #define _m_waiters __u.__vi[2]
89 #define _m_prev __u.__p[3]
90 #define _m_next __u.__p[4]
91 #define _m_count __u.__i[5]
92 #define _c_shared __u.__p[0]
93 #define _c_seq __u.__vi[2]
94 #define _c_waiters __u.__vi[3]
95 #define _c_clock __u.__i[4]
96 #define _c_lock __u.__vi[8]
97 #define _c_head __u.__p[1]
98 #define _c_tail __u.__p[5]
99 #define _rw_lock __u.__vi[0]
100 #define _rw_waiters __u.__vi[1]
101 #define _rw_shared __u.__i[2]
102 #define _b_lock __u.__vi[0]
103 #define _b_waiters __u.__vi[1]
104 #define _b_limit __u.__i[2]
105 #define _b_count __u.__vi[3]
106 #define _b_waiters2 __u.__vi[4]
107 #define _b_inst __u.__p[3]
108 
109 #ifndef TP_OFFSET
110 #define TP_OFFSET 0
111 #endif
112 
113 #ifndef DTP_OFFSET
114 #define DTP_OFFSET 0
115 #endif
116 
117 #ifdef TLS_ABOVE_TP
118 #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + TP_OFFSET)
119 #define __pthread_self() ((pthread_t)(__get_tp() - sizeof(struct __pthread) - TP_OFFSET))
120 #else
121 #define TP_ADJ(p) (p)
122 #define __pthread_self() ((pthread_t)__get_tp())
123 #endif
124 
125 #ifndef tls_mod_off_t
126 #define tls_mod_off_t size_t
127 #endif
128 
129 #define SIGTIMER 32
130 #define SIGCANCEL 33
131 #define SIGSYNCCALL 34
132 
133 #define SIGALL_SET ((sigset_t *)(const unsigned long long [2]){ -1,-1 })
134 #define SIGPT_SET \
135 	((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \
136 	[sizeof(long)==4] = 3UL<<(32*(sizeof(long)>4)) })
137 #define SIGTIMER_SET \
138 	((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \
139 	 0x80000000 })
140 
141 void *__tls_get_addr(tls_mod_off_t *);
142 hidden int __init_tp(void *);
143 hidden void *__copy_tls(unsigned char *);
144 hidden void __reset_tls();
145 
146 hidden void __membarrier_init(void);
147 hidden void __dl_thread_cleanup(void);
148 hidden void __testcancel();
149 hidden void __do_cleanup_push(struct __ptcb *);
150 hidden void __do_cleanup_pop(struct __ptcb *);
151 hidden void __pthread_tsd_run_dtors();
152 
153 hidden void __pthread_key_delete_synccall(void (*)(void *), void *);
154 hidden int __pthread_key_delete_impl(pthread_key_t);
155 
156 extern hidden volatile size_t __pthread_tsd_size;
157 extern hidden void *__pthread_tsd_main[];
158 extern hidden volatile int __eintr_valid_flag;
159 
160 hidden int __clone(int (*)(void *), void *, int, void *, ...);
161 hidden int __set_thread_area(void *);
162 hidden int __libc_sigaction(int, const struct sigaction *, struct sigaction *);
163 hidden void __unmapself(void *, size_t);
164 
165 hidden int __timedwait(volatile int *, int, clockid_t, const struct timespec *, int);
166 hidden int __timedwait_cp(volatile int *, int, clockid_t, const struct timespec *, int);
167 hidden void __wait(volatile int *, volatile int *, int, int);
__wake(volatile void * addr,int cnt,int priv)168 static inline void __wake(volatile void *addr, int cnt, int priv)
169 {
170 	if (priv) priv = FUTEX_PRIVATE;
171 	if (cnt<0) cnt = INT_MAX;
172 	__syscall(SYS_futex, addr, FUTEX_WAKE|priv, cnt) != -ENOSYS ||
173 	__syscall(SYS_futex, addr, FUTEX_WAKE, cnt);
174 }
__futexwait(volatile void * addr,int val,int priv)175 static inline void __futexwait(volatile void *addr, int val, int priv)
176 {
177 	if (priv) priv = FUTEX_PRIVATE;
178 	__syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0) != -ENOSYS ||
179 	__syscall(SYS_futex, addr, FUTEX_WAIT, val, 0);
180 }
181 
182 hidden void __acquire_ptc(void);
183 hidden void __release_ptc(void);
184 hidden void __inhibit_ptc(void);
185 
186 hidden void __tl_lock(void);
187 hidden void __tl_unlock(void);
188 hidden void __tl_sync(pthread_t);
189 
190 extern hidden volatile int __thread_list_lock;
191 
192 extern hidden volatile int __abort_lock[1];
193 
194 extern hidden unsigned __default_stacksize;
195 extern hidden unsigned __default_guardsize;
196 
197 #define DEFAULT_STACK_SIZE 131072
198 #define DEFAULT_GUARD_SIZE 8192
199 
200 #define DEFAULT_STACK_MAX (8<<20)
201 #define DEFAULT_GUARD_MAX (1<<20)
202 
203 #define __ATTRP_C11_THREAD ((void*)(uintptr_t)-1)
204 
205 #endif
206