summaryrefslogtreecommitdiff
path: root/src/libpthread.c
blob: f640c457150b4aea302807cbce53bb873576a04f (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <memory.h>
#include <pthread.h>
#include <semaphore.h>
#include <assert.h>
#include <sys/mman.h>

typedef struct {
   union {
      struct {
         unsigned int count;
#ifdef __LP64__
         int __reserved[3];
#endif
      } bionic;
      sem_t *glibc;
   };
} bionic_sem_t;

typedef struct {
   union {
      struct {
         uint32_t flags;
         void* stack_base;
         size_t stack_size;
         size_t guard_size;
         int32_t sched_policy;
         int32_t sched_priority;
#ifdef __LP64__
         char __reserved[16];
#endif
      } bionic;
      pthread_attr_t *glibc;
   };
} bionic_attr_t;

typedef struct {
   union {
#if defined(__LP64__)
      int32_t __private[10];
#else
      int32_t __private[1];
#endif
      pthread_mutex_t *glibc;
   };
} bionic_mutex_t;

typedef struct {
   union {
      long __private;
      pthread_mutexattr_t *glibc;
   };
} bionic_mutexattr_t;

static const struct {
   bionic_mutex_t bionic;
   pthread_mutex_t glibc;
} bionic_mutex_init_map[] = {
   { .bionic = {{{ ((PTHREAD_MUTEX_NORMAL & 3) << 14) }}}, .glibc = PTHREAD_MUTEX_INITIALIZER },
   { .bionic = {{{ ((PTHREAD_MUTEX_RECURSIVE & 3) << 14) }}}, .glibc = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP },
   { .bionic = {{{ ((PTHREAD_MUTEX_ERRORCHECK & 3) << 14) }}}, .glibc = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP },
};

typedef struct {
   union {
#if defined(__LP64__)
      int32_t __private[12];
#else
      int32_t __private[1];
#endif
      pthread_cond_t *glibc;
   };
} bionic_cond_t;

typedef struct {
   union {
      long __private;
      pthread_condattr_t *glibc;
   };
} bionic_condattr_t;

typedef int bionic_key_t;
_Static_assert(sizeof(bionic_key_t) == sizeof(pthread_key_t), "bionic_key_t and pthread_key_t size mismatch");

typedef int bionic_once_t;
_Static_assert(sizeof(bionic_once_t) == sizeof(pthread_once_t), "bionic_once_t and pthread_once_t size mismatch");

typedef long bionic_pthread_t;
_Static_assert(sizeof(bionic_pthread_t) == sizeof(pthread_t), "bionic_pthread_t and pthread_t size mismatch");

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))

// For checking, if our glibc version is mapped to memory.
// Used for sanity checking and static initialization below.
#define IS_MAPPED(x) is_mapped(x->glibc, sizeof(x))

// For handling static initialization.
#define INIT_IF_NOT_MAPPED(x, init) do { if (!IS_MAPPED(x)) init(x); } while(0)

static bool
is_mapped(void *mem, const size_t sz)
{
   const size_t ps = sysconf(_SC_PAGESIZE);
   assert(ps > 0);
   unsigned char vec[(sz + ps - 1) / ps];
   return !mincore(mem, sz, vec);
}

void
bionic___pthread_cleanup_push(void *c, void *routine, void *arg)
{
   assert(0 && "implement");
}

void
bionic___pthread_cleanup_pop(void *c, int execute)
{
   assert(0 && "implement");
}

int
bionic_pthread_cond_timedwait_relative_np(bionic_cond_t *cond, bionic_mutex_t *mutex, const struct timespec *reltime)
{
   assert(0 && "implement");
   return 0;
}

int
bionic_pthread_cond_timedwait_monotonic_np(bionic_cond_t *cond, bionic_mutex_t *mutex, const struct timespec *abstime)
{
   assert(0 && "implement");
   return 0;
}

int
bionic_sem_destroy(bionic_sem_t *sem)
{
   assert(sem);
   int ret = 0;
   if (IS_MAPPED(sem)) {
      ret = sem_destroy(sem->glibc);
      munmap(sem->glibc, sizeof(sem_t));
   }
   return ret;
}

