forked from mosamosa/GSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmtimer.cpp
180 lines (137 loc) · 3.4 KB
/
mmtimer.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Author: mosa Ÿe5bW6vDOJ. <[email protected]>
*
* This code is hereby placed in the public domain.
*/
#pragma comment(lib, "winmm.lib")
#include <windows.h>
#include <mmsystem.h>
#include <process.h>
#include <math.h>
#include "mmtimer.h"
#include "tools.h"
//-----------------------------------------------------------------------------
static struct sMMTimerGlobal
{
TIMECAPS tc;
sMMTimerGlobal()
{
timeGetDevCaps(&tc, sizeof(TIMECAPS));
timeBeginPeriod(tc.wPeriodMin);
}
~sMMTimerGlobal()
{
timeEndPeriod(tc.wPeriodMin);
}
}MMTimerGlobal;
//-----------------------------------------------------------------------------
void CMicroTimer::timerThread(void *arg)
{
CMicroTimer *_this = (CMicroTimer *)arg;
__int64 freq, org, _org, crr, obj;
double remain;
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
QueryPerformanceCounter((LARGE_INTEGER*)&_org);
while(!_this->terminate)
{
QueryPerformanceCounter((LARGE_INTEGER*)&crr);
if(orgTime)
org = orgTime;
else
org = _org;
obj = (__int64)(_this->interval * (double)freq);
obj = obj - ((crr - org) % obj);
remain = (double)obj / (double)freq;
obj += crr;
remain *= 1000.0;
remain -= error; // Sleep()‚ÌŒë·
remain = floor(remain);
if(remain >= 1.0)
Sleep((DWORD)remain);
for(;;)
{
QueryPerformanceCounter((LARGE_INTEGER*)&crr);
if(crr >= obj || _this->terminate)
break;
}
_this->eventTimer.set();
}
_this->state = TERMINATED;
_this->eventThread.set();
}
//-----------------------------------------------------------------------------
CMicroTimer::CMicroTimer() :
eventTimer(false, false, NULL),
eventThread(true, true, NULL)
{
state = TERMINATED;
terminate = false;
interval = 1.0;
}
//-----------------------------------------------------------------------------
CMicroTimer::~CMicroTimer()
{
stop();
}
//-----------------------------------------------------------------------------
bool CMicroTimer::start(double sec)
{
stop();
if(sec <= 0.002)
return false;
if(state != RUNNING)
{
interval = sec;
terminate = false;
state = RUNNING;
eventThread.reset();
if((long)_beginthread(timerThread, 0, this) == -1L)
{
state = TERMINATED;
eventThread.set();
return false;
}
return true;
}
return false;
}
//-----------------------------------------------------------------------------
void CMicroTimer::stop()
{
if(state == RUNNING)
{
terminate = true;
eventThread.wait((DWORD)(interval * 1000.0) + 100);
state = TERMINATED;
}
}
//-----------------------------------------------------------------------------
double CMicroTimer::error = 0.0;
__int64 CMicroTimer::orgTime = 0;
void CMicroTimer::calcError()
{
HANDLE hThread;
__int64 start, end;
double ave = 0.0;
int i, pri;
const int interval = 10, loop = 10;
hThread = GetCurrentThread();
pri = GetThreadPriority(hThread);
SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
for(i=0; i<loop; i++)
{
QueryPerformanceCounter((LARGE_INTEGER*)&start);
Sleep(interval);
QueryPerformanceCounter((LARGE_INTEGER*)&end);
ave += pc2time(end-start)*1000.0 - (double)interval;
}
ave /= loop;
SetThreadPriority(hThread, pri);
error = ave + 0.1;
if(error < 0.0)
error = 0.0;
if(error > 1.5)
error = 1.5;
}
//-----------------------------------------------------------------------------