2 * C11 <threads.h> emulation library
4 * (C) Copyright yohhoy 2012.
5 * Distributed under the Boost Software License, Version 1.0.
7 * Permission is hereby granted, free of charge, to any person or organization
8 * obtaining a copy of the software and accompanying documentation covered by
9 * this license (the "Software") to use, reproduce, display, distribute,
10 * execute, and transmit the Software, and to prepare [[derivative work]]s of the
11 * Software, and to permit third-parties to whom the Software is furnished to
12 * do so, all subject to the following:
14 * The copyright notices in the Software and this entire statement, including
15 * the above license grant, this restriction and the following disclaimer,
16 * must be included in all copies of the Software, in whole or in part, and
17 * all derivative works of the Software, unless such copies or derivative
18 * works are solely in the form of machine-executable object code generated by
19 * a source language processor.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
37 #include <stdint.h> /* for intptr_t */
42 EMULATED_THREADS_USE_NATIVE_TIMEDLOCK
43 Use pthread_mutex_timedlock() for `mtx_timedlock()'
44 Otherwise use mtx_trylock() + *busy loop* emulation.
46 #if !defined(__CYGWIN__) && !defined(__APPLE__) && !defined(__NetBSD__)
47 #define EMULATED_THREADS_USE_NATIVE_TIMEDLOCK
53 /*---------------------------- macros ----------------------------*/
54 #define ONCE_FLAG_INIT PTHREAD_ONCE_INIT
55 #ifdef INIT_ONCE_STATIC_INIT
56 #define TSS_DTOR_ITERATIONS PTHREAD_DESTRUCTOR_ITERATIONS
58 #define TSS_DTOR_ITERATIONS 1 // assume TSS dtor MAY be called at least once.
61 // FIXME: temporary non-standard hack to ease transition
62 #define _MTX_INITIALIZER_NP PTHREAD_MUTEX_INITIALIZER
64 /*---------------------------- types ----------------------------*/
65 typedef pthread_cond_t cnd_t
;
66 typedef pthread_t thrd_t
;
67 typedef pthread_key_t tss_t
;
68 typedef pthread_mutex_t mtx_t
;
69 typedef pthread_once_t once_flag
;
73 Implementation limits:
74 - Conditionally emulation for "mutex with timeout"
75 (see EMULATED_THREADS_USE_NATIVE_TIMEDLOCK macro)
77 struct impl_thrd_param
{
83 impl_thrd_routine(void *p
)
85 struct impl_thrd_param pack
= *((struct impl_thrd_param
*)p
);
87 return (void*)(intptr_t)pack
.func(pack
.arg
);
91 /*--------------- 7.25.2 Initialization functions ---------------*/
94 call_once(once_flag
*flag
, void (*func
)(void))
96 pthread_once(flag
, func
);
100 /*------------- 7.25.3 Condition variable functions -------------*/
103 cnd_broadcast(cnd_t
*cond
)
105 assert(cond
!= NULL
);
106 return (pthread_cond_broadcast(cond
) == 0) ? thrd_success
: thrd_error
;
111 cnd_destroy(cnd_t
*cond
)
114 pthread_cond_destroy(cond
);
119 cnd_init(cnd_t
*cond
)
121 assert(cond
!= NULL
);
122 return (pthread_cond_init(cond
, NULL
) == 0) ? thrd_success
: thrd_error
;
127 cnd_signal(cnd_t
*cond
)
129 assert(cond
!= NULL
);
130 return (pthread_cond_signal(cond
) == 0) ? thrd_success
: thrd_error
;
135 cnd_timedwait(cnd_t
*cond
, mtx_t
*mtx
, const xtime
*xt
)
137 struct timespec abs_time
;
141 assert(cond
!= NULL
);
144 abs_time
.tv_sec
= xt
->sec
;
145 abs_time
.tv_nsec
= xt
->nsec
;
147 rt
= pthread_cond_timedwait(cond
, mtx
, &abs_time
);
150 return (rt
== 0) ? thrd_success
: thrd_error
;
155 cnd_wait(cnd_t
*cond
, mtx_t
*mtx
)
158 assert(cond
!= NULL
);
159 return (pthread_cond_wait(cond
, mtx
) == 0) ? thrd_success
: thrd_error
;
163 /*-------------------- 7.25.4 Mutex functions --------------------*/
166 mtx_destroy(mtx_t
*mtx
)
169 pthread_mutex_destroy(mtx
);
173 * XXX: Workaround when building with -O0 and without pthreads link.
175 * In such cases constant folding and dead code elimination won't be
176 * available, thus the compiler will always add the pthread_mutexattr*
177 * functions into the binary. As we try to link, we'll fail as the
178 * symbols are unresolved.
180 * Ideally we'll enable the optimisations locally, yet that does not
183 * So the alternative workaround is to annotate the symbols as weak.
184 * Thus the linker will be happy and things don't clash when building
185 * with -O1 or greater.
187 #if defined(HAVE_FUNC_ATTRIBUTE_WEAK) && !defined(__CYGWIN__)
188 __attribute__((weak
))
189 int pthread_mutexattr_init(pthread_mutexattr_t
*attr
);
191 __attribute__((weak
))
192 int pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int type
);
194 __attribute__((weak
))
195 int pthread_mutexattr_destroy(pthread_mutexattr_t
*attr
);
200 mtx_init(mtx_t
*mtx
, int type
)
202 pthread_mutexattr_t attr
;
204 if (type
!= mtx_plain
&& type
!= mtx_timed
&& type
!= mtx_try
205 && type
!= (mtx_plain
|mtx_recursive
)
206 && type
!= (mtx_timed
|mtx_recursive
)
207 && type
!= (mtx_try
|mtx_recursive
))
210 if ((type
& mtx_recursive
) == 0) {
211 pthread_mutex_init(mtx
, NULL
);
215 pthread_mutexattr_init(&attr
);
216 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
217 pthread_mutex_init(mtx
, &attr
);
218 pthread_mutexattr_destroy(&attr
);
227 return (pthread_mutex_lock(mtx
) == 0) ? thrd_success
: thrd_error
;
231 mtx_trylock(mtx_t
*mtx
);
238 mtx_timedlock(mtx_t
*mtx
, const xtime
*xt
)
244 #ifdef EMULATED_THREADS_USE_NATIVE_TIMEDLOCK
248 ts
.tv_nsec
= xt
->nsec
;
249 rt
= pthread_mutex_timedlock(mtx
, &ts
);
252 return (rt
== ETIMEDOUT
) ? thrd_busy
: thrd_error
;
254 time_t expire
= time(NULL
);
256 while (mtx_trylock(mtx
) != thrd_success
) {
257 time_t now
= time(NULL
);
270 mtx_trylock(mtx_t
*mtx
)
273 return (pthread_mutex_trylock(mtx
) == 0) ? thrd_success
: thrd_busy
;
278 mtx_unlock(mtx_t
*mtx
)
281 return (pthread_mutex_unlock(mtx
) == 0) ? thrd_success
: thrd_error
;
285 /*------------------- 7.25.5 Thread functions -------------------*/
288 thrd_create(thrd_t
*thr
, thrd_start_t func
, void *arg
)
290 struct impl_thrd_param
*pack
;
292 pack
= (struct impl_thrd_param
*)malloc(sizeof(struct impl_thrd_param
));
293 if (!pack
) return thrd_nomem
;
296 if (pthread_create(thr
, NULL
, impl_thrd_routine
, pack
) != 0) {
307 return pthread_self();
312 thrd_detach(thrd_t thr
)
314 return (pthread_detach(thr
) == 0) ? thrd_success
: thrd_error
;
319 thrd_equal(thrd_t thr0
, thrd_t thr1
)
321 return pthread_equal(thr0
, thr1
);
328 pthread_exit((void*)(intptr_t)res
);
333 thrd_join(thrd_t thr
, int *res
)
336 if (pthread_join(thr
, &code
) != 0)
339 *res
= (int)(intptr_t)code
;
345 thrd_sleep(const xtime
*xt
)
349 req
.tv_sec
= xt
->sec
;
350 req
.tv_nsec
= xt
->nsec
;
351 nanosleep(&req
, NULL
);
362 /*----------- 7.25.6 Thread-specific storage functions -----------*/
365 tss_create(tss_t
*key
, tss_dtor_t dtor
)
368 return (pthread_key_create(key
, dtor
) == 0) ? thrd_success
: thrd_error
;
373 tss_delete(tss_t key
)
375 pthread_key_delete(key
);
382 return pthread_getspecific(key
);
387 tss_set(tss_t key
, void *val
)
389 return (pthread_setspecific(key
, val
) == 0) ? thrd_success
: thrd_error
;
393 /*-------------------- 7.25.7 Time functions --------------------*/
396 xtime_get(xtime
*xt
, int base
)
399 if (base
== TIME_UTC
) {
400 xt
->sec
= time(NULL
);