File: timer.cpp

package info (click to toggle)
btanks 0.9.8083-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 43,488 kB
  • ctags: 6,859
  • sloc: cpp: 46,422; ansic: 12,005; xml: 4,262; python: 313; makefile: 15; sh: 13
file content (173 lines) | stat: -rw-r--r-- 5,069 bytes parent folder | download | duplicates (5)
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
/* sdlx - c++ wrapper for libSDL
 * Copyright (C) 2005-2007 Vladimir Menshakov
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "timer.h"
#include "mrt/ioexception.h"

#ifdef _WINDOWS
#	include <windows.h>
#elif defined __APPLE__
#	include <errno.h>
#else
#	include <time.h>
#	include <errno.h>

static clockid_t clock_id = CLOCK_REALTIME;

#endif

using namespace sdlx;

Timer::Timer() {
#ifdef _WINDOWS
#ifdef SDLX_TIMER_USES_QPC
	tm = new LARGE_INTEGER;
	freq = new LARGE_INTEGER;
	if (!QueryPerformanceFrequency(freq)) 
		throw_ex(("QueryPerformanceFrequency failed"));
#elif defined __APPLE__
	//some stupid initialization
#else 
	TIMECAPS caps;
	if (timeGetDevCaps(&caps, sizeof(caps)) != TIMERR_NOERROR) 
		throw_ex(("timeGetDevCaps failed"));
	res = caps.wPeriodMin;
	LOG_DEBUG(("minimum timer's period: %d", res));
#endif
#endif
}

Timer::~Timer() {
#ifdef _WINDOWS
#ifdef SDLX_TIMER_USES_QPC
	delete tm; delete freq;
#endif
#endif
}


void Timer::reset() {
#ifdef _WINDOWS
#	ifdef SDLX_TIMER_USES_QPC
	if (!QueryPerformanceCounter(tm)) 
		throw_ex(("QueryPerformanceCounter failed"));
#	else 
	if (timeBeginPeriod(res) != TIMERR_NOERROR)
		throw_ex(("timeBeginPeriod(%d) failed", res));
	tm = timeGetTime();
	if (timeEndPeriod(res) != TIMERR_NOERROR)
		throw_ex(("timeEndPeriod(%d) failed", res));
#	endif
#elif defined __APPLE__
	if (gettimeofday(&tv, NULL) == -1)
		throw_io(("gettimeofday"));
#else
	if (clock_gettime(clock_id, &tm) != 0)
		throw_io(("clock_gettime"));
#endif
}

const int Timer::microdelta() const {
#ifdef _WINDOWS
#	ifdef SDLX_TIMER_USES_QPC
	LARGE_INTEGER now;
	if (!QueryPerformanceCounter(&now)) 
		throw_ex(("QueryPerformanceCounter failed"));
	//LOG_DEBUG(("%I64d - %I64d = %I64d, freq: %I64d", now.QuadPart, tm->QuadPart, (now.QuadPart - tm->QuadPart), freq->QuadPart));
	return (now.QuadPart - tm->QuadPart) * 1000000 / freq->QuadPart;
#	else
	if (timeBeginPeriod(res) != TIMERR_NOERROR)
		throw_ex(("timeBeginPeriod(%d) failed", res));
	int now = timeGetTime();
	if (timeEndPeriod(res) != TIMERR_NOERROR)
		throw_ex(("timeEndPeriod(%d) failed", res));
	return 1000 * (now - tm);
#	endif
#elif defined __APPLE__
	struct timeval now;
	if (gettimeofday(&now, NULL) == -1)
		throw_io(("gettimeofday"));
	return ((int)now.tv_sec - (int)tv.tv_sec) *1000000 + (int)now.tv_usec - (int)tv.tv_usec;
#else
	struct timespec now;
	if (clock_gettime(clock_id, &now) != 0)
		throw_io(("clock_gettime"));
	return ((int)now.tv_sec - (int)tm.tv_sec) *1000000 + ((int)now.tv_nsec - (int)tm.tv_nsec) / 1000;
#endif
}


void Timer::microsleep(const char *why, const int micros) {
#ifdef _WINDOWS
	timeBeginPeriod(1);
	//LOG_DEBUG(("microsleep('%s', %d)", why, micros));

	/*
	LARGE_INTEGER t1, t2, freq;

	bool done = false;
	QueryPerformanceFrequency(&freq);
	QueryPerformanceCounter(&t1);
	do {
		QueryPerformanceCounter(&t2);
                
		int ticks_passed = (int)((__int64)(t2.QuadPart) - (__int64)(t1.QuadPart));
		int ticks_left = micros - ticks_passed;

		if (t2.QuadPart < t1.QuadPart)    // time wrap
			done = true;

        if (ticks_passed >= micros)
        	done = true;
                
		if (!done) {
			// if > 0.002s left, do Sleep(1), which will actually sleep some 
            //   steady amount, probably 1-2 ms,
            //   and do so in a nice way (cpu meter drops; laptop battery spared).
            // otherwise, do a few Sleep(0)'s, which just give up the timeslice,
            //   but don't really save cpu or battery, but do pass a tiny
            //   amount of time.
            if (ticks_left > (int)(freq.QuadPart) * 2 / 1000)
     			Sleep(1);
            else                        
               //for (int i=0; i < 10; i++) 
                            Sleep(0);  // causes thread to give up its timeslice
		}
	} while (!done);        
	*/
	Sleep(micros / 1000);
	
	timeEndPeriod(1);
#else 
	struct timespec ts, rem;
	
	ts.tv_sec = micros / 1000000;
	ts.tv_nsec = (micros % 1000000) * 1000;
	
	do {
		//LOG_DEBUG(("nanosleep(%s, %u.%u)", why, (unsigned)ts.tv_sec, (unsigned)ts.tv_nsec));
		int r = ::nanosleep(&ts, &rem);
		if (r == 0) 
			return;
		
		if (r == -1 && errno != EINTR)
			throw_io(("nanosleep(%s, %u.%u, %u.%u)", why, (unsigned)ts.tv_sec, (unsigned)ts.tv_nsec, (unsigned)rem.tv_sec, (unsigned)rem.tv_nsec));
		ts = rem;
	} while (rem.tv_nsec != 0 || rem.tv_sec != 0);
#endif
}