X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fstructure%2Fsubsystem_mgr.cxx;h=9488faea51a06c1b2c58d624adc4f11f6a5c60c7;hb=1f6555c9ad3fb75cb474fbf99b46333830285514;hp=1385d8eab493a5181e2b94f837be431bfeb22eb4;hpb=006f90997a8eef6704de2511e38fcc786672308d;p=simgear.git diff --git a/simgear/structure/subsystem_mgr.cxx b/simgear/structure/subsystem_mgr.cxx index 1385d8ea..9488faea 100644 --- a/simgear/structure/subsystem_mgr.cxx +++ b/simgear/structure/subsystem_mgr.cxx @@ -5,8 +5,10 @@ #include "exception.hxx" #include "subsystem_mgr.hxx" +#include - + +const int SG_MAX_SUBSYSTEM_EXCEPTIONS = 4; //////////////////////////////////////////////////////////////////////// // Implementation of SGSubsystem //////////////////////////////////////////////////////////////////////// @@ -102,16 +104,19 @@ void SGSubsystem::stamp(const string& name) // Implementation of SGSubsystemGroup. //////////////////////////////////////////////////////////////////////// -SGSubsystemGroup::SGSubsystemGroup () +SGSubsystemGroup::SGSubsystemGroup () : + _fixedUpdateTime(-1.0), + _updateTimeRemainder(0.0) { } SGSubsystemGroup::~SGSubsystemGroup () { - for (unsigned int i = 0; i < _members.size(); i++) + // reverse order to prevent order dependency problems + for (unsigned int i = _members.size(); i > 0; i--) { - _members[i]->printTimingStatistics(); - delete _members[i]; + _members[i-1]->printTimingStatistics(); + delete _members[i-1]; } } @@ -146,25 +151,39 @@ SGSubsystemGroup::bind () void SGSubsystemGroup::unbind () { - for (unsigned int i = 0; i < _members.size(); i++) - _members[i]->subsystem->unbind(); + // reverse order to prevent order dependency problems + for (unsigned int i = _members.size(); i > 0; i--) + _members[i-1]->subsystem->unbind(); } void SGSubsystemGroup::update (double delta_time_sec) { - for (unsigned int i = 0; i < _members.size(); i++) - { - SGTimeStamp timeStamp = SGTimeStamp::now(); - _members[i]->update(delta_time_sec); // indirect call - timeStamp = timeStamp - SGTimeStamp::now(); - double b = timeStamp.toUSecs(); - _members[i]->updateExecutionTime(b); - double threshold = _members[i]->getTimeWarningThreshold(); - if (( b > threshold ) && (b > 10000)) { - _members[i]->printTimingInformation(b); - } + int loopCount = 1; + // if dt == 0.0, we are paused, so we need to run one iteration + // of our members; if we have a fixed update time, we compute a + // loop count, and locally adjust dt + if ((delta_time_sec > 0.0) && (_fixedUpdateTime > 0.0)) { + double localDelta = delta_time_sec + _updateTimeRemainder; + loopCount = SGMiscd::roundToInt(localDelta / _fixedUpdateTime); + _updateTimeRemainder = delta_time_sec - (loopCount * _fixedUpdateTime); + delta_time_sec = _fixedUpdateTime; } + + while (loopCount-- > 0) { + for (unsigned int i = 0; i < _members.size(); i++) + { + SGTimeStamp timeStamp = SGTimeStamp::now(); + _members[i]->update(delta_time_sec); // indirect call + timeStamp = timeStamp - SGTimeStamp::now(); + double b = timeStamp.toUSecs(); + _members[i]->updateExecutionTime(b); + double threshold = _members[i]->getTimeWarningThreshold(); + if (( b > threshold ) && (b > 10000)) { + _members[i]->printTimingInformation(b); + } + } + } // of multiple update loop } void @@ -229,6 +248,12 @@ SGSubsystemGroup::remove_subsystem (const string &name) } } +void +SGSubsystemGroup::set_fixed_update_time(double dt) +{ + _fixedUpdateTime = dt; +} + void SGSubsystemGroup::Member::printTimingStatistics () { @@ -283,7 +308,8 @@ SGSubsystemGroup::Member::Member () subsystem(0), min_step_sec(0), elapsed_sec(0), - collectTimeStats(false) + collectTimeStats(false), + exceptionCount(0) { } @@ -301,11 +327,26 @@ void SGSubsystemGroup::Member::update (double delta_time_sec) { elapsed_sec += delta_time_sec; - if (elapsed_sec >= min_step_sec) { - if (!subsystem->is_suspended()) { - subsystem->update(elapsed_sec); - elapsed_sec = 0; - } + if (elapsed_sec < min_step_sec) { + return; + } + + if (subsystem->is_suspended()) { + return; + } + + try { + subsystem->update(elapsed_sec); + elapsed_sec = 0; + } catch (sg_exception& e) { + SG_LOG(SG_GENERAL, SG_ALERT, "caught exception processing subsystem:" << name + << "\nmessage:" << e.getMessage()); + + if (++exceptionCount > SG_MAX_SUBSYSTEM_EXCEPTIONS) { + SG_LOG(SG_GENERAL, SG_ALERT, "(exceptionCount=" << exceptionCount << + ", suspending)"); + subsystem->suspend(); + } } } @@ -379,7 +420,8 @@ SGSubsystemMgr::bind () void SGSubsystemMgr::unbind () { - for (int i = 0; i < MAX_GROUPS; i++) + // reverse order to prevent order dependency problems + for (int i = MAX_GROUPS-1; i >= 0; i--) _groups[i].unbind(); }