From ca9eb81d0b8624bfee6dac7b062747b01e8aeb4b Mon Sep 17 00:00:00 2001 From: stijn Date: Sun, 25 Oct 2015 12:40:24 +0100 Subject: windows: Add usleep() implementation for msvc port Also make sleep.c self-contained by moving initialization code, instead of having part of the code in init.c, and add a header file to accomodate this. msec_sleep() now uses the usleep() implementation as well. --- windows/sleep.c | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) (limited to 'windows/sleep.c') diff --git a/windows/sleep.c b/windows/sleep.c index a7e5125e5..214d6d622 100644 --- a/windows/sleep.c +++ b/windows/sleep.c @@ -25,10 +25,52 @@ */ #include +#include +#include -extern HANDLE hSleepEvent; +HANDLE waitTimer = NULL; + +void init_sleep(void) { + waitTimer = CreateWaitableTimer(NULL, TRUE, NULL); +} + +void deinit_sleep(void) { + if (waitTimer != NULL) { + CloseHandle(waitTimer); + waitTimer = NULL; + } +} + +int usleep_impl(__int64 usec) { + if (waitTimer == NULL) { + errno = EAGAIN; + return -1; + } + if (usec < 0 || usec > LLONG_MAX / 10) { + errno = EINVAL; + return -1; + } + + LARGE_INTEGER ft; + ft.QuadPart = -10 * usec; // 100 nanosecond interval, negative value = relative time + if (SetWaitableTimer(waitTimer, &ft, 0, NULL, NULL, 0) == 0) { + errno = EINVAL; + return -1; + } + if (WaitForSingleObject(waitTimer, INFINITE) != WAIT_OBJECT_0) { + errno = EAGAIN; + return -1; + } + return 0; +} + +#ifdef _MSC_VER // mingw and the likes provide their own usleep() +int usleep(__int64 usec) { + return usleep_impl(usec); +} +#endif void msec_sleep(double msec) { - ResetEvent(hSleepEvent); - WaitForSingleObjectEx(hSleepEvent, msec, FALSE); + const double usec = msec * 1000.0; + usleep_impl(usec > (double)LLONG_MAX ? LLONG_MAX : (__int64)usec); } -- cgit v1.2.3