25 #ifndef _TINYCTHREAD_H_ 26 #define _TINYCTHREAD_H_ 56 #if !defined(_TTHREAD_PLATFORM_DEFINED_) 57 #if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__) 58 #define _TTHREAD_WIN32_ 60 #define _TTHREAD_POSIX_ 62 #define _TTHREAD_PLATFORM_DEFINED_ 66 #if defined(_TTHREAD_POSIX_) 68 #if !defined(_GNU_SOURCE) 71 #if !defined(_POSIX_C_SOURCE) || ((_POSIX_C_SOURCE - 0) < 199309L) 72 #undef _POSIX_C_SOURCE 73 #define _POSIX_C_SOURCE 199309L 75 #if !defined(_XOPEN_SOURCE) || ((_XOPEN_SOURCE - 0) < 500) 77 #define _XOPEN_SOURCE 500 86 #if defined(_TTHREAD_POSIX_) 88 #elif defined(_TTHREAD_WIN32_) 89 #ifndef WIN32_LEAN_AND_MEAN 90 #define WIN32_LEAN_AND_MEAN 91 #define __UNDEF_LEAN_AND_MEAN 94 #ifdef __UNDEF_LEAN_AND_MEAN 95 #undef WIN32_LEAN_AND_MEAN 96 #undef __UNDEF_LEAN_AND_MEAN 101 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L 102 #define TTHREAD_NORETURN _Noreturn 103 #elif defined(__GNUC__) 104 #define TTHREAD_NORETURN __attribute__((__noreturn__)) 106 #define TTHREAD_NORETURN 113 #define _TTHREAD_EMULATE_TIMESPEC_GET_ 115 #if defined(_TTHREAD_WIN32_) 116 struct _tthread_timespec {
120 #define timespec _tthread_timespec 123 int _tthread_timespec_get(
struct timespec *ts,
int base);
124 #define timespec_get _tthread_timespec_get 128 #define TINYCTHREAD_VERSION_MAJOR 1 130 #define TINYCTHREAD_VERSION_MINOR 2 132 #define TINYCTHREAD_VERSION (TINYCTHREAD_VERSION_MAJOR * 100 + TINYCTHREAD_VERSION_MINOR) 153 #if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201102L)) && !defined(_Thread_local) 154 #if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_CC) || defined(__IBMCPP__) 155 #define _Thread_local __thread 157 #define _Thread_local __declspec(thread) 159 #elif defined(__GNUC__) && defined(__GNUC_MINOR__) && (((__GNUC__ << 8) | __GNUC_MINOR__) < ((4 << 8) | 9)) 160 #define _Thread_local __thread 164 #if defined(_TTHREAD_WIN32_) 165 #define TSS_DTOR_ITERATIONS (4) 167 #define TSS_DTOR_ITERATIONS PTHREAD_DESTRUCTOR_ITERATIONS 172 #define thrd_success 1 173 #define thrd_timedout 2 180 #define mtx_recursive 2 183 #if defined(_TTHREAD_WIN32_) 194 typedef pthread_mutex_t mtx_t;
253 #if defined(_TTHREAD_WIN32_) 256 unsigned int mWaitersCount;
257 CRITICAL_SECTION mWaitersCountLock;
260 typedef pthread_cond_t cnd_t;
305 int cnd_wait(cnd_t *cond, mtx_t *mtx);
319 int cnd_timedwait(cnd_t *cond, mtx_t *mtx,
const struct timespec *ts);
322 #if defined(_TTHREAD_WIN32_) 323 typedef HANDLE thrd_t;
325 typedef pthread_t thrd_t;
372 TTHREAD_NORETURN
void thrd_exit(
int res);
396 int thrd_sleep(
const struct timespec *duration,
struct timespec *remaining);
405 #if defined(_TTHREAD_WIN32_) 408 typedef pthread_key_t tss_t;
451 int tss_set(tss_t key,
void *val);
453 #if defined(_TTHREAD_WIN32_) 455 LONG
volatile status;
456 CRITICAL_SECTION lock;
458 #define ONCE_FLAG_INIT {0,} 460 #define once_flag pthread_once_t 461 #define ONCE_FLAG_INIT PTHREAD_ONCE_INIT 469 #if defined(_TTHREAD_WIN32_) 470 void call_once(once_flag *flag,
void (*func)(
void));
472 #define call_once(flag,func) pthread_once(flag,func) int mtx_trylock(mtx_t *mtx)
Try to lock the given mutex.
thrd_t thrd_current(void)
Identify the calling thread.
int mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
Lock the given mutex, or block until a specific point in time.
int mtx_unlock(mtx_t *mtx)
Unlock the given mutex.
int tss_set(tss_t key, void *val)
Set the value for a thread-specific storage.
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
Create a new thread.
void thrd_yield(void)
Yield execution to another thread.
int mtx_init(mtx_t *mtx, int type)
Create a mutex object.
int(* thrd_start_t)(void *arg)
Thread start function.
Definition: tinycthread.h:336
int cnd_init(cnd_t *cond)
Create a condition variable object.
TTHREAD_NORETURN void thrd_exit(int res)
Terminate execution of the calling thread.
int cnd_broadcast(cnd_t *cond)
Broadcast a condition variable.
void mtx_destroy(mtx_t *mtx)
Release any resources used by the given mutex.
int thrd_sleep(const struct timespec *duration, struct timespec *remaining)
Put the calling thread to sleep.
void * tss_get(tss_t key)
Get the value for a thread-specific storage.
void(* tss_dtor_t)(void *val)
Destructor function for a thread-specific storage.
Definition: tinycthread.h:414
#define call_once(flag, func)
Invoke a callback exactly once.
Definition: tinycthread.h:472
int cnd_wait(cnd_t *cond, mtx_t *mtx)
Wait for a condition variable to become signaled.
int cnd_signal(cnd_t *cond)
Signal a condition variable.
int tss_create(tss_t *key, tss_dtor_t dtor)
Create a thread-specific storage.
int mtx_lock(mtx_t *mtx)
Lock the given mutex.
int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts)
Wait for a condition variable to become signaled.
int thrd_join(thrd_t thr, int *res)
Wait for a thread to terminate.
void tss_delete(tss_t key)
Delete a thread-specific storage.
int thrd_detach(thrd_t thr)
Dispose of any resources allocated to the thread when that thread exits.
void cnd_destroy(cnd_t *cond)
Release any resources used by the given condition variable.
int thrd_equal(thrd_t thr0, thrd_t thr1)
Compare two thread identifiers.