]> git.mxchange.org Git - flightgear.git/blob - Time/event.c
48fa973206b609b325793ace72dc8c302714f32c
[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 <sys/timeb.h> /* for ftime() and struct timeb */
32 #else
33 #  include <sys/time.h>  /* for get/setitimer, gettimeofday, struct timeval */
34 #endif USE_FTIME
35
36
37 #include "event.h"
38
39
40 #define MAX_EVENTS 100    /* size of event table */
41 #define MAX_RUN_QUEUE 100 /* size of run queue */
42
43
44 struct fgEVENT {
45     char description[256];
46
47     void (*event)();  /* pointer to function */
48     int status;       /* status flag */
49
50     long interval;    /* interval in ms between each iteration of this event */
51                       
52 #ifdef USE_FTIME
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 */
56 #else
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 */
60     struct timezone tz;
61 #endif /* USE_FTIME */
62
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 */
67 };
68
69
70 /* Event table */
71 struct fgEVENT events[MAX_EVENTS];
72 int event_ptr;
73
74
75 /* Run Queue */
76 int queue[MAX_RUN_QUEUE];
77 int queue_front;
78 int queue_end;
79
80
81 /* initialize the run queue */
82 void initq() {
83     queue_front = queue_end = 0;
84 }
85
86
87 /* return queue empty status */
88 int emptyq() {
89     if ( queue_front == queue_end ) {
90         return(1);
91     } else {
92         return(0);
93     }
94 }
95
96
97 /* return queue full status */
98 int fullq() {
99     if ( (queue_end + 1) % MAX_RUN_QUEUE == queue_front ) {
100         return(1);
101     } else {
102         return(0);
103     }
104 }
105
106
107 /* add a member to the back of the queue */
108 void addq(int ptr) {
109     if ( !fullq() ) {
110         queue[queue_end] = ptr;
111         events[ptr].status = FG_EVENT_QUEUED;
112
113         queue_end = (queue_end + 1) % MAX_RUN_QUEUE;
114     } else {
115         printf("RUN QUEUE FULL!!!\n");
116     }
117
118     /* printf("Queued function %d (%d %d)\n", ptr, queue_front, queue_end); */
119 }
120
121
122 /* remove a member from the front of the queue */
123 int popq() {
124     int ptr;
125
126     if ( !emptyq() ) {
127         ptr = queue[queue_front];
128         /* printf("Popped position %d = %d\n", queue_front, ptr); */
129         queue_front = (queue_front + 1) % MAX_RUN_QUEUE;
130         return(ptr);
131     } else {
132         printf("PANIC:  RUN QUEUE IS EMPTY!!!\n");
133         exit(0);
134     }
135 }
136
137
138 /* run a specified event */
139 void fgEventRun(int ptr) {
140     struct fgEVENT *e;
141     long duration;
142
143     e = &events[ptr];
144     
145     /* record starting time */
146 #ifdef USE_FTIME
147     ftime(&e->last_run);
148 #else
149     gettimeofday(&e->last_run, &e->tz);
150 #endif /* USE_FTIME */
151
152     /* run the event */
153     (*e->event)();
154
155     /* increment the counter for this event */
156     e->count++;
157
158     /* update the event status */
159     e->status = FG_EVENT_READY;
160
161     /* calculate duration and stats */
162 #ifdef USE_FTIME
163     ftime(&e->current);
164     duration = 1000 * (e->current.time - e->last_run.time) + 
165         (e->current.millitm - e->last_run.millitm);
166 #else
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 */
172
173     e->cum_time += duration;
174
175     if ( duration < e->min_time ) {
176         e->min_time = duration;
177     }
178
179     if ( duration > e->max_time ) {
180         e->max_time = duration;
181     }
182
183     /* determine the next absolute run time */
184 #ifdef USE_FTIME
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;
188 #else
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 */
193
194 }
195
196
197 /* Initialize the scheduling subsystem */
198 void fgEventInit() {
199     event_ptr = 0;
200     initq();
201 }
202
203
204 /* Register an event with the scheduler, returns a pointer into the
205  * event table */
206 int fgEventRegister(char *desc, void (*event)(), int status, int interval) {
207     struct fgEVENT *e;
208
209     e = &events[event_ptr];
210
211     if ( strlen(desc) < 256 ) {
212         strcpy(e->description, desc);
213     } else {
214         strncpy(e->description, desc, 255);
215         e->description[255] = '\0';
216     }
217
218     e->event = event;
219     e->status = status;
220     e->interval = interval;
221
222     e->cum_time = 0;
223     e->min_time = 100000;
224     e->max_time = 0;
225     e->count = 0;
226
227     /* Actually run the event */
228     fgEventRun(event_ptr);
229
230     event_ptr++;
231
232     return(event_ptr - 1);
233 }
234
235
236 /* Update the scheduling parameters for an event */
237 void fgEventUpdate() {
238 }
239
240
241 /* Delete a scheduled event */
242 void fgEventDelete() {
243 }
244
245
246 /* Temporarily suspend scheduling of an event */
247 void fgEventSuspend() {
248 }
249
250
251 /* Resume scheduling and event */
252 void fgEventResume() {
253 }
254
255
256 /* Dump scheduling stats */
257 void fgEventPrintStats() {
258     int i;
259
260     printf("Event Stats\n");
261
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);
267     }
268 }
269
270
271 /* Add pending jobs to the run queue and run the job at the front of
272  * the queue */
273 void fgEventProcess() {
274 #ifdef USE_FTIME
275     struct timeb current;
276 #else
277     struct timeval current;
278     struct timezone tz;
279 #endif /* USE_FTIME */
280     int i;
281
282     /* printf("Processing events\n"); */
283     
284     /* get the current time */
285 #ifdef USE_FTIME
286     ftime(&current);
287 #else
288     gettimeofday(&current, &tz);
289 #endif /* USE_FTIME */
290
291     /* printf("Checking if anything is ready to move to the run queue\n"); */
292
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 ) {
296 #ifdef USE_FTIME
297             if ( current.time > events[i].next_run.time ) {
298                 addq(i);
299             } else if ( (current.time == events[i].next_run.time) && 
300                         (current.millitm >= events[i].next_run.millitm) ) {
301                 addq(i);
302             }
303 #else
304             if ( current.tv_sec > events[i].next_run.tv_sec ) {
305                 addq(i);
306             } else if ( (current.tv_sec == events[i].next_run.tv_sec) && 
307                         (current.tv_usec >= events[i].next_run.tv_usec) ) {
308                 addq(i);
309             }
310
311 #endif /* USE_FTIME */
312         }
313     }
314
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"); */
317     if ( !emptyq() ) {
318         /* printf("Yep, running it\n"); */
319         i = popq();
320         fgEventRun(i);
321     }
322 }
323
324
325 /* $Log$
326 /* Revision 1.1  1997/12/30 04:19:22  curt
327 /* Initial revision.
328 /*
329  */