1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
10
// OpenWalnut is free software: you can redistribute it and/or modify
11
// it under the terms of the GNU Lesser General Public License as published by
12
// the Free Software Foundation, either version 3 of the License, or
13
// (at your option) any later version.
15
// OpenWalnut is distributed in the hope that it will be useful,
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
// GNU Lesser General Public License for more details.
20
// You should have received a copy of the GNU Lesser General Public License
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
23
//---------------------------------------------------------------------------
25
#ifndef WWORKERTHREAD_H
26
#define WWORKERTHREAD_H
28
#include <string> // because of std::size_t
31
#include <boost/shared_ptr.hpp>
32
#include <boost/signals2/signal.hpp>
35
#include "WException.h"
36
#include "WThreadedRunner.h"
39
* A worker thread that belongs to a \see WThreadedFunction object.
41
template< class Function_T >
42
class WWorkerThread : public WThreadedRunner
45
//! a type for stop signals
46
typedef boost::signals2::signal< void () > StopSignal;
48
//! a type for exception signals
49
typedef boost::signals2::signal< void ( WException const& ) > ExceptionSignal;
53
//! a type for stop callbacks
54
typedef boost::function< void () > StopFunction;
56
//! a type for exception callbacks
57
typedef boost::function< void ( WException const& ) > ExceptionFunction;
60
* Default constructor.
62
* \param func A pointer to the function object.
63
* \param id A thread id.
64
* \param numThreads The number of threads.
66
WWorkerThread( boost::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads );
71
virtual ~WWorkerThread();
74
* Subscribe a function to the exception signal.
76
* \param func The function.
78
void subscribeExceptionSignal( ExceptionFunction func );
81
* Subscribe a function to the stop signal.
83
* \param func The function.
85
void subscribeStopSignal( StopFunction func );
89
* The thread's main function.
91
virtual void threadMain();
95
* WWorkerThread is non-copyable, so the copy constructor is not implemented.
97
WWorkerThread( WWorkerThread const& ); // NOLINT
100
* WWorkerThread is non-copyable, so the copy operator is not implemented.
102
* \return this worker-thread.
104
WWorkerThread& operator = ( WWorkerThread const& );
106
//! the functor called in threadMain()
107
boost::shared_ptr< Function_T > m_func;
109
//! a thread id between 0 and m_numThreads - 1
112
//! the number of threads
113
std::size_t m_numThreads;
115
//! the exception signal
116
ExceptionSignal m_exceptionSignal;
119
StopSignal m_stopSignal;
122
template< class Function_T >
123
WWorkerThread< Function_T >::WWorkerThread( boost::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads )
126
m_numThreads( numThreads ),
130
if( id >= numThreads )
132
throw WException( std::string( "The id of this thread is not valid." ) );
136
throw WException( std::string( "No thread function provided!" ) );
140
template< class Function_T >
141
WWorkerThread< Function_T >::~WWorkerThread()
143
m_exceptionSignal.disconnect_all_slots();
144
m_stopSignal.disconnect_all_slots();
147
template< class Function_T >
148
void WWorkerThread< Function_T >::subscribeExceptionSignal( ExceptionFunction func )
152
m_exceptionSignal.connect( func );
156
template< class Function_T >
157
void WWorkerThread< Function_T >::subscribeStopSignal( StopFunction func )
161
m_stopSignal.connect( func );
165
template< class Function_T >
166
void WWorkerThread< Function_T >::threadMain()
172
m_func->operator() ( m_id, m_numThreads, m_shutdownFlag );
174
catch( WException const& e )
176
m_exceptionSignal( e );
179
catch( std::exception const& e )
181
WException w( std::string( e.what() ) );
182
m_exceptionSignal( w );
187
WException w( std::string( "An exception was thrown." ) );
188
m_exceptionSignal( w );
195
#endif // WWORKERTHREAD_H