chiark / gitweb /
data structure for features
[trains.git] / hostside / eventhelp.c
1 /*
2  * daemons
3  * helpers for event handling
4  */
5
6 #include <assert.h>
7 #include <limits.h>
8
9 #include "daemons.h"
10
11 oop_source *events;
12
13 static void *toev_callback(oop_source *source, struct timeval tv, void *t_v) {
14   TimeoutEvent *toev= t_v;
15   toev->running= 0;
16   toev->callback(toev);
17   return OOP_CONTINUE;
18 }
19
20 void toev_init(TimeoutEvent *toev) { toev->running= 0; }
21
22 void toev_start(TimeoutEvent *toev) {
23   toev_stop(toev);
24   if (toev->duration==-1) return;
25   toev->running= 1;
26   mgettimeofday(&toev->abs);
27   assert(toev->duration < INT_MAX/1000);
28   toev->abs.tv_usec += toev->duration * 1000;
29   toev->abs.tv_sec += toev->abs.tv_usec / 1000000;
30   toev->abs.tv_usec %= 1000000;
31   events->on_time(events, toev->abs, toev_callback, toev);
32 }
33
34 void toev_stop(TimeoutEvent *toev) {
35   if (!toev->running) return;
36   toev->running= 0;
37   events->cancel_time(events, toev->abs, toev_callback, toev);
38 }