opensurgsim
BasicThread.h
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 
25 #include "SurgSim/Framework/Barrier.h"
26 #include "SurgSim/Framework/Timer.h"
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 
53  // As per https://docs.microsoft.com/en-us/cpp/cpp/exception-specifications-throw-cpp?view=vs-2019
54  // noexcept(false) has implementation from Visual Studio 2017 15.5
55 #if defined(_MSC_VER) && _MSC_VER < 1912
56  virtual ~BasicThread() throw(...);
57 #else
58  virtual ~BasicThread() noexcept(false);
59 #endif
60 
63 
71  void start(std::shared_ptr<Barrier> startupBarrier = nullptr, bool isSynchronous = false);
72 
77  void stop();
78 
81  void setIdle(bool isIdle);
82 
85  bool isIdle();
86 
89  bool isInitialized();
90 
93  bool isRunning() const;
94 
96  void operator()();
97 
99  boost::thread& getThread();
100 
102  std::string getName() const;
103 
106  void setRate(double val)
107  {
108  m_period = boost::chrono::duration<double>(1.0 / val);
109  }
110 
119  bool setSynchronous(bool val);
120 
123  bool isSynchronous();
124 
128  double getCpuTime() const;
129 
132  size_t getUpdateCount() const;
133 
136 
137  bool ignoresExceptions() const;
138  void setIgnoreExceptions(bool val);
139 protected:
140 
143 
147  bool initialize();
148 
153  bool startUp();
154 
155  bool waitForBarrier(bool success);
156 
157  virtual bool executeInitialization();
158 
160  std::shared_ptr<SurgSim::Framework::Logger> m_logger;
161 
162 private:
163  std::string m_name;
164 
165  boost::thread m_thisThread;
166  boost::chrono::duration<double> m_period;
167  std::shared_ptr<Barrier> m_startupBarrier;
168 
169  // Protects the start and stop functions so on can only execute once the other is done
170  boost::mutex m_mutexStartStop;
171 
172  bool m_isIdle;
173  bool m_isInitialized;
174  bool m_isRunning;
175  bool m_stopExecution;
176  bool m_isSynchronous;
177  bool m_ignoreExceptions;
178 
179  virtual bool doInitialize() = 0;
180  virtual bool doStartUp() = 0;
181 
186  virtual bool doUpdate(double dt);
187 
190  virtual void doBeforeStop();
191 };
192 
193 }; // namespace Framework
194 }; // namespace SurgSim
195 
196 #endif // SURGSIM_FRAMEWORK_BASICTHREAD_H
boost::thread & getThread()
Definition: BasicThread.cpp:105
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
void start(std::shared_ptr< Barrier > startupBarrier=nullptr, bool isSynchronous=false)
Live cycle functions, public implementation.
Definition: BasicThread.cpp:90
void stop()
Stopping the execution, blocks until the running thread has actually stopped,.
Definition: BasicThread.cpp:200
void setRate(double val)
Set the update rate of the thread.
Definition: BasicThread.h:106
bool startUp()
Trigger the startup of this object, this will be called after all other threads doInit() was called t...
Definition: BasicThread.cpp:85
std::shared_ptr< SurgSim::Framework::Logger > m_logger
Logger for this thread.
Definition: BasicThread.h:160
Timer class, measures execution times.
Definition: Timer.h:31
std::string getName() const
Definition: BasicThread.cpp:233
bool setSynchronous(bool val)
Sets the thread to synchronized execution in concert with the startup barrier, the startup barrier ha...
Definition: BasicThread.cpp:274
bool isRunning() const
Query if this object is running.
Definition: BasicThread.cpp:73
bool initialize()
Trigger the initialization of this object, this will be called before all other threads doStartup() a...
Definition: BasicThread.cpp:78
virtual ~BasicThread() noexcept(false)
C++11 introduced noexcept.
Definition: BasicThread.cpp:54
Timer m_timer
Timer to measure the actual time taken to doUpdate.
Definition: BasicThread.h:142
void operator()()
This is what boost::thread executes on thread creation.
Definition: BasicThread.cpp:110
bool isSynchronous()
Query if this object is synchronized.
Definition: BasicThread.cpp:283
size_t getUpdateCount() const
Definition: BasicThread.cpp:293
Basic thread implementation, tries to maintain a constant rate, supplies startup an initialization...
Definition: BasicThread.h:48
bool isIdle()
Query if this thread is in idle state or not.
Definition: BasicThread.cpp:228
void setIdle(bool isIdle)
Set/Unset the thread in an idle state (doUpdate() called or not in the update() method) ...
Definition: BasicThread.cpp:223
double getCpuTime() const
Definition: BasicThread.cpp:288
bool isInitialized()
Query if this object is initialized.
Definition: BasicThread.cpp:68
void resetCpuTimeAndUpdateCount()
Reset the cpu time and the update count to 0.
Definition: BasicThread.cpp:298