diff options
Diffstat (limited to '')
-rw-r--r-- | winpr/libwinpr/synch/timer.c | 11 | ||||
-rw-r--r-- | winpr/libwinpr/synch/wait.c | 66 |
2 files changed, 17 insertions, 60 deletions
diff --git a/winpr/libwinpr/synch/timer.c b/winpr/libwinpr/synch/timer.c index 8238a88..354382c 100644 --- a/winpr/libwinpr/synch/timer.c +++ b/winpr/libwinpr/synch/timer.c @@ -707,11 +707,11 @@ static void timespec_add_ms(struct timespec* tspec, UINT32 ms) static void timespec_gettimeofday(struct timespec* tspec) { - struct timeval tval; WINPR_ASSERT(tspec); - gettimeofday(&tval, NULL); - tspec->tv_sec = tval.tv_sec; - tspec->tv_nsec = tval.tv_usec * 1000; + + const UINT64 ns = winpr_GetUnixTimeNS(); + tspec->tv_sec = WINPR_TIME_NS_TO_S(ns); + tspec->tv_nsec = WINPR_TIME_NS_REM_NS(ns); } static INT64 timespec_compare(const struct timespec* tspec1, const struct timespec* tspec2) @@ -881,12 +881,13 @@ static void* TimerQueueThread(void* arg) status = pthread_cond_timedwait(&(timerQueue->cond), &(timerQueue->cond_mutex), &timeout); FireExpiredTimerQueueTimers(timerQueue); + const BOOL bCancelled = timerQueue->bCancelled; pthread_mutex_unlock(&(timerQueue->cond_mutex)); if ((status != ETIMEDOUT) && (status != 0)) break; - if (timerQueue->bCancelled) + if (bCancelled) break; } diff --git a/winpr/libwinpr/synch/wait.c b/winpr/libwinpr/synch/wait.c index 3bef657..865f583 100644 --- a/winpr/libwinpr/synch/wait.c +++ b/winpr/libwinpr/synch/wait.c @@ -59,60 +59,16 @@ #include "../pipe/pipe.h" -/* clock_gettime is not implemented on OSX prior to 10.12 */ -#if defined(__MACH__) && defined(__APPLE__) - -#include <mach/mach_time.h> - -#ifndef CLOCK_REALTIME -#define CLOCK_REALTIME 0 -#endif - -#ifndef CLOCK_MONOTONIC -#define CLOCK_MONOTONIC 0 -#endif - -/* clock_gettime is not implemented on OSX prior to 10.12 */ -int _mach_clock_gettime(int clk_id, struct timespec* t); - -int _mach_clock_gettime(int clk_id, struct timespec* t) -{ - UINT64 time = 0; - double seconds = 0.0; - double nseconds = 0.0; - mach_timebase_info_data_t timebase = { 0 }; - mach_timebase_info(&timebase); - time = mach_absolute_time(); - nseconds = ((double)time * (double)timebase.numer) / ((double)timebase.denom); - seconds = ((double)time * (double)timebase.numer) / ((double)timebase.denom * 1e9); - t->tv_sec = seconds; - t->tv_nsec = nseconds; - return 0; -} - -/* if clock_gettime is declared, then __CLOCK_AVAILABILITY will be defined */ -#ifdef __CLOCK_AVAILABILITY -/* If we compiled with Mac OSX 10.12 or later, then clock_gettime will be declared - * * but it may be NULL at runtime. So we need to check before using it. */ -int _mach_safe_clock_gettime(int clk_id, struct timespec* t); - -int _mach_safe_clock_gettime(int clk_id, struct timespec* t) +static struct timespec ts_from_ns(void) { - if (clock_gettime) - { - return clock_gettime(clk_id, t); - } - - return _mach_clock_gettime(clk_id, t); + const UINT64 ns = winpr_GetUnixTimeNS(); + struct timespec timeout = { 0 }; + timeout.tv_sec = WINPR_TIME_NS_TO_S(ns); + timeout.tv_nsec = WINPR_TIME_NS_REM_NS(ns); + ; + return timeout; } -#define clock_gettime _mach_safe_clock_gettime -#else -#define clock_gettime _mach_clock_gettime -#endif - -#endif - /** * Drop in replacement for pthread_mutex_timedlock * http://code.google.com/p/android/issues/detail?id=7807 @@ -148,14 +104,14 @@ STATIC_NEEDED int pthread_mutex_timedlock(pthread_mutex_t* mutex, unsigned long long diff = 0; int retcode = -1; /* This is just to avoid a completely busy wait */ - clock_gettime(CLOCK_MONOTONIC, &timenow); + timenow = ts_from_ns(); diff = ts_difftime(&timenow, timeout); sleepytime.tv_sec = diff / 1000000000LL; sleepytime.tv_nsec = diff % 1000000000LL; while ((retcode = pthread_mutex_trylock(mutex)) == EBUSY) { - clock_gettime(CLOCK_MONOTONIC, &timenow); + timenow = ts_from_ns(); if (ts_difftime(timeout, &timenow) >= 0) { @@ -237,8 +193,8 @@ DWORD WaitForSingleObjectEx(HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertabl if (dwMilliseconds != INFINITE) { int status = 0; - struct timespec timeout = { 0 }; - clock_gettime(CLOCK_MONOTONIC, &timeout); + struct timespec timeout = ts_from_ns(); + ts_add_ms(&timeout, dwMilliseconds); status = pthread_mutex_timedlock(&mutex->mutex, &timeout); |