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.
35 #include <stdint.h> /* for intptr_t */
40 EMULATED_THREADS_USE_NATIVE_TIMEDLOCK
41 Use pthread_mutex_timedlock() for `mtx_timedlock()'
42 Otherwise use mtx_trylock() + *busy loop* emulation.
44 #if !defined(__CYGWIN__)
45 #define EMULATED_THREADS_USE_NATIVE_TIMEDLOCK
51 /*---------------------------- macros ----------------------------*/
52 #define ONCE_FLAG_INIT PTHREAD_ONCE_INIT
53 #ifdef INIT_ONCE_STATIC_INIT
54 #define TSS_DTOR_ITERATIONS PTHREAD_DESTRUCTOR_ITERATIONS
56 #define TSS_DTOR_ITERATIONS 1 // assume TSS dtor MAY be called at least once.
59 // FIXME: temporary non-standard hack to ease transition
60 #define _MTX_INITIALIZER_NP PTHREAD_MUTEX_INITIALIZER
62 /*---------------------------- types ----------------------------*/
63 typedef pthread_cond_t cnd_t
;
64 typedef pthread_t thrd_t
;
65 typedef pthread_key_t tss_t
;
66 typedef pthread_mutex_t mtx_t
;
67 typedef pthread_once_t once_flag
;
71 Implementation limits:
72 - Conditionally emulation for "mutex with timeout"
73 (see EMULATED_THREADS_USE_NATIVE_TIMEDLOCK macro)
75 struct impl_thrd_param
{
81 impl_thrd_routine(void *p
)
83 struct impl_thrd_param pack
= *((struct impl_thrd_param
*)p
);
85 return (void*)(intptr_t)pack
.func(pack
.arg
);
89 /*--------------- 7.25.2 Initialization functions ---------------*/
92 call_once(once_flag
*flag
, void (*func
)(void))
94 pthread_once(flag
, func
);
98 /*------------- 7.25.3 Condition variable functions -------------*/
101 cnd_broadcast(cnd_t
*cond
)
103 if (!cond
) return thrd_error
;
104 pthread_cond_broadcast(cond
);
110 cnd_destroy(cnd_t
*cond
)
113 pthread_cond_destroy(cond
);
118 cnd_init(cnd_t
*cond
)
120 if (!cond
) return thrd_error
;
121 pthread_cond_init(cond
, NULL
);
127 cnd_signal(cnd_t
*cond
)
129 if (!cond
) return thrd_error
;
130 pthread_cond_signal(cond
);
136 cnd_timedwait(cnd_t
*cond
, mtx_t
*mtx
, const xtime
*xt
)
138 struct timespec abs_time
;
140 if (!cond
|| !mtx
|| !xt
) return thrd_error
;
141 rt
= pthread_cond_timedwait(cond
, mtx
, &abs_time
);
144 return (rt
== 0) ? thrd_success
: thrd_error
;
149 cnd_wait(cnd_t
*cond
, mtx_t
*mtx
)
151 if (!cond
|| !mtx
) return thrd_error
;
152 pthread_cond_wait(cond
, mtx
);
157 /*-------------------- 7.25.4 Mutex functions --------------------*/
160 mtx_destroy(mtx_t
*mtx
)
163 pthread_mutex_destroy(mtx
);
168 mtx_init(mtx_t
*mtx
, int type
)
170 pthread_mutexattr_t attr
;
171 if (!mtx
) return thrd_error
;
172 if (type
!= mtx_plain
&& type
!= mtx_timed
&& type
!= mtx_try
173 && type
!= (mtx_plain
|mtx_recursive
)
174 && type
!= (mtx_timed
|mtx_recursive
)
175 && type
!= (mtx_try
|mtx_recursive
))
177 pthread_mutexattr_init(&attr
);
178 if ((type
& mtx_recursive
) != 0) {
179 #if defined(__linux__) || defined(__linux)
180 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE_NP
);
182 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
185 pthread_mutex_init(mtx
, &attr
);
186 pthread_mutexattr_destroy(&attr
);
194 if (!mtx
) return thrd_error
;
195 pthread_mutex_lock(mtx
);
201 mtx_timedlock(mtx_t
*mtx
, const xtime
*xt
)
203 if (!mtx
|| !xt
) return thrd_error
;
205 #ifdef EMULATED_THREADS_USE_NATIVE_TIMEDLOCK
209 ts
.tv_nsec
= xt
->nsec
;
210 rt
= pthread_mutex_timedlock(mtx
, &ts
);
213 return (rt
== ETIMEDOUT
) ? thrd_busy
: thrd_error
;
215 time_t expire
= time(NULL
);
217 while (mtx_trylock(mtx
) != thrd_success
) {
218 time_t now
= time(NULL
);
231 mtx_trylock(mtx_t
*mtx
)
233 if (!mtx
) return thrd_error
;
234 return (pthread_mutex_trylock(mtx
) == 0) ? thrd_success
: thrd_busy
;
239 mtx_unlock(mtx_t
*mtx
)
241 if (!mtx
) return thrd_error
;
242 pthread_mutex_unlock(mtx
);
247 /*------------------- 7.25.5 Thread functions -------------------*/
250 thrd_create(thrd_t
*thr
, thrd_start_t func
, void *arg
)
252 struct impl_thrd_param
*pack
;
253 if (!thr
) return thrd_error
;
254 pack
= (struct impl_thrd_param
*)malloc(sizeof(struct impl_thrd_param
));
255 if (!pack
) return thrd_nomem
;
258 if (pthread_create(thr
, NULL
, impl_thrd_routine
, pack
) != 0) {
269 return pthread_self();
274 thrd_detach(thrd_t thr
)
276 return (pthread_detach(thr
) == 0) ? thrd_success
: thrd_error
;
281 thrd_equal(thrd_t thr0
, thrd_t thr1
)
283 return pthread_equal(thr0
, thr1
);
290 pthread_exit((void*)(intptr_t)res
);
295 thrd_join(thrd_t thr
, int *res
)
298 if (pthread_join(thr
, &code
) != 0)
301 *res
= (int)(intptr_t)code
;
307 thrd_sleep(const xtime
*xt
)
311 req
.tv_sec
= xt
->sec
;
312 req
.tv_nsec
= xt
->nsec
;
313 nanosleep(&req
, NULL
);
324 /*----------- 7.25.6 Thread-specific storage functions -----------*/
327 tss_create(tss_t
*key
, tss_dtor_t dtor
)
329 if (!key
) return thrd_error
;
330 return (pthread_key_create(key
, dtor
) == 0) ? thrd_success
: thrd_error
;
335 tss_delete(tss_t key
)
337 pthread_key_delete(key
);
344 return pthread_getspecific(key
);
349 tss_set(tss_t key
, void *val
)
351 return (pthread_setspecific(key
, val
) == 0) ? thrd_success
: thrd_error
;
355 /*-------------------- 7.25.7 Time functions --------------------*/
358 xtime_get(xtime
*xt
, int base
)
361 if (base
== TIME_UTC
) {
362 xt
->sec
= time(NULL
);