SimulationThread.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Tue Dec 21 22:43:07 CET 2004 SimulationThread.cxx
3 
4  SimulationThread.cxx - description
5  -------------------
6  begin : Tue December 21 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 
40 #include "SimulationThread.hpp"
41 #include "../os/TimeService.hpp"
42 #include "SimulationActivity.hpp"
43 #include "../Logger.hpp"
44 #include "../os/threads.hpp"
45 #include "../os/MainThread.hpp"
46 
47 #include "../os/StartStopManager.hpp"
48 namespace RTT {
49  using namespace extras;
50  namespace
51  {
52  // Stop it before the application quits.
53  void stopSIMThread()
54  {
56  }
57 
58  os::CleanupFunction SIMCleanup( &stopSIMThread );
59  }
60 }
61 
62 namespace RTT {
63  using namespace extras;
64  using namespace os;
65 
66  // The static class variables
68 
70  {
71  if ( !_instance )
72  {
73  _instance.reset( new SimulationThread(period) );
74  }
75 
76  return _instance;
77  }
78 
80  {
81  _instance.reset();
82  return true;
83  }
84 
85 
88  "SimulationThread",
89  period),
90  beat( TimeService::Instance() ), maxsteps_(0), sim_running(false)
91  {
92  Logger::In in("SimulationThread");
94  Logger::log() << Logger::Info << this->getName() <<" created with "<< this->getPeriod() <<"s periodicity";
95  Logger::log() << Logger::Info << " and priority " << this->getPriority() << Logger::endl;
96  }
97 
99  {
100  this->stop();
101  }
102 
104  {
105  return sim_running || Thread::isRunning();
106  }
107 
109  {
110  maxsteps_ = 0;
111  return os::Thread::start();
112  }
113 
114  bool SimulationThread::start(unsigned int maxsteps)
115  {
116  if (maxsteps == 0)
117  return false;
118  maxsteps_ = maxsteps;
119  return os::Thread::start();
120  }
121 
122  bool SimulationThread::run(unsigned int ms)
123  {
124  if ( ms == 0 || this->isRunning() || this->initialize() == false )
125  return false;
126  unsigned int cur = 0;
127  this->sim_running = true;
128  while( cur != ms ) {
129  ++cur;
131  beat->secondsChange(this->getPeriod());
132  }
133  this->sim_running = false;
134  this->finalize();
135  return true;
136  }
138  return os::MainThread::Instance();
139  }
140 
142  {
143  Logger::In in("SimulationThread");
144  Logger::log() << Logger::Info << "SimulationThread takes over system time."<<Logger::nl;
145  Logger::log() << Logger::Info << "System time will increase significantly faster."<<Logger::endl;
146 
147  // we will update the clock in step()
148  beat->enableSystemClock( false );
149 
150  cursteps = 0;
151  // No TimerThread::initialize() to allow 'freeze'
152  return true;
153  }
154 
156  {
157  Logger::In in("SimulationThread");
158  Logger::log() << Logger::Info << "SimulationThread releases system time."<<Logger::endl;
159  // release systemclock again.
160  beat->enableSystemClock( true );
161 
162  // DO NOT CALL TimerThread::finalize(), since we want to be able to start/stop the
163  // SimulationThread and inspect the activities still running.
164  }
165 
167  {
168  ++cursteps;
169 
170  if ( maxsteps_ == 0 || cursteps < maxsteps_ + 1 ) {
172  beat->secondsChange(this->getPeriod());
173  }
174 
175  // call stop once :
176  if ( cursteps == maxsteps_ ) { // if maxsteps == 0, will never call stop().
177  this->stop();
178  }
179  }
180 
181 }
static ThreadInterface * Instance()
Definition: MainThread.cpp:58
virtual bool run(unsigned int maxsteps)
static std::ostream & nl(std::ostream &__os)
Definition: Logger.cpp:373
virtual bool setScheduler(int sched_type)
Definition: Thread.cpp:476
static SimulationThreadPtr Instance(double period=0.001)
boost::shared_ptr< SimulationThread > SimulationThreadPtr
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
virtual os::ThreadInterface * simthread()
void enableSystemClock(bool yes_no)
Definition: TimeService.cpp:95
static SimulationThreadPtr _instance
static Logger & log()
Definition: Logger.cpp:117
virtual bool start()
Definition: Thread.cpp:345
const int LowestPriority
Definition: ecosthreads.cpp:44
virtual bool stop()
Definition: Thread.cpp:424
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
virtual int getPriority() const
Definition: Thread.cpp:593
virtual Seconds getPeriod() const
Definition: Thread.cpp:598
virtual bool isRunning() const
#define ORO_SCHED_OTHER
Definition: ecos/fosi.h:62
Seconds secondsChange(Seconds delta)
virtual const char * getName() const
Definition: Thread.cpp:645
virtual bool isRunning() const
Definition: Thread.cpp:466


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:36