]> git.mxchange.org Git - flightgear.git/blob - Time/event.c
Tweaks to help building with MSVC++
[flightgear.git] / Time / event.c
1 /**************************************************************************
2  * event.c -- Flight Gear periodic event scheduler
3  *
4  * Written by Curtis Olson, started December 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #include <string.h>
28 #include <stdio.h>
29
30 #ifdef USE_FTIME
31 #  include <stdlib.h>
32 #  include <sys/timeb.h> /* for ftime() and struct timeb */
33 #else
34 #  include <sys/time.h>  /* for get/setitimer, gettimeofday, struct timeval */
35 #endif /* USE_FTIME */
36
37
38 #include "event.h"
39
40
41 #define MAX_EVENTS 100    /* size of event table */
42 #define MAX_RUN_QUEUE 100 /* size of run queue */
43
44
45 struct fgEVENT {
46     char description[256];
47
48     void (*event)();  /* pointer to function */
49     int status;       /* status flag */
50
51     long interval;    /* interval in ms between each iteration of this event */
52                       
53 #ifdef USE_FTIME
54     struct timeb last_run;    /* absolute time for last run */
55     struct timeb current;     /* current time */
56     struct timeb next_run;    /* absolute time for next run */
57 #else
58     struct timeval last_run;  /* absolute time for last run */
59     struct timeval current;   /* current time */
60     struct timeval next_run;  /* absolute time for next run */
61     struct timezone tz;
62 #endif /* USE_FTIME */
63
64     long cum_time;    /* cumulative processor time of this event */
65     long min_time;    /* time of quickest execution */
66     long max_time;    /* time of slowest execution */
67     long count;       /* number of times executed */
68 };
69
70
71 /* Event table */
72 struct fgEVENT events[MAX_EVENTS];
73 int event_ptr;
74
75
76 /* Run Queue */
77 int queue[MAX_RUN_QUEUE];
78 int queue_front;
79 int queue_end;
80
81
82 /* initialize the run queue */
83 void initq() {
84     queue_front = queue_end = 0;
85 }
86
87
88 /* return queue empty status */
89 int emptyq() {
90     if ( queue_front == queue_end ) {
91         return(1);
92     } else {
93         return(0);
94     }
95 }
96
97
98 /* return queue full status */
99 int fullq() {
100     if ( (queue_end + 1) % MAX_RUN_QUEUE == queue_front ) {
101         return(1);
102     } else {
103         return(0);
104     }
105 }
106
107
108 /* add a member to the back of the queue */
109 void addq(int ptr) {
110     if ( !fullq() ) {
111         queue[queue_end] = ptr;
112         events[ptr].status = FG_EVENT_QUEUED;
113
114         queue_end = (queue_end + 1) % MAX_RUN_QUEUE;
115     } else {
116         printf("RUN QUEUE FULL!!!\n");
117     }
118
119     /* printf("Queued function %d (%d %d)\n", ptr, queue_front, queue_end); */
120 }
121
122
123 /* remove a member from the front of the queue */
124 int popq() {
125     int ptr;
126
127     if ( !emptyq() ) {
128         ptr = queue[queue_front];
129         /* printf("Popped position %d = %d\n", queue_front, ptr); */
130         queue_front = (queue_front + 1) % MAX_RUN_QUEUE;
131         return(ptr);
132     } else {
133         printf("PANIC:  RUN QUEUE IS EMPTY!!!\n");
134         exit(0);
135     }
136 }
137
138
139 /* run a specified event */
140 void fgEventRun(int ptr) {
141     struct fgEVENT *e;
142     long duration;
143
144     e = &events[ptr];
145     
146     printf("Running %s\n", e->description);
147
148     /* record starting time */
149 #ifdef USE_FTIME
150     ftime(&e->last_run);
151 #else
152     gettimeofday(&e->last_run, &e->tz);
153 #endif /* USE_FTIME */
154
155     /* run the event */
156     (*e->event)();
157
158     /* increment the counter for this event */
159     e->count++;
160
161     /* update the event status */
162     e->status = FG_EVENT_READY;
163
164     /* calculate duration and stats */
165 #ifdef USE_FTIME
166     ftime(&e->current);
167     duration = 1000 * (e->current.time - e->last_run.time) + 
168         (e->current.millitm - e->last_run.millitm);
169 #else
170     gettimeofday(&e->current, &e->tz);
171     duration = 1000000 * (e->current.tv_sec - e->last_run.tv_sec) + 
172         (e->current.tv_usec - e->last_run.tv_usec);
173     duration /= 1000;  /* convert back to milleseconds */
174 #endif /* USE_FTIME */
175
176     e->cum_time += duration;
177
178     if ( duration < e->min_time ) {
179         e->min_time = duration;
180     }
181
182     if ( duration > e->max_time ) {
183         e->max_time = duration;
184     }
185
186     /* determine the next absolute run time */
187 #ifdef USE_FTIME
188     e->next_run.time = e->last_run.time + 
189         (e->last_run.millitm + e->interval) / 1000;
190     e->next_run.millitm = (e->last_run.millitm + e->interval) % 1000;
191 #else
192     e->next_run.tv_sec = e->last_run.tv_sec +
193         (e->last_run.tv_usec + e->interval * 1000) / 1000000;
194     e->next_run.tv_usec = (e->last_run.tv_usec + e->interval * 1000) % 1000000;
195 #endif /* USE_FTIME */
196
197 }
198
199
200 /* Initialize the scheduling subsystem */
201 void fgEventInit() {
202     printf("Initializing event manager\n");
203     event_ptr = 0;
204     initq();
205 }
206
207
208 /* Register an event with the scheduler, returns a pointer into the
209  * event table */
210 int fgEventRegister(char *desc, void (*event)(), int status, int interval) {
211     struct fgEVENT *e;
212
213     e = &events[event_ptr];
214
215     printf("Registering event: %s\n", desc);
216
217     if ( strlen(desc) < 256 ) {
218         strcpy(e->description, desc);
219     } else {
220         strncpy(e->description, desc, 255);
221         e->description[255] = '\0';
222     }
223
224     e->event = event;
225     e->status = status;
226     e->interval = interval;
227
228     e->cum_time = 0;
229     e->min_time = 100000;
230     e->max_time = 0;
231     e->count = 0;
232
233     /* Actually run the event */
234     fgEventRun(event_ptr);
235
236     event_ptr++;
237
238     return(event_ptr - 1);
239 }
240
241
242 /* Update the scheduling parameters for an event */
243 void fgEventUpdate() {
244 }
245
246
247 /* Delete a scheduled event */
248 void fgEventDelete() {
249 }
250
251
252 /* Temporarily suspend scheduling of an event */
253 void fgEventSuspend() {
254 }
255
256
257 /* Resume scheduling and event */
258 void fgEventResume() {
259 }
260
261
262 /* Dump scheduling stats */
263 void fgEventPrintStats() {
264     int i;
265
266     if ( event_ptr > 0 ) {
267         printf("\n");
268         printf("Event Stats\n");
269         printf("-----------\n");
270
271         for ( i = 0; i < event_ptr; i++ ) {
272             printf("  %-20s  int=%.2fs cum=%ld min=%ld max=%ld count=%ld ave=%.2f\n",
273                    events[i].description, 
274                    events[i].interval / 1000.0,
275                    events[i].cum_time, 
276                    events[i].min_time, events[i].max_time, events[i].count, 
277                    events[i].cum_time / (double)events[i].count);
278         }
279         printf("\n");
280     }
281 }
282
283
284 /* Add pending jobs to the run queue and run the job at the front of
285  * the queue */
286 void fgEventProcess() {
287 #ifdef USE_FTIME
288     struct timeb current;
289 #else
290     struct timeval current;
291     struct timezone tz;
292 #endif /* USE_FTIME */
293     int i;
294
295     printf("Processing events\n");
296     
297     /* get the current time */
298 #ifdef USE_FTIME
299     ftime(&current);
300 #else
301     gettimeofday(&current, &tz);
302 #endif /* USE_FTIME */
303
304     /* printf("Checking if anything is ready to move to the run queue\n"); */
305
306     /* see if anything else is ready to be placed on the run queue */
307     for ( i = 0; i < event_ptr; i++ ) {
308         if ( events[i].status == FG_EVENT_READY ) {
309 #ifdef USE_FTIME
310             if ( current.time > events[i].next_run.time ) {
311                 addq(i);
312             } else if ( (current.time == events[i].next_run.time) && 
313                         (current.millitm >= events[i].next_run.millitm) ) {
314                 addq(i);
315             }
316 #else
317             if ( current.tv_sec > events[i].next_run.tv_sec ) {
318                 addq(i);
319             } else if ( (current.tv_sec == events[i].next_run.tv_sec) && 
320                         (current.tv_usec >= events[i].next_run.tv_usec) ) {
321                 addq(i);
322             }
323
324 #endif /* USE_FTIME */
325         }
326     }
327
328     /* Checking to see if there is anything on the run queue */
329     /* printf("Checking to see if there is anything on the run queue\n"); */
330     if ( !emptyq() ) {
331         /* printf("Yep, running it\n"); */
332         i = popq();
333         fgEventRun(i);
334     }
335 }
336
337
338 /* $Log$
339 /* Revision 1.5  1998/01/06 01:20:27  curt
340 /* Tweaks to help building with MSVC++
341 /*
342  * Revision 1.4  1997/12/31 17:46:50  curt
343  * Tweaked fg_time.c to be able to use ftime() instead of gettimeofday()
344  *
345  * Revision 1.3  1997/12/30 22:22:42  curt
346  * Further integration of event manager.
347  *
348  * Revision 1.2  1997/12/30 20:47:58  curt
349  * Integrated new event manager with subsystem initializations.
350  *
351  * Revision 1.1  1997/12/30 04:19:22  curt
352  * Initial revision.
353  *
354  */