19#if defined(MYTHREAD_POSIX) || defined(MYTHREAD_WIN95) \
20 || defined(MYTHREAD_VISTA)
21# define MYTHREAD_ENABLED 1
25#ifdef MYTHREAD_ENABLED
51#define mythread_sync(mutex) mythread_sync_helper1(mutex, __LINE__)
52#define mythread_sync_helper1(mutex, line) mythread_sync_helper2(mutex, line)
53#define mythread_sync_helper2(mutex, line) \
54 for (unsigned int mythread_i_ ## line = 0; \
56 ? (mythread_mutex_unlock(&(mutex)), 0) \
57 : (mythread_mutex_lock(&(mutex)), 1); \
58 mythread_i_ ## line = 1) \
59 for (unsigned int mythread_j_ ## line = 0; \
60 !mythread_j_ ## line; \
61 mythread_j_ ## line = 1)
65#if !defined(MYTHREAD_ENABLED)
72#define mythread_once(func) \
74 static bool once_ = false; \
82#if !(defined(_WIN32) && !defined(__CYGWIN__))
87mythread_sigmask(
int how,
const sigset_t *restrict set,
88 sigset_t *restrict oset)
90 int ret = sigprocmask(how, set, oset);
97#elif defined(MYTHREAD_POSIX)
109#define MYTHREAD_RET_TYPE void *
110#define MYTHREAD_RET_VALUE NULL
112typedef pthread_t mythread;
113typedef pthread_mutex_t mythread_mutex;
117#ifdef HAVE_CLOCK_GETTIME
124typedef struct timespec mythread_condtime;
128#define mythread_once(func) \
130 static pthread_once_t once_ = PTHREAD_ONCE_INIT; \
131 pthread_once(&once_, &func); \
138mythread_sigmask(
int how,
const sigset_t *restrict set,
139 sigset_t *restrict oset)
146 int ret = pthread_sigmask(how, set, oset);
156mythread_create(mythread *thread,
void *(*func)(
void *arg),
void *arg)
162 mythread_sigmask(SIG_SETMASK, &all, &old);
163 const int ret = pthread_create(thread,
NULL, func, arg);
164 mythread_sigmask(SIG_SETMASK, &old,
NULL);
171mythread_join(mythread thread)
173 return pthread_join(thread,
NULL);
179mythread_mutex_init(mythread_mutex *mutex)
181 return pthread_mutex_init(mutex,
NULL);
185mythread_mutex_destroy(mythread_mutex *mutex)
187 int ret = pthread_mutex_destroy(mutex);
193mythread_mutex_lock(mythread_mutex *mutex)
195 int ret = pthread_mutex_lock(mutex);
201mythread_mutex_unlock(mythread_mutex *mutex)
203 int ret = pthread_mutex_unlock(mutex);
219mythread_cond_init(mythread_cond *mycond)
221#ifdef HAVE_CLOCK_GETTIME
223# if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && HAVE_DECL_CLOCK_MONOTONIC
225 pthread_condattr_t condattr;
230 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0
231 && pthread_condattr_init(&condattr) == 0) {
232 int ret = pthread_condattr_setclock(
233 &condattr, CLOCK_MONOTONIC);
235 ret = pthread_cond_init(&mycond->cond, &condattr);
237 pthread_condattr_destroy(&condattr);
240 mycond->clk_id = CLOCK_MONOTONIC;
250 mycond->clk_id = CLOCK_REALTIME;
253 return pthread_cond_init(&mycond->cond,
NULL);
257mythread_cond_destroy(mythread_cond *cond)
259 int ret = pthread_cond_destroy(&cond->cond);
265mythread_cond_signal(mythread_cond *cond)
267 int ret = pthread_cond_signal(&cond->cond);
273mythread_cond_wait(mythread_cond *cond, mythread_mutex *mutex)
275 int ret = pthread_cond_wait(&cond->cond, mutex);
283mythread_cond_timedwait(mythread_cond *cond, mythread_mutex *mutex,
284 const mythread_condtime *condtime)
286 int ret = pthread_cond_timedwait(&cond->cond, mutex, condtime);
294mythread_condtime_set(mythread_condtime *condtime,
const mythread_cond *cond,
297 condtime->tv_sec = timeout_ms / 1000;
298 condtime->tv_nsec = (timeout_ms % 1000) * 1000000;
300#ifdef HAVE_CLOCK_GETTIME
302 int ret = clock_gettime(cond->clk_id, &now);
306 condtime->tv_sec +=
now.tv_sec;
307 condtime->tv_nsec +=
now.tv_nsec;
312 gettimeofday(&now,
NULL);
314 condtime->tv_sec +=
now.tv_sec;
315 condtime->tv_nsec +=
now.tv_usec * 1000L;
319 if (condtime->tv_nsec >= 1000000000L) {
320 condtime->tv_nsec -= 1000000000L;
326#elif defined(MYTHREAD_WIN95) || defined(MYTHREAD_VISTA)
332#define WIN32_LEAN_AND_MEAN
335# define _WIN32_WINNT 0x0600
340#define MYTHREAD_RET_TYPE unsigned int __stdcall
341#define MYTHREAD_RET_VALUE 0
343typedef HANDLE mythread;
344typedef CRITICAL_SECTION mythread_mutex;
347typedef HANDLE mythread_cond;
349typedef CONDITION_VARIABLE mythread_cond;
367#define mythread_once(func) \
369 static INIT_ONCE once_ = INIT_ONCE_STATIC_INIT; \
371 if (!InitOnceBeginInitialize(&once_, 0, &pending_, NULL)) \
375 if (!InitOnceComplete(&once, 0, NULL)) \
387mythread_create(mythread *thread,
388 unsigned int (__stdcall *func)(
void *arg),
void *arg)
394 *thread = (HANDLE)
ret;
399mythread_join(mythread thread)
403 if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0)
406 if (!CloseHandle(thread))
414mythread_mutex_init(mythread_mutex *mutex)
416 InitializeCriticalSection(mutex);
421mythread_mutex_destroy(mythread_mutex *mutex)
423 DeleteCriticalSection(mutex);
427mythread_mutex_lock(mythread_mutex *mutex)
429 EnterCriticalSection(mutex);
433mythread_mutex_unlock(mythread_mutex *mutex)
435 LeaveCriticalSection(mutex);
440mythread_cond_init(mythread_cond *cond)
443 *cond = CreateEvent(
NULL, FALSE, FALSE,
NULL);
444 return *cond ==
NULL ? -1 : 0;
446 InitializeConditionVariable(cond);
452mythread_cond_destroy(mythread_cond *cond)
462mythread_cond_signal(mythread_cond *cond)
467 WakeConditionVariable(cond);
472mythread_cond_wait(mythread_cond *cond, mythread_mutex *mutex)
475 LeaveCriticalSection(mutex);
476 WaitForSingleObject(*cond, INFINITE);
477 EnterCriticalSection(mutex);
479 BOOL ret = SleepConditionVariableCS(cond, mutex, INFINITE);
486mythread_cond_timedwait(mythread_cond *cond, mythread_mutex *mutex,
487 const mythread_condtime *condtime)
490 LeaveCriticalSection(mutex);
493 DWORD elapsed = GetTickCount() - condtime->start;
495 ? 0 : condtime->timeout - elapsed;
498 DWORD ret = WaitForSingleObject(*cond, timeout);
501 EnterCriticalSection(mutex);
503 return ret == WAIT_TIMEOUT;
505 BOOL ret = SleepConditionVariableCS(cond, mutex, timeout);
506 assert(
ret || GetLastError() == ERROR_TIMEOUT);
512mythread_condtime_set(mythread_condtime *condtime,
const mythread_cond *cond,
516 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