BasicThread.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_BASICTHREAD_H
17 #define SURGSIM_FRAMEWORK_BASICTHREAD_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include <boost/thread.hpp>
23 #include <boost/chrono.hpp>
24 
27 
28 namespace SurgSim
29 {
30 namespace Framework
31 {
32 
33 class Component;
34 class Runtime;
35 
49 {
50 public:
51  explicit BasicThread(const std::string& name = "Unknown Thread");
52 #ifdef _MSC_VER
53  virtual ~BasicThread() throw(...); // Visual Studio does not support noexcept. The throw(...) is optional.
54 #else
55  virtual ~BasicThread() noexcept(false);
56 #endif
57 
60 
68  void start(std::shared_ptr<Barrier> startupBarrier = nullptr, bool isSynchronous = false);
69 
74  void stop();
75 
78  void setIdle(bool isIdle);
79 
82  bool isIdle();
83 
86  bool isInitialized();
87 
90  bool isRunning() const;
91 
93  void operator()();
94 
96  boost::thread& getThread();
97 
99  std::string getName() const;
100 
103  void setRate(double val)
104  {
105  m_period = boost::chrono::duration<double>(1.0 / val);
106  }
107 
116  bool setSynchronous(bool val);
117 
120  bool isSynchronous();
121 
125  double getCpuTime() const;
126 
129  size_t getUpdateCount() const;
130 
133 
134 protected:
135 
138 
142  bool initialize();
143 
148  bool startUp();
149 
150  bool waitForBarrier(bool success);
151 
152  virtual bool executeInitialization();
153 
155  std::shared_ptr<SurgSim::Framework::Logger> m_logger;
156 
157 private:
158  std::string m_name;
159 
160  boost::thread m_thisThread;
161  boost::chrono::duration<double> m_period;
162  std::shared_ptr<Barrier> m_startupBarrier;
163 
164  // Protects the start and stop functions so on can only execute once the other is done
165  boost::mutex m_mutexStartStop;
166 
167  bool m_isIdle;
172 
173  virtual bool doInitialize() = 0;
174  virtual bool doStartUp() = 0;
175 
180  virtual bool doUpdate(double dt);
181 
184  virtual void doBeforeStop();
185 };
186 
187 }; // namespace Framework
188 }; // namespace SurgSim
189 
190 #endif // SURGSIM_FRAMEWORK_BASICTHREAD_H
SurgSim::Framework::BasicThread::isInitialized
bool isInitialized()
Query if this object is initialized.
Definition: BasicThread.cpp:65
SurgSim::Framework::BasicThread::m_isIdle
bool m_isIdle
Definition: BasicThread.h:167
SurgSim::Framework::BasicThread::getThread
boost::thread & getThread()
Definition: BasicThread.cpp:102
SurgSim::Framework::Timer
Timer class, measures execution times.
Definition: Timer.h:30
SurgSim::Framework::BasicThread::setSynchronous
bool setSynchronous(bool val)
Sets the thread to synchronized execution in concert with the startup barrier, the startup barrier ha...
Definition: BasicThread.cpp:265
SurgSim::Framework::BasicThread::isRunning
bool isRunning() const
Query if this object is running.
Definition: BasicThread.cpp:70
SurgSim::Framework::BasicThread::initialize
bool initialize()
Trigger the initialization of this object, this will be called before all other threads doStartup() a...
Definition: BasicThread.cpp:75
SurgSim::Framework::BasicThread::m_logger
std::shared_ptr< SurgSim::Framework::Logger > m_logger
Logger for this thread.
Definition: BasicThread.h:155
SurgSim::Framework::BasicThread::waitForBarrier
bool waitForBarrier(bool success)
Definition: BasicThread.cpp:256
SurgSim::Framework::BasicThread::executeInitialization
virtual bool executeInitialization()
Definition: BasicThread.cpp:228
SurgSim::Framework::BasicThread::setRate
void setRate(double val)
Set the update rate of the thread.
Definition: BasicThread.h:103
SurgSim::Framework::BasicThread::startUp
bool startUp()
Trigger the startup of this object, this will be called after all other threads doInit() was called t...
Definition: BasicThread.cpp:82
SurgSim::Framework::BasicThread::isSynchronous
bool isSynchronous()
Query if this object is synchronized.
Definition: BasicThread.cpp:274
SurgSim::Framework::BasicThread::doInitialize
virtual bool doInitialize()=0
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::BasicThread::isIdle
bool isIdle()
Query if this thread is in idle state or not.
Definition: BasicThread.cpp:218
SurgSim::Framework::BasicThread::m_name
std::string m_name
Definition: BasicThread.h:158
Barrier.h
Timer.h
SurgSim::Framework::BasicThread::stop
void stop()
Stopping the execution, blocks until the running thread has actually stopped,.
Definition: BasicThread.cpp:190
SurgSim::Framework::BasicThread::doBeforeStop
virtual void doBeforeStop()
Prepares the thread for its execution to be stopped.
Definition: BasicThread.cpp:299
SurgSim::Framework::BasicThread::m_isRunning
bool m_isRunning
Definition: BasicThread.h:169
SurgSim::Framework::BasicThread
Basic thread implementation, tries to maintain a constant rate, supplies startup an initialization,...
Definition: BasicThread.h:48
SurgSim::Framework::BasicThread::~BasicThread
virtual ~BasicThread() noexcept(false)
C++11 introduced noexcept.
Definition: BasicThread.cpp:51
SurgSim::Framework::BasicThread::setIdle
void setIdle(bool isIdle)
Set/Unset the thread in an idle state (doUpdate() called or not in the update() method)
Definition: BasicThread.cpp:213
SurgSim::Framework::BasicThread::getCpuTime
double getCpuTime() const
Definition: BasicThread.cpp:279
SurgSim::Framework::BasicThread::doUpdate
virtual bool doUpdate(double dt)
Implementation of actual work function for this thread, this has a default implementation to handle d...
Definition: BasicThread.cpp:294
SurgSim::Framework::BasicThread::operator()
void operator()()
This is what boost::thread executes on thread creation.
Definition: BasicThread.cpp:107
SurgSim::Framework::BasicThread::m_timer
Timer m_timer
Timer to measure the actual time taken to doUpdate.
Definition: BasicThread.h:137
SurgSim::Framework::BasicThread::m_stopExecution
bool m_stopExecution
Definition: BasicThread.h:170
SurgSim::Framework::BasicThread::getName
std::string getName() const
Definition: BasicThread.cpp:223
SurgSim::Framework::BasicThread::resetCpuTimeAndUpdateCount
void resetCpuTimeAndUpdateCount()
Reset the cpu time and the update count to 0.
Definition: BasicThread.cpp:289
SurgSim::Framework::BasicThread::m_thisThread
boost::thread m_thisThread
Definition: BasicThread.h:160
SurgSim::Framework::BasicThread::m_isSynchronous
bool m_isSynchronous
Definition: BasicThread.h:171
SurgSim::Framework::BasicThread::m_mutexStartStop
boost::mutex m_mutexStartStop
Definition: BasicThread.h:165
SurgSim::Framework::BasicThread::m_isInitialized
bool m_isInitialized
Definition: BasicThread.h:168
SurgSim::Framework::BasicThread::m_startupBarrier
std::shared_ptr< Barrier > m_startupBarrier
Definition: BasicThread.h:162
SurgSim::Framework::BasicThread::getUpdateCount
size_t getUpdateCount() const
Definition: BasicThread.cpp:284
SurgSim::Framework::BasicThread::BasicThread
BasicThread(const std::string &name="Unknown Thread")
Definition: BasicThread.cpp:31
SurgSim::Framework::BasicThread::doStartUp
virtual bool doStartUp()=0
SurgSim::Framework::BasicThread::start
void start(std::shared_ptr< Barrier > startupBarrier=nullptr, bool isSynchronous=false)
C++11 introduced noexcept.
Definition: BasicThread.cpp:87
SurgSim::Framework::BasicThread::m_period
boost::chrono::duration< double > m_period
Definition: BasicThread.h:161