aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/flext/source/fltimer.cpp
blob: dffe1eb6170947a565d7306bdbfbef967fc02ba7 (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* 

flext - C++ layer for Max/MSP and pd (pure data) externals

Copyright (c) 2001-2003 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

/*! \file fltimer.cpp
	\brief flext timer functions and classes   
*/

#include "flext.h"

#if FLEXT_OS == FLEXT_OS_WIN
#include <windows.h>
#elif FLEXT_OS == FLEXT_OS_LINUX || FLEXT_OS == FLEXT_OS_IRIX || FLEXT_OSAPI == FLEXT_OSAPI_MAC_MACH
#include <unistd.h>
#include <sys/time.h>
#elif FLEXT_OS == FLEXT_OS_MAC
#include <Timer.h>
#include <Threads.h>
#endif



double flext::GetTime()
{
#if FLEXT_SYS == FLEXT_SYS_PD
	return clock_gettimesince(0)*0.001;
#elif FLEXT_SYS == FLEXT_SYS_MAX
	double tm;
	clock_getftime(&tm);
	return tm*0.001;
#else
	#error Not implemented
#endif
}

double flext::GetTimeGrain()
{
#if FLEXT_SYS == FLEXT_SYS_PD
	return 0;
#elif FLEXT_SYS == FLEXT_SYS_MAX
	return 0.001;
#else
	#error Not implemented
#endif
}

static double getstarttime();
static double starttime = getstarttime();

static double getstarttime()
{
	starttime = 0;
	return flext::GetOSTime();
}

double flext::GetOSTime()
{
	double tm;

#if FLEXT_OS == FLEXT_OS_WIN
    LARGE_INTEGER frq,cnt;
    if(QueryPerformanceFrequency(&frq) && QueryPerformanceCounter(&cnt))
        tm = (double)(cnt.QuadPart)/frq.QuadPart;
    else {
        SYSTEMTIME systm;
        FILETIME fltm;
        GetSystemTime(&systm);
        SystemTimeToFileTime(&systm,&fltm);
        tm = (double)((LARGE_INTEGER *)&fltm)->QuadPart*0.001;
    }
#elif FLEXT_OS == FLEXT_OS_LINUX || FLEXT_OS == FLEXT_OS_IRIX || FLEXT_OSAPI == FLEXT_OSAPI_MAC_MACH // POSIX
	timeval tmv;
	gettimeofday(&tmv,NULL);
	tm = tmv.tv_sec+tmv.tv_usec*1.e-6;
#elif FLEXT_OS == FLEXT_OS_MAC // that's just for OS9 & Carbon!
	UnsignedWide tick;
	Microseconds(&tick);
	tm = (tick.hi*((double)(1L<<((sizeof tick.lo)*4))*(double)(1L<<((sizeof tick.lo)*4)))+tick.lo)*1.e-6; 
#else
	#error Not implemented
#endif
	return tm-starttime;
}

void flext::Sleep(double s)
{
#if FLEXT_OS == FLEXT_OS_WIN
	::Sleep((long)(s*1000.));
#elif FLEXT_OS == FLEXT_OS_LINUX || FLEXT_OS == FLEXT_OS_IRIX || FLEXT_OSAPI == FLEXT_OSAPI_MAC_MACH // POSIX
	usleep((long)(s*1000000.));
#elif FLEXT_OS == FLEXT_OS_MAC // that's just for OS9 & Carbon!
	UnsignedWide tick;
	Microseconds(&tick);
	double target = tick.hi*((double)(1L<<((sizeof tick.lo)*4))*(double)(1L<<((sizeof tick.lo)*4)))+tick.lo+s*1.e6; 
	for(;;) {
		// this is just a loop running until the time has passed - stone age (but we yield at least)
		Microseconds(&tick);
		if(target <= tick.hi*((double)(1L<<((sizeof tick.lo)*4))*(double)(1L<<((sizeof tick.lo)*4)))+tick.lo) break;
		YieldToAnyThread(); // yielding surely reduces the timing precision (but we're civilized)
	}
#else
	#error Not implemented
#endif
}


/* \param qu determines whether timed messages should be queued (low priority - only when supported by the system).
*/
flext::Timer::Timer(bool qu):
	queued(qu),
	clss(NULL),userdata(NULL),
	period(0)
{
#if FLEXT_SYS == FLEXT_SYS_PD
	clk = (t_clock *)clock_new(this,(t_method)callback);
#elif FLEXT_SYS == FLEXT_SYS_MAX
	clk = (t_clock *)clock_new(this,(t_method)callback);
	if(queued) qelem = (t_qelem *)qelem_new(this,(method)queuefun);
#else
	#error Not implemented
#endif
}

flext::Timer::~Timer()
{
#if FLEXT_SYS == FLEXT_SYS_PD
	clock_free(clk);
#elif FLEXT_SYS == FLEXT_SYS_MAX
	clock_free(clk);
	if(queued) qelem_free(qelem);
#else
	#error Not implemented
#endif
}

bool flext::Timer::Reset()
{
#if FLEXT_SYS == FLEXT_SYS_PD
	clock_unset(clk);
#elif FLEXT_SYS == FLEXT_SYS_MAX
	clock_unset(clk);
	if(queued) qelem_unset(qelem);
#else
	#error Not implemented
#endif
	return true;
}

/*! \param tm absolute time (in seconds)
	\param data user data
	\param dopast if set events with times lying in the past will be triggered immediately, if not set they are ignored
	\return true on success
*/
bool flext::Timer::At(double tm,void *data,bool dopast)
{
	userdata = data;
	period = 0;
#if FLEXT_SYS == FLEXT_SYS_PD 
	const double ms = tm*1000.;
	if(dopast || clock_gettimesince(ms) <= 0)
		clock_set(clk,ms);
#elif FLEXT_SYS == FLEXT_SYS_MAX
	const double ms = tm*1000.;
	double cur;
	clock_getftime(&cur);
	if(cur <= ms)
		clock_fdelay(clk,ms-cur);
	else if(dopast) // trigger timer is past
		clock_fdelay(clk,0);
#else
	#error Not implemented
#endif
	return true;
}

/*! \param tm relative time (in seconds)
	\param data user data
	\return true on success
*/
bool flext::Timer::Delay(double tm,void *data)
{
	userdata = data;
	period = 0;
#if FLEXT_SYS == FLEXT_SYS_PD 
	clock_delay(clk,tm*1000);
#elif FLEXT_SYS == FLEXT_SYS_MAX
	clock_fdelay(clk,tm*1000.);
#else
	#error Not implemented
#endif
	return true;
}

/*! \param tm relative time between periodic events (in seconds)
	\param data user data
	\return true on success
	\note the first event will be delayed by tm
*/
bool flext::Timer::Periodic(double tm,void *data) 
{
	userdata = data;
	period = tm;
#if FLEXT_SYS == FLEXT_SYS_PD 
	clock_delay(clk,tm*1000);
#elif FLEXT_SYS == FLEXT_SYS_MAX
	clock_fdelay(clk,tm*1000.);
#else
	#error Not implemented
#endif
	return true;
}

/*! \brief Callback function for system clock.
	\todo Make periodic events scheduled as such.
*/
void flext::Timer::callback(Timer *tmr)
{
	if(tmr->period) {
		// clearly it would be more precise if the periodic event is scheduled as such
		// and not retriggered every time
#if FLEXT_SYS == FLEXT_SYS_PD 
		clock_delay(tmr->clk,tmr->period*1000);
#elif FLEXT_SYS == FLEXT_SYS_MAX
		clock_fdelay(tmr->clk,tmr->period*1000.);
#else
	#error Not implemented
#endif
	}

	if(tmr->cback) {
#if FLEXT_SYS == FLEXT_SYS_MAX
		if(tmr->queued) 
			qelem_set(tmr->qelem);
		else
#endif
			tmr->Work();
	}
}

#if FLEXT_SYS == FLEXT_SYS_MAX
/*! \brief Callback function for low priority clock (for queued messages).
*/
void flext::Timer::queuefun(Timer *tmr) { tmr->Work(); }
#endif

/*! \brief Virtual worker function - by default it calls the user callback function.
	\remark The respective callback parameter format is chosen depending on whether clss is defined or not.
*/
void flext::Timer::Work()
{
	if(clss) 
		((bool (*)(flext_base *,void *))cback)(clss,userdata);
	else
		cback(userdata);
}