Line data Source code
1 :
2 : /* Copyright (c) 2011-2014, Stefan Eilemann <eile@eyescale.ch>
3 : *
4 : * This file is part of Collage <https://github.com/Eyescale/Collage>
5 : *
6 : * This library is free software; you can redistribute it and/or modify it under
7 : * the terms of the GNU Lesser General Public License version 2.1 as published
8 : * by the Free Software Foundation.
9 : *
10 : * This library is distributed in the hope that it will be useful, but WITHOUT
11 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 : * details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public License
16 : * along with this library; if not, write to the Free Software Foundation, Inc.,
17 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #ifndef CO_WORKER_H
21 : #define CO_WORKER_H
22 :
23 : #include <co/api.h> // CO_API definition
24 : #include <co/types.h>
25 : #include <limits.h>
26 : #include <lunchbox/thread.h> // base class
27 :
28 : #ifdef COLLAGE_SHARED
29 : #define CO_WORKER_API CO_API
30 : #else
31 : #define CO_WORKER_API
32 : #endif
33 :
34 : namespace co
35 : {
36 : /** A worker thread processing items out of a CommandQueue. */
37 : template <class Q>
38 : class WorkerThread : public lunchbox::Thread
39 : {
40 : public:
41 : /** Construct a new worker thread. @version 1.0 */
42 53 : explicit WorkerThread(const size_t maxSize = ULONG_MAX)
43 53 : : _commands(maxSize)
44 : {
45 53 : }
46 :
47 : /** Destruct the worker. @version 1.0 */
48 51 : virtual ~WorkerThread() {}
49 : /** @return the queue to the worker thread. @version 1.0 */
50 142 : Q* getWorkerQueue() { return &_commands; }
51 : protected:
52 : /** @sa lunchbox::Thread::run() */
53 : CO_WORKER_API void run() override;
54 :
55 : /** @return true to stop the worker thread. @version 1.0 */
56 0 : virtual bool stopRunning() { return false; }
57 : /** @return true to indicate pending idle tasks. @version 1.0 */
58 0 : virtual bool notifyIdle() { return false; }
59 : private:
60 : /** The receiver->worker thread command queue. */
61 : Q _commands;
62 : };
63 :
64 : typedef WorkerThread<CommandQueue> Worker; // instantiated in worker.cpp
65 : }
66 : #endif // CO_WORKER_H
|