]> git.mxchange.org Git - simgear.git/blob - simgear/structure/subsystem_mgr.cxx
Quiet a message.
[simgear.git] / simgear / structure / 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       _updateTimeRemainder = delta_time_sec - (loopCount * _fixedUpdateTime);
238       delta_time_sec = _fixedUpdateTime;
239     }
240
241     bool recordTime = (reportTimingCb != NULL);
242     SGTimeStamp timeStamp;
243     while (loopCount-- > 0) {
244       for( size_t i = 0; i < _members.size(); i++ )
245       {
246           if (recordTime)
247               timeStamp = SGTimeStamp::now();
248
249           _members[i]->update(delta_time_sec); // indirect call
250
251           if ((recordTime)&&(reportTimingCb))
252           {
253               timeStamp = SGTimeStamp::now() - timeStamp;
254               _members[i]->updateExecutionTime(timeStamp.toUSecs());
255           }
256       }
257     } // of multiple update loop
258 }
259
260 void
261 SGSubsystemGroup::reportTiming(void)
262 {
263     for( size_t i = _members.size(); i > 0; i-- )
264     {
265         _members[i-1]->reportTiming();
266     }
267 }
268
269 void
270 SGSubsystemGroup::suspend ()
271 {
272     for( size_t i = 0; i < _members.size(); i++ )
273         _members[i]->subsystem->suspend();
274 }
275
276 void
277 SGSubsystemGroup::resume ()
278 {
279     for( size_t i = 0; i < _members.size(); i++ )
280         _members[i]->subsystem->resume();
281 }
282
283 string_list
284 SGSubsystemGroup::member_names() const
285 {
286         string_list result;
287         for( size_t i = 0; i < _members.size(); i++ )
288                 result.push_back( _members[i]->name );
289         
290         return result;
291 }
292
293 bool
294 SGSubsystemGroup::is_suspended () const
295 {
296     return false;
297 }
298
299 void
300 SGSubsystemGroup::set_subsystem (const string &name, SGSubsystem * subsystem,
301                                  double min_step_sec)
302 {
303     Member * member = get_member(name, true);
304     if (member->subsystem != 0)
305         delete member->subsystem;
306     member->name = name;
307     member->subsystem = subsystem;
308     member->min_step_sec = min_step_sec;
309 }
310
311 SGSubsystem *
312 SGSubsystemGroup::get_subsystem (const string &name)
313 {
314     Member * member = get_member(name);
315     if (member != 0)
316         return member->subsystem;
317     else
318         return 0;
319 }
320
321 void
322 SGSubsystemGroup::remove_subsystem (const string &name)
323 {
324     MemberVec::iterator it = _members.begin();
325     for (; it != _members.end(); ++it) {
326         if (name == (*it)->name) {
327             delete *it;
328             _members.erase(it);
329             return;
330         }
331     }
332     
333     SG_LOG(SG_GENERAL, SG_WARN, "remove_subsystem: missing:" << name);
334 }
335
336 void
337 SGSubsystemGroup::set_fixed_update_time(double dt)
338 {
339   _fixedUpdateTime = dt;
340 }
341
342 bool
343 SGSubsystemGroup::has_subsystem (const string &name) const
344 {
345     return (((SGSubsystemGroup *)this)->get_member(name) != 0);
346 }
347
348 SGSubsystemGroup::Member *
349 SGSubsystemGroup::get_member (const string &name, bool create)
350 {
351     for( size_t i = 0; i < _members.size(); i++ ) {
352         if (_members[i]->name == name)
353             return _members[i];
354     }
355     if (create) {
356         Member * member = new Member;
357         _members.push_back(member);
358         return member;
359     } else {
360         return 0;
361     }
362 }
363
364
365 ////////////////////////////////////////////////////////////////////////
366 // Implementation of SGSubsystemGroup::Member
367 ////////////////////////////////////////////////////////////////////////
368
369
370 SGSubsystemGroup::Member::Member ()
371     : name(""),
372       subsystem(0),
373       min_step_sec(0),
374       elapsed_sec(0),
375       exceptionCount(0),
376       initTime(0)
377 {
378 }
379
380 // This shouldn't be called due to subsystem pointer ownership issues.
381 SGSubsystemGroup::Member::Member (const Member &)
382 {
383 }
384
385 SGSubsystemGroup::Member::~Member ()
386 {
387 }
388
389 void
390 SGSubsystemGroup::Member::update (double delta_time_sec)
391 {
392     elapsed_sec += delta_time_sec;
393     if (elapsed_sec < min_step_sec) {
394         return;
395     }
396     
397     if (subsystem->is_suspended()) {
398         return;
399     }
400     
401     try {
402       subsystem->update(elapsed_sec);
403       elapsed_sec = 0;
404     } catch (sg_exception& e) {
405       SG_LOG(SG_GENERAL, SG_ALERT, "caught exception processing subsystem:" << name
406         << "\nmessage:" << e.getMessage());
407       
408       if (++exceptionCount > SG_MAX_SUBSYSTEM_EXCEPTIONS) {
409         SG_LOG(SG_GENERAL, SG_ALERT, "(exceptionCount=" << exceptionCount <<
410           ", suspending)");
411         subsystem->suspend();
412       }
413     }
414 }
415
416
417 ////////////////////////////////////////////////////////////////////////
418 // Implementation of SGSubsystemMgr.
419 ////////////////////////////////////////////////////////////////////////
420
421
422 SGSubsystemMgr::SGSubsystemMgr () :
423   _initPosition(0)
424 {
425   for (int i = 0; i < MAX_GROUPS; i++) {
426     _groups[i] = new SGSubsystemGroup;
427   }
428 }
429
430 SGSubsystemMgr::~SGSubsystemMgr ()
431 {
432   // ensure get_subsystem returns NULL from now onwards,
433   // before the SGSubsystemGroup destructors are run
434   _subsystem_map.clear();
435   
436   for (int i = 0; i < MAX_GROUPS; i++) {
437     delete _groups[i];
438   }
439 }
440
441 void
442 SGSubsystemMgr::init ()
443 {
444     for (int i = 0; i < MAX_GROUPS; i++)
445             _groups[i]->init();
446 }
447
448 SGSubsystem::InitStatus
449 SGSubsystemMgr::incrementalInit()
450 {
451   if (_initPosition >= MAX_GROUPS)
452     return INIT_DONE;
453   
454   InitStatus memberStatus = _groups[_initPosition]->incrementalInit();  
455   if (memberStatus == INIT_DONE)
456     ++_initPosition;
457   
458   return INIT_CONTINUE;
459 }
460
461 void
462 SGSubsystemMgr::postinit ()
463 {
464     for (int i = 0; i < MAX_GROUPS; i++)
465             _groups[i]->postinit();
466 }
467
468 void
469 SGSubsystemMgr::reinit ()
470 {
471     for (int i = 0; i < MAX_GROUPS; i++)
472             _groups[i]->reinit();
473 }
474
475 void
476 SGSubsystemMgr::shutdown ()
477 {
478     // reverse order to prevent order dependency problems
479     for (int i = MAX_GROUPS-1; i >= 0; i--)
480         _groups[i]->shutdown();
481   
482     _initPosition = 0;
483 }
484
485
486 void
487 SGSubsystemMgr::bind ()
488 {
489     for (int i = 0; i < MAX_GROUPS; i++)
490         _groups[i]->bind();
491 }
492
493 void
494 SGSubsystemMgr::unbind ()
495 {
496     // reverse order to prevent order dependency problems
497     for (int i = MAX_GROUPS-1; i >= 0; i--)
498         _groups[i]->unbind();
499 }
500
501 void
502 SGSubsystemMgr::update (double delta_time_sec)
503 {
504     for (int i = 0; i < MAX_GROUPS; i++) {
505         _groups[i]->update(delta_time_sec);
506     }
507 }
508
509 void
510 SGSubsystemMgr::suspend ()
511 {
512     for (int i = 0; i < MAX_GROUPS; i++)
513         _groups[i]->suspend();
514 }
515
516 void
517 SGSubsystemMgr::resume ()
518 {
519     for (int i = 0; i < MAX_GROUPS; i++)
520         _groups[i]->resume();
521 }
522
523 bool
524 SGSubsystemMgr::is_suspended () const
525 {
526     return false;
527 }
528
529 void
530 SGSubsystemMgr::add (const char * name, SGSubsystem * subsystem,
531                      GroupType group, double min_time_sec)
532 {
533     SG_LOG(SG_GENERAL, SG_DEBUG, "Adding subsystem " << name);
534     get_group(group)->set_subsystem(name, subsystem, min_time_sec);
535
536     if (_subsystem_map.find(name) != _subsystem_map.end()) {
537         SG_LOG(SG_GENERAL, SG_ALERT, "Adding duplicate subsystem " << name);
538         throw sg_exception("duplicate subsystem:" + std::string(name));
539     }
540     _subsystem_map[name] = subsystem;
541 }
542
543 void
544 SGSubsystemMgr::remove(const char* name)
545 {
546   SubsystemDict::iterator s =_subsystem_map.find(name);
547   if (s == _subsystem_map.end()) {
548     return;
549   }
550   
551   _subsystem_map.erase(s);
552   
553 // tedious part - we don't know which group the subsystem belongs too
554   for (int i = 0; i < MAX_GROUPS; i++) {
555     if (_groups[i]->get_subsystem(name) != NULL) {
556       _groups[i]->remove_subsystem(name);
557       break;
558     }
559   } // of groups iteration
560 }
561
562
563 SGSubsystemGroup *
564 SGSubsystemMgr::get_group (GroupType group)
565 {
566     return _groups[group];
567 }
568
569 SGSubsystem *
570 SGSubsystemMgr::get_subsystem (const string &name) const
571 {
572     SubsystemDict::const_iterator s =_subsystem_map.find(name);
573
574     if (s == _subsystem_map.end())
575         return 0;
576     else
577         return s->second;
578 }
579
580 /** Trigger the timing callback to report data for all subsystems. */
581 void
582 SGSubsystemMgr::reportTiming()
583 {
584     for (int i = 0; i < MAX_GROUPS; i++) {
585         _groups[i]->reportTiming();
586     } // of groups iteration
587 }
588
589 // end of subsystem_mgr.cxx