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