31 #if defined(THREAD_UNIT_TESTS) 41 #if !defined(_WIN32) && !defined(_WIN64) 62 #if defined(_WIN32) || defined(_WIN64) 70 #if defined(_WIN32) || defined(_WIN64) 71 thread = CreateThread(NULL, 0, fn, parameter, 0, NULL);
73 pthread_attr_init(&attr);
74 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
75 if (pthread_create(&thread, &attr, fn, parameter) != 0)
77 pthread_attr_destroy(&attr);
94 #if defined(_WIN32) || defined(_WIN64) 95 mutex = CreateMutex(NULL, 0, NULL);
99 mutex =
malloc(
sizeof(pthread_mutex_t));
101 *rc = pthread_mutex_init(mutex, NULL);
117 #if defined(_WIN32) || defined(_WIN64) 119 rc = WaitForSingleObject(mutex, INFINITE);
121 rc = pthread_mutex_lock(mutex);
138 #if defined(_WIN32) || defined(_WIN64) 140 if (ReleaseMutex(mutex) == 0)
145 rc = pthread_mutex_unlock(mutex);
161 #if defined(_WIN32) || defined(_WIN64) 162 rc = CloseHandle(mutex);
164 rc = pthread_mutex_destroy(mutex);
178 #if defined(_WIN32) || defined(_WIN64) 179 return GetCurrentThreadId();
181 return pthread_self();
196 #if defined(_WIN32) || defined(_WIN64) 204 sem = CreateSemaphore(
212 sem = dispatch_semaphore_create(0L);
213 *rc = (sem == NULL) ? -1 : 0;
215 sem =
malloc(
sizeof(sem_t));
217 *rc = sem_init(sem, 0, 0);
236 #if !defined(_WIN32) && !defined(_WIN64) && !defined(OSX) 238 #if defined(USE_TRYWAIT) 240 useconds_t interval = 10000;
241 int count = (1000 * timeout) / interval;
248 #if defined(_WIN32) || defined(_WIN64) 250 rc = WaitForSingleObject(sem, timeout < 0 ? 0 : timeout);
251 if (rc == WAIT_TIMEOUT)
255 rc = (int)dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, (int64_t)timeout*1000000L));
258 #elif defined(USE_TRYWAIT) 259 while (++i < count && (rc = sem_trywait(sem)) != 0)
261 if (rc == -1 && ((rc = errno) != EAGAIN))
273 if (clock_gettime(CLOCK_REALTIME, &ts) != -1)
275 ts.tv_sec += timeout;
276 rc = sem_timedwait(sem, &ts);
294 #if defined(_WIN32) || defined(_WIN64) 296 return WaitForSingleObject(sem, 0) == WAIT_OBJECT_0;
299 return dispatch_semaphore_wait(sem, DISPATCH_TIME_NOW) == 0;
303 return sem_trywait(sem) == 0;
318 #if defined(_WIN32) || defined(_WIN64) 319 if (SetEvent(sem) == 0)
322 rc = (int)dispatch_semaphore_signal(sem);
325 int rc1 = sem_getvalue(sem, &val);
328 else if (val == 0 && sem_post(sem) == -1)
346 #if defined(_WIN32) || defined(_WIN64) 347 rc = CloseHandle(sem);
349 dispatch_release(sem);
351 rc = sem_destroy(sem);
359 #if !defined(_WIN32) && !defined(_WIN64) 368 pthread_condattr_t attr;
372 pthread_condattr_init(&attr);
378 if ((rc = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)) == 0)
379 use_clock_monotonic = 1;
381 Log(
LOG_ERROR, -1,
"Error %d calling pthread_condattr_setclock(CLOCK_MONOTONIC)", rc);
387 *rc = pthread_cond_init(&condvar->
cond, &attr);
388 *rc = pthread_mutex_init(&condvar->
mutex, NULL);
404 pthread_mutex_lock(&condvar->
mutex);
405 rc = pthread_cond_signal(&condvar->
cond);
406 pthread_mutex_unlock(&condvar->
mutex);
419 struct timespec cond_timeout;
422 #if defined(__APPLE__) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 423 struct timeval cur_time;
424 gettimeofday(&cur_time, NULL);
425 cond_timeout.tv_sec = cur_time.tv_sec + timeout;
426 cond_timeout.tv_nsec = cur_time.tv_usec * 1000;
428 clock_gettime(CLOCK_REALTIME, &cond_timeout);
430 cond_timeout.tv_sec += timeout;
432 pthread_mutex_lock(&condvar->
mutex);
433 rc = pthread_cond_timedwait(&condvar->
cond, &condvar->
mutex, &cond_timeout);
434 pthread_mutex_unlock(&condvar->
mutex);
448 rc = pthread_mutex_destroy(&condvar->
mutex);
449 rc = pthread_cond_destroy(&condvar->
cond);
457 #if defined(THREAD_UNIT_TESTS) 459 #if defined(_WIN32) || defined(_WINDOWS) 460 #define mqsleep(A) Sleep(1000*A) 461 #define START_TIME_TYPE DWORD 462 static DWORD start_time = 0;
465 return GetTickCount();
468 #define mqsleep sleep 469 #define START_TIME_TYPE struct timespec 472 static struct timespec start;
473 clock_gettime(CLOCK_REALTIME, &start);
477 #define mqsleep sleep 478 #define START_TIME_TYPE struct timeval 482 struct timeval start_time;
483 gettimeofday(&start_time, NULL);
492 return GetTickCount() - start_time;
496 long elapsed(
struct timespec start)
498 struct timespec now, res;
500 clock_gettime(CLOCK_REALTIME, &now);
501 ntimersub(now, start, res);
502 return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
507 struct timeval now, res;
509 gettimeofday(&now, NULL);
510 timersub(&now, &start_time, &res);
511 return (res.tv_sec)*1000 + (res.tv_usec)/1000;
526 printf(
"Assertion failed, file %s, line %d, description: %s\n", filename, lineno,
description);
536 printf(
"Assertion succeeded, file %s, line %d, description: %s\n", filename, lineno,
description);
539 #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) 540 #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) 549 printf(
"This should return immediately as it was posted already\n");
551 assert(
"rc 1 from wait_cond", rc == 1,
"rc was %d", rc);
553 printf(
"This should hang around a few seconds\n");
555 assert(
"rc 1 from wait_cond", rc == 1,
"rc was %d", rc);
557 printf(
"Secondary cond thread ending\n");
568 printf(
"Post secondary so it should return immediately\n");
570 assert(
"rc 0 from signal cond", rc == 0,
"rc was %d", rc);
572 printf(
"Starting secondary thread\n");
577 printf(
"post secondary\n");
579 assert(
"rc 1 from signal cond", rc == 1,
"rc was %d", rc);
583 printf(
"Main thread ending\n");
594 printf(
"Secondary semaphore pointer %p\n", sem);
597 assert(
"rc 1 from check_sem", rc == 1,
"rc was %d", rc);
599 printf(
"Secondary thread about to wait\n");
601 printf(
"Secondary thread returned from wait %d\n", rc);
603 printf(
"Secondary thread about to wait\n");
605 printf(
"Secondary thread returned from wait %d\n", rc);
608 printf(
"Secondary thread ending\n");
619 printf(
"Primary semaphore pointer %p\n", sem);
622 assert(
"rc 0 from check_sem", rc == 0,
"rc was %d\n", rc);
624 printf(
"post secondary so then check should be 1\n");
626 assert(
"rc 0 from post_sem", rc == 0,
"rc was %d\n", rc);
629 assert(
"rc 1 from check_sem", rc == 1,
"rc was %d", rc);
631 printf(
"Starting secondary thread\n");
636 assert(
"rc 1 from check_sem", rc == 1,
"rc was %d", rc);
638 printf(
"post secondary\n");
640 assert(
"rc 1 from post_sem", rc == 1,
"rc was %d", rc);
644 printf(
"Main thread ending\n");
650 int main(
int argc,
char *argv[])
int Thread_post_sem(sem_type sem)
int Thread_wait_cond(cond_type condvar, int timeout)
enum MQTTPropertyCodes value
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
sem_type Thread_create_sem(int *rc)
int Thread_lock_mutex(mutex_type mutex)
int Thread_destroy_cond(cond_type condvar)
long elapsed(START_TIME_TYPE start_time)
static int cond(LexState *ls)
thread_return_type(* thread_fn)(void *)
mutex_type Thread_create_mutex(int *rc)
basic_thread< reference > thread
thread_return_type cond_secondary(void *n)
static thread_return_type WINAPI sem_secondary(void *n)
int Thread_unlock_mutex(mutex_type mutex)
void Log(enum LOG_LEVELS log_level, int msgno, const char *format,...)
void myassert(char *filename, int lineno, char *description, int value, char *format,...)
#define assert(a, b, c, d)
int Thread_wait_sem(sem_type sem, int timeout)
cond_type Thread_create_cond(int *rc)
#define thread_return_type
thread_id_type Thread_getid(void)
int Thread_check_sem(sem_type sem)
int Thread_signal_cond(cond_type condvar)
thread_type Thread_start(thread_fn fn, void *parameter)
int Thread_destroy_sem(sem_type sem)
int Thread_destroy_mutex(mutex_type mutex)
int main(int argc, char **argv)
START_TIME_TYPE start_clock(void)