~ubuntu-branches/ubuntu/wily/openwalnut/wily-proposed

« back to all changes in this revision

Viewing changes to .pc/gcc5.patch/src/core/common/WWorkerThread.h

  • Committer: Package Import Robot
  • Author(s): Michael Terry
  • Date: 2015-08-12 13:14:55 UTC
  • Revision ID: package-import@ubuntu.com-20150812131455-cwndvoy9wwx34ya2
Tags: 1.4.0~rc1+hg3a3147463ee2-1ubuntu4
* debian/patches/gcc5.patch:
  - Work around incompatibility between boost+gcc5 and Qt4, fixing FTBFS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//---------------------------------------------------------------------------
 
2
//
 
3
// Project: OpenWalnut ( http://www.openwalnut.org )
 
4
//
 
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
 
6
// For more information see http://www.openwalnut.org/copying
 
7
//
 
8
// This file is part of OpenWalnut.
 
9
//
 
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.
 
14
//
 
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.
 
19
//
 
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/>.
 
22
//
 
23
//---------------------------------------------------------------------------
 
24
 
 
25
#ifndef WWORKERTHREAD_H
 
26
#define WWORKERTHREAD_H
 
27
 
 
28
#include <string> // because of std::size_t
 
29
#include <exception>
 
30
 
 
31
#include <boost/shared_ptr.hpp>
 
32
#include <boost/signals2/signal.hpp>
 
33
 
 
34
#include "WAssert.h"
 
35
#include "WException.h"
 
36
#include "WThreadedRunner.h"
 
37
 
 
38
/**
 
39
 * A worker thread that belongs to a \see WThreadedFunction object.
 
40
 */
 
41
template< class Function_T >
 
42
class WWorkerThread : public WThreadedRunner
 
43
{
 
44
    // typedefs
 
45
    //! a type for stop signals
 
46
    typedef boost::signals2::signal< void () > StopSignal;
 
47
 
 
48
    //! a type for exception signals
 
49
    typedef boost::signals2::signal< void ( WException const& ) > ExceptionSignal;
 
50
 
 
51
public:
 
52
    //typedefs
 
53
    //! a type for stop callbacks
 
54
    typedef boost::function< void () > StopFunction;
 
55
 
 
56
    //! a type for exception callbacks
 
57
    typedef boost::function< void ( WException const& ) > ExceptionFunction;
 
58
 
 
59
    /**
 
60
     * Default constructor.
 
61
     *
 
62
     * \param func A pointer to the function object.
 
63
     * \param id A thread id.
 
64
     * \param numThreads The number of threads.
 
65
     */
 
66
    WWorkerThread( boost::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads );
 
67
 
 
68
    /**
 
69
     * Default destructor.
 
70
     */
 
71
    virtual ~WWorkerThread();
 
72
 
 
73
    /**
 
74
     * Subscribe a function to the exception signal.
 
75
     *
 
76
     * \param func The function.
 
77
     */
 
78
    void subscribeExceptionSignal( ExceptionFunction func );
 
79
 
 
80
    /**
 
81
     * Subscribe a function to the stop signal.
 
82
     *
 
83
     * \param func The function.
 
84
     */
 
85
    void subscribeStopSignal( StopFunction func );
 
86
 
 
87
protected:
 
88
    /**
 
89
     * The thread's main function.
 
90
     */
 
91
    virtual void threadMain();
 
92
 
 
93
private:
 
94
    /**
 
95
     * WWorkerThread is non-copyable, so the copy constructor is not implemented.
 
96
     */
 
97
    WWorkerThread( WWorkerThread const& ); // NOLINT
 
98
 
 
99
    /**
 
100
     * WWorkerThread is non-copyable, so the copy operator is not implemented.
 
101
     *
 
102
     * \return this worker-thread.
 
103
     */
 
104
    WWorkerThread& operator = ( WWorkerThread const& );
 
105
 
 
106
    //! the functor called in threadMain()
 
107
    boost::shared_ptr< Function_T > m_func;
 
108
 
 
109
    //! a thread id between 0 and m_numThreads - 1
 
110
    std::size_t m_id;
 
111
 
 
112
    //! the number of threads
 
113
    std::size_t m_numThreads;
 
114
 
 
115
    //! the exception signal
 
116
    ExceptionSignal m_exceptionSignal;
 
117
 
 
118
    //! the stop signal
 
119
    StopSignal m_stopSignal;
 
120
};
 
121
 
 
122
template< class Function_T >
 
123
WWorkerThread< Function_T >::WWorkerThread( boost::shared_ptr< Function_T > func, std::size_t id, std::size_t numThreads )
 
124
    : m_func( func ),
 
125
      m_id( id ),
 
126
      m_numThreads( numThreads ),
 
127
      m_exceptionSignal(),
 
128
      m_stopSignal()
 
129
{
 
130
    if( id >= numThreads )
 
131
    {
 
132
        throw WException( std::string( "The id of this thread is not valid." ) );
 
133
    }
 
134
    if( !m_func )
 
135
    {
 
136
        throw WException( std::string( "No thread function provided!" ) );
 
137
    }
 
138
}
 
139
 
 
140
template< class Function_T >
 
141
WWorkerThread< Function_T >::~WWorkerThread()
 
142
{
 
143
    m_exceptionSignal.disconnect_all_slots();
 
144
    m_stopSignal.disconnect_all_slots();
 
145
}
 
146
 
 
147
template< class Function_T >
 
148
void WWorkerThread< Function_T >::subscribeExceptionSignal( ExceptionFunction func )
 
149
{
 
150
    if( func )
 
151
    {
 
152
        m_exceptionSignal.connect( func );
 
153
    }
 
154
}
 
155
 
 
156
template< class Function_T >
 
157
void WWorkerThread< Function_T >::subscribeStopSignal( StopFunction func )
 
158
{
 
159
    if( func )
 
160
    {
 
161
        m_stopSignal.connect( func );
 
162
    }
 
163
}
 
164
 
 
165
template< class Function_T >
 
166
void WWorkerThread< Function_T >::threadMain()
 
167
{
 
168
    if( m_func )
 
169
    {
 
170
        try
 
171
        {
 
172
            m_func->operator() ( m_id, m_numThreads, m_shutdownFlag );
 
173
        }
 
174
        catch( WException const& e )
 
175
        {
 
176
            m_exceptionSignal( e );
 
177
            return;
 
178
        }
 
179
        catch( std::exception const& e )
 
180
        {
 
181
            WException w( std::string( e.what() ) );
 
182
            m_exceptionSignal( w );
 
183
            return;
 
184
        }
 
185
        catch( ... )
 
186
        {
 
187
            WException w( std::string( "An exception was thrown." ) );
 
188
            m_exceptionSignal( w );
 
189
            return;
 
190
        }
 
191
    }
 
192
    m_stopSignal();
 
193
}
 
194
 
 
195
#endif  // WWORKERTHREAD_H