2 * C11 <threads.h> emulation library
4 * (C) Copyright yohhoy 2012.
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See copy at http://www.boost.org/LICENSE_1_0.txt)
14 #include <stdint.h> /* for intptr_t */
19 EMULATED_THREADS_USE_NATIVE_TIMEDLOCK
20 Use pthread_mutex_timedlock() for `mtx_timedlock()'
21 Otherwise use mtx_trylock() + *busy loop* emulation.
23 #if !defined(__CYGWIN__)
24 #define EMULATED_THREADS_USE_NATIVE_TIMEDLOCK
30 /*---------------------------- macros ----------------------------*/
31 #define ONCE_FLAG_INIT PTHREAD_ONCE_INIT
32 #ifdef INIT_ONCE_STATIC_INIT
33 #define TSS_DTOR_ITERATIONS PTHREAD_DESTRUCTOR_ITERATIONS
35 #define TSS_DTOR_ITERATIONS 1 // assume TSS dtor MAY be called at least once.
38 // FIXME: temporary non-standard hack to ease transition
39 #define _MTX_INITIALIZER_NP PTHREAD_MUTEX_INITIALIZER
41 /*---------------------------- types ----------------------------*/
42 typedef pthread_cond_t cnd_t
;
43 typedef pthread_t thrd_t
;
44 typedef pthread_key_t tss_t
;
45 typedef pthread_mutex_t mtx_t
;
46 typedef pthread_once_t once_flag
;
50 Implementation limits:
51 - Conditionally emulation for "mutex with timeout"
52 (see EMULATED_THREADS_USE_NATIVE_TIMEDLOCK macro)
54 struct impl_thrd_param
{
60 impl_thrd_routine(void *p
)
62 struct impl_thrd_param pack
= *((struct impl_thrd_param
*)p
);
64 return (void*)(intptr_t)pack
.func(pack
.arg
);
68 /*--------------- 7.25.2 Initialization functions ---------------*/
71 call_once(once_flag
*flag
, void (*func
)(void))
73 pthread_once(flag
, func
);
77 /*------------- 7.25.3 Condition variable functions -------------*/
80 cnd_broadcast(cnd_t
*cond
)
82 if (!cond
) return thrd_error
;
83 pthread_cond_broadcast(cond
);
89 cnd_destroy(cnd_t
*cond
)
92 pthread_cond_destroy(cond
);
99 if (!cond
) return thrd_error
;
100 pthread_cond_init(cond
, NULL
);
106 cnd_signal(cnd_t
*cond
)
108 if (!cond
) return thrd_error
;
109 pthread_cond_signal(cond
);
115 cnd_timedwait(cnd_t
*cond
, mtx_t
*mtx
, const xtime
*xt
)
117 struct timespec abs_time
;
119 if (!cond
|| !mtx
|| !xt
) return thrd_error
;
120 rt
= pthread_cond_timedwait(cond
, mtx
, &abs_time
);
123 return (rt
== 0) ? thrd_success
: thrd_error
;
128 cnd_wait(cnd_t
*cond
, mtx_t
*mtx
)
130 if (!cond
|| !mtx
) return thrd_error
;
131 pthread_cond_wait(cond
, mtx
);
136 /*-------------------- 7.25.4 Mutex functions --------------------*/
139 mtx_destroy(mtx_t
*mtx
)
142 pthread_mutex_destroy(mtx
);
147 mtx_init(mtx_t
*mtx
, int type
)
149 pthread_mutexattr_t attr
;
150 if (!mtx
) return thrd_error
;
151 if (type
!= mtx_plain
&& type
!= mtx_timed
&& type
!= mtx_try
152 && type
!= (mtx_plain
|mtx_recursive
)
153 && type
!= (mtx_timed
|mtx_recursive
)
154 && type
!= (mtx_try
|mtx_recursive
))
156 pthread_mutexattr_init(&attr
);
157 if ((type
& mtx_recursive
) != 0) {
158 #if defined(__linux__) || defined(__linux)
159 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE_NP
);
161 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
164 pthread_mutex_init(mtx
, &attr
);
165 pthread_mutexattr_destroy(&attr
);
173 if (!mtx
) return thrd_error
;
174 pthread_mutex_lock(mtx
);
180 mtx_timedlock(mtx_t
*mtx
, const xtime
*xt
)
182 if (!mtx
|| !xt
) return thrd_error
;
184 #ifdef EMULATED_THREADS_USE_NATIVE_TIMEDLOCK
188 ts
.tv_nsec
= xt
->nsec
;
189 rt
= pthread_mutex_timedlock(mtx
, &ts
);
192 return (rt
== ETIMEDOUT
) ? thrd_busy
: thrd_error
;
194 time_t expire
= time(NULL
);
196 while (mtx_trylock(mtx
) != thrd_success
) {
197 time_t now
= time(NULL
);
210 mtx_trylock(mtx_t
*mtx
)
212 if (!mtx
) return thrd_error
;
213 return (pthread_mutex_trylock(mtx
) == 0) ? thrd_success
: thrd_busy
;
218 mtx_unlock(mtx_t
*mtx
)
220 if (!mtx
) return thrd_error
;
221 pthread_mutex_unlock(mtx
);
226 /*------------------- 7.25.5 Thread functions -------------------*/
229 thrd_create(thrd_t
*thr
, thrd_start_t func
, void *arg
)
231 struct impl_thrd_param
*pack
;
232 if (!thr
) return thrd_error
;
233 pack
= (struct impl_thrd_param
*)malloc(sizeof(struct impl_thrd_param
));
234 if (!pack
) return thrd_nomem
;
237 if (pthread_create(thr
, NULL
, impl_thrd_routine
, pack
) != 0) {
248 return pthread_self();
253 thrd_detach(thrd_t thr
)
255 return (pthread_detach(thr
) == 0) ? thrd_success
: thrd_error
;
260 thrd_equal(thrd_t thr0
, thrd_t thr1
)
262 return pthread_equal(thr0
, thr1
);
269 pthread_exit((void*)(intptr_t)res
);
274 thrd_join(thrd_t thr
, int *res
)
277 if (pthread_join(thr
, &code
) != 0)
280 *res
= (int)(intptr_t)code
;
286 thrd_sleep(const xtime
*xt
)
290 req
.tv_sec
= xt
->sec
;
291 req
.tv_nsec
= xt
->nsec
;
292 nanosleep(&req
, NULL
);
303 /*----------- 7.25.6 Thread-specific storage functions -----------*/
306 tss_create(tss_t
*key
, tss_dtor_t dtor
)
308 if (!key
) return thrd_error
;
309 return (pthread_key_create(key
, dtor
) == 0) ? thrd_success
: thrd_error
;
314 tss_delete(tss_t key
)
316 pthread_key_delete(key
);
323 return pthread_getspecific(key
);
328 tss_set(tss_t key
, void *val
)
330 return (pthread_setspecific(key
, val
) == 0) ? thrd_success
: thrd_error
;
334 /*-------------------- 7.25.7 Time functions --------------------*/
337 xtime_get(xtime
*xt
, int base
)
340 if (base
== TIME_UTC
) {
341 xt
->sec
= time(NULL
);