]> git.mxchange.org Git - simgear.git/blob - subsystem_mgr.cxx
b01a0ffe05831c463158a3b7813b36b2c28abc3b
[simgear.git] / subsystem_mgr.cxx
1 // Written by David Megginson, started 2000-12
2 //
3 // Copyright (C) 2000  David Megginson, david@megginson.com
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19 // $Id$
20
21 #ifdef HAVE_CONFIG_H
22 #  include <simgear_config.h>
23 #endif
24
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/timing/timestamp.hxx>
27
28 #include "exception.hxx"
29 #include "subsystem_mgr.hxx"
30
31 #include <simgear/math/SGMath.hxx>
32 #include "SGSmplstat.hxx"
33
34 const int SG_MAX_SUBSYSTEM_EXCEPTIONS = 4;
35
36 using std::string;
37
38 ////////////////////////////////////////////////////////////////////////
39 // Implementation of SGSubsystem
40 ////////////////////////////////////////////////////////////////////////
41
42 SGSubsystemTimingCb SGSubsystem::reportTimingCb = NULL;
43 void* SGSubsystem::reportTimingUserData = NULL;
44
45 SGSubsystem::SGSubsystem ()
46   : _suspended(false)
47 {
48 }
49
50 SGSubsystem::~SGSubsystem ()
51 {
52 }
53
54 void
55 SGSubsystem::init ()
56 {
57 }
58
59 SGSubsystem::InitStatus
60 SGSubsystem::incrementalInit ()
61 {
62   init();
63   return INIT_DONE;
64 }
65
66 void
67 SGSubsystem::postinit ()
68 {
69 }
70
71 void
72 SGSubsystem::reinit ()
73 {
74 }
75
76 void
77 SGSubsystem::shutdown ()
78 {
79 }
80
81 void
82 SGSubsystem::bind ()
83 {
84 }
85
86 void
87 SGSubsystem::unbind ()
88 {
89 }
90
91 void
92 SGSubsystem::suspend ()
93 {
94   _suspended = true;
95 }
96
97 void
98 SGSubsystem::suspend (bool suspended)
99 {
100   _suspended = suspended;
101 }
102
103 void
104 SGSubsystem::resume ()
105 {
106   _suspended = false;
107 }
108
109 bool
110 SGSubsystem::is_suspended () const
111 {
112   return _suspended;
113 }
114
115 void SGSubsystem::stamp(const string& name)
116 {
117     timingInfo.push_back(TimingInfo(name, SGTimeStamp::now()));
118 }
119
120 ////////////////////////////////////////////////////////////////////////
121 // Implementation of SGSubsystemGroup.
122 ////////////////////////////////////////////////////////////////////////
123
124 class SGSubsystemGroup::Member
125 {    
126 private:
127     Member (const Member &member);
128 public:
129     Member ();
130     virtual ~Member ();
131     
132     virtual void update (double delta_time_sec);
133
134     void reportTiming(void) { if (reportTimingCb) reportTimingCb(reportTimingUserData, name, &timeStat); }
135     void updateExecutionTime(double time) { timeStat += time;}
136
137     SampleStatistic timeStat;
138     std::string name;
139     SGSharedPtr<SGSubsystem> subsystem;
140     double min_step_sec;
141     double elapsed_sec;
142     bool collectTimeStats;
143     int exceptionCount;
144     int initTime;
145 };
146
147
148
149 SGSubsystemGroup::SGSubsystemGroup () :
150   _fixedUpdateTime(-1.0),
151   _updateTimeRemainder(0.0),
152   _initPosition(0)
153 {
154 }
155
156 SGSubsystemGroup::~SGSubsystemGroup ()
157 {
158     // reverse order to prevent order dependency problems
159     for( size_t i = _members.size(); i > 0; i-- )
160     {
161         delete _members[i-1];
162     }
163 }
164
165 void
166 SGSubsystemGroup::init ()
167 {
168     for( size_t i = 0; i < _members.size(); i++ )
169         _members[i]->subsystem->init();
170 }
171
172 SGSubsystem::InitStatus
173 SGSubsystemGroup::incrementalInit()
174 {
175   if (_initPosition >= _members.size())
176     return INIT_DONE;
177   
178   SGTimeStamp st;
179   st.stamp();
180   InitStatus memberStatus = _members[_initPosition]->subsystem->incrementalInit();
181   _members[_initPosition]->initTime += st.elapsedMSec();
182   
183   if (memberStatus == INIT_DONE)
184     ++_initPosition;
185   
186   return INIT_CONTINUE;
187 }
188
189 void
190 SGSubsystemGroup::postinit ()
191 {
192     for( size_t i = 0; i < _members.size(); i++ )
193         _members[i]->subsystem->postinit();
194 }
195
196 void
197 SGSubsystemGroup::reinit ()
198 {
199     for( size_t i = 0; i < _members.size(); i++ )
200         _members[i]->subsystem->reinit();
201 }
202
203 void
204 SGSubsystemGroup::shutdown ()
205 {
206     // reverse order to prevent order dependency problems
207     for( size_t i = _members.size(); i > 0; i-- )
208         _members[i-1]->subsystem->shutdown();
209   _initPosition = 0;
210 }
211
212 void
213 SGSubsystemGroup::bind ()
214 {
215     for( size_t i = 0; i < _members.size(); i++ )
216         _members[i]->subsystem->bind();
217 }
218
219 void
220 SGSubsystemGroup::unbind ()
221 {
222     // reverse order to prevent order dependency problems
223     for( size_t i = _members.size(); i > 0; i-- )
224        _members[i-1]->subsystem->unbind();
225 }
226
227 void
228 SGSubsystemGroup::update (double delta_time_sec)
229 {
230     int loopCount = 1;
231     // if dt == 0.0, we are paused, so we need to run one iteration
232     // of our members; if we have a fixed update time, we compute a
233     // loop count, and locally adjust dt
234     if ((delta_time_sec > 0.0) && (_fixedUpdateTime > 0.0)) {
235       double localDelta = delta_time_sec + _updateTimeRemainder;
236       loopCount = SGMiscd::roundToInt(localDelta / _fixedUpdateTime);
237       loopCount = std::max(0, loopCount);
238       _updateTimeRemainder = delta_time_sec - (loopCount * _fixedUpdateTime);
239       delta_time_sec = _fixedUpdateTime;
240     }
241
242     bool recordTime = (reportTimingCb != NULL);
243     SGTimeStamp timeStamp;
244     while (loopCount-- > 0) {
245       for( size_t i = 0; i < _members.size(); i++ )
246       {
247           if (recordTime)
248               timeStamp = SGTimeStamp::now();
249
250           _members[i]->update(delta_time_sec); // indirect call
251
252           if ((recordTime)&&(reportTimingCb))
253           {
254               timeStamp = SGTimeStamp::now() - timeStamp;
255               _members[i]->updateExecutionTime(timeStamp.toUSecs());
256           }
257       }
258     } // of multiple update loop
259 }
260
261 void
262 SGSubsystemGroup::reportTiming(void)
263 {
264     for( size_t i = _members.size(); i > 0; i-- )
265     {
266         _members[i-1]->reportTiming();
267     }
268 }
269
270 void
271 SGSubsystemGroup::suspend ()
272 {
273     for( size_t i = 0; i < _members.size(); i++ )
274         _members[i]->subsystem->suspend();
275 }
276
277 void
278 SGSubsystemGroup::resume ()
279 {
280     for( size_t i = 0; i < _members.size(); i++ )
281         _members[i]->subsystem->resume();
282 }
283
284 string_list
285 SGSubsystemGroup::member_names() const
286 {
287         string_list result;
288         for( size_t i = 0; i < _members.size(); i++ )
289                 result.push_back( _members[i]->name );
290         
291         return result;
292 }
293
294 bool
295 SGSubsystemGroup::is_suspended () const
296 {
297     return false;
298 }
299
300 void
301 SGSubsystemGroup::set_subsystem (const string &name, SGSubsystem * subsystem,
302                                  double min_step_sec)
303 {
304     Member * member = get_member(name, true);
305     member->name = name;
306     member->subsystem = subsystem;
307     member->min_step_sec = min_step_sec;
308 }
309
310 SGSubsystem *
311 SGSubsystemGroup::get_subsystem (const string &name)
312 {
313     Member * member = get_member(name);
314     if (member != 0)
315         return member->subsystem;
316     else
317         return 0;
318 }
319
320 void
321 SGSubsystemGroup::remove_subsystem (const string &name)
322 {
323     MemberVec::iterator it = _members.begin();
324     for (; it != _members.end(); ++it) {
325         if (name == (*it)->name) {
326             delete *it;
327             _members.erase(it);
328             return;
329         }
330     }
331     
332     SG_LOG(SG_GENERAL, SG_WARN, "remove_subsystem: missing:" << name);
333 }
334
335 //------------------------------------------------------------------------------
336 void SGSubsystemGroup::clearSubsystems()
337 {
338   for( MemberVec::iterator it = _members.begin();
339                            it != _members.end();
340                          ++it )
341     delete *it;
342   _members.clear();
343 }
344
345 void
346 SGSubsystemGroup::set_fixed_update_time(double dt)
347 {
348   _fixedUpdateTime = dt;
349 }
350
351 bool
352 SGSubsystemGroup::has_subsystem (const string &name) const
353 {
354     return (((SGSubsystemGroup *)this)->get_member(name) != 0);
355 }
356
357 SGSubsystemGroup::Member *
358 SGSubsystemGroup::get_member (const string &name, bool create)
359 {
360     for( size_t i = 0; i < _members.size(); i++ ) {
361         if (_members[i]->name == name)
362             return _members[i];
363     }
364     if (create) {
365         Member * member = new Member;
366         _members.push_back(member);
367         return member;
368     } else {
369         return 0;
370     }
371 }
372
373
374 ////////////////////////////////////////////////////////////////////////
375 // Implementation of SGSubsystemGroup::Member
376 ////////////////////////////////////////////////////////////////////////
377
378
379 SGSubsystemGroup::Member::Member ()
380     : name(""),
381       subsystem(0),
382       min_step_sec(0),
383       elapsed_sec(0),
384       exceptionCount(0),
385       initTime(0)
386 {
387 }
388
389 // This shouldn't be called due to subsystem pointer ownership issues.
390 SGSubsystemGroup::Member::Member (const Member &)
391 {
392 }
393
394 SGSubsystemGroup::Member::~Member ()
395 {
396 }
397
398 void
399 SGSubsystemGroup::Member::update (double delta_time_sec)
400 {
401     elapsed_sec += delta_time_sec;
402     if (elapsed_sec < min_step_sec) {
403         return;
404     }
405     
406     if (subsystem->is_suspended()) {
407         return;
408     }
409     
410     try {
411       subsystem->update(elapsed_sec);
412       elapsed_sec = 0;
413     } catch (sg_exception& e) {
414       SG_LOG(SG_GENERAL, SG_ALERT, "caught exception processing subsystem:" << name
415         << "\nmessage:" << e.getMessage());
416       
417       if (++exceptionCount > SG_MAX_SUBSYSTEM_EXCEPTIONS) {
418         SG_LOG(SG_GENERAL, SG_ALERT, "(exceptionCount=" << exceptionCount <<
419           ", suspending)");
420         subsystem->suspend();
421       }
422     }
423 }
424
425
426 ////////////////////////////////////////////////////////////////////////
427 // Implementation of SGSubsystemMgr.
428 ////////////////////////////////////////////////////////////////////////
429
430
431 SGSubsystemMgr::SGSubsystemMgr () :
432   _groups(MAX_GROUPS),
433   _initPosition(0)
434 {
435   for (int i = 0; i < MAX_GROUPS; i++)
436     _groups[i].reset(new SGSubsystemGroup);
437 }
438
439 SGSubsystemMgr::~SGSubsystemMgr ()
440 {
441   // ensure get_subsystem returns NULL from now onwards,
442   // before the SGSubsystemGroup destructors are run
443   _subsystem_map.clear();
444   _groups.clear();
445 }
446
447 void
448 SGSubsystemMgr::init ()
449 {
450     for (int i = 0; i < MAX_GROUPS; i++)
451             _groups[i]->init();
452 }
453
454 SGSubsystem::InitStatus
455 SGSubsystemMgr::incrementalInit()
456 {
457   if (_initPosition >= MAX_GROUPS)
458     return INIT_DONE;
459   
460   InitStatus memberStatus = _groups[_initPosition]->incrementalInit();  
461   if (memberStatus == INIT_DONE)
462     ++_initPosition;
463   
464   return INIT_CONTINUE;
465 }
466
467 void
468 SGSubsystemMgr::postinit ()
469 {
470     for (int i = 0; i < MAX_GROUPS; i++)
471             _groups[i]->postinit();
472 }
473
474 void
475 SGSubsystemMgr::reinit ()
476 {
477     for (int i = 0; i < MAX_GROUPS; i++)
478             _groups[i]->reinit();
479 }
480
481 void
482 SGSubsystemMgr::shutdown ()
483 {
484     // reverse order to prevent order dependency problems
485     for (int i = MAX_GROUPS-1; i >= 0; i--)
486         _groups[i]->shutdown();
487   
488     _initPosition = 0;
489 }
490
491
492 void
493 SGSubsystemMgr::bind ()
494 {
495     for (int i = 0; i < MAX_GROUPS; i++)
496         _groups[i]->bind();
497 }
498
499 void
500 SGSubsystemMgr::unbind ()
501 {
502     // reverse order to prevent order dependency problems
503     for (int i = MAX_GROUPS-1; i >= 0; i--)
504         _groups[i]->unbind();
505 }
506
507 void
508 SGSubsystemMgr::update (double delta_time_sec)
509 {
510     for (int i = 0; i < MAX_GROUPS; i++) {
511         _groups[i]->update(delta_time_sec);
512     }
513 }
514
515 void
516 SGSubsystemMgr::suspend ()
517 {
518     for (int i = 0; i < MAX_GROUPS; i++)
519         _groups[i]->suspend();
520 }
521
522 void
523 SGSubsystemMgr::resume ()
524 {
525     for (int i = 0; i < MAX_GROUPS; i++)
526         _groups[i]->resume();
527 }
528
529 bool
530 SGSubsystemMgr::is_suspended () const
531 {
532     return false;
533 }
534
535 void
536 SGSubsystemMgr::add (const char * name, SGSubsystem * subsystem,
537                      GroupType group, double min_time_sec)
538 {
539     SG_LOG(SG_GENERAL, SG_DEBUG, "Adding subsystem " << name);
540     get_group(group)->set_subsystem(name, subsystem, min_time_sec);
541
542     if (_subsystem_map.find(name) != _subsystem_map.end()) {
543         SG_LOG(SG_GENERAL, SG_ALERT, "Adding duplicate subsystem " << name);
544         throw sg_exception("duplicate subsystem:" + std::string(name));
545     }
546     _subsystem_map[name] = subsystem;
547 }
548
549 void
550 SGSubsystemMgr::remove(const char* name)
551 {
552   SubsystemDict::iterator s =_subsystem_map.find(name);
553   if (s == _subsystem_map.end()) {
554     return;
555   }
556   
557   _subsystem_map.erase(s);
558   
559 // tedious part - we don't know which group the subsystem belongs too
560   for (int i = 0; i < MAX_GROUPS; i++) {
561     if (_groups[i]->get_subsystem(name) != NULL) {
562       _groups[i]->remove_subsystem(name);
563       break;
564     }
565   } // of groups iteration
566 }
567
568
569 SGSubsystemGroup *
570 SGSubsystemMgr::get_group (GroupType group)
571 {
572     return _groups[group];
573 }
574
575 SGSubsystem *
576 SGSubsystemMgr::get_subsystem (const string &name) const
577 {
578     SubsystemDict::const_iterator s =_subsystem_map.find(name);
579
580     if (s == _subsystem_map.end())
581         return 0;
582     else
583         return s->second;
584 }
585
586 /** Trigger the timing callback to report data for all subsystems. */
587 void
588 SGSubsystemMgr::reportTiming()
589 {
590     for (int i = 0; i < MAX_GROUPS; i++) {
591         _groups[i]->reportTiming();
592     } // of groups iteration
593 }
594
595 // end of subsystem_mgr.cxx