Orocos Real-Time Toolkit  2.9.0
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
67  SimulationThreadPtr SimulationThread::_instance;
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 }
The TimeService is used for system-wide time keeping and conversions.
Definition: TimeService.hpp:35
This Periodic Thread is meant for executing a PeriodicActivity object periodically.
Definition: TimerThread.hpp:69
static ThreadInterface * Instance()
Return an object which represents the main thread.
Definition: MainThread.cpp:58
virtual ~SimulationThread()
Destructor.
SimulationThread(double period)
Constructor.
virtual bool run(unsigned int maxsteps)
Execute maxsteps steps immediately.
static std::ostream & nl(std::ostream &__os)
Insert a newline &#39; &#39; in the ostream.
Definition: Logger.cpp:373
virtual bool setScheduler(int sched_type)
Change the scheduler policy in which this thread runs.
Definition: Thread.cpp:469
static SimulationThreadPtr Instance(double period=0.001)
Create the SimulationThread with a given period.
A thread which is being run.
boost::shared_ptr< SimulationThread > SimulationThreadPtr
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
virtual os::ThreadInterface * simthread()
Always returns the MainThread.
void enableSystemClock(bool yes_no)
Enables or disables reading the system clock.
Definition: TimeService.cpp:76
This thread is the simulated real-time periodic thread in the Orocos system.
Notify the Logger in which &#39;module&#39; the message occured.
Definition: Logger.hpp:159
static Logger & log()
As Instance(), but more userfriendly.
Definition: Logger.cpp:117
static bool Release()
Releases the SimulationThread Reference counting might aid in making this call safe.
virtual bool start()
Start the Thread.
Definition: Thread.cpp:338
const int LowestPriority
An integer denoting the lowest priority of the selected OS.
Definition: ecosthreads.cpp:44
virtual bool stop()
Stop the Thread.
Definition: Thread.cpp:417
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
virtual int getPriority() const
The priority of this Thread.
Definition: Thread.cpp:586
virtual Seconds getPeriod() const
Get the periodicity in Seconds.
Definition: Thread.cpp:591
virtual bool isRunning() const
Returns true if thread is running or run( unsigned int ) is being invoked.
#define ORO_SCHED_OTHER
Definition: fosi.h:50
Seconds secondsChange(Seconds delta)
Change the time with delta seconds.
virtual bool start()
Start the Thread.
virtual const char * getName() const
Read the name of this task.
Definition: Thread.cpp:633
virtual bool isRunning() const
Returns whether the thread is running.
Definition: Thread.cpp:459