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
85 #if defined(_TTHREAD_POSIX_)
87 #elif defined(_TTHREAD_WIN32_)
88 #ifndef WIN32_LEAN_AND_MEAN
89 #define WIN32_LEAN_AND_MEAN
90 #define __UNDEF_LEAN_AND_MEAN
93 #ifdef __UNDEF_LEAN_AND_MEAN
94 #undef WIN32_LEAN_AND_MEAN
95 #undef __UNDEF_LEAN_AND_MEAN
100 #if defined(__GNUC__)
101 #define TTHREAD_NORETURN __attribute__((noreturn))
103 #define TTHREAD_NORETURN
110 #define _TTHREAD_EMULATE_TIMESPEC_GET_
112 #if defined(_TTHREAD_WIN32_)
113 struct _tthread_timespec {
117 #define timespec _tthread_timespec
120 int _tthread_timespec_get(
struct timespec *ts,
int base);
121 #define timespec_get _tthread_timespec_get
125 #define TINYCTHREAD_VERSION_MAJOR 1
127 #define TINYCTHREAD_VERSION_MINOR 2
129 #define TINYCTHREAD_VERSION (TINYCTHREAD_VERSION_MAJOR * 100 + TINYCTHREAD_VERSION_MINOR)
151 #if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201102L)) && !defined(_Thread_local)
152 #if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_CC) || defined(__IBMCPP__)
153 #define _Thread_local __thread
155 #define _Thread_local __declspec(thread)
160 #if defined(_TTHREAD_WIN32_)
161 #define TSS_DTOR_ITERATIONS (4)
163 #define TSS_DTOR_ITERATIONS PTHREAD_DESTRUCTOR_ITERATIONS
168 #define thrd_success 1
169 #define thrd_timedout 2
176 #define mtx_recursive 2
179 #if defined(_TTHREAD_WIN32_)
190 typedef pthread_mutex_t mtx_t;
244 #if defined(_TTHREAD_WIN32_)
247 unsigned int mWaitersCount;
248 CRITICAL_SECTION mWaitersCountLock;
251 typedef pthread_cond_t cnd_t;
296 int cnd_wait(cnd_t *cond, mtx_t *mtx);
310 int cnd_timedwait(cnd_t *cond, mtx_t *mtx,
const struct timespec *ts);
313 #if defined(_TTHREAD_WIN32_)
314 typedef HANDLE thrd_t;
316 typedef pthread_t thrd_t;
363 void thrd_exit(
int res) TTHREAD_NORETURN;
387 int thrd_sleep(
const struct timespec *duration,
struct timespec *remaining);
396 #if defined(_TTHREAD_WIN32_)
399 typedef pthread_key_t tss_t;
442 int tss_set(tss_t key,
void *val);
444 #if defined(_TTHREAD_WIN32_)
446 LONG
volatile status;
447 CRITICAL_SECTION lock;
449 #define ONCE_FLAG_INIT {0,}
451 #define once_flag pthread_once_t
452 #define ONCE_FLAG_INIT PTHREAD_ONCE_INIT
460 #if defined(_TTHREAD_WIN32_)
461 void call_once(once_flag *flag,
void (*func)(
void));
463 #define call_once(flag,func) pthread_once(flag,func)
int(* thrd_start_t)(void *arg)
Thread start function.
Definition: tinycthread.h:327
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)
NOT YET IMPLEMENTED.
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 cnd_init(cnd_t *cond)
Create a condition variable object.
void thrd_exit(int res) TTHREAD_NORETURN
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.
#define call_once(flag, func)
Invoke a callback exactly once.
Definition: tinycthread.h:463
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.
void(* tss_dtor_t)(void *val)
Destructor function for a thread-specific storage.
Definition: tinycthread.h:405
int thrd_equal(thrd_t thr0, thrd_t thr1)
Compare two thread identifiers.