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
| | #ifndef _THREADS_H
#define _THREADS_H
/* This one is explicitly allowed to be included. */
#include <time.h>
#ifdef __cplusplus
extern "C" {
#endif
#define __NEED_cnd_t
#define __NEED_mtx_t
/* Until we come up with a better naming scheme, we need to expose
some pthread types. */
#define __NEED_pthread_cond_t
#define __NEED_pthread_mutex_t
#define __NEED_pthread_once_t
#define __NEED_pthread_t
#define __NEED_pthread_key_t
#include <bits/alltypes.h>
#include <bits/errno.h>
typedef pthread_once_t once_flag;
typedef pthread_t thrd_t;
typedef pthread_key_t tss_t;
typedef int (*thrd_start_t)(void*);
typedef void (*tss_dtor_t)(void*);
#ifndef __THRD_EXPERIMENTAL
# define __THRD_EXPERIMENTAL 1
#endif
/* The following list of 9 integer constants makes up for the binary
compatibility of this C thread implementation. You must never
link code against versions of the C library that do not agree
upon these ABI parameters.
Additionally this implementation assumes that the 5 types have
the same size across C libraries and that these types can be
initialized by the default initializer.
The values for the 9 parameters are not fixed for now. Depending
on the choices of other implementations and the evolution of the
C standard they may still change. This should happen rarely, but
you have to consider the C thread feature to be experimental
until then, and be prepared to recompile your binary when linking
against a different implementation or a new version.
The macro __THRD_EXPERIMENTAL will be defined as long as we
consider this ABI to be unstable. This comes with some link time
checks and an overhead of some bytes. At your own risk you may
switch this check off by defining __THRD_EXPERIMENTAL macro to
0. */
#define __THRD_SUCCESS 0
#define __THRD_BUSY EBUSY
#define __THRD_ERROR EINVAL
#define __THRD_NOMEM ENOMEM
#define __THRD_TIMEDOUT ETIMEDOUT
#define __MTX_PLAIN 0
#define __MTX_RECURSIVE 1
#define __MTX_TIMED 2
#define TSS_DTOR_ITERATIONS 4
#define __THRD_CONCAT10_(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) \
_0 ## _ ## _1 ## _ ## _2 ## _ ## _3 ## _ ## _4 ## _ ## _5 ## _ ## _6 ## _ ## _7 ## _ ## _8 ## _ ## _9
#define __THRD_CONCAT10(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) \
__THRD_CONCAT10_(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9)
#define __THRD_ABI \
__THRD_CONCAT10(__thrd_abi, \
__THRD_SUCCESS, \
__THRD_BUSY, \
__THRD_ERROR, \
__THRD_NOMEM, \
__THRD_TIMEDOUT, \
__MTX_PLAIN, \
__MTX_RECURSIVE, \
__MTX_TIMED, \
TSS_DTOR_ITERATIONS \
)
#define __THRD_SHFT(X, Y) (((X) << 3) ^ (Y))
enum {
__thrd_abi =
__THRD_SHFT(sizeof(cnd_t),
__THRD_SHFT(sizeof(mtx_t),
__THRD_SHFT(sizeof(once_flag),
__THRD_SHFT(sizeof(thrd_t),
sizeof(tss_t))))),
};
extern unsigned const __THRD_ABI;
#define __THRD_ABI_CHECK (1/(__THRD_ABI == __thrd_abi))
#if __THRD_EXPERIMENTAL
# define __THRD_ABI_MARK __attribute__((used)) static unsigned const*const __thrd_abi_mark = &__THRD_ABI
#else
# define __THRD_ABI_MARK typedef unsigned __thrd_abi_dummy_type
#endif
enum {
thrd_success = __THRD_SUCCESS,
thrd_busy = __THRD_BUSY,
thrd_error = __THRD_ERROR,
thrd_nomem = __THRD_NOMEM,
thrd_timedout = __THRD_TIMEDOUT,
};
enum {
mtx_plain = __MTX_PLAIN,
mtx_recursive = __MTX_RECURSIVE,
// all mutexes are timed, here. so this is a no-op
mtx_timed = __MTX_TIMED,
};
#define ONCE_FLAG_INIT { 0 }
#define thread_local _Thread_local
int thrd_create(thrd_t *, thrd_start_t, void *);
_Noreturn void thrd_exit(int);
int thrd_detach(thrd_t);
int thrd_join(thrd_t, int *);
int thrd_sleep(const struct timespec *, struct timespec *);
void thrd_yield(void);
thrd_t thrd_current(void);
int thrd_equal(thrd_t, thrd_t);
#define thrd_equal(A, B) ((A) == (B))
void call_once(once_flag *, void (*)(void));
int mtx_init(mtx_t *, int);
void mtx_destroy(mtx_t *);
int mtx_lock(mtx_t *);
int mtx_timedlock(mtx_t *restrict, const struct timespec *restrict);
int mtx_trylock(mtx_t *);
int mtx_unlock(mtx_t *);
int cnd_init(cnd_t *);
void cnd_destroy(cnd_t *);
int cnd_broadcast(cnd_t *);
int cnd_signal(cnd_t *);
int cnd_timedwait(cnd_t *restrict, mtx_t *restrict, const struct timespec *restrict);
int cnd_wait(cnd_t *, mtx_t *);
int tss_create(tss_t *, tss_dtor_t);
void tss_delete(tss_t key);
int tss_set(tss_t, void *);
void *tss_get(tss_t);
#ifdef __cplusplus
}
#endif
#endif
|