1 #pragma once
2 
3 #include <features.h>
4 #include <time.h>
5 
6 #ifdef __cplusplus
7 extern "C" {
8 typedef unsigned long thrd_t;
9 #else
10 typedef struct __pthread* thrd_t;
11 #define thread_local _Thread_local
12 #endif
13 
14 typedef unsigned tss_t;
15 typedef int (*thrd_start_t)(void*);
16 typedef void (*tss_dtor_t)(void*);
17 
18 #define __NEED_cnd_t
19 #define __NEED_mtx_t
20 #define __NEED_once_flag
21 
22 #include <bits/alltypes.h>
23 
24 #define TSS_DTOR_ITERATIONS 4
25 
26 enum {
27     thrd_success = 0,
28     thrd_busy = 1,
29     thrd_error = 2,
30     thrd_nomem = 3,
31     thrd_timedout = 4,
32 };
33 
34 // These are bitfield values; initialize with e.g. (mtx_plain|mtx_timed).
35 // mtx_recursive is not implemented.
36 enum {
37     mtx_plain = 0,
38     mtx_recursive = 1,
39     mtx_timed = 2,
40 };
41 
42 #ifdef _ALL_SOURCE
43 #define MTX_INIT \
44     {}
45 #define CND_INIT \
46     {}
47 #endif
48 
49 #define ONCE_FLAG_INIT 0
50 
51 int thrd_create(thrd_t*, thrd_start_t, void*);
52 #ifdef _ALL_SOURCE
53 // |name| is silently truncated to a maximum of ZX_MAX_NAME_LEN-1 characters.
54 int thrd_create_with_name(thrd_t*, thrd_start_t, void*, const char* name);
55 #endif
56 _Noreturn void thrd_exit(int);
57 
58 int thrd_detach(thrd_t);
59 int thrd_join(thrd_t, int*);
60 
61 int thrd_sleep(const struct timespec*, struct timespec*);
62 void thrd_yield(void);
63 
64 thrd_t thrd_current(void);
65 int thrd_equal(thrd_t, thrd_t);
66 #ifndef __cplusplus
67 #define thrd_equal(A, B) ((A) == (B))
68 #endif
69 
70 void call_once(once_flag*, void (*)(void));
71 
72 int mtx_init(mtx_t*, int);
73 void mtx_destroy(mtx_t*);
74 
75 int mtx_lock(mtx_t* __m)
76 #ifdef __clang__
77     __attribute__((__acquire_capability__(__m)))
78 #endif
79 ;
80 int mtx_timedlock(mtx_t* __restrict, const struct timespec* __restrict);
81 int mtx_trylock(mtx_t*);
82 int mtx_unlock(mtx_t* __m)
83 #ifdef __clang__
84     __attribute__((__release_capability__(__m)))
85 #endif
86 ;
87 
88 int cnd_init(cnd_t*);
89 void cnd_destroy(cnd_t*);
90 
91 int cnd_broadcast(cnd_t*);
92 int cnd_signal(cnd_t*);
93 
94 int cnd_timedwait(cnd_t* __restrict, mtx_t* __restrict, const struct timespec* __restrict);
95 int cnd_wait(cnd_t*, mtx_t*);
96 
97 int tss_create(tss_t*, tss_dtor_t);
98 void tss_delete(tss_t key);
99 
100 int tss_set(tss_t, void*);
101 void* tss_get(tss_t);
102 
103 #ifdef __cplusplus
104 }
105 #endif
106