copy over sync mods in live plotter. might make common code an ext reference in the future

This commit is contained in:
2025-09-07 14:08:07 -05:00
parent ab639e635a
commit 0e210b4fbb

View File

@@ -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<TimeSpan>(milliseconds);
TimeSpan ts;
ts.QuadPart = static_cast<int64_t>(milliseconds/1000.0)*freq.QuadPart;
return ts;
}
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) {
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) {
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) {
return static_cast<double>(sp);
return static_cast<double>(sp.QuadPart*1000)/static_cast<double>(freq.QuadPart);
}
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) {
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) {
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