forked from prashanthbollam/cplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cpp
More file actions
68 lines (59 loc) · 1.08 KB
/
Timer.cpp
File metadata and controls
68 lines (59 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "System.h"
#include <chrono>
#include <thread>
#include <time.h>
#include <cmath>
double now()
{
auto now = std::chrono::high_resolution_clock::now();
auto duration = now.time_since_epoch();
using sec = std::chrono::duration<double, std::ratio<1, 1>>;
return std::chrono::duration_cast<sec>(duration).count();
}
void sleep(double t)
{
if (t <= 0.0) return;
Sleep((DWORD)(t*1000.0));
}
static void CALLBACK TimerCallback(void *timer_, BOOLEAN TimerOrWaitFired)
{
assert(timer_);
Timer &t = *((Timer*)timer_);
if (t.callback) t.callback();
}
Timer::Timer(double dt) : dt_(dt), timer(0)
{
}
Timer::~Timer()
{
stop();
}
void Timer::start()
{
if (timer) return;
if (!CreateTimerQueueTimer(&timer, NULL, ::TimerCallback, this,
(DWORD)(dt_*1000.0),
(DWORD)(dt_*1000.0),
WT_EXECUTEINTIMERTHREAD))
{
assert(false);
timer = 0;
}
}
void Timer::stop()
{
if (!timer) return;
if (!DeleteTimerQueueTimer(NULL, timer, NULL))
{
if (GetLastError() != ERROR_IO_PENDING)
{
assert(false);
return;
}
}
timer = 0;
}
bool Timer::running() const
{
return timer != 0;
}