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 */
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 /* record starting time */
149 gettimeofday(&e->last_run, &e->tz);
150 #endif /* USE_FTIME */
155 /* increment the counter for this event */
158 /* update the event status */
159 e->status = FG_EVENT_READY;
161 /* calculate duration and stats */
164 duration = 1000 * (e->current.time - e->last_run.time) +
165 (e->current.millitm - e->last_run.millitm);
167 gettimeofday(&e->current, &e->tz);
168 duration = 1000000 * (e->current.tv_sec - e->last_run.tv_sec) +
169 (e->current.tv_usec - e->last_run.tv_usec);
170 duration /= 1000; /* convert back to milleseconds */
171 #endif /* USE_FTIME */
173 e->cum_time += duration;
175 if ( duration < e->min_time ) {
176 e->min_time = duration;
179 if ( duration > e->max_time ) {
180 e->max_time = duration;
183 /* determine the next absolute run time */
185 e->next_run.time = e->last_run.time +
186 (e->last_run.millitm + e->interval) / 1000;
187 e->next_run.millitm = (e->last_run.millitm + e->interval) % 1000;
189 e->next_run.tv_sec = e->last_run.tv_sec +
190 (e->last_run.tv_usec + e->interval * 1000) / 1000000;
191 e->next_run.tv_usec = (e->last_run.tv_usec + e->interval * 1000) % 1000000;
192 #endif /* USE_FTIME */
197 /* Initialize the scheduling subsystem */
204 /* Register an event with the scheduler, returns a pointer into the
206 int fgEventRegister(char *desc, void (*event)(), int status, int interval) {
209 e = &events[event_ptr];
211 if ( strlen(desc) < 256 ) {
212 strcpy(e->description, desc);
214 strncpy(e->description, desc, 255);
215 e->description[255] = '\0';
220 e->interval = interval;
223 e->min_time = 100000;
227 /* Actually run the event */
228 fgEventRun(event_ptr);
232 return(event_ptr - 1);
236 /* Update the scheduling parameters for an event */
237 void fgEventUpdate() {
241 /* Delete a scheduled event */
242 void fgEventDelete() {
246 /* Temporarily suspend scheduling of an event */
247 void fgEventSuspend() {
251 /* Resume scheduling and event */
252 void fgEventResume() {
256 /* Dump scheduling stats */
257 void fgEventPrintStats() {
260 printf("Event Stats\n");
262 for ( i = 0; i < event_ptr; i++ ) {
263 printf(" %s cum=%d min=%d max=%d count=%d ave=%.2f\n",
264 events[i].description, events[i].cum_time, events[i].min_time,
265 events[i].max_time, events[i].count,
266 events[i].cum_time / (double)events[i].count);
271 /* Add pending jobs to the run queue and run the job at the front of
273 void fgEventProcess() {
275 struct timeb current;
277 struct timeval current;
279 #endif /* USE_FTIME */
282 /* printf("Processing events\n"); */
284 /* get the current time */
288 gettimeofday(¤t, &tz);
289 #endif /* USE_FTIME */
291 /* printf("Checking if anything is ready to move to the run queue\n"); */
293 /* see if anything else is ready to be placed on the run queue */
294 for ( i = 0; i < event_ptr; i++ ) {
295 if ( events[i].status == FG_EVENT_READY ) {
297 if ( current.time > events[i].next_run.time ) {
299 } else if ( (current.time == events[i].next_run.time) &&
300 (current.millitm >= events[i].next_run.millitm) ) {
304 if ( current.tv_sec > events[i].next_run.tv_sec ) {
306 } else if ( (current.tv_sec == events[i].next_run.tv_sec) &&
307 (current.tv_usec >= events[i].next_run.tv_usec) ) {
311 #endif /* USE_FTIME */
315 /* Checking to see if there is anything on the run queue */
316 /* printf("Checking to see if there is anything on the run queue\n"); */
318 /* printf("Yep, running it\n"); */
326 /* Revision 1.1 1997/12/30 04:19:22 curt