1998-05-22 21:14:53 +00:00
|
|
|
// event.cxx -- Flight Gear periodic event scheduler
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started December 1997.
|
|
|
|
//
|
|
|
|
// Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
1997-12-30 04:19:22 +00:00
|
|
|
|
|
|
|
|
1998-04-24 00:52:24 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
1998-04-03 22:12:53 +00:00
|
|
|
|
1999-01-07 20:25:32 +00:00
|
|
|
#include <string>
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1999-01-07 20:25:32 +00:00
|
|
|
#include "Include/compiler.h"
|
|
|
|
|
|
|
|
#include STL_ALGORITHM
|
|
|
|
#include STL_FUNCTIONAL
|
|
|
|
|
|
|
|
#ifdef FG_HAVE_STD_INCLUDES
|
|
|
|
# include <cstdio>
|
|
|
|
# ifdef HAVE_STDLIB_H
|
|
|
|
# include <cstdlib>
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# include <stdio.h>
|
|
|
|
# ifdef HAVE_STDLIB_H
|
|
|
|
# include <stdlib.h>
|
|
|
|
# endif
|
1998-04-03 22:12:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined( HAVE_WINDOWS_H ) && defined(__MWERKS__)
|
1998-05-22 21:14:53 +00:00
|
|
|
# include <windows.h> // For Metrowerks environment
|
|
|
|
# include <winbase.h> // There is no ANSI/MSL time function that
|
|
|
|
// contains milliseconds
|
1998-04-03 22:12:53 +00:00
|
|
|
#endif
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-11-06 21:17:31 +00:00
|
|
|
#include <Debug/logstream.hxx>
|
1998-04-24 00:52:24 +00:00
|
|
|
|
|
|
|
#include "event.hxx"
|
|
|
|
|
1999-01-07 20:25:32 +00:00
|
|
|
FG_USING_STD(for_each);
|
|
|
|
FG_USING_STD(mem_fun);
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
fgEVENT_MGR global_events;
|
1997-12-30 04:19:22 +00:00
|
|
|
|
|
|
|
|
1998-08-29 13:11:31 +00:00
|
|
|
fgEVENT::fgEVENT( const string& desc,
|
|
|
|
const fgCallback& cb,
|
1999-01-07 20:25:32 +00:00
|
|
|
EventState evt_status,
|
|
|
|
int evt_interval )
|
1998-08-29 13:11:31 +00:00
|
|
|
: description(desc),
|
|
|
|
event_cb(cb.clone()),
|
1999-01-07 20:25:32 +00:00
|
|
|
status(evt_status),
|
|
|
|
interval(evt_interval),
|
1998-08-29 13:11:31 +00:00
|
|
|
cum_time(0),
|
|
|
|
min_time(100000),
|
|
|
|
max_time(0),
|
|
|
|
count(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-01-07 20:25:32 +00:00
|
|
|
#if 0
|
1998-08-29 13:11:31 +00:00
|
|
|
fgEVENT::fgEVENT( const fgEVENT& evt )
|
|
|
|
: description(evt.description),
|
|
|
|
event_cb(evt.event_cb),
|
|
|
|
status(evt.status),
|
|
|
|
interval(evt.interval),
|
|
|
|
last_run(evt.last_run),
|
|
|
|
current(evt.current),
|
|
|
|
next_run(evt.next_run),
|
|
|
|
cum_time(evt.cum_time),
|
|
|
|
min_time(evt.min_time),
|
|
|
|
max_time(evt.max_time),
|
|
|
|
count(evt.count)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-01-07 20:25:32 +00:00
|
|
|
fgEVENT&
|
|
|
|
fgEVENT::operator= ( const fgEVENT& evt )
|
|
|
|
{
|
|
|
|
if ( this != &evt )
|
|
|
|
{
|
|
|
|
description = evt.description;
|
|
|
|
event_cb = evt.event_cb;
|
|
|
|
status = evt.status;
|
|
|
|
interval = evt.interval;
|
|
|
|
last_run = evt.last_run;
|
|
|
|
current = evt.current;
|
|
|
|
next_run = evt.next_run;
|
|
|
|
cum_time = evt.cum_time;
|
|
|
|
min_time = evt.min_time;
|
|
|
|
max_time = evt.max_time;
|
|
|
|
count = evt.count;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-08-29 13:11:31 +00:00
|
|
|
fgEVENT::~fgEVENT()
|
|
|
|
{
|
1999-01-07 20:25:32 +00:00
|
|
|
delete event_cb;
|
1998-08-29 13:11:31 +00:00
|
|
|
}
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-08-29 13:11:31 +00:00
|
|
|
void
|
|
|
|
fgEVENT::run()
|
|
|
|
{
|
1998-11-09 23:41:51 +00:00
|
|
|
FG_LOG(FG_EVENT, FG_INFO, "Running " << description );
|
1997-12-30 20:47:34 +00:00
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// record starting time
|
1998-12-04 01:32:46 +00:00
|
|
|
last_run.stamp();
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// run the event
|
1998-08-29 13:11:31 +00:00
|
|
|
event_cb->call( (void**)NULL );
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// increment the counter for this event
|
1998-08-29 13:11:31 +00:00
|
|
|
count++;
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// update the event status
|
1998-08-29 13:11:31 +00:00
|
|
|
status = FG_EVENT_READY;
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// calculate duration and stats
|
1998-12-04 01:32:46 +00:00
|
|
|
current.stamp();
|
|
|
|
long duration = current - last_run;
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-08-29 13:11:31 +00:00
|
|
|
cum_time += duration;
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-08-29 13:11:31 +00:00
|
|
|
if ( duration < min_time ) {
|
|
|
|
min_time = duration;
|
1997-12-30 04:19:22 +00:00
|
|
|
}
|
|
|
|
|
1998-08-29 13:11:31 +00:00
|
|
|
if ( duration > max_time ) {
|
|
|
|
max_time = duration;
|
1997-12-30 04:19:22 +00:00
|
|
|
}
|
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// determine the next absolute run time
|
1998-12-04 01:32:46 +00:00
|
|
|
next_run = last_run + interval;
|
1998-08-29 13:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Dump scheduling stats
|
1998-09-15 02:09:24 +00:00
|
|
|
int
|
1998-08-29 13:11:31 +00:00
|
|
|
fgEVENT::PrintStats() const
|
|
|
|
{
|
1998-11-09 23:41:51 +00:00
|
|
|
FG_LOG( FG_EVENT, FG_INFO,
|
1998-11-07 19:07:06 +00:00
|
|
|
" " << description
|
|
|
|
<< " int=" << interval / 1000.0
|
|
|
|
<< " cum=" << cum_time
|
|
|
|
<< " min=" << min_time
|
|
|
|
<< " max=" << max_time
|
|
|
|
<< " count=" << count
|
|
|
|
<< " ave=" << cum_time / (double)count );
|
1998-09-15 02:09:24 +00:00
|
|
|
return 0;
|
1997-12-30 04:19:22 +00:00
|
|
|
}
|
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// Constructor
|
|
|
|
fgEVENT_MGR::fgEVENT_MGR( void ) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize the scheduling subsystem
|
|
|
|
void fgEVENT_MGR::Init( void ) {
|
1998-11-09 23:41:51 +00:00
|
|
|
FG_LOG(FG_EVENT, FG_INFO, "Initializing event manager" );
|
1998-05-22 21:14:53 +00:00
|
|
|
|
1998-08-29 13:11:31 +00:00
|
|
|
run_queue.erase( run_queue.begin(), run_queue.end() );
|
|
|
|
event_table.erase( event_table.begin(), event_table.end() );
|
1997-12-30 04:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-08-29 13:11:31 +00:00
|
|
|
// Register an event with the scheduler.
|
|
|
|
void
|
|
|
|
fgEVENT_MGR::Register( const string& desc,
|
|
|
|
const fgCallback& cb,
|
|
|
|
fgEVENT::EventState status,
|
|
|
|
int interval )
|
1998-01-19 18:40:15 +00:00
|
|
|
{
|
1999-01-09 13:37:32 +00:00
|
|
|
// convert interval specified in milleseconds to usec
|
|
|
|
fgEVENT* e = new fgEVENT( desc, cb, status, interval * 1000 );
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-11-09 23:41:51 +00:00
|
|
|
FG_LOG( FG_EVENT, FG_INFO, "Registering event: " << desc );
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// Actually run the event
|
1999-01-07 20:25:32 +00:00
|
|
|
e->run();
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// Now add to event_table
|
|
|
|
event_table.push_back(e);
|
1997-12-30 04:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// Update the scheduling parameters for an event
|
|
|
|
void fgEVENT_MGR::Update( void ) {
|
1997-12-30 04:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// Delete a scheduled event
|
|
|
|
void fgEVENT_MGR::Delete( void ) {
|
1997-12-30 04:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// Temporarily suspend scheduling of an event
|
|
|
|
void fgEVENT_MGR::Suspend( void ) {
|
1997-12-30 04:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// Resume scheduling and event
|
|
|
|
void fgEVENT_MGR::Resume( void ) {
|
1997-12-30 04:19:22 +00:00
|
|
|
}
|
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// Dump scheduling stats
|
1998-08-29 13:11:31 +00:00
|
|
|
void
|
|
|
|
fgEVENT_MGR::PrintStats()
|
|
|
|
{
|
1998-11-09 23:41:51 +00:00
|
|
|
FG_LOG( FG_EVENT, FG_INFO, "" );
|
|
|
|
FG_LOG( FG_EVENT, FG_INFO, "Event Stats" );
|
|
|
|
FG_LOG( FG_EVENT, FG_INFO, "-----------" );
|
1998-05-22 21:14:53 +00:00
|
|
|
|
1999-01-07 20:25:32 +00:00
|
|
|
ConstEventIterator first = event_table.begin();
|
|
|
|
ConstEventIterator last = event_table.end();
|
|
|
|
while ( first != last )
|
|
|
|
{
|
|
|
|
(*first)->PrintStats();
|
|
|
|
++first;
|
|
|
|
}
|
|
|
|
#if 0 // msvc++ 6.0 barfs at mem_fun()
|
1998-08-29 13:11:31 +00:00
|
|
|
for_each( event_table.begin(),
|
|
|
|
event_table.end(),
|
1999-01-07 20:25:32 +00:00
|
|
|
mem_fun( &fgEVENT::PrintStats ) );
|
|
|
|
#endif
|
1998-11-09 23:41:51 +00:00
|
|
|
FG_LOG( FG_EVENT, FG_INFO, "");
|
1997-12-30 04:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// Add pending jobs to the run queue and run the job at the front of
|
|
|
|
// the queue
|
|
|
|
void fgEVENT_MGR::Process( void ) {
|
|
|
|
fgEVENT *e_ptr;
|
1999-01-09 13:37:32 +00:00
|
|
|
FGTimeStamp cur_time;
|
1998-05-22 21:14:53 +00:00
|
|
|
unsigned int i, size;
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_EVENT, FG_DEBUG, "Processing events" );
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// get the current time
|
1998-12-04 01:32:46 +00:00
|
|
|
cur_time.stamp();
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_EVENT, FG_DEBUG,
|
1998-12-04 01:32:46 +00:00
|
|
|
" Current timestamp = " << cur_time.get_seconds() );
|
1998-04-09 18:40:13 +00:00
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// printf("Checking if anything is ready to move to the run queue\n");
|
1997-12-30 04:19:22 +00:00
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// see if anything else is ready to be placed on the run queue
|
|
|
|
size = event_table.size();
|
|
|
|
// while ( current != last ) {
|
|
|
|
for ( i = 0; i < size; i++ ) {
|
|
|
|
// e = *current++;
|
1999-01-07 20:25:32 +00:00
|
|
|
e_ptr = event_table[i];
|
1998-08-29 13:11:31 +00:00
|
|
|
if ( e_ptr->status == fgEVENT::FG_EVENT_READY ) {
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_EVENT, FG_DEBUG,
|
1998-12-04 01:32:46 +00:00
|
|
|
" Item " << i << " current " << cur_time.get_seconds()
|
|
|
|
<< " next run @ " << e_ptr->next_run.get_seconds() );
|
|
|
|
if ( ( e_ptr->next_run - cur_time ) <= 0 ) {
|
1998-05-22 21:14:53 +00:00
|
|
|
run_queue.push_back(e_ptr);
|
1998-08-29 13:11:31 +00:00
|
|
|
e_ptr->status = fgEVENT::FG_EVENT_QUEUED;
|
1998-01-31 00:42:57 +00:00
|
|
|
}
|
1997-12-30 04:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// Checking to see if there is anything on the run queue
|
|
|
|
// printf("Checking to see if there is anything on the run queue\n");
|
|
|
|
if ( run_queue.size() ) {
|
|
|
|
// printf("Yep, running it\n");
|
|
|
|
e_ptr = run_queue.front();
|
|
|
|
run_queue.pop_front();
|
1998-08-29 13:11:31 +00:00
|
|
|
e_ptr->run();
|
1997-12-30 04:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-22 21:14:53 +00:00
|
|
|
// Destructor
|
|
|
|
fgEVENT_MGR::~fgEVENT_MGR( void ) {
|
1999-01-07 20:25:32 +00:00
|
|
|
EventIterator first = event_table.begin();
|
|
|
|
EventIterator last = event_table.end();
|
|
|
|
for ( ; first != last; ++first )
|
|
|
|
{
|
|
|
|
delete (*first);
|
|
|
|
}
|
|
|
|
|
|
|
|
run_queue.erase( run_queue.begin(), run_queue.end() );
|
|
|
|
event_table.erase( event_table.begin(), event_table.end() );
|
1998-05-22 21:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|