copy over sync mods in live plotter. might make common code an ext reference in the future
This commit is contained in:
57
inc/sync.h
57
inc/sync.h
@@ -11,13 +11,16 @@ typedef CRITICAL_SECTION Mutex;
|
|||||||
typedef CONDITION_VARIABLE ConditionVar;
|
typedef CONDITION_VARIABLE ConditionVar;
|
||||||
typedef HANDLE Semaphore;
|
typedef HANDLE Semaphore;
|
||||||
typedef HANDLE Thread;
|
typedef HANDLE Thread;
|
||||||
typedef DWORD TimeSpan;
|
typedef LARGE_INTEGER TimeSpan;
|
||||||
typedef DWORD WINAPI (*ThreadFunc)(_In_ LPVOID lpParameter);
|
typedef DWORD (WINAPI *ThreadFunc)(_In_ LPVOID lpParameter);
|
||||||
typedef LPVOID ThreadArg
|
typedef LPVOID ThreadArg;
|
||||||
const TimeSpan infinite_ts = INFINITE;
|
|
||||||
|
const TimeSpan infinite_ts = LLONG_MAX;
|
||||||
|
static const LARGE_INTEGER freq;
|
||||||
|
QueryPerformanceFrequency(&freq);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Thread make_thread(ThreadFunc t);
|
Thread make_thread(ThreadFunc t, ThreadArg a);
|
||||||
void join(Thread t);
|
void join(Thread t);
|
||||||
|
|
||||||
Mutex make_mutex();
|
Mutex make_mutex();
|
||||||
@@ -41,6 +44,8 @@ TimeSpan from_ms(double milliseconds);
|
|||||||
TimeSpan from_s(double seconds);
|
TimeSpan from_s(double seconds);
|
||||||
TimeSpan from_min(double minutes);
|
TimeSpan from_min(double minutes);
|
||||||
TimeSpan from_hours(double hours);
|
TimeSpan from_hours(double hours);
|
||||||
|
TimeSpan now();
|
||||||
|
TimeSpan operator-(const TimeSpan &a, const TimeSpan &b);
|
||||||
|
|
||||||
double to_ms(TimeSpan &sp);
|
double to_ms(TimeSpan &sp);
|
||||||
double to_s(TimeSpan &sp);
|
double to_s(TimeSpan &sp);
|
||||||
@@ -51,7 +56,7 @@ double to_hours(TimeSpan &sp);
|
|||||||
|
|
||||||
Thread make_thread(ThreadFunc f, ThreadArg a) {
|
Thread make_thread(ThreadFunc f, ThreadArg a) {
|
||||||
DWORD tid;
|
DWORD tid;
|
||||||
return CreateThread(NULL, 0, t, a, 0, &tid);
|
return CreateThread(NULL, 0, f, a, 0, &tid);
|
||||||
}
|
}
|
||||||
|
|
||||||
void join(Thread t) {
|
void join(Thread t) {
|
||||||
@@ -87,7 +92,7 @@ ConditionVar make_condition_var() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void wait(ConditionVar &c, Mutex &m, TimeSpan ts) {
|
void wait(ConditionVar &c, Mutex &m, TimeSpan ts) {
|
||||||
SleepConditionVariable(&c, &m, ts);
|
SleepConditionVariableCS(&c, &m, ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wake_one(ConditionVar &c) {
|
void wake_one(ConditionVar &c) {
|
||||||
@@ -111,7 +116,7 @@ void wait(Semaphore &s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void post(Semaphore &s) {
|
void post(Semaphore &s) {
|
||||||
ReleaseSemaphore(s);
|
ReleaseSemaphore(s, 1, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dispose(Semaphore &s) {
|
void dispose(Semaphore &s) {
|
||||||
@@ -119,35 +124,55 @@ void dispose(Semaphore &s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TimeSpan from_ms(double milliseconds) {
|
TimeSpan from_ms(double milliseconds) {
|
||||||
return static_cast<TimeSpan>(milliseconds);
|
TimeSpan ts;
|
||||||
|
ts.QuadPart = static_cast<int64_t>(milliseconds/1000.0)*freq.QuadPart;
|
||||||
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
TimeSpan from_s(double seconds) {
|
TimeSpan from_s(double seconds) {
|
||||||
return static_cast<TimeSpan>(seconds*1000.0);
|
TimeSpan ts;
|
||||||
|
ts.QuadPart = static_cast<int64_t>(seconds)*freq.QuadPart;
|
||||||
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
TimeSpan from_min(double minutes) {
|
TimeSpan from_min(double minutes) {
|
||||||
return static_cast<TimeSpan>(minutes*60.0*1000.0);
|
TimeSpan ts;
|
||||||
|
ts.QuadPart = static_cast<int64_t>(minutes*60.0)*freq.QuadPart;
|
||||||
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
TimeSpan from_hours(double hours) {
|
TimeSpan from_hours(double hours) {
|
||||||
return static_cast<TimeSpan>(hours*60.0*60.0*1000.0);
|
TimeSpan ts;
|
||||||
|
ts.QuadPart = static_cast<int64_t>(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) {
|
double to_ms(TimeSpan &sp) {
|
||||||
return static_cast<double>(sp);
|
return static_cast<double>(sp.QuadPart*1000)/static_cast<double>(freq.QuadPart);
|
||||||
}
|
}
|
||||||
|
|
||||||
double to_s(TimeSpan &sp) {
|
double to_s(TimeSpan &sp) {
|
||||||
return static_cast<double>(sp)/1000.0;
|
return static_cast<double>(sp.QuadPart)/static_cast<double>(freq.QuadPart);
|
||||||
}
|
}
|
||||||
|
|
||||||
double to_min(TimeSpan &sp) {
|
double to_min(TimeSpan &sp) {
|
||||||
return static_cast<double>(sp)/(1000.0*60.0);
|
return static_cast<double>(sp.QuadPart)/static_cast<double>(freq.QuadPart*60);
|
||||||
}
|
}
|
||||||
|
|
||||||
double to_hours(TimeSpan &sp) {
|
double to_hours(TimeSpan &sp) {
|
||||||
return static_cast<double>(sp)/(1000.0*60.0*60.0);
|
return static_cast<double>(sp.QuadPart)/static_cast<double>(freq.QuadPart*60*60);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user