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 **************************************************************************/
31 # include <sys/timeb.h> /* for ftime() and struct timeb */
33 # include <sys/time.h> /* for get/setitimer, gettimeofday, struct timeval */
34 #endif /* USE_FTIME */
40 #define MAX_EVENTS 100 /* size of event table */
41 #define MAX_RUN_QUEUE 100 /* size of run queue */
45 char description[256];
47 void (*event)(); /* pointer to function */
48 int status; /* status flag */
50 long interval; /* interval in ms between each iteration of this event */
53 struct timeb last_run; /* absolute time for last run */
54 struct timeb current; /* current time */
55 struct timeb next_run; /* absolute time for next run */
57 struct timeval last_run; /* absolute time for last run */
58 struct timeval current; /* current time */
59 struct timeval next_run; /* absolute time for next run */
61 #endif /* USE_FTIME */
63 long cum_time; /* cumulative processor time of this event */
64 long min_time; /* time of quickest execution */
65 long max_time; /* time of slowest execution */
66 long count; /* number of times executed */
71 struct fgEVENT events[MAX_EVENTS];
76 int queue[MAX_RUN_QUEUE];
81 /* initialize the run queue */
83 queue_front = queue_end = 0;
87 /* return queue empty status */
89 if ( queue_front == queue_end ) {
97 /* return queue full status */
99 if ( (queue_end + 1) % MAX_RUN_QUEUE == queue_front ) {
107 /* add a member to the back of the queue */
110 queue[queue_end] = ptr;
111 events[ptr].status = FG_EVENT_QUEUED;
113 queue_end = (queue_end + 1) % MAX_RUN_QUEUE;
115 printf("RUN QUEUE FULL!!!\n");
118 /* printf("Queued function %d (%d %d)\n", ptr, queue_front, queue_end); */
122 /* remove a member from the front of the queue */
127 ptr = queue[queue_front];
128 /* printf("Popped position %d = %d\n", queue_front, ptr); */
129 queue_front = (queue_front + 1) % MAX_RUN_QUEUE;
132 printf("PANIC: RUN QUEUE IS EMPTY!!!\n");
138 /* run a specified event */
139 void fgEventRun(int ptr) {
145 printf("Running %s\n", e->description);
147 /* record starting time */
151 gettimeofday(&e->last_run, &e->tz);
152 #endif /* USE_FTIME */
157 /* increment the counter for this event */
160 /* update the event status */
161 e->status = FG_EVENT_READY;
163 /* calculate duration and stats */
166 duration = 1000 * (e->current.time - e->last_run.time) +
167 (e->current.millitm - e->last_run.millitm);
169 gettimeofday(&e->current, &e->tz);
170 duration = 1000000 * (e->current.tv_sec - e->last_run.tv_sec) +
171 (e->current.tv_usec - e->last_run.tv_usec);
172 duration /= 1000; /* convert back to milleseconds */
173 #endif /* USE_FTIME */
175 e->cum_time += duration;
177 if ( duration < e->min_time ) {
178 e->min_time = duration;
181 if ( duration > e->max_time ) {
182 e->max_time = duration;
185 /* determine the next absolute run time */
187 e->next_run.time = e->last_run.time +
188 (e->last_run.millitm + e->interval) / 1000;
189 e->next_run.millitm = (e->last_run.millitm + e->interval) % 1000;
191 e->next_run.tv_sec = e->last_run.tv_sec +
192 (e->last_run.tv_usec + e->interval * 1000) / 1000000;
193 e->next_run.tv_usec = (e->last_run.tv_usec + e->interval * 1000) % 1000000;
194 #endif /* USE_FTIME */
199 /* Initialize the scheduling subsystem */
201 printf("Initializing event manager\n");
207 /* Register an event with the scheduler, returns a pointer into the
209 int fgEventRegister(char *desc, void (*event)(), int status, int interval) {
212 e = &events[event_ptr];
214 printf("Registering event: %s\n", desc);
216 if ( strlen(desc) < 256 ) {
217 strcpy(e->description, desc);
219 strncpy(e->description, desc, 255);
220 e->description[255] = '\0';
225 e->interval = interval;
228 e->min_time = 100000;
232 /* Actually run the event */
233 fgEventRun(event_ptr);
237 return(event_ptr - 1);
241 /* Update the scheduling parameters for an event */
242 void fgEventUpdate() {
246 /* Delete a scheduled event */
247 void fgEventDelete() {
251 /* Temporarily suspend scheduling of an event */
252 void fgEventSuspend() {
256 /* Resume scheduling and event */
257 void fgEventResume() {
261 /* Dump scheduling stats */
262 void fgEventPrintStats() {
265 if ( event_ptr > 0 ) {
267 printf("Event Stats\n");
268 printf("-----------\n");
270 for ( i = 0; i < event_ptr; i++ ) {
271 printf(" %-20s int=%.2fs cum=%d min=%d max=%d count=%d ave=%.2f\n",
272 events[i].description,
273 events[i].interval / 1000.0,
275 events[i].min_time, events[i].max_time, events[i].count,
276 events[i].cum_time / (double)events[i].count);
283 /* Add pending jobs to the run queue and run the job at the front of
285 void fgEventProcess() {
287 struct timeb current;
289 struct timeval current;
291 #endif /* USE_FTIME */
294 printf("Processing events\n");
296 /* get the current time */
300 gettimeofday(¤t, &tz);
301 #endif /* USE_FTIME */
303 /* printf("Checking if anything is ready to move to the run queue\n"); */
305 /* see if anything else is ready to be placed on the run queue */
306 for ( i = 0; i < event_ptr; i++ ) {
307 if ( events[i].status == FG_EVENT_READY ) {
309 if ( current.time > events[i].next_run.time ) {
311 } else if ( (current.time == events[i].next_run.time) &&
312 (current.millitm >= events[i].next_run.millitm) ) {
316 if ( current.tv_sec > events[i].next_run.tv_sec ) {
318 } else if ( (current.tv_sec == events[i].next_run.tv_sec) &&
319 (current.tv_usec >= events[i].next_run.tv_usec) ) {
323 #endif /* USE_FTIME */
327 /* Checking to see if there is anything on the run queue */
328 /* printf("Checking to see if there is anything on the run queue\n"); */
330 /* printf("Yep, running it\n"); */
338 /* Revision 1.4 1997/12/31 17:46:50 curt
339 /* Tweaked fg_time.c to be able to use ftime() instead of gettimeofday()
341 * Revision 1.3 1997/12/30 22:22:42 curt
342 * Further integration of event manager.
344 * Revision 1.2 1997/12/30 20:47:58 curt
345 * Integrated new event manager with subsystem initializations.
347 * Revision 1.1 1997/12/30 04:19:22 curt