/
timer.pmc
369 lines (262 loc) · 7.63 KB
/
timer.pmc
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
Copyright (C) 2001-2003, The Perl Foundation.
$Id$
=head1 NAME
src/pmc/timer.pmc - Timer
=head1 SYNOPSIS
new P0, .Timer
set P0[.PARROT_TIMER_SEC], I_seconds # whole seconds
set P0[.PARROT_TIMER_USEC], I_micro seconds # and/or micro seconds
set P0[.PARROT_TIMER_NSEC], N_seconds_frac # or fraction in seconds
set P0[.PARROT_TIMER_REPEAT], I_repeat # 0 = one shot ...
set P0[.PARROT_TIMER_HANDLER], P_sub # set handler sub PMC
invoke P0 # start timer
set P0[.PARROT_TIMER_RUNNING], 1 # same
new P0, .Timer, P1 # set everything
set I0, P0[.PARROT_TIMER_SEC] # query current timer status
set N0, P0[.PARROT_TIMER_NSEC]
...
set P0[.PARROT_TIMER_RUNNING], 0 # turn timer off
=head1 DESCRIPTION
This is the Timer base class
Running timers are kept in a linked list. Each timer has a tick count,
which gets decremented if the system timer expires. If the tick count
reaches zero, the timer handler gets invoked via Parrot's event handling
code.
The Timer resolution is operating system dependent. It's only guaranteed
that the Timer will fire some time after the programmed interval.
The Timer stops after invoking the handler (repeat+1) times. To create a
Timer that will run forever, set "repeat" to -1. Turning the Timer off
preserves set values; the Timer is not destroyed.
When setting both C<PARROT_TIMER_SEC> and C<PARROT_TIMER_USEC> it must
be done in that sequence, whole seconds first. If a timer is constructed
with an initializer and it should be run immediately, the
C<PARROT_TIMER_RUNNING> value has to be in the last key of the
initializer.
=head2 Functions
=over 4
=cut
*/
#include "parrot/parrot.h"
/* interface constants */
/* &gen_from_enum(timer.pasm) */
typedef enum {
PARROT_TIMER_SEC,
PARROT_TIMER_USEC,
PARROT_TIMER_NSEC,
PARROT_TIMER_REPEAT,
PARROT_TIMER_RUNNING,
PARROT_TIMER_HANDLER,
PARROT_TIMER_MAX
} parrot_timer_enum_t;
/* &end_gen */
/*
=item C<static void
add_timer(Interp* interp, PMC *pmc)>
Adds a new timer event to the event queue.
=cut
*/
static void
add_timer(Interp* interp, PMC *pmc)
{
parrot_timer_event *t = PMC_struct_val(pmc);
if (!PMC_IS_NULL(t->sub))
Parrot_new_timer_event(interp, pmc, t->abs_time,
t->interval, t->repeat, t->sub, EVENT_TYPE_TIMER);
}
/*
=item C<static void
del_timer(Interp* interp, PMC *pmc)>
Deletes the timer event.
=cut
*/
static void
del_timer(Interp* interp, PMC *pmc)
{
Parrot_del_timer_event(interp, pmc);
}
pmclass Timer does event {
/*
=item C<void init()>
Initializes the timer.
=cut
*/
void init() {
/*
* t->abs_time actually is relative, its converted to an
* absolute time, when the timer is started
*/
parrot_timer_event *t =
mem_sys_allocate_zeroed(sizeof (parrot_timer_event));
PMC_struct_val(SELF) = t;
PObj_active_destroy_SET(SELF);
++INTERP->arena_base->num_early_DOD_PMCs;
}
/*
=item C<void init_pmc(PMC *init)>
See F<docs/pdds/pdd02_vtables.pod> for initializers.
=cut
*/
void init_pmc(PMC *init) {
INTVAL n = VTABLE_get_integer(INTERP, init);
INTVAL i;
INTVAL val;
FLOATVAL nval;
PMC *pval;
SELF.init();
for (i = 0; i < n; i += 2) {
INTVAL key = VTABLE_get_integer_keyed_int(INTERP, init, i);
switch (key) {
case PARROT_TIMER_SEC:
case PARROT_TIMER_USEC:
case PARROT_TIMER_REPEAT:
case PARROT_TIMER_RUNNING:
val =
VTABLE_get_integer_keyed_int(INTERP, init, i+1);
SELF.set_integer_keyed_int(key, val);
break;
case PARROT_TIMER_NSEC:
nval =
VTABLE_get_number_keyed_int(INTERP, init, i+1);
SELF.set_number_keyed_int(key, nval);
break;
case PARROT_TIMER_HANDLER:
pval =
VTABLE_get_pmc_keyed_int(INTERP, init, i+1);
SELF.set_pmc_keyed_int(key, pval);
break;
}
}
}
/*
=item C<void destroy()>
Destroys the timer.
=cut
*/
void destroy() {
parrot_timer_event *t = PMC_struct_val(SELF);
del_timer(INTERP, SELF);
mem_sys_free(t);
}
/*
=item C<INTVAL get_integer_keyed_int(INTVAL key)>
Returns the timer info for C<key>.
=cut
*/
INTVAL get_integer_keyed_int(INTVAL key) {
parrot_timer_event *t = PMC_struct_val(SELF);
switch (key) {
case PARROT_TIMER_SEC:
return (INTVAL)t->abs_time;
case PARROT_TIMER_USEC:
return (INTVAL)((t->abs_time - (INTVAL)t->abs_time) *1000000.0);
case PARROT_TIMER_REPEAT:
return (INTVAL) t->repeat;
case PARROT_TIMER_RUNNING:
return 0;
}
return -1;
}
/*
=item C<PMC *get_pmc_keyed_int(INTVAL key)>
Returns the PMC associated with C<key>.
=cut
*/
PMC* get_pmc_keyed_int(INTVAL key) {
parrot_timer_event *t = PMC_struct_val(SELF);
switch (key) {
case PARROT_TIMER_HANDLER:
return t->sub;
}
return PMCNULL;
}
/*
=item C<FLOATVAL get_number_keyed_int(INTVAL key)>
Returns the number associated with C<key>.
=cut
*/
FLOATVAL get_number_keyed_int(INTVAL key) {
parrot_timer_event *t = PMC_struct_val(SELF);
switch (key) {
case PARROT_TIMER_NSEC:
return t->abs_time;
}
return -1.0;
}
/*
=item C<void set_integer_keyed_int(INTVAL key, INTVAL value)>
Sets the value associated with C<key> to C<value>.
=cut
*/
void set_integer_keyed_int(INTVAL key, INTVAL value) {
parrot_timer_event *t = PMC_struct_val(SELF);
switch (key) {
case PARROT_TIMER_SEC:
t->abs_time = value;
break;
case PARROT_TIMER_USEC:
t->abs_time += value / 1000000.0;
break;
case PARROT_TIMER_REPEAT:
t->repeat = value;
break;
case PARROT_TIMER_RUNNING:
if (value) {
add_timer(INTERP, SELF);
}
else {
del_timer(INTERP, SELF);
}
break;
}
}
/*
=item C<void set_pmc_keyed_int(INTVAL key, PMC *value)>
Sets the PMC associated with C<key> to C<*value>.
=cut
*/
void set_pmc_keyed_int(INTVAL key, PMC* value) {
parrot_timer_event *t = PMC_struct_val(SELF);
switch (key) {
case PARROT_TIMER_HANDLER:
t->sub = value;
break;
}
}
/*
=item C<void *invoke(void *next)>
Adds the timer to the event queue.
=cut
*/
void* invoke(void *next) {
add_timer(INTERP, SELF);
return next;
}
/*
=item C<void set_number_keyed_int(INTVAL key, FLOATVAL value)>
Sets the floating-point value associated with C<key> to C<value>.
=cut
*/
void set_number_keyed_int(INTVAL key, FLOATVAL value) {
parrot_timer_event *t = PMC_struct_val(SELF);
switch (key) {
case PARROT_TIMER_NSEC:
t->abs_time = value;
break;
}
}
}
/*
=back
=head1 HISTORY
Initial proposal by leo 2003.07.15
Initial revision 2003.07.17
Switch to events 2004.01.07
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/