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)
111#ifndef HAVE_CLOCK_GETTIME
112# include <sys/time.h>
115#define MYTHREAD_RET_TYPE void *
116#define MYTHREAD_RET_VALUE NULL
118typedef pthread_t mythread;
119typedef pthread_mutex_t mythread_mutex;
123#ifdef HAVE_CLOCK_GETTIME
130typedef struct timespec mythread_condtime;
134#define mythread_once(func) \
136 static pthread_once_t once_ = PTHREAD_ONCE_INIT; \
137 pthread_once(&once_, &func); \
144mythread_sigmask(
int how,
const sigset_t *restrict set,
145 sigset_t *restrict oset)
152 int ret = pthread_sigmask(how, set, oset);
162mythread_create(mythread *thread,
void *(*func)(
void *arg),
void *arg)
168 mythread_sigmask(SIG_SETMASK, &all, &old);
169 const int ret = pthread_create(thread,
NULL, func, arg);
170 mythread_sigmask(SIG_SETMASK, &old,
NULL);
177mythread_join(mythread thread)
179 return pthread_join(thread,
NULL);
185mythread_mutex_init(mythread_mutex *mutex)
187 return pthread_mutex_init(mutex,
NULL);
191mythread_mutex_destroy(mythread_mutex *mutex)
193 int ret = pthread_mutex_destroy(mutex);
199mythread_mutex_lock(mythread_mutex *mutex)
201 int ret = pthread_mutex_lock(mutex);
207mythread_mutex_unlock(mythread_mutex *mutex)
209 int ret = pthread_mutex_unlock(mutex);
225mythread_cond_init(mythread_cond *mycond)
227#ifdef HAVE_CLOCK_GETTIME
228# if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && \
229 defined(HAVE_CLOCK_MONOTONIC)
231 pthread_condattr_t condattr;
236 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0
237 && pthread_condattr_init(&condattr) == 0) {
238 int ret = pthread_condattr_setclock(
239 &condattr, CLOCK_MONOTONIC);
241 ret = pthread_cond_init(&mycond->cond, &condattr);
243 pthread_condattr_destroy(&condattr);
246 mycond->clk_id = CLOCK_MONOTONIC;
256 mycond->clk_id = CLOCK_REALTIME;
259 return pthread_cond_init(&mycond->cond,
NULL);
263mythread_cond_destroy(mythread_cond *cond)
265 int ret = pthread_cond_destroy(&cond->cond);
271mythread_cond_signal(mythread_cond *cond)
273 int ret = pthread_cond_signal(&cond->cond);
279mythread_cond_wait(mythread_cond *cond, mythread_mutex *mutex)
281 int ret = pthread_cond_wait(&cond->cond, mutex);
289mythread_cond_timedwait(mythread_cond *cond, mythread_mutex *mutex,
290 const mythread_condtime *condtime)
292 int ret = pthread_cond_timedwait(&cond->cond, mutex, condtime);
300mythread_condtime_set(mythread_condtime *condtime,
const mythread_cond *cond,
303 condtime->tv_sec = timeout_ms / 1000;
304 condtime->tv_nsec = (timeout_ms % 1000) * 1000000;
306#ifdef HAVE_CLOCK_GETTIME
308 int ret = clock_gettime(cond->clk_id, &now);
312 condtime->tv_sec +=
now.tv_sec;
313 condtime->tv_nsec +=
now.tv_nsec;
318 gettimeofday(&now,
NULL);
320 condtime->tv_sec +=
now.tv_sec;
321 condtime->tv_nsec +=
now.tv_usec * 1000L;
325 if (condtime->tv_nsec >= 1000000000L) {
326 condtime->tv_nsec -= 1000000000L;
332#elif defined(MYTHREAD_WIN95) || defined(MYTHREAD_VISTA)
338#define WIN32_LEAN_AND_MEAN
341# define _WIN32_WINNT 0x0600
346#define MYTHREAD_RET_TYPE unsigned int __stdcall
347#define MYTHREAD_RET_VALUE 0
349typedef HANDLE mythread;
350typedef CRITICAL_SECTION mythread_mutex;
353typedef HANDLE mythread_cond;
355typedef CONDITION_VARIABLE mythread_cond;
373#define mythread_once(func) \
375 static INIT_ONCE once_ = INIT_ONCE_STATIC_INIT; \
377 if (!InitOnceBeginInitialize(&once_, 0, &pending_, NULL)) \
381 if (!InitOnceComplete(&once, 0, NULL)) \
393mythread_create(mythread *thread,
394 unsigned int (__stdcall *func)(
void *arg),
void *arg)
400 *thread = (HANDLE)
ret;
405mythread_join(mythread thread)
409 if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0)
412 if (!CloseHandle(thread))
420mythread_mutex_init(mythread_mutex *mutex)
422 InitializeCriticalSection(mutex);
427mythread_mutex_destroy(mythread_mutex *mutex)
429 DeleteCriticalSection(mutex);
433mythread_mutex_lock(mythread_mutex *mutex)
435 EnterCriticalSection(mutex);
439mythread_mutex_unlock(mythread_mutex *mutex)
441 LeaveCriticalSection(mutex);
446mythread_cond_init(mythread_cond *cond)
449 *cond = CreateEvent(
NULL, FALSE, FALSE,
NULL);
450 return *cond ==
NULL ? -1 : 0;
452 InitializeConditionVariable(cond);
458mythread_cond_destroy(mythread_cond *cond)
468mythread_cond_signal(mythread_cond *cond)
473 WakeConditionVariable(cond);
478mythread_cond_wait(mythread_cond *cond, mythread_mutex *mutex)
481 LeaveCriticalSection(mutex);
482 WaitForSingleObject(*cond, INFINITE);
483 EnterCriticalSection(mutex);
485 BOOL ret = SleepConditionVariableCS(cond, mutex, INFINITE);
492mythread_cond_timedwait(mythread_cond *cond, mythread_mutex *mutex,
493 const mythread_condtime *condtime)
496 LeaveCriticalSection(mutex);
499 DWORD elapsed = GetTickCount() - condtime->start;
501 ? 0 : condtime->timeout - elapsed;
504 DWORD ret = WaitForSingleObject(*cond, timeout);
507 EnterCriticalSection(mutex);
509 return ret == WAIT_TIMEOUT;
511 BOOL ret = SleepConditionVariableCS(cond, mutex, timeout);
512 assert(
ret || GetLastError() == ERROR_TIMEOUT);
518mythread_condtime_set(mythread_condtime *condtime,
const mythread_cond *cond,
522 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