18#if defined(MYTHREAD_POSIX) || defined(MYTHREAD_WIN95) \
19 || defined(MYTHREAD_VISTA)
20# define MYTHREAD_ENABLED 1
24#ifdef MYTHREAD_ENABLED
50#define mythread_sync(mutex) mythread_sync_helper1(mutex, __LINE__)
51#define mythread_sync_helper1(mutex, line) mythread_sync_helper2(mutex, line)
52#define mythread_sync_helper2(mutex, line) \
53 for (unsigned int mythread_i_ ## line = 0; \
55 ? (mythread_mutex_unlock(&(mutex)), 0) \
56 : (mythread_mutex_lock(&(mutex)), 1); \
57 mythread_i_ ## line = 1) \
58 for (unsigned int mythread_j_ ## line = 0; \
59 !mythread_j_ ## line; \
60 mythread_j_ ## line = 1)
64#if !defined(MYTHREAD_ENABLED)
71#define mythread_once(func) \
73 static bool once_ = false; \
81#if !(defined(_WIN32) && !defined(__CYGWIN__)) && !defined(__wasm__)
86mythread_sigmask(
int how,
const sigset_t *restrict set,
87 sigset_t *restrict oset)
89 int ret = sigprocmask(how, set, oset);
96#elif defined(MYTHREAD_POSIX)
110#ifndef HAVE_CLOCK_GETTIME
111# include <sys/time.h>
128# include <sys/types.h>
129# define sigset_t _sigset_t
130# define sigfillset(set_ptr) do { *(set_ptr) = 0; } while (0)
133#define MYTHREAD_RET_TYPE void *
134#define MYTHREAD_RET_VALUE NULL
136typedef pthread_t mythread;
137typedef pthread_mutex_t mythread_mutex;
141#ifdef HAVE_CLOCK_GETTIME
148typedef struct timespec mythread_condtime;
152#define mythread_once(func) \
154 static pthread_once_t once_ = PTHREAD_ONCE_INIT; \
155 pthread_once(&once_, &func); \
164mythread_sigmask(
int how,
const sigset_t *restrict set,
165 sigset_t *restrict oset)
167#if defined(__VMS) || defined(__MINGW32__)
172 int ret = pthread_sigmask(how, set, oset);
182mythread_create(mythread *thread,
void *(*func)(
void *arg),
void *arg)
188 mythread_sigmask(SIG_SETMASK, &all, &old);
189 const int ret = pthread_create(thread,
NULL, func, arg);
190 mythread_sigmask(SIG_SETMASK, &old,
NULL);
197mythread_join(mythread thread)
199 return pthread_join(thread,
NULL);
205mythread_mutex_init(mythread_mutex *mutex)
207 return pthread_mutex_init(mutex,
NULL);
211mythread_mutex_destroy(mythread_mutex *mutex)
213 int ret = pthread_mutex_destroy(mutex);
219mythread_mutex_lock(mythread_mutex *mutex)
221 int ret = pthread_mutex_lock(mutex);
227mythread_mutex_unlock(mythread_mutex *mutex)
229 int ret = pthread_mutex_unlock(mutex);
245mythread_cond_init(mythread_cond *mycond)
247#ifdef HAVE_CLOCK_GETTIME
248# if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && \
249 defined(HAVE_CLOCK_MONOTONIC)
251 pthread_condattr_t condattr;
256 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0
257 && pthread_condattr_init(&condattr) == 0) {
258 int ret = pthread_condattr_setclock(
259 &condattr, CLOCK_MONOTONIC);
261 ret = pthread_cond_init(&mycond->cond, &condattr);
263 pthread_condattr_destroy(&condattr);
266 mycond->clk_id = CLOCK_MONOTONIC;
276 mycond->clk_id = CLOCK_REALTIME;
279 return pthread_cond_init(&mycond->cond,
NULL);
283mythread_cond_destroy(mythread_cond *cond)
285 int ret = pthread_cond_destroy(&cond->cond);
291mythread_cond_signal(mythread_cond *cond)
293 int ret = pthread_cond_signal(&cond->cond);
299mythread_cond_wait(mythread_cond *cond, mythread_mutex *mutex)
301 int ret = pthread_cond_wait(&cond->cond, mutex);
309mythread_cond_timedwait(mythread_cond *cond, mythread_mutex *mutex,
310 const mythread_condtime *condtime)
312 int ret = pthread_cond_timedwait(&cond->cond, mutex, condtime);
320mythread_condtime_set(mythread_condtime *condtime,
const mythread_cond *cond,
323 condtime->tv_sec = (time_t)(timeout_ms / 1000);
324 condtime->tv_nsec = (long)((timeout_ms % 1000) * 1000000);
326#ifdef HAVE_CLOCK_GETTIME
328 int ret = clock_gettime(cond->clk_id, &now);
332 condtime->tv_sec +=
now.tv_sec;
333 condtime->tv_nsec +=
now.tv_nsec;
338 gettimeofday(&now,
NULL);
340 condtime->tv_sec +=
now.tv_sec;
341 condtime->tv_nsec +=
now.tv_usec * 1000L;
345 if (condtime->tv_nsec >= 1000000000L) {
346 condtime->tv_nsec -= 1000000000L;
352#elif defined(MYTHREAD_WIN95) || defined(MYTHREAD_VISTA)
358#define WIN32_LEAN_AND_MEAN
361# define _WIN32_WINNT 0x0600
366#define MYTHREAD_RET_TYPE unsigned int __stdcall
367#define MYTHREAD_RET_VALUE 0
369typedef HANDLE mythread;
370typedef CRITICAL_SECTION mythread_mutex;
373typedef HANDLE mythread_cond;
375typedef CONDITION_VARIABLE mythread_cond;
393#define mythread_once(func) \
395 static INIT_ONCE once_ = INIT_ONCE_STATIC_INIT; \
397 if (!InitOnceBeginInitialize(&once_, 0, &pending_, NULL)) \
401 if (!InitOnceComplete(&once_, 0, NULL)) \
413mythread_create(mythread *thread,
414 unsigned int (__stdcall *func)(
void *arg),
void *arg)
420 *thread = (HANDLE)
ret;
425mythread_join(mythread thread)
429 if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0)
432 if (!CloseHandle(thread))
440mythread_mutex_init(mythread_mutex *mutex)
442 InitializeCriticalSection(mutex);
447mythread_mutex_destroy(mythread_mutex *mutex)
449 DeleteCriticalSection(mutex);
453mythread_mutex_lock(mythread_mutex *mutex)
455 EnterCriticalSection(mutex);
459mythread_mutex_unlock(mythread_mutex *mutex)
461 LeaveCriticalSection(mutex);
466mythread_cond_init(mythread_cond *cond)
469 *cond = CreateEvent(
NULL, FALSE, FALSE,
NULL);
470 return *cond ==
NULL ? -1 : 0;
472 InitializeConditionVariable(cond);
478mythread_cond_destroy(mythread_cond *cond)
488mythread_cond_signal(mythread_cond *cond)
493 WakeConditionVariable(cond);
498mythread_cond_wait(mythread_cond *cond, mythread_mutex *mutex)
501 LeaveCriticalSection(mutex);
502 WaitForSingleObject(*cond, INFINITE);
503 EnterCriticalSection(mutex);
505 BOOL ret = SleepConditionVariableCS(cond, mutex, INFINITE);
512mythread_cond_timedwait(mythread_cond *cond, mythread_mutex *mutex,
513 const mythread_condtime *condtime)
516 LeaveCriticalSection(mutex);
519 DWORD elapsed = GetTickCount() - condtime->start;
521 ? 0 : condtime->timeout - elapsed;
524 DWORD ret = WaitForSingleObject(*cond, timeout);
527 EnterCriticalSection(mutex);
529 return ret == WAIT_TIMEOUT;
531 BOOL ret = SleepConditionVariableCS(cond, mutex, timeout);
532 assert(
ret || GetLastError() == ERROR_TIMEOUT);
538mythread_condtime_set(mythread_condtime *condtime,
const mythread_cond *cond,
542 condtime->start = GetTickCount();
UINT32 DWORD
Definition 7zTypes.h:194
int BOOL
Definition 7zMain.c:321
#define assert(condition)
Definition lz4.c:273
now
Definition rateLimiter.py:31
timeout
Definition run.py:691
size_t uintptr_t
Definition fuzzer.c:71
#define NULL
Definition getopt1.c:37
Common includes, definitions, system-specific things etc.
ret
Definition zlib_interface.c:30