OpenWalnut  1.4.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes
WThreadedRunner Class Reference

Base class for all classes needing to be executed in a separate thread. More...

#include <WThreadedRunner.h>

+ Inheritance diagram for WThreadedRunner:

List of all members.

Public Types

typedef boost::function< void(void) > THREADFUNCTION
 Type used for simple thread functions.

Public Member Functions

 WThreadedRunner ()
 Default constructor.
virtual ~WThreadedRunner ()
 Destructor.
virtual void run ()
 Run thread.
void run (THREADFUNCTION f)
 Run thread.
void wait (bool requestFinish=false)
 Wait for the thread to be finished.
virtual void requestStop ()
 This method's purpose is to request a stop without waiting for it.
virtual boost::signals2::connection subscribeSignal (THREAD_SIGNAL signal, t_ThreadErrorSignalHandlerType notifier)
 Connects a specified notify function with a signal this thread instance is offering.
const WBoolFlagisCrashed () const
 Checks whether this thread has been crashed.
const std::string & getCrashMessage () const
 Get the message of the exception finally causing the crash.
void setThreadName (std::string name)
 Set the name of the thread.
std::string getThreadName () const
 Returns the current thread name.

Static Public Member Functions

static void setThisThreadName (std::string name)
 Static function to set the name of the calling thread.

Protected Member Functions

virtual void threadMain ()
 Function that has to be overwritten for execution.
virtual void notifyStop ()
 Gets called when the thread should be stopped.
void yield () const
 Give remaining execution timeslice to another thread.
void sleep (const int32_t t) const
 Sets thread asleep.
void msleep (const int32_t t) const
 Sets thread asleep.
void waitForStop ()
 Let the thread sleep until a stop request was given.
virtual void onThreadException (const WException &e)
 This method is called if an exception was caught, which came from the custom thread code.
void handleDeadlyException (const WException &e, std::string sender="WThreadedRunner")
 Handle the specified exception which was not caught in the thread, which basically means the thread has crashed.

Protected Attributes

boost::thread m_thread
 Thread instance.
WBoolFlag m_shutdownFlag
 Condition getting fired whenever the thread should quit.
WBoolFlag m_isCrashed
 True whenever an exception is thrown during threadMain.
std::string m_crashMessage
 The crash message.

Private Member Functions

 WThreadedRunner (const WThreadedRunner &rhs)
 Disallow copy construction.
WThreadedRunneroperator= (const WThreadedRunner &rhs)
 Disallow copy assignment.
void threadMainSave ()
 The is the thread entry point.

Private Attributes

t_ThreadErrorSignalType signal_thread_error
 Signal fired whenever a thread throws an exception/error.
std::string m_threadName
 This threads name.

Detailed Description

Base class for all classes needing to be executed in a separate thread.

Definition at line 44 of file WThreadedRunner.h.


Member Typedef Documentation

typedef boost::function< void ( void ) > WThreadedRunner::THREADFUNCTION

Type used for simple thread functions.

Definition at line 50 of file WThreadedRunner.h.


Constructor & Destructor Documentation

Default constructor.

Definition at line 39 of file WThreadedRunner.cpp.

Destructor.

Definition at line 48 of file WThreadedRunner.cpp.

Disallow copy construction.

Parameters:
rhsthe other threaded runner.

Member Function Documentation

const std::string & WThreadedRunner::getCrashMessage ( ) const

Get the message of the exception finally causing the crash.

Returns:
the message

Definition at line 172 of file WThreadedRunner.cpp.

References m_crashMessage.

std::string WThreadedRunner::getThreadName ( ) const

Returns the current thread name.

Returns:
the name, empty if no name was specified.

Definition at line 167 of file WThreadedRunner.cpp.

References m_threadName.

Referenced by threadMainSave().

void WThreadedRunner::handleDeadlyException ( const WException e,
std::string  sender = "WThreadedRunner" 
) [protected]

Handle the specified exception which was not caught in the thread, which basically means the thread has crashed.

This triggers the error notification and marks the thread as crashed. If you write your own exception/error mechanism (like WModule), you should take care that these method gets called.

Notes:
this method does not re-throw the exception
Notes:
you should specify a custom sender string if you overwrite onThreadException.
Parameters:
ethe exception
senderallows to customize the sender information in the log entry created by this method.

Definition at line 63 of file WThreadedRunner.cpp.

References wlog::error(), m_crashMessage, m_isCrashed, signal_thread_error, and WException::what().

Referenced by onThreadException(), WProjectFile::onThreadException(), and WModule::onThreadException().

Checks whether this thread has been crashed.

This will be true whenever the code in the thread throws an unhandled exception.

Returns:
true if there has been an exception during threadMain().

Definition at line 53 of file WThreadedRunner.cpp.

References m_isCrashed.

void WThreadedRunner::msleep ( const int32_t  t) const [protected]

Sets thread asleep.

Parameters:
ttime to sleep in microseconds.

Definition at line 145 of file WThreadedRunner.cpp.

References sleep().

void WThreadedRunner::notifyStop ( ) [protected, virtual]

Gets called when the thread should be stopped.

The purpose of this method is to allow derived classes to handle this kind of event.

Reimplemented in WGraphicsEngine.

Definition at line 131 of file WThreadedRunner.cpp.

Referenced by requestStop().

void WThreadedRunner::onThreadException ( const WException e) [protected, virtual]

This method is called if an exception was caught, which came from the custom thread code.

This method is virtual and allows you to overwrite the default behaviour. If you overwrite this method, you should call handleDeadlyException or WThreadedRunner::onThreadException if you are finished with your customized code.

Parameters:
ethe exception that was caught.

Reimplemented in WModule, and WProjectFile.

