chiark / gitweb /
realtime: movpos: debug output: exclude some more stuff from the default movpos output
[trains.git] / hostside / eventhelp.c
1 /*
2  * realtime
3  * helpers for event handling
4  */
5
6 #include <assert.h>
7 #include <limits.h>
8
9 #include "realtime.h"
10
11 const char toev_fast_pclass[]= "fast";
12
13 void real_mgettimeofday(struct timeval *tv) {
14   int r;
15   r= gettimeofday(tv,0);
16   if (r) diee("gettimeofday failed");
17 }
18
19 static int toev_sim_show(TimeoutEvent *toev) {
20   return simlog_full || toev->pclass!=toev_fast_pclass;
21 }
22
23 void *toev_callback(oop_source *source, struct timeval tv, void *t_v) {
24   TimeoutEvent *toev= t_v;
25   if (toev_sim_show(toev)) {
26     simlog("timer-event %s.%s\n",toev->pclass,toev->pinst);
27     simlog_flush();
28   }
29   toev->running= 0;
30   toev->callback(toev);
31   return OOP_CONTINUE;
32 }
33
34 void toev_init(TimeoutEvent *toev) { toev->running= 0; }
35
36 void toev_start(TimeoutEvent *toev) {
37   toev_stop(toev);
38   if (toev->duration==-1) return;
39   toev->running= 1;
40   if (toev_sim_show(toev)) mgettimeofday(&toev->abs);
41   else real_mgettimeofday(&toev->abs);
42   assert(toev->duration < INT_MAX/1000);
43   toev->abs.tv_usec += toev->duration * 1000;
44   toev->abs.tv_sec += toev->abs.tv_usec / 1000000;
45   toev->abs.tv_usec %= 1000000;
46   if (simulate) sim_toev_start(toev);
47   else events->on_time(events, toev->abs, toev_callback, toev);
48 }
49
50 void toev_stop(TimeoutEvent *toev) {
51   if (!toev->running) return;
52   toev->running= 0;
53   if (simulate) sim_toev_stop(toev);
54   else events->cancel_time(events, toev->abs, toev_callback, toev);
55 }
56
57 void mgettimeofday(struct timeval *tv) {
58   if (simulate) {
59     sim_mgettimeofday(tv);
60   } else {
61     real_mgettimeofday(tv);
62   }
63   simlog("timestamp %ld.%06ld\n",tv->tv_sec,tv->tv_usec);
64   simlog_flush();
65 }