diff --git a/inc/sync.h b/inc/sync.h index d6eb76b..593993d 100644 --- a/inc/sync.h +++ b/inc/sync.h @@ -10,14 +10,17 @@ namespace sync { typedef CRITICAL_SECTION Mutex; typedef CONDITION_VARIABLE ConditionVar; typedef HANDLE Semaphore; -typedef HANDLE Thread; -typedef DWORD TimeSpan; -typedef DWORD WINAPI (*ThreadFunc)(_In_ LPVOID lpParameter); -typedef LPVOID ThreadArg -const TimeSpan infinite_ts = INFINITE; +typedef HANDLE Thread; +typedef LARGE_INTEGER TimeSpan; +typedef DWORD (WINAPI *ThreadFunc)(_In_ LPVOID lpParameter); +typedef LPVOID ThreadArg; + +const TimeSpan infinite_ts = LLONG_MAX; +static const LARGE_INTEGER freq; +QueryPerformanceFrequency(&freq); #endif -Thread make_thread(ThreadFunc t); +Thread make_thread(ThreadFunc t, ThreadArg a); void join(Thread t); Mutex make_mutex(); @@ -41,6 +44,8 @@ TimeSpan from_ms(double milliseconds); TimeSpan from_s(double seconds); TimeSpan from_min(double minutes); TimeSpan from_hours(double hours); +TimeSpan now(); +TimeSpan operator-(const TimeSpan &a, const TimeSpan &b); double to_ms(TimeSpan &sp); double to_s(TimeSpan &sp); @@ -51,7 +56,7 @@ double to_hours(TimeSpan &sp); Thread make_thread(ThreadFunc f, ThreadArg a) { DWORD tid; - return CreateThread(NULL, 0, t, a, 0, &tid); + return CreateThread(NULL, 0, f, a, 0, &tid); } void join(Thread t) { @@ -87,7 +92,7 @@ ConditionVar make_condition_var() { } void wait(ConditionVar &c, Mutex &m, TimeSpan ts) { - SleepConditionVariable(&c, &m, ts); + SleepConditionVariableCS(&c, &m, ts); } void wake_one(ConditionVar &c) { @@ -111,7 +116,7 @@ void wait(Semaphore &s) { } void post(Semaphore &s) { - ReleaseSemaphore(s); + ReleaseSemaphore(s, 1, NULL); } void dispose(Semaphore &s) { @@ -119,35 +124,55 @@ void dispose(Semaphore &s) { } TimeSpan from_ms(double milliseconds) { - return static_cast(milliseconds); + TimeSpan ts; + ts.QuadPart = static_cast(milliseconds/1000.0)*freq.QuadPart; + return ts; } TimeSpan from_s(double seconds) { - return static_cast(seconds*1000.0); + TimeSpan ts; + ts.QuadPart = static_cast(seconds)*freq.QuadPart; + return ts; } TimeSpan from_min(double minutes) { - return static_cast(minutes*60.0*1000.0); + TimeSpan ts; + ts.QuadPart = static_cast(minutes*60.0)*freq.QuadPart; + return ts; } TimeSpan from_hours(double hours) { - return static_cast(hours*60.0*60.0*1000.0); + TimeSpan ts; + ts.QuadPart = static_cast(hours*60.0*60.0)*freq.QuadPart; + return ts; +} + +TimeSpan now() { + TimeSpan ts; + QueryPerformanceCounter(&ts); + return ts; +} + +TimeSpan operator-(const TimeSpan &a, TimeSpan &b) { + TimeSpan ts; + ts.QuadPart = a.QuadPart - b.QuadPart; + return ts; } double to_ms(TimeSpan &sp) { - return static_cast(sp); + return static_cast(sp.QuadPart*1000)/static_cast(freq.QuadPart); } double to_s(TimeSpan &sp) { - return static_cast(sp)/1000.0; + return static_cast(sp.QuadPart)/static_cast(freq.QuadPart); } double to_min(TimeSpan &sp) { - return static_cast(sp)/(1000.0*60.0); + return static_cast(sp.QuadPart)/static_cast(freq.QuadPart*60); } double to_hours(TimeSpan &sp) { - return static_cast(sp)/(1000.0*60.0*60.0); + return static_cast(sp.QuadPart)/static_cast(freq.QuadPart*60*60); } #endif