1 /**************************************************************************
2 * event.c -- Flight Gear periodic event scheduler
4 * Written by Curtis Olson, started December 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
32 # include <sys/timeb.h> /* for ftime() and struct timeb */
34 # include <sys/time.h> /* for get/setitimer, gettimeofday, struct timeval */
35 #endif /* USE_FTIME */
41 #define MAX_EVENTS 100 /* size of event table */
42 #define MAX_RUN_QUEUE 100 /* size of run queue */
46 char description[256];
48 void (*event)( void ); /* pointer to function */
49 int status; /* status flag */
51 long interval; /* interval in ms between each iteration of this event */
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 */
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 */
62 #endif /* USE_FTIME */
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 */
72 struct fgEVENT events[MAX_EVENTS];
77 int queue[MAX_RUN_QUEUE];
82 /* initialize the run queue */
84 queue_front = queue_end = 0;
88 /* return queue empty status */
90 if ( queue_front == queue_end ) {
98 /* return queue full status */
100 if ( (queue_end + 1) % MAX_RUN_QUEUE == queue_front ) {
108 /* add a member to the back of the queue */
111 queue[queue_end] = ptr;
112 events[ptr].status = FG_EVENT_QUEUED;
114 queue_end = (queue_end + 1) % MAX_RUN_QUEUE;
116 printf("RUN QUEUE FULL!!!\n");
119 /* printf("Queued function %d (%d %d)\n", ptr, queue_front, queue_end); */
123 /* remove a member from the front of the queue */
128 ptr = queue[queue_front];
129 /* printf("Popped position %d = %d\n", queue_front, ptr); */
130 queue_front = (queue_front + 1) % MAX_RUN_QUEUE;
133 printf("PANIC: RUN QUEUE IS EMPTY!!!\n");
139 /* run a specified event */
140 void fgEventRun(int ptr) {
146 printf("Running %s\n", e->description);
148 /* record starting time */
152 gettimeofday(&e->last_run, &e->tz);
153 #endif /* USE_FTIME */
158 /* increment the counter for this event */
161 /* update the event status */
162 e->status = FG_EVENT_READY;
164 /* calculate duration and stats */
167 duration = 1000 * (e->current.time - e->last_run.time) +
168 (e->current.millitm - e->last_run.millitm);
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 */
176 e->cum_time += duration;
178 if ( duration < e->min_time ) {
179 e->min_time = duration;
182 if ( duration > e->max_time ) {
183 e->max_time = duration;
186 /* determine the next absolute run time */
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;
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 */
200 /* Initialize the scheduling subsystem */
201 void fgEventInit( void ) {
202 printf("Initializing event manager\n");
208 /* Register an event with the scheduler, returns a pointer into the
210 int fgEventRegister(char *desc, void (*event)( void ), int status,
215 e = &events[event_ptr];
217 printf("Registering event: %s\n", desc);
219 if ( strlen(desc) < 256 ) {
220 strcpy(e->description, desc);
222 strncpy(e->description, desc, 255);
223 e->description[255] = '\0';
228 e->interval = interval;
231 e->min_time = 100000;
235 /* Actually run the event */
236 fgEventRun(event_ptr);
240 return(event_ptr - 1);
244 /* Update the scheduling parameters for an event */
245 void fgEventUpdate( void ) {
249 /* Delete a scheduled event */
250 void fgEventDelete( void ) {
254 /* Temporarily suspend scheduling of an event */
255 void fgEventSuspend( void ) {
259 /* Resume scheduling and event */
260 void fgEventResume( void ) {
264 /* Dump scheduling stats */
265 void fgEventPrintStats( void ) {
268 if ( event_ptr > 0 ) {
270 printf("Event Stats\n");
271 printf("-----------\n");
273 for ( i = 0; i < event_ptr; i++ ) {
274 printf(" %-20s int=%.2fs cum=%ld min=%ld max=%ld count=%ld ave=%.2f\n",
275 events[i].description,
276 events[i].interval / 1000.0,
278 events[i].min_time, events[i].max_time, events[i].count,
279 events[i].cum_time / (double)events[i].count);
286 /* Add pending jobs to the run queue and run the job at the front of
288 void fgEventProcess( void ) {
290 struct timeb current;
292 struct timeval current;
294 #endif /* USE_FTIME */
297 printf("Processing events\n");
299 /* get the current time */
303 gettimeofday(¤t, &tz);
304 #endif /* USE_FTIME */
306 /* printf("Checking if anything is ready to move to the run queue\n"); */
308 /* see if anything else is ready to be placed on the run queue */
309 for ( i = 0; i < event_ptr; i++ ) {
310 if ( events[i].status == FG_EVENT_READY ) {
312 if ( current.time > events[i].next_run.time ) {
314 } else if ( (current.time == events[i].next_run.time) &&
315 (current.millitm >= events[i].next_run.millitm) ) {
319 if ( current.tv_sec > events[i].next_run.tv_sec ) {
321 } else if ( (current.tv_sec == events[i].next_run.tv_sec) &&
322 (current.tv_usec >= events[i].next_run.tv_usec) ) {
326 #endif /* USE_FTIME */
330 /* Checking to see if there is anything on the run queue */
331 /* printf("Checking to see if there is anything on the run queue\n"); */
333 /* printf("Yep, running it\n"); */
341 /* Revision 1.6 1998/01/19 18:40:39 curt
342 /* Tons of little changes to clean up the code and to remove fatal errors
343 /* when building with the c++ compiler.
345 * Revision 1.5 1998/01/06 01:20:27 curt
346 * Tweaks to help building with MSVC++
348 * Revision 1.4 1997/12/31 17:46:50 curt
349 * Tweaked fg_time.c to be able to use ftime() instead of gettimeofday()
351 * Revision 1.3 1997/12/30 22:22:42 curt
352 * Further integration of event manager.
354 * Revision 1.2 1997/12/30 20:47:58 curt
355 * Integrated new event manager with subsystem initializations.
357 * Revision 1.1 1997/12/30 04:19:22 curt