1 |
dpavlin |
1 |
/// @file systimer.cc |
2 |
|
|
/// @author Kimball Thurston |
3 |
|
|
/// |
4 |
|
|
|
5 |
|
|
// |
6 |
|
|
// Copyright (c) 2004 Kimball Thurston |
7 |
|
|
// |
8 |
|
|
// This program is free software; you can redistribute it and/or modify |
9 |
|
|
// it under the terms of the GNU General Public License version 2 as |
10 |
|
|
// published by the Free Software Foundation. |
11 |
|
|
// |
12 |
|
|
// This program is distributed in the hope that it will be useful, |
13 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
// GNU General Public License for more details. |
16 |
|
|
// |
17 |
|
|
// You should have received a copy of the GNU General Public License |
18 |
|
|
// along with this program; if not, write to the Free Software |
19 |
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
|
|
// |
21 |
|
|
// Some stuff from msdn.microsoft.com documentation |
22 |
|
|
// |
23 |
|
|
|
24 |
|
|
#include <sys/types.h> |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
|
27 |
|
|
#define WIN32_LEAN_AND_MEAN |
28 |
|
|
|
29 |
|
|
#include <tchar.h> |
30 |
|
|
#include <windows.h> |
31 |
|
|
#include <time.h> |
32 |
|
|
#include <mmsystem.h> |
33 |
|
|
|
34 |
|
|
#undef FASTCALL |
35 |
|
|
|
36 |
|
|
#include "system/sys.h" |
37 |
|
|
|
38 |
|
|
#include "debug/tracers.h" |
39 |
|
|
#include "system/systimer.h" |
40 |
|
|
|
41 |
|
|
struct sys_timer_struct |
42 |
|
|
{ |
43 |
|
|
MMRESULT timerID; |
44 |
|
|
UINT timerRes; |
45 |
|
|
sys_timer_callback cb_func; |
46 |
|
|
}; |
47 |
|
|
|
48 |
|
|
bool sys_create_timer(sys_timer *t, sys_timer_callback cb_func) |
49 |
|
|
{ |
50 |
|
|
#define TARGET_RESOLUTION 1 // 1-millisecond target resolution |
51 |
|
|
|
52 |
|
|
TIMECAPS tc; |
53 |
|
|
UINT timerRes; |
54 |
|
|
|
55 |
|
|
if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR) { |
56 |
|
|
// Error; application can't continue. |
57 |
|
|
return false; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
timerRes = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax); |
61 |
|
|
if (timeBeginPeriod(timerRes) != TIMERR_NOERROR) { |
62 |
|
|
// Error; application can't continue. |
63 |
|
|
return false; |
64 |
|
|
} |
65 |
|
|
sys_timer_struct * timer = new sys_timer_struct; |
66 |
|
|
timer->timerID = 0; |
67 |
|
|
timer->timerRes = timerRes; |
68 |
|
|
timer->cb_func = cb_func; |
69 |
|
|
(*t) = reinterpret_cast<sys_timer*>(timer); |
70 |
|
|
return true; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
void sys_delete_timer(sys_timer t) |
74 |
|
|
{ |
75 |
|
|
sys_timer_struct * timer = reinterpret_cast<sys_timer_struct *>(t); |
76 |
|
|
|
77 |
|
|
if (timer->timerID) |
78 |
|
|
timeKillEvent(timer->timerID); |
79 |
|
|
timeEndPeriod(timer->timerRes); |
80 |
|
|
delete timer; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
static inline long long int toMSecs(time_t secs, long int nanosecs) |
84 |
|
|
{ |
85 |
|
|
return secs * 1000 + (nanosecs + 500*1000) / (1000*1000); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
void CALLBACK TimeProc(UINT uID, UINT UMsg, DWORD dwUser, DWORD dw1, DWORD dw2) |
89 |
|
|
{ |
90 |
|
|
sys_timer t = reinterpret_cast<sys_timer>(dwUser); |
91 |
|
|
sys_timer_struct * timer = reinterpret_cast<sys_timer_struct *>(dwUser); |
92 |
|
|
timer->timerID = 0; |
93 |
|
|
timer->cb_func(t); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
void sys_set_timer(sys_timer t, time_t secs, long int nanosecs, bool periodic) |
97 |
|
|
{ |
98 |
|
|
sys_timer_struct * timer = reinterpret_cast<sys_timer_struct *>(t); |
99 |
|
|
UINT msecs = toMSecs(secs, nanosecs); |
100 |
|
|
if (msecs == 0) { |
101 |
|
|
timer->cb_func(t); |
102 |
|
|
} else { |
103 |
|
|
if (timer->timerID) timeKillEvent(timer->timerID); |
104 |
|
|
timer->timerID = timeSetEvent(msecs, timer->timerRes, TimeProc, reinterpret_cast<DWORD>(timer), (periodic) ? TIME_PERIODIC : TIME_ONESHOT); |
105 |
|
|
if (!timer->timerID) { |
106 |
|
|
ht_printf("baeh! %d %d \n", msecs, timer->timerRes); |
107 |
|
|
exit(-1); |
108 |
|
|
} |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
uint64 sys_get_timer_resolution(sys_timer t) |
113 |
|
|
{ |
114 |
|
|
sys_timer_struct * timer = reinterpret_cast<sys_timer_struct *>(t); |
115 |
|
|
return timer->timerRes; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
uint64 sys_get_hiresclk_ticks() |
119 |
|
|
{ |
120 |
|
|
uint64 counter; |
121 |
|
|
static uint64 lastCounter = 0; |
122 |
|
|
static uint64 counterBase = 0; |
123 |
|
|
// FIXME: make a mutex around here |
124 |
|
|
QueryPerformanceCounter((_LARGE_INTEGER *)&counter); |
125 |
|
|
if (counter < lastCounter) { |
126 |
|
|
// overflow |
127 |
|
|
counterBase += lastCounter; |
128 |
|
|
} |
129 |
|
|
lastCounter = counter; |
130 |
|
|
return counter; // + counterBase; |
131 |
|
|
// FIXME: mutex until here |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
uint64 sys_get_hiresclk_ticks_per_second() |
135 |
|
|
{ |
136 |
|
|
uint64 frq; |
137 |
|
|
QueryPerformanceFrequency((_LARGE_INTEGER *)&frq); |
138 |
|
|
return frq; |
139 |
|
|
} |