BasicThread.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_FRAMEWORK_BASICTHREAD_H
#define SURGSIM_FRAMEWORK_BASICTHREAD_H
#include <memory>
#include <string>
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include "SurgSim/Framework/Barrier.h"
#include "SurgSim/Framework/Timer.h"
namespace SurgSim
{
namespace Framework
{
class Component;
class Runtime;
class BasicThread
{
public:
explicit BasicThread(const std::string& name = "Unknown Thread");
#ifdef _MSC_VER
virtual ~BasicThread() throw(...); // Visual Studio does not support noexcept. The throw(...) is optional.
#else
virtual ~BasicThread() noexcept(false);
#endif
void start(std::shared_ptr<Barrier> startupBarrier = nullptr, bool isSynchronous = false);
void stop();
void setIdle(bool isIdle);
bool isIdle();
bool isInitialized();
bool isRunning() const;
void operator()();
boost::thread& getThread();
std::string getName() const;
void setRate(double val)
{
m_period = boost::chrono::duration<double>(1.0 / val);
}
bool setSynchronous(bool val);
bool isSynchronous();
double getCpuTime() const;
size_t getUpdateCount() const;
void resetCpuTimeAndUpdateCount();
protected:
Timer m_timer;
bool initialize();
bool startUp();
bool waitForBarrier(bool success);
virtual bool executeInitialization();
std::shared_ptr<SurgSim::Framework::Logger> m_logger;
private:
std::string m_name;
boost::thread m_thisThread;
boost::chrono::duration<double> m_period;
std::shared_ptr<Barrier> m_startupBarrier;
// Protects the start and stop functions so on can only execute once the other is done
boost::mutex m_mutexStartStop;
bool m_isIdle;
bool m_isInitialized;
bool m_isRunning;
bool m_stopExecution;
bool m_isSynchronous;
virtual bool doInitialize() = 0;
virtual bool doStartUp() = 0;
virtual bool doUpdate(double dt);
virtual void doBeforeStop();
};
}; // namespace Framework
}; // namespace SurgSim
#endif // SURGSIM_FRAMEWORK_BASICTHREAD_H
SurgSim::Framework::BasicThread::m_thisThread
boost::thread m_thisThread
Definition: BasicThread.h:160
SurgSim::Framework::BasicThread::getThread
boost::thread & getThread()
Definition: BasicThread.cpp:102
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::BasicThread::m_isSynchronous
bool m_isSynchronous
Definition: BasicThread.h:171
SurgSim::Framework::BasicThread::waitForBarrier
bool waitForBarrier(bool success)
Definition: BasicThread.cpp:256
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::stop
void stop()
Stopping the execution, blocks until the running thread has actually stopped,.
Definition: BasicThread.cpp:190
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::m_logger
std::shared_ptr< SurgSim::Framework::Logger > m_logger
Logger for this thread.
Definition: BasicThread.h:155
Timer.h
SurgSim::Framework::BasicThread::executeInitialization
virtual bool executeInitialization()
Definition: BasicThread.cpp:228
SurgSim::Framework::Timer
Timer class, measures execution times.
Definition: Timer.h:30
SurgSim::Framework::BasicThread::getName
std::string getName() const
Definition: BasicThread.cpp:223
SurgSim::Framework::BasicThread::m_isIdle
bool m_isIdle
Definition: BasicThread.h:167
SurgSim::Framework::BasicThread::m_isRunning
bool m_isRunning
Definition: BasicThread.h:169
SurgSim::Framework::BasicThread::doBeforeStop
virtual void doBeforeStop()
Prepares the thread for its execution to be stopped.
Definition: BasicThread.cpp:299
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::doInitialize
virtual bool doInitialize()=0
SurgSim::Framework::BasicThread::BasicThread
BasicThread(const std::string &name="Unknown Thread")
Definition: BasicThread.cpp:31
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::~BasicThread
virtual ~BasicThread() noexcept(false)
C++11 introduced noexcept.
Definition: BasicThread.cpp:51
SurgSim::Framework::BasicThread::m_timer
Timer m_timer
Timer to measure the actual time taken to doUpdate.
Definition: BasicThread.h:137
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_name
std::string m_name
Definition: BasicThread.h:158
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::isSynchronous
bool isSynchronous()
Query if this object is synchronized.
Definition: BasicThread.cpp:274
SurgSim::Framework::BasicThread::getUpdateCount
size_t getUpdateCount() const
Definition: BasicThread.cpp:284
SurgSim::Framework::BasicThread
Basic thread implementation, tries to maintain a constant rate, supplies startup an initialization...
Definition: BasicThread.h:48
SurgSim::Framework::BasicThread::isIdle
bool isIdle()
Query if this thread is in idle state or not.
Definition: BasicThread.cpp:218
SurgSim::Framework::BasicThread::doStartUp
virtual bool doStartUp()=0
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::m_stopExecution
bool m_stopExecution
Definition: BasicThread.h:170
Barrier.h
SurgSim::Framework::BasicThread::m_startupBarrier
std::shared_ptr< Barrier > m_startupBarrier
Definition: BasicThread.h:162
SurgSim::Framework::BasicThread::m_period
boost::chrono::duration< double > m_period
Definition: BasicThread.h:161
SurgSim::Framework::BasicThread::isInitialized
bool isInitialized()
Query if this object is initialized.
Definition: BasicThread.cpp:65
SurgSim::Framework::BasicThread::resetCpuTimeAndUpdateCount
void resetCpuTimeAndUpdateCount()
Reset the cpu time and the update count to 0.
Definition: BasicThread.cpp:289