1 /*
2 * SPDX-License-Identifier: BSL-1.0
3 * Copyright yohhoy 2012.
4 */
5 #include <stdlib.h>
6 #include <assert.h>
7 #include <limits.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <sched.h>
11 #include <stdint.h> /* for intptr_t */
12
13 #include "c11/threads.h"
14
15 /*
16 Configuration macro:
17
18 EMULATED_THREADS_USE_NATIVE_TIMEDLOCK
19 Use pthread_mutex_timedlock() for `mtx_timedlock()'
20 Otherwise use mtx_trylock() + *busy loop* emulation.
21 */
22 #if !defined(__CYGWIN__) && !defined(__APPLE__) && !defined(__NetBSD__)
23 #define EMULATED_THREADS_USE_NATIVE_TIMEDLOCK
24 #endif
25
26 /*---------------------------- types ----------------------------*/
27
28 /*
29 Implementation limits:
30 - Conditionally emulation for "mutex with timeout"
31 (see EMULATED_THREADS_USE_NATIVE_TIMEDLOCK macro)
32 */
33 struct impl_thrd_param {
34 thrd_start_t func;
35 void *arg;
36 };
37
38 static void *
impl_thrd_routine(void * p)39 impl_thrd_routine(void *p)
40 {
41 struct impl_thrd_param pack = *((struct impl_thrd_param *)p);
42 free(p);
43 return (void*)(intptr_t)pack.func(pack.arg);
44 }
45
46
47 /*--------------- 7.25.2 Initialization functions ---------------*/
48 // 7.25.2.1
49 void
call_once(once_flag * flag,void (* func)(void))50 call_once(once_flag *flag, void (*func)(void))
51 {
52 pthread_once(flag, func);
53 }
54
55
56 /*------------- 7.25.3 Condition variable functions -------------*/
57 // 7.25.3.1
58 int
cnd_broadcast(cnd_t * cond)59 cnd_broadcast(cnd_t *cond)
60 {
61 assert(cond != NULL);
62 return (pthread_cond_broadcast(cond) == 0) ? thrd_success : thrd_error;
63 }
64
65 // 7.25.3.2
66 void
cnd_destroy(cnd_t * cond)67 cnd_destroy(cnd_t *cond)
68 {
69 assert(cond);
70 pthread_cond_destroy(cond);
71 }
72
73 // 7.25.3.3
74 int
cnd_init(cnd_t * cond)75 cnd_init(cnd_t *cond)
76 {
77 assert(cond != NULL);
78 return (pthread_cond_init(cond, NULL) == 0) ? thrd_success : thrd_error;
79 }
80
81 // 7.25.3.4
82 int
cnd_signal(cnd_t * cond)83 cnd_signal(cnd_t *cond)
84 {
85 assert(cond != NULL);
86 return (pthread_cond_signal(cond) == 0) ? thrd_success : thrd_error;
87 }
88
89 // 7.25.3.5
90 int
cnd_timedwait(cnd_t * cond,mtx_t * mtx,const struct timespec * abs_time)91 cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *abs_time)
92 {
93 int rt;
94
95 assert(mtx != NULL);
96 assert(cond != NULL);
97 assert(abs_time != NULL);
98
99 rt = pthread_cond_timedwait(cond, mtx, abs_time);
100 if (rt == ETIMEDOUT)
101 return thrd_timedout;
102 return (rt == 0) ? thrd_success : thrd_error;
103 }
104
105 // 7.25.3.6
106 int
cnd_wait(cnd_t * cond,mtx_t * mtx)107 cnd_wait(cnd_t *cond, mtx_t *mtx)
108 {
109 assert(mtx != NULL);
110 assert(cond != NULL);
111 return (pthread_cond_wait(cond, mtx) == 0) ? thrd_success : thrd_error;
112 }
113
114
115 /*-------------------- 7.25.4 Mutex functions --------------------*/
116 // 7.25.4.1
117 void
mtx_destroy(mtx_t * mtx)118 mtx_destroy(mtx_t *mtx)
119 {
120 assert(mtx != NULL);
121 pthread_mutex_destroy(mtx);
122 }
123
124 /*
125 * XXX: Workaround when building with -O0 and without pthreads link.
126 *
127 * In such cases constant folding and dead code elimination won't be
128 * available, thus the compiler will always add the pthread_mutexattr*
129 * functions into the binary. As we try to link, we'll fail as the
130 * symbols are unresolved.
131 *
132 * Ideally we'll enable the optimisations locally, yet that does not
133 * seem to work.
134 *
135 * So the alternative workaround is to annotate the symbols as weak.
136 * Thus the linker will be happy and things don't clash when building
137 * with -O1 or greater.
138 */
139 #if defined(HAVE_FUNC_ATTRIBUTE_WEAK) && !defined(__CYGWIN__)
140 __attribute__((weak))
141 int pthread_mutexattr_init(pthread_mutexattr_t *attr);
142
143 __attribute__((weak))
144 int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
145
146 __attribute__((weak))
147 int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
148 #endif
149
150 // 7.25.4.2
151 int
mtx_init(mtx_t * mtx,int type)152 mtx_init(mtx_t *mtx, int type)
153 {
154 pthread_mutexattr_t attr;
155 assert(mtx != NULL);
156 if (type != mtx_plain && type != mtx_timed
157 && type != (mtx_plain|mtx_recursive)
158 && type != (mtx_timed|mtx_recursive))
159 return thrd_error;
160
161 if ((type & mtx_recursive) == 0) {
162 pthread_mutex_init(mtx, NULL);
163 return thrd_success;
164 }
165
166 pthread_mutexattr_init(&attr);
167 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
168 pthread_mutex_init(mtx, &attr);
169 pthread_mutexattr_destroy(&attr);
170 return thrd_success;
171 }
172
173 // 7.25.4.3
174 int
mtx_lock(mtx_t * mtx)175 mtx_lock(mtx_t *mtx)
176 {
177 assert(mtx != NULL);
178 return (pthread_mutex_lock(mtx) == 0) ? thrd_success : thrd_error;
179 }
180
181 static int
threads_timespec_compare(const struct timespec * a,const struct timespec * b)182 threads_timespec_compare(const struct timespec *a, const struct timespec *b)
183 {
184 if (a->tv_sec < b->tv_sec) {
185 return -1;
186 } else if (a->tv_sec > b->tv_sec) {
187 return 1;
188 } else if (a->tv_nsec < b->tv_nsec) {
189 return -1;
190 } else if (a->tv_nsec > b->tv_nsec) {
191 return 1;
192 }
193 return 0;
194 }
195
196 // 7.25.4.4
197 int
mtx_timedlock(mtx_t * mtx,const struct timespec * ts)198 mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
199 {
200 assert(mtx != NULL);
201 assert(ts != NULL);
202
203 {
204 #ifdef EMULATED_THREADS_USE_NATIVE_TIMEDLOCK
205 int rt;
206 rt = pthread_mutex_timedlock(mtx, ts);
207 if (rt == 0)
208 return thrd_success;
209 return (rt == ETIMEDOUT) ? thrd_timedout : thrd_error;
210 #else
211 while (mtx_trylock(mtx) != thrd_success) {
212 struct timespec now;
213 if (timespec_get(&now, TIME_UTC) != TIME_UTC) {
214 return thrd_error;
215 }
216 if (threads_timespec_compare(ts, &now) < 0)
217 return thrd_timedout;
218 // busy loop!
219 thrd_yield();
220 }
221 return thrd_success;
222 #endif
223 }
224 }
225
226 // 7.25.4.5
227 int
mtx_trylock(mtx_t * mtx)228 mtx_trylock(mtx_t *mtx)
229 {
230 assert(mtx != NULL);
231 return (pthread_mutex_trylock(mtx) == 0) ? thrd_success : thrd_busy;
232 }
233
234 // 7.25.4.6
235 int
mtx_unlock(mtx_t * mtx)236 mtx_unlock(mtx_t *mtx)
237 {
238 assert(mtx != NULL);
239 return (pthread_mutex_unlock(mtx) == 0) ? thrd_success : thrd_error;
240 }
241
242
243 /*------------------- 7.25.5 Thread functions -------------------*/
244 // 7.25.5.1
245 int
thrd_create(thrd_t * thr,thrd_start_t func,void * arg)246 thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
247 {
248 struct impl_thrd_param *pack;
249 assert(thr != NULL);
250 pack = (struct impl_thrd_param *)malloc(sizeof(struct impl_thrd_param));
251 if (!pack) return thrd_nomem;
252 pack->func = func;
253 pack->arg = arg;
254 if (pthread_create(thr, NULL, impl_thrd_routine, pack) != 0) {
255 free(pack);
256 return thrd_error;
257 }
258 return thrd_success;
259 }
260
261 // 7.25.5.2
262 thrd_t
thrd_current(void)263 thrd_current(void)
264 {
265 return pthread_self();
266 }
267
268 // 7.25.5.3
269 int
thrd_detach(thrd_t thr)270 thrd_detach(thrd_t thr)
271 {
272 return (pthread_detach(thr) == 0) ? thrd_success : thrd_error;
273 }
274
275 // 7.25.5.4
276 int
thrd_equal(thrd_t thr0,thrd_t thr1)277 thrd_equal(thrd_t thr0, thrd_t thr1)
278 {
279 return pthread_equal(thr0, thr1);
280 }
281
282 // 7.25.5.5
283 _Noreturn
284 void
thrd_exit(int res)285 thrd_exit(int res)
286 {
287 pthread_exit((void*)(intptr_t)res);
288 }
289
290 // 7.25.5.6
291 int
thrd_join(thrd_t thr,int * res)292 thrd_join(thrd_t thr, int *res)
293 {
294 void *code;
295 if (pthread_join(thr, &code) != 0)
296 return thrd_error;
297 if (res)
298 *res = (int)(intptr_t)code;
299 return thrd_success;
300 }
301
302 // 7.25.5.7
303 int
thrd_sleep(const struct timespec * time_point,struct timespec * remaining)304 thrd_sleep(const struct timespec *time_point, struct timespec *remaining)
305 {
306 assert(time_point != NULL);
307 return nanosleep(time_point, remaining);
308 }
309
310 // 7.25.5.8
311 void
thrd_yield(void)312 thrd_yield(void)
313 {
314 sched_yield();
315 }
316
317
318 /*----------- 7.25.6 Thread-specific storage functions -----------*/
319 // 7.25.6.1
320 int
tss_create(tss_t * key,tss_dtor_t dtor)321 tss_create(tss_t *key, tss_dtor_t dtor)
322 {
323 assert(key != NULL);
324 return (pthread_key_create(key, dtor) == 0) ? thrd_success : thrd_error;
325 }
326
327 // 7.25.6.2
328 void
tss_delete(tss_t key)329 tss_delete(tss_t key)
330 {
331 pthread_key_delete(key);
332 }
333
334 // 7.25.6.3
335 void *
tss_get(tss_t key)336 tss_get(tss_t key)
337 {
338 return pthread_getspecific(key);
339 }
340
341 // 7.25.6.4
342 int
tss_set(tss_t key,void * val)343 tss_set(tss_t key, void *val)
344 {
345 return (pthread_setspecific(key, val) == 0) ? thrd_success : thrd_error;
346 }
347