-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpthreads.h
More file actions
287 lines (219 loc) · 9.63 KB
/
pthreads.h
File metadata and controls
287 lines (219 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#pragma once
#include <atomic>
#include <cstdint>
#include <pthread.h>
#include <libc_shim.h>
#include "meta.h"
#include "argrewrite.h"
#include "common.h"
namespace shim {
namespace bionic {
using atomic_uintptr_t = std::atomic_uintptr_t;
static_assert(alignof(atomic_uintptr_t) == alignof(uintptr_t));
static_assert(sizeof(atomic_uintptr_t) == sizeof(uintptr_t));
struct pthread_mutex_t {
#if defined(__LP64__)
int32_t data[10];
#else
int32_t data[1];
#endif
};
static_assert(alignof(pthread_mutex_t) == 4);
constexpr size_t mutex_init_value = 0;
constexpr size_t recursive_mutex_init_value = 0x4000;
constexpr size_t errorcheck_mutex_init_value = 0x8000;
template<class T> atomic_uintptr_t *get_payload_pointer(T *p) {
#if defined(__LP64__)
uintptr_t v = (uintptr_t)p;
// leave the first 4 bytes untouched (for init value)
static constexpr size_t header_size = sizeof(int32_t);
static constexpr size_t pointer_alignment = alignof(atomic_uintptr_t);
static constexpr size_t max_header_size = header_size + pointer_alignment - 1;
v += max_header_size;
v &= -pointer_alignment;
static_assert(max_header_size + sizeof(atomic_uintptr_t) < sizeof(*p), "Not enough space for payload pointer");
p = (T *)v;
#else
static_assert(alignof(atomic_uintptr_t) == alignof(T));
static_assert(sizeof(atomic_uintptr_t) == sizeof(T));
#endif
return (atomic_uintptr_t *)p;
}
inline bool is_mutex_initialized(uintptr_t v) {
#if defined(__LP64__)
return v != 0;
#else
constexpr uintptr_t INIT_MUTEX_MASK = ~(3u << 14);
return (v & INIT_MUTEX_MASK) != 0;
#endif
}
::pthread_mutex_t * mutex_static_initializer(int32_t init_value, bionic::atomic_uintptr_t *p, uintptr_t payload);
struct pthread_cond_t {
#if defined(__LP64__)
int32_t data[12];
#else
int32_t data[1];
#endif
};
inline bool is_cond_initialized(uintptr_t v) {
return v != 0;
}
::pthread_cond_t * cond_static_initializer(bionic::atomic_uintptr_t *p, uintptr_t payload);
struct pthread_rwlock_t {
#if defined(__LP64__)
int32_t data[14];
#else
int32_t data[1];
#endif
};
inline bool is_rwlock_initialized(uintptr_t v) {
return v != 0;
}
::pthread_rwlock_t * rwlock_static_initializer(bionic::atomic_uintptr_t *p, uintptr_t payload);
template <>
inline auto to_host<pthread_mutex_t>(pthread_mutex_t const *o) {
atomic_uintptr_t *payload_ptr = get_payload_pointer(const_cast<pthread_mutex_t *>(o));
uintptr_t payload = atomic_load_explicit(payload_ptr, std::memory_order_relaxed);
if(is_mutex_initialized(payload)) [[likely]] {
return (::pthread_mutex_t *)payload;
}
return mutex_static_initializer(o->data[0], payload_ptr, payload);
}
template <>
inline auto to_host<pthread_cond_t>(pthread_cond_t const *o) {
atomic_uintptr_t *payload_ptr = get_payload_pointer(const_cast<pthread_cond_t *>(o));
uintptr_t payload = atomic_load_explicit(payload_ptr, std::memory_order_relaxed);
if(is_cond_initialized(payload)) [[likely]] {
return (::pthread_cond_t *)payload;
}
return cond_static_initializer(payload_ptr, payload);
}
template <>
inline auto to_host<pthread_rwlock_t>(pthread_rwlock_t const *o) {
atomic_uintptr_t *payload_ptr = get_payload_pointer(const_cast<pthread_rwlock_t *>(o));
uintptr_t payload = atomic_load_explicit(payload_ptr, std::memory_order_relaxed);
if(is_rwlock_initialized(payload)) [[likely]] {
return (::pthread_rwlock_t *)payload;
}
return rwlock_static_initializer(payload_ptr, payload);
}
enum class mutex_type : uint32_t {
NORMAL = 0,
RECURSIVE = 1,
ERRORCHECK = 2,
END = 2
};
static int to_host_mutex_type(mutex_type type);
struct pthread_mutexattr_t {
mutex_type type : 4;
};
struct pthread_condattr_t {
bool shared : 1;
clock_type clock : 2;
};
enum class sched_policy {
OTHER = 0
};
static int to_host_sched_policy(sched_policy type);
static sched_policy from_host_sched_policy(int type);
struct pthread_attr_t {
bool detached : 1;
bool user_stack : 1;
uint32_t filler : 32-2;
void* stack_base;
size_t stack_size;
size_t guard_size;
sched_policy sched_policy_val;
int sched_priority;
#if defined(__LP64__)
int64_t priv[2];
#endif
};
struct sched_param {
int sched_priority;
};
struct pthread_cleanup_t {
pthread_cleanup_t *prev;
void (*routine)(void *);
void *arg;
};
using pthread_key_t = uint32_t;
using pthread_once_t = int;
struct pthread_cleanup_holder {
pthread_cleanup_t *current = nullptr;
~pthread_cleanup_holder();
};
extern thread_local pthread_cleanup_holder cleanup;
}
using pthread_attr_t = bionic::pthread_attr_t;
using pthread_mutex_t = bionic::pthread_mutex_t;
using pthread_mutexattr_t = bionic::pthread_mutexattr_t;
using pthread_cond_t = bionic::pthread_cond_t;
using pthread_condattr_t = bionic::pthread_condattr_t;
using pthread_rwlock_t = bionic::pthread_rwlock_t;
using pthread_key_t = bionic::pthread_key_t;
using pthread_once_t = bionic::pthread_once_t;
struct host_pthread_attr {
::pthread_attr_t attr;
host_pthread_attr(bionic::pthread_attr_t const *bionic_attr);
~host_pthread_attr();
};
struct host_mutexattr {
::pthread_mutexattr_t attr;
host_mutexattr(bionic::pthread_mutexattr_t const *bionic_attr);
~host_mutexattr();
};
struct host_condattr {
::pthread_condattr_t attr;
host_condattr(bionic::pthread_condattr_t const *bionic_attr);
~host_condattr();
};
int pthread_create(pthread_t *thread, pthread_attr_t const *attr, void* (*fn)(void *), void *arg);
int pthread_setschedparam(pthread_t thread, bionic::sched_policy policy, const bionic::sched_param *param);
int pthread_getschedparam(pthread_t thread, bionic::sched_policy *policy, bionic::sched_param *param);
int pthread_getattr_np(pthread_t th, pthread_attr_t* attr);
int pthread_attr_init(pthread_attr_t *attr);
int pthread_attr_destroy(pthread_attr_t *attr);
int pthread_attr_setdetachstate(pthread_attr_t *attr, int value);
int pthread_attr_getdetachstate(pthread_attr_t *attr, int *value);
int pthread_attr_setschedparam(pthread_attr_t *attr, const bionic::sched_param *value);
int pthread_attr_getschedparam(pthread_attr_t *attr, bionic::sched_param *value);
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t value);
int pthread_attr_getstack(pthread_attr_t *attr, void **stackaddr, size_t *stacksize);
int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *value);
int pthread_setname_np(pthread_t thread, const char* name);
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
int pthread_cond_destroy(pthread_cond_t* cond);
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
int pthread_cond_broadcast(pthread_cond_t *cond);
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *ts);
int pthread_condattr_init(pthread_condattr_t *attr);
int pthread_condattr_destroy(pthread_condattr_t *attr);
int pthread_condattr_setclock(pthread_condattr_t *attr, int clock);
int pthread_condattr_getclock(const pthread_condattr_t *attr, int *clock);
int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
int pthread_rwlock_destroy(pthread_rwlock_t* rwlock);
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
int pthread_key_delete(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void *value);
void *pthread_getspecific(pthread_key_t key);
void pthread_cleanup_push_impl(bionic::pthread_cleanup_t *c, void (*cb)(void *), void *arg);
void pthread_cleanup_pop_impl(bionic::pthread_cleanup_t *c, int execute);
int pthread_once(pthread_once_t *control, void (*routine)());
pid_t pthread_gettid_np(pthread_t thread);
void add_pthread_shimmed_symbols(std::vector<shimmed_symbol> &list);
namespace detail {
template <>
struct arg_rewrite<::pthread_rwlock_t *> : bionic_ptr_rewriter<typename ::pthread_rwlock_t *, pthread_rwlock_t *> {};
}
}