Open Lighting Architecture  0.9.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExecutorThread.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * ExecutorThread.h
17  * Run callbacks in a separate thread.
18  * Copyright (C) 2015 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_THREAD_EXECUTORTHREAD_H_
22 #define INCLUDE_OLA_THREAD_EXECUTORTHREAD_H_
23 
24 #include <ola/Callback.h>
25 #include <ola/io/SelectServer.h>
26 #include <ola/thread/Thread.h>
27 #include <ola/thread/ConsumerThread.h>
28 
29 #include <memory>
30 #include <queue>
31 
32 namespace ola {
33 namespace thread {
34 
55  public:
61  : m_shutdown(false),
62  m_thread(&m_callback_queue, &m_shutdown, &m_mutex, &m_condition_var,
63  options) {
64  }
65 
66  ~ExecutorThread();
67 
68  void Execute(ola::BaseCallback0<void> *callback);
69 
76  void DrainCallbacks();
77 
85  bool Start();
86 
97  bool Stop();
98 
99  private:
100  // CallbackThread m_thread;
101  std::queue<ola::BaseCallback0<void>*> m_callback_queue;
102  bool m_shutdown;
103  Mutex m_mutex;
104  ConditionVariable m_condition_var;
105  ConsumerThread m_thread;
106 
107  void RunRemaining();
108 
109  DISALLOW_COPY_AND_ASSIGN(ExecutorThread);
110 };
111 
112 } // namespace thread
113 } // namespace ola
114 #endif // INCLUDE_OLA_THREAD_EXECUTORTHREAD_H_