1 // event.cxx -- Flight Gear periodic event scheduler
3 // Written by Curtis Olson, started December 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 // (Log is kept at end of this file)
36 #if defined( HAVE_WINDOWS_H ) && defined(__MWERKS__)
37 # include <windows.h> // For Metrowerks environment
38 # include <winbase.h> // There is no ANSI/MSL time function that
39 // contains milliseconds
42 #include <Debug/fg_debug.h>
47 fgEVENT_MGR global_events;
50 // run a specified event
51 static void RunEvent(fgEVENT *e) {
54 printf("Running %s\n", e->description);
56 // record starting time
57 timestamp(&(e->last_run));
62 // increment the counter for this event
65 // update the event status
66 e->status = FG_EVENT_READY;
68 // calculate duration and stats
69 timestamp(&(e->current));
70 duration = timediff(&(e->last_run), &(e->current));
72 e->cum_time += duration;
74 if ( duration < e->min_time ) {
75 e->min_time = duration;
78 if ( duration > e->max_time ) {
79 e->max_time = duration;
82 // determine the next absolute run time
83 timesum(&(e->next_run), &(e->last_run), e->interval);
88 fgEVENT_MGR::fgEVENT_MGR( void ) {
92 // Initialize the scheduling subsystem
93 void fgEVENT_MGR::Init( void ) {
94 printf("Initializing event manager\n");
96 while ( event_table.size() ) {
97 event_table.pop_front();
101 while ( run_queue.size() ) {
102 run_queue.pop_front();
107 // Register an event with the scheduler
108 void fgEVENT_MGR::Register(char *desc, void (*event)( void ), int status,
113 printf("Registering event: %s\n", desc);
115 if ( strlen(desc) < 256 ) {
116 strcpy(e.description, desc);
118 strncpy(e.description, desc, 255);
119 e.description[255] = '\0';
124 e.interval = interval;
131 // Actually run the event
134 // Now add to event_table
135 event_table.push_back(e);
139 // Update the scheduling parameters for an event
140 void fgEVENT_MGR::Update( void ) {
144 // Delete a scheduled event
145 void fgEVENT_MGR::Delete( void ) {
149 // Temporarily suspend scheduling of an event
150 void fgEVENT_MGR::Suspend( void ) {
154 // Resume scheduling and event
155 void fgEVENT_MGR::Resume( void ) {
159 // Dump scheduling stats
160 void fgEVENT_MGR::PrintStats( void ) {
161 deque < fgEVENT > :: iterator current = event_table.begin();
162 deque < fgEVENT > :: iterator last = event_table.end();
166 printf("Event Stats\n");
167 printf("-----------\n");
169 while ( current != last ) {
171 printf(" %-20s int=%.2fs cum=%ld min=%ld max=%ld count=%ld ave=%.2f\n",
174 e.cum_time, e.min_time, e.max_time, e.count,
175 e.cum_time / (double)e.count);
182 // Add pending jobs to the run queue and run the job at the front of
184 void fgEVENT_MGR::Process( void ) {
186 fg_timestamp cur_time;
187 unsigned int i, size;
189 fgPrintf(FG_EVENT, FG_DEBUG, "Processing events\n");
191 // get the current time
192 timestamp(&cur_time);
194 fgPrintf( FG_EVENT, FG_DEBUG,
195 " Current timestamp = %ld\n", cur_time.seconds);
197 // printf("Checking if anything is ready to move to the run queue\n");
199 // see if anything else is ready to be placed on the run queue
200 size = event_table.size();
201 // while ( current != last ) {
202 for ( i = 0; i < size; i++ ) {
204 e_ptr = &event_table[i];
205 if ( e_ptr->status == FG_EVENT_READY ) {
206 fgPrintf(FG_EVENT, FG_DEBUG,
207 " Item %d, current %d, next run @ %ld\n",
208 i, cur_time.seconds, e_ptr->next_run.seconds);
209 if ( timediff(&cur_time, &(e_ptr->next_run)) <= 0) {
210 run_queue.push_back(e_ptr);
211 e_ptr->status = FG_EVENT_QUEUED;
216 // Checking to see if there is anything on the run queue
217 // printf("Checking to see if there is anything on the run queue\n");
218 if ( run_queue.size() ) {
219 // printf("Yep, running it\n");
220 e_ptr = run_queue.front();
221 run_queue.pop_front();
228 fgEVENT_MGR::~fgEVENT_MGR( void ) {
232 void fgEventPrintStats( void ) {
233 global_events.PrintStats();
238 // Revision 1.3 1998/05/22 21:14:53 curt
239 // Rewrote event.cxx in C++ as a class using STL for the internal event list
240 // and run queue this removes the arbitrary list sizes and makes things much
241 // more dynamic. Because this is C++-classified we can now have multiple
242 // event_tables if we'd ever want them.
244 // Revision 1.2 1998/04/25 22:06:33 curt
245 // Edited cvs log messages in source files ... bad bad bad!
247 // Revision 1.1 1998/04/24 00:52:26 curt
248 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
250 // Separated out lighting calcs into their own file.
252 // Revision 1.13 1998/04/18 04:14:08 curt
253 // Moved fg_debug.c to it's own library.
255 // Revision 1.12 1998/04/09 18:40:13 curt
256 // We had unified some of the platform disparate time handling code, and
257 // there was a bug in timesum() which calculated a new time stamp based on
258 // the current time stamp + offset. This hosed the periodic event processing
259 // logic because you'd never arrive at the time the event was scheduled for.
260 // Sky updates and lighting changes are handled via this event mechanism so
261 // they never changed ... it is fixed now.
263 // Revision 1.11 1998/04/03 22:12:55 curt
264 // Converting to Gnu autoconf system.
265 // Centralized time handling differences.
267 // Revision 1.10 1998/03/14 00:28:34 curt
268 // replaced a printf() with an fgPrintf().
270 // Revision 1.9 1998/01/31 00:43:44 curt
271 // Added MetroWorks patches from Carmen Volpe.
273 // Revision 1.8 1998/01/27 00:48:05 curt
274 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
275 // system and commandline/config file processing code.
277 // Revision 1.7 1998/01/19 19:27:19 curt
278 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
279 // This should simplify things tremendously.
281 // Revision 1.6 1998/01/19 18:40:39 curt
282 // Tons of little changes to clean up the code and to remove fatal errors
283 // when building with the c++ compiler.
285 // Revision 1.5 1998/01/06 01:20:27 curt
286 // Tweaks to help building with MSVC++
288 // Revision 1.4 1997/12/31 17:46:50 curt
289 // Tweaked fg_time.c to be able to use ftime() instead of gettimeofday()
291 // Revision 1.3 1997/12/30 22:22:42 curt
292 // Further integration of event manager.
294 // Revision 1.2 1997/12/30 20:47:58 curt
295 // Integrated new event manager with subsystem initializations.
297 // Revision 1.1 1997/12/30 04:19:22 curt