Definition at line 58 of file WThreadedRunner.cpp.

References handleDeadlyException().

Referenced by threadMainSave().

WThreadedRunner& WThreadedRunner::operator= ( const WThreadedRunner rhs) [private]

Disallow copy assignment.

Parameters:
rhsthe other threaded runner.
Returns:
this.
void WThreadedRunner::requestStop ( ) [virtual]

This method's purpose is to request a stop without waiting for it.

Definition at line 94 of file WThreadedRunner.cpp.

References m_shutdownFlag, and notifyStop().

Referenced by WModuleContainer::moduleError(), WWorkerThreadTest::testStopThread(), and wait().

void WThreadedRunner::run ( ) [virtual]

Run thread.

This does not start threadMain(() but runs a specified function instead.

Parameters:
fthe function to run instead of the threadMain method.

Definition at line 80 of file WThreadedRunner.cpp.

References m_thread.

void WThreadedRunner::setThisThreadName ( std::string  name) [static]

Static function to set the name of the calling thread.

Parameters:
namethe name.

Definition at line 177 of file WThreadedRunner.cpp.

Referenced by threadMainSave().

void WThreadedRunner::setThreadName ( std::string  name)

Set the name of the thread.

This can be handy for debugging as it gets set on Linux as the pthread name. You MUST set this before starting the thread.

Parameters:
namethe name

Definition at line 162 of file WThreadedRunner.cpp.

References m_threadName.

Referenced by WModule::initialize(), WGraphicsEngine::WGraphicsEngine(), and WKernel::WKernel().

void WThreadedRunner::sleep ( const int32_t  t) const [protected]

Sets thread asleep.

Parameters:
ttime to sleep in seconds.

Definition at line 140 of file WThreadedRunner.cpp.

Referenced by WModuleImpl::moduleMain(), msleep(), WThreadedRunnerTest::testSleep(), and WThreadedRunnerImpl::threadMain().

boost::signals2::connection WThreadedRunner::subscribeSignal ( THREAD_SIGNAL  signal,
t_ThreadErrorSignalHandlerType  notifier 
) [virtual]

Connects a specified notify function with a signal this thread instance is offering.

Exceptions:
WSignalSubscriptionFailedthrown if the signal can't be connected.
Parameters:
signalthe signal to connect to.
notifierthe notifier function to bind.
Returns:
connection descriptor.

Definition at line 150 of file WThreadedRunner.cpp.

References signal_thread_error.

void WThreadedRunner::threadMain ( ) [protected, virtual]

Function that has to be overwritten for execution.

It gets executed in a separate thread after run() has been called.

Reimplemented in WModule, WKernel, WProjectFile, WGraphicsEngine, WBatchLoader, WKdTreeThread, WWorkerThread< Function_T >, WModuleCombiner, WCreateColorArraysThread, and WThreadedRunnerImpl.

Definition at line 126 of file WThreadedRunner.cpp.

References WLogger::addLogMessage(), and WLogger::getLogger().

Referenced by threadMainSave().

void WThreadedRunner::threadMainSave ( ) [private]

The is the thread entry point.

It does exception handling and calls threadMain.

Definition at line 108 of file WThreadedRunner.cpp.

References getThreadName(), onThreadException(), setThisThreadName(), and threadMain().

Referenced by run().

void WThreadedRunner::wait ( bool  requestFinish = false)

Wait for the thread to be finished.

Parameters:
requestFinishtrue if the thread should be notified.

Definition at line 85 of file WThreadedRunner.cpp.

References m_thread, and requestStop().

Referenced by WDataSetFibers::init(), WWorkerThreadTest::testExceptions(), WWorkerThreadTest::testMultipleThreads(), WThreadedRunnerTest::testRun(), WWorkerThreadTest::testSingleThread(), WWorkerThreadTest::testStopThread(), and WKdTree::WKdTree().

void WThreadedRunner::waitForStop ( ) [protected]

Let the thread sleep until a stop request was given.

Definition at line 103 of file WThreadedRunner.cpp.

References m_shutdownFlag, and WFlag< T >::wait().

Referenced by WKernel::threadMain().

void WThreadedRunner::yield ( ) const [protected]

Give remaining execution timeslice to another thread.

Definition at line 135 of file WThreadedRunner.cpp.

References m_thread.


Member Data Documentation

std::string WThreadedRunner::m_crashMessage [protected]

The crash message.

Only filled if m_isCrashed is true.

Definition at line 210 of file WThreadedRunner.h.

Referenced by getCrashMessage(), and handleDeadlyException().

True whenever an exception is thrown during threadMain.

Definition at line 205 of file WThreadedRunner.h.

Referenced by handleDeadlyException(), isCrashed(), WModuleContainer::moduleError(), and WModule::WModule().

Condition getting fired whenever the thread should quit.

This is useful for waiting for stop requests.

Definition at line 178 of file WThreadedRunner.h.

Referenced by WKernel::isFinishRequested(), WModuleImpl::moduleMain(), requestStop(), WThreadedRunnerImpl::threadMain(), waitForStop(), and WModule::WModule().

boost::thread WThreadedRunner::m_thread [protected]

Thread instance.

Definition at line 149 of file WThreadedRunner.h.

Referenced by run(), wait(), and yield().

std::string WThreadedRunner::m_threadName [private]

This threads name.

Definition at line 241 of file WThreadedRunner.h.

Referenced by getThreadName(), and setThreadName().

t_ThreadErrorSignalType WThreadedRunner::signal_thread_error [private]

Signal fired whenever a thread throws an exception/error.

Definition at line 231 of file WThreadedRunner.h.

Referenced by handleDeadlyException(), and subscribeSignal().


The documentation for this class was generated from the following files: