24 #ifndef _TINYCTHREAD_H_ 25 #define _TINYCTHREAD_H_ 51 #if !defined(_TTHREAD_PLATFORM_DEFINED_) 52 #if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__) 53 #define _TTHREAD_WIN32_ 55 #define _TTHREAD_POSIX_ 57 #define _TTHREAD_PLATFORM_DEFINED_ 61 #if defined(_TTHREAD_POSIX_) 63 #if !defined(_GNU_SOURCE) 66 #if !defined(_POSIX_C_SOURCE) || ((_POSIX_C_SOURCE - 0) < 199309L) 67 #undef _POSIX_C_SOURCE 68 #define _POSIX_C_SOURCE 199309L 70 #if !defined(_XOPEN_SOURCE) || ((_XOPEN_SOURCE - 0) < 500) 72 #define _XOPEN_SOURCE 500 80 #if defined(_TTHREAD_POSIX_) 83 #elif defined(_TTHREAD_WIN32_) 84 #ifndef WIN32_LEAN_AND_MEAN 85 #define WIN32_LEAN_AND_MEAN 86 #define __UNDEF_LEAN_AND_MEAN 89 #ifdef __UNDEF_LEAN_AND_MEAN 90 #undef WIN32_LEAN_AND_MEAN 91 #undef __UNDEF_LEAN_AND_MEAN 100 #ifdef CLOCK_REALTIME 101 #define TIME_UTC CLOCK_REALTIME 108 #if defined(_TTHREAD_WIN32_) || defined(__APPLE_CC__) 109 #define _TTHREAD_EMULATE_CLOCK_GETTIME_ 111 #if defined(_TTHREAD_WIN32_) 112 struct _ttherad_timespec {
116 #define timespec _ttherad_timespec 120 typedef int _tthread_clockid_t;
121 #define clockid_t _tthread_clockid_t 124 int _tthread_clock_gettime(clockid_t clk_id,
struct timespec *ts);
125 #define clock_gettime _tthread_clock_gettime 126 #ifndef CLOCK_REALTIME 127 #define CLOCK_REALTIME 0 133 #define TINYCTHREAD_VERSION_MAJOR 1 135 #define TINYCTHREAD_VERSION_MINOR 1 137 #define TINYCTHREAD_VERSION (TINYCTHREAD_VERSION_MAJOR * 100 + TINYCTHREAD_VERSION_MINOR) 159 #if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201102L)) && !defined(_Thread_local) 160 #if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_CC) || defined(__IBMCPP__) 161 #define _Thread_local __thread 163 #define _Thread_local __declspec(thread) 168 #define TSS_DTOR_ITERATIONS 0 172 #define thrd_success 1 173 #define thrd_timeout 2 181 #define mtx_recursive 8 184 #if defined(_TTHREAD_WIN32_) 186 CRITICAL_SECTION mHandle;
191 typedef pthread_mutex_t mtx_t;
245 #if defined(_TTHREAD_WIN32_) 248 unsigned int mWaitersCount;
249 CRITICAL_SECTION mWaitersCountLock;
252 typedef pthread_cond_t cnd_t;
297 int cnd_wait(cnd_t *cond, mtx_t *mtx);
311 int cnd_timedwait(cnd_t *cond, mtx_t *mtx,
const struct timespec *ts);
314 #if defined(_TTHREAD_WIN32_) 315 typedef HANDLE thrd_t;
317 typedef pthread_t thrd_t;
386 int thrd_sleep(
const struct timespec *time_point,
struct timespec *remaining);
395 #if defined(_TTHREAD_WIN32_) 398 typedef pthread_key_t tss_t;
439 int tss_set(tss_t key,
void *val);
int mtx_trylock(mtx_t *mtx)
Try to lock the given mutex.
Definition: tinycthread.c:109
thrd_t thrd_current(void)
Identify the calling thread.
Definition: tinycthread.c:393
int mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
NOT YET IMPLEMENTED.
Definition: tinycthread.c:101
int mtx_unlock(mtx_t *mtx)
Unlock the given mutex.
Definition: tinycthread.c:124
int tss_set(tss_t key, void *val)
Set the value for a thread-specific storage.
Definition: tinycthread.c:561
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
Create a new thread.
Definition: tinycthread.c:361
void thrd_yield(void)
Yield execution to another thread.
Definition: tinycthread.c:512
int mtx_init(mtx_t *mtx, int type)
Create a mutex object.
Definition: tinycthread.c:56
int(* thrd_start_t)(void *arg)
Thread start function.
Definition: tinycthread.h:328
int cnd_init(cnd_t *cond)
Create a condition variable object.
Definition: tinycthread.c:140
int cnd_broadcast(cnd_t *cond)
Broadcast a condition variable.
Definition: tinycthread.c:211
void mtx_destroy(mtx_t *mtx)
Release any resources used by the given mutex.
Definition: tinycthread.c:77
void * tss_get(tss_t key)
Get the value for a thread-specific storage.
Definition: tinycthread.c:552
void(* tss_dtor_t)(void *val)
Destructor function for a thread-specific storage.
Definition: tinycthread.h:404
int cnd_wait(cnd_t *cond, mtx_t *mtx)
Wait for a condition variable to become signaled.
Definition: tinycthread.c:285
void thrd_exit(int res)
Terminate execution of the calling thread.
Definition: tinycthread.c:418
int cnd_signal(cnd_t *cond)
Signal a condition variable.
Definition: tinycthread.c:186
int tss_create(tss_t *key, tss_dtor_t dtor)
Create a thread-specific storage.
Definition: tinycthread.c:521
int mtx_lock(mtx_t *mtx)
Lock the given mutex.
Definition: tinycthread.c:86
int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts)
Wait for a condition variable to become signaled.
Definition: tinycthread.c:294
int thrd_join(thrd_t thr, int *res)
Wait for a thread to terminate.
Definition: tinycthread.c:432
int thrd_sleep(const struct timespec *time_point, struct timespec *remaining)
Put the calling thread to sleep.
Definition: tinycthread.c:465
void tss_delete(tss_t key)
Delete a thread-specific storage.
Definition: tinycthread.c:543
int thrd_detach(thrd_t thr)
NOT YET IMPLEMENTED.
Definition: tinycthread.c:402
void cnd_destroy(cnd_t *cond)
Release any resources used by the given condition variable.
Definition: tinycthread.c:169
int thrd_equal(thrd_t thr0, thrd_t thr1)
Compare two thread identifiers.
Definition: tinycthread.c:409