int
bionic_sem_init(bionic_sem_t *sem, int pshared, unsigned int value)
{
   assert(sem);
   // From SEM_INIT(3)
   // Initializing a semaphore that has already been initialized results in underined behavior.
   *sem = (bionic_sem_t){0};
   sem->glibc = mmap(NULLsizeof(sem_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -10);
   return sem_init(sem->glibc, pshared, value);
}

int
bionic_sem_post(bionic_sem_t *sem)
{
   assert(sem && IS_MAPPED(sem));
   return sem_post(sem->glibc);
}

int
bionic_sem_wait(bionic_sem_t *sem)
{
   assert(sem && IS_MAPPED(sem));
   return sem_wait(sem->glibc);
}

int
bionic_sem_trywait(bionic_sem_t *sem)
{
   assert(sem && IS_MAPPED(sem));
   return sem_trywait(sem->glibc);
}

int
bionic_sem_timedwait(bionic_sem_t *sem, const struct timespec *abs_timeout)
{
   assert(sem && IS_MAPPED(sem) && abs_timeout);
   return sem_timedwait(sem->glibc, abs_timeout);
}

int
bionic_pthread_attr_destroy(bionic_attr_t *attr)
{
   assert(attr);
   int ret = 0;
   if (IS_MAPPED(attr)) {
      ret = pthread_attr_destroy(attr->glibc);
      munmap(attr->glibc, sizeof(pthread_attr_t));
   }
   return ret;
}

int
bionic_pthread_attr_init(bionic_attr_t *attr)
{
   assert(attr);
   // From PTHREAD_ATTR_INIT(3)
   // Calling `pthread_attr_init` on a thread attributes object that has already been initialized results in ud.
   *attr = (bionic_attr_t){0};
   attr->glibc = mmap(NULLsizeof(pthread_attr_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -10);
   return pthread_attr_init(attr->glibc);
}

int
bionic_pthread_getattr_np(bionic_pthread_t thread, bionic_attr_t *attr)
{
   assert(thread && attr);
   *attr = (bionic_attr_t){0};
   attr->glibc = mmap(NULLsizeof(pthread_attr_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -10);
   return pthread_getattr_np((pthread_t)thread, attr->glibc);
}

int
bionic_pthread_attr_settstack(bionic_attr_t *attr, void *stackaddr, size_t stacksize)
{
   assert(attr && IS_MAPPED(attr));
   return pthread_attr_setstack(attr->glibc, stackaddr, stacksize);
}

int
bionic_pthread_attr_getstack(const bionic_attr_t *attr, void *stackaddr, size_t *stacksize)
{
   assert(attr && IS_MAPPED(attr));
   return pthread_attr_getstack(attr->glibc, stackaddr, stacksize);
}

int
bionic_pthread_attr_setstacksize(bionic_attr_t *attr, size_t stacksize)
{
   assert(attr && IS_MAPPED(attr));
   return pthread_attr_setstacksize(attr->glibc, stacksize);
}

int
bionic_pthread_attr_getstacksize(const bionic_attr_t *attr, size_t *stacksize)
{
   assert(attr && IS_MAPPED(attr));
   return pthread_attr_getstacksize(attr->glibc, stacksize);
}

int
bionic_pthread_attr_setschedpolicy(bionic_attr_t *attr, int policy)
{
   assert(attr && IS_MAPPED(attr));
   return pthread_attr_setschedpolicy(attr->glibc, policy);
}

int
bionic_pthread_attr_getschedpolicy(bionic_attr_t *attr, int *policy)
{
   assert(attr && IS_MAPPED(attr));
   return pthread_attr_getschedpolicy(attr->glibc, policy);
}

int
bionic_pthread_attr_setschedparam(bionic_attr_t *attr, const struct sched_param *param)
{
   assert(attr && IS_MAPPED(attr));
   return pthread_attr_setschedparam(attr->glibc, param);
}

int
bionic_pthread_attr_getschedparam(bionic_attr_t *attr, struct sched_param *param)
{
   assert(attr && IS_MAPPED(attr));
   return pthread_attr_getschedparam(attr->glibc, param);
}

int
bionic_pthread_attr_setdetachstate(bionic_attr_t *attr, int detachstate)
{
   assert(attr && IS_MAPPED(attr));
   return pthread_attr_setdetachstate(attr->glibc, detachstate);
}

int
bionic_pthread_attr_getdetachstate(bionic_attr_t *attr, int *detachstate)
{
   assert(attr && IS_MAPPED(attr));
   return pthread_attr_getdetachstate(attr->glibc, detachstate);
}

int
bionic_pthread_create(bionic_pthread_t *thread, const bionic_attr_t *attr, void* (*start)(void*), void *arg)
{
   assert(thread && (!attr || IS_MAPPED(attr)));
   return pthread_create((pthread_t*)thread, (attr ? attr->glibc : NULL), start, arg);
}

int
bionic_pthread_mutexattr_settype(bionic_mutexattr_t *attr, int type)
{
   assert(attr && IS_MAPPED(attr));
   return pthread_mutexattr_settype(attr->glibc, type);
}

int
bionic_pthread_mutexattr_destroy(bionic_mutexattr_t *attr)
{
   assert(attr);
   int ret = 0;
   if (IS_MAPPED(attr)) {
      ret = pthread_mutexattr_destroy(attr->glibc);
      munmap(attr->glibc, sizeof(pthread_mutexattr_t));
   }
   return ret;
}

int
bionic_pthread_mutexattr_init(bionic_mutexattr_t *attr)
{
   assert(attr);
   // From PTHREAD_MUTEXATTR_INIT(3)
   // The results of initializing an already initialized mutex attributes object are undefined.
   *attr = (bionic_mutexattr_t){0};
   attr->glibc = mmap(NULLsizeof(pthread_mutexattr_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -10);
   return pthread_mutexattr_init(attr->glibc);
}

static void
default_pthread_mutex_init(bionic_mutex_t *mutex)
{
   assert(mutex);
   mutex->glibc = mmap(NULLsizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -10);

   for (size_t i = 0; i < ARRAY_SIZE(bionic_mutex_init_map); ++i) {
      if (!memcmp(&bionic_mutex_init_map[i].bionic, mutex, sizeof(*mutex)))
         continue;

      memcpy(mutex->glibc, &bionic_mutex_init_map[i].glibc, sizeof(bionic_mutex_init_map[i].glibc));
      return;
   }

   assert(0 && "no such default initializer???");
}

int
bionic_pthread_mutex_destroy(bionic_mutex_t *mutex)
{
   assert(mutex);
   int ret = 0;
   if (IS_MAPPED(mutex)) {
      ret = pthread_mutex_destroy(mutex->glibc);
      munmap(mutex->glibc, sizeof(pthread_mutex_t));
   }
   return ret;
}

int
bionic_pthread_mutex_init(bionic_mutex_t *mutex, const bionic_mutexattr_t *attr)
{
   assert(mutex && (!attr || IS_MAPPED(attr)));
   // From PTHREAD_MUTEX_INIT(3)
   // Attempting to initialize an already initialized mutex result in undefined behavior.
   *mutex = (bionic_mutex_t){0};
   mutex->glibc = mmap(NULLsizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -10);
   return pthread_mutex_init(mutex->glibc, (attr ? attr->glibc : NULL));
}

int
bionic_pthread_mutex_lock(bionic_mutex_t *mutex)
{
   assert(mutex);
   INIT_IF_NOT_MAPPED(mutex, default_pthread_mutex_init);
   return pthread_mutex_lock(mutex->glibc);
}

int
bionic_pthread_mutex_trylock(bionic_mutex_t *mutex)
{
   assert(mutex);
   INIT_IF_NOT_MAPPED(mutex, default_pthread_mutex_init);
   return pthread_mutex_trylock(mutex->glibc);
}

int
bionic_pthread_mutex_unlock(bionic_mutex_t *mutex)
{
   assert(mutex);
   INIT_IF_NOT_MAPPED(mutex, default_pthread_mutex_init);
   return pthread_mutex_unlock(mutex->glibc);
}

static void
default_pthread_cond_init(bionic_cond_t *cond)
{
   assert(cond);
   cond->glibc = mmap(NULLsizeof(pthread_cond_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -10);
   memset(cond->glibc, 0sizeof(pthread_cond_t));
}

int
bionic_pthread_cond_destroy(bionic_cond_t *cond)
{
   assert(cond);
   int ret = 0;
   if (IS_MAPPED(cond)) {
      ret = pthread_cond_destroy(cond->glibc);
      munmap(cond->glibc, sizeof(pthread_cond_t));
   }
   return ret;
}

int
bionic_pthread_cond_init(bionic_cond_t *cond, const bionic_condattr_t *attr)
{
   assert(cond && (!attr || IS_MAPPED(attr)));
   // From PTHREAD_COND_INIT(3)
   // Attempting to initialize an already initialized mutex result in undefined behavior.
   *cond = (bionic_cond_t){0};
   cond->glibc = mmap(NULLsizeof(pthread_cond_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -10);
   return pthread_cond_init(cond->glibc, (attr ? attr->glibc : NULL));
}

int
bionic_pthread_cond_broadcast(bionic_cond_t *cond)
{
   assert(cond);
   INIT_IF_NOT_MAPPED(cond, default_pthread_cond_init);
   return pthread_cond_broadcast(cond->glibc);
}

int
bionic_pthread_cond_signal(bionic_cond_t *cond)
{
   assert(cond);
   INIT_IF_NOT_MAPPED(cond, default_pthread_cond_init);
   return pthread_cond_signal(cond->glibc);
}

int
bionic_pthread_cond_wait(bionic_cond_t *cond, bionic_mutex_t *mutex)
{
   assert(cond && mutex);
   INIT_IF_NOT_MAPPED(cond, default_pthread_cond_init);
   INIT_IF_NOT_MAPPED(mutex, default_pthread_mutex_init);
   return pthread_cond_wait(cond->glibc, mutex->glibc);
}

int
bionic_pthread_cond_timedwait(bionic_cond_t *cond, bionic_mutex_t *mutex, const struct timespec *abs_timeout)
{
   assert(cond && mutex);
   INIT_IF_NOT_MAPPED(cond, default_pthread_cond_init);
   INIT_IF_NOT_MAPPED(mutex, default_pthread_mutex_init);
   return pthread_cond_timedwait(cond->glibc, mutex->glibc, abs_timeout);
}