]> git.mxchange.org Git - flightgear.git/blob - Time/event.c
Merged in make system changes from Bob Kuehne <rpk@sgi.com>
[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 <Time/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)( void );  /* 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( void ) {
84     queue_front = queue_end = 0;
85 }
86
87
88 /* return queue empty status */
89 int emptyq( void ) {
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( void ) {
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( void ) {
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( void ) {
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)( void ), int status, 
211                     int interval)
212 {
213     struct fgEVENT *e;
214
215     e = &events[event_ptr];
216
217     printf("Registering event: %s\n", desc);
218
219     if ( strlen(desc) < 256 ) {
220         strcpy(e->description, desc);
221     } else {
222         strncpy(e->description, desc, 255);
223         e->description[255] = '\0';
224     }
225
226     e->event = event;
227     e->status = status;
228     e->interval = interval;
229
230     e->cum_time = 0;
231     e->min_time = 100000;
232     e->max_time = 0;
233     e->count = 0;
234
235     /* Actually run the event */
236     fgEventRun(event_ptr);
237
238     event_ptr++;
239
240     return(event_ptr - 1);
241 }
242
243
244 /* Update the scheduling parameters for an event */
245 void fgEventUpdate( void ) {
246 }
247
248
249 /* Delete a scheduled event */
250 void fgEventDelete( void ) {
251 }
252
253
254 /* Temporarily suspend scheduling of an event */
255 void fgEventSuspend( void ) {
256 }
257
258
259 /* Resume scheduling and event */
260 void fgEventResume( void ) {
261 }
262
263
264 /* Dump scheduling stats */
265 void fgEventPrintStats( void ) {
266     int i;
267
268     if ( event_ptr > 0 ) {
269         printf("\n");
270         printf("Event Stats\n");
271         printf("-----------\n");
272
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,
277                    events[i].cum_time, 
278                    events[i].min_time, events[i].max_time, events[i].count, 
279                    events[i].cum_time / (double)events[i].count);
280         }
281         printf("\n");
282     }
283 }
284
285
286 /* Add pending jobs to the run queue and run the job at the front of
287  * the queue */
288 void fgEventProcess( void ) {
289 #ifdef USE_FTIME
290     struct timeb current;
291 #else
292     struct timeval current;
293     struct timezone tz;
294 #endif /* USE_FTIME */
295     int i;
296
297     printf("Processing events\n");
298     
299     /* get the current time */
300 #ifdef USE_FTIME
301     ftime(&current);
302 #else
303     gettimeofday(&current, &tz);
304 #endif /* USE_FTIME */
305
306     /* printf("Checking if anything is ready to move to the run queue\n"); */
307
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 ) {
311 #ifdef USE_FTIME
312             if ( current.time > events[i].next_run.time ) {
313                 addq(i);
314             } else if ( (current.time == events[i].next_run.time) && 
315                         (current.millitm >= events[i].next_run.millitm) ) {
316                 addq(i);
317             }
318 #else
319             if ( current.tv_sec > events[i].next_run.tv_sec ) {
320                 addq(i);
321             } else if ( (current.tv_sec == events[i].next_run.tv_sec) && 
322                         (current.tv_usec >= events[i].next_run.tv_usec) ) {
323                 addq(i);
324             }
325
326 #endif /* USE_FTIME */
327         }
328     }
329
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"); */
332     if ( !emptyq() ) {
333         /* printf("Yep, running it\n"); */
334         i = popq();
335         fgEventRun(i);
336     }
337 }
338
339
340 /* $Log$
341 /* Revision 1.7  1998/01/19 19:27:19  curt
342 /* Merged in make system changes from Bob Kuehne <rpk@sgi.com>
343 /* This should simplify things tremendously.
344 /*
345  * Revision 1.6  1998/01/19 18:40:39  curt
346  * Tons of little changes to clean up the code and to remove fatal errors
347  * when building with the c++ compiler.
348  *
349  * Revision 1.5  1998/01/06 01:20:27  curt
350  * Tweaks to help building with MSVC++
351  *
352  * Revision 1.4  1997/12/31 17:46:50  curt
353  * Tweaked fg_time.c to be able to use ftime() instead of gettimeofday()
354  *
355  * Revision 1.3  1997/12/30 22:22:42  curt
356  * Further integration of event manager.
357  *
358  * Revision 1.2  1997/12/30 20:47:58  curt
359  * Integrated new event manager with subsystem initializations.
360  *
361  * Revision 1.1  1997/12/30 04:19:22  curt
362  * Initial revision.
363  *
364  */