]> git.sesse.net Git - casparcg/blob - common/executor.h
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / common / executor.h
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #pragma once\r
23 \r
24 #include "except.h"\r
25 #include "enum_class.h"\r
26 #include "log.h"\r
27 \r
28 #include <tbb/atomic.h>\r
29 #include <tbb/concurrent_priority_queue.h>\r
30 #include <tbb/concurrent_queue.h>\r
31 \r
32 #include <boost/thread.hpp>\r
33 \r
34 #include <functional>\r
35 \r
36 namespace caspar {\r
37                 \r
38 struct task_priority_def\r
39 {\r
40         enum type\r
41         {\r
42                 lowest_priority = 0,\r
43                 lower_priority,\r
44                 low_priority,\r
45                 normal_priority,\r
46                 high_priority,\r
47                 higher_priority\r
48         };\r
49 };\r
50 typedef enum_class<task_priority_def> task_priority;\r
51 \r
52 class executor\r
53 {       \r
54         struct priority_function\r
55         {\r
56                 int                                             priority;\r
57                 std::function<void()>   func;\r
58 \r
59                 priority_function()\r
60                 {\r
61                 }\r
62 \r
63                 template<typename F>\r
64                 priority_function(int priority, F&& func)\r
65                         : priority(priority)\r
66                         , func(std::forward<F>(func))\r
67                 {\r
68                 }\r
69 \r
70                 void operator()()\r
71                 {\r
72                         func();\r
73                 }\r
74 \r
75                 bool operator<(const priority_function& other) const\r
76                 {\r
77                         return priority < other.priority;\r
78                 }\r
79         };\r
80 \r
81         executor(const executor&);\r
82         executor& operator=(const executor&);\r
83         \r
84         typedef tbb::concurrent_priority_queue<priority_function>       function_queue_t;\r
85         \r
86         const std::wstring                                                                                      name_;\r
87         tbb::atomic<bool>                                                                                       is_running_;\r
88         function_queue_t                                                                                        execution_queue_;\r
89         tbb::concurrent_bounded_queue<int>                                                      semaphore_;\r
90         boost::thread                                                                                           thread_;        \r
91                 \r
92 public:         \r
93         executor(const std::wstring& name)\r
94                 : name_(name)\r
95         {\r
96                 is_running_ = true;\r
97                 thread_ = boost::thread([this]{run();});\r
98         }\r
99         \r
100         virtual ~executor()\r
101         {\r
102                 try\r
103                 {\r
104                         internal_begin_invoke([this]\r
105                         {\r
106                                 is_running_ = false;\r
107                         }).wait();\r
108                 }\r
109                 catch(...)\r
110                 {\r
111                         CASPAR_LOG_CURRENT_EXCEPTION();\r
112                         is_running_ = false;\r
113                 }\r
114                 thread_.join();\r
115         }\r
116         \r
117         template<typename Func>\r
118         auto begin_invoke(Func&& func, task_priority priority = task_priority::normal_priority) -> boost::unique_future<decltype(func())> // noexcept\r
119         {       \r
120                 if(execution_queue_.size() > 128)\r
121                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("executor overflow.") << source_info(name_));\r
122                 \r
123                 if(!is_running_)\r
124                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("executor not running.") << source_info(name_));\r
125 \r
126                 return internal_begin_invoke(std::forward<Func>(func), priority);\r
127         }\r
128 \r
129         template<typename Func>\r
130         auto invoke(Func&& func, task_priority prioriy = task_priority::normal_priority) -> decltype(func()) // noexcept\r
131         {\r
132                 if(is_current())  // Avoids potential deadlock.\r
133                         return func();\r
134                 \r
135                 return begin_invoke(std::forward<Func>(func), prioriy).get();\r
136         }\r
137 \r
138         void yield()\r
139         {\r
140                 if(!is_current())\r
141                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Executor can only yield inside of thread context.")  << source_info(name_));\r
142 \r
143                 int dummy;\r
144                 if(!semaphore_.try_pop(dummy))\r
145                         return;\r
146 \r
147                 priority_function func;\r
148                 if(execution_queue_.try_pop(func))\r
149                         func();\r
150         }\r
151 \r
152         void set_capacity(std::size_t capacity)\r
153         {\r
154                 semaphore_.set_capacity(capacity);\r
155         }\r
156 \r
157         std::size_t capacity() const\r
158         {\r
159                 return semaphore_.capacity();\r
160         }\r
161         \r
162         void clear()\r
163         {               \r
164                 priority_function func;\r
165                 while(execution_queue_.try_pop(func));\r
166         }\r
167                                 \r
168         void stop()\r
169         {\r
170                 invoke([this]\r
171                 {\r
172                         is_running_ = false;\r
173                 });\r
174         }\r
175 \r
176         void wait()\r
177         {\r
178                 invoke([]{}, task_priority::lowest_priority);\r
179         }\r
180                 \r
181         function_queue_t::size_type size() const \r
182         {\r
183                 return execution_queue_.size(); \r
184         }\r
185                 \r
186         bool is_running() const\r
187         {\r
188                 return is_running_; \r
189         }       \r
190 \r
191         bool is_current() const\r
192         {\r
193                 return boost::this_thread::get_id() == thread_.get_id();\r
194         }\r
195                 \r
196 private:        \r
197                                                                 \r
198         template<typename Func>\r
199         auto internal_begin_invoke(Func&& func, task_priority priority = task_priority::normal_priority) -> boost::unique_future<decltype(func())> // noexcept\r
200         {                                       \r
201                 typedef typename std::remove_reference<Func>::type      function_type;\r
202                 typedef decltype(func())                                                        result_type;\r
203                 typedef boost::packaged_task<result_type>                       task_type;\r
204                                                                 \r
205                 std::unique_ptr<task_type> task;\r
206 \r
207                 // Use pointers since the boost thread library doesn't fully support move semantics.\r
208 \r
209                 auto raw_func2 = new function_type(std::forward<Func>(func));\r
210                 try\r
211                 {\r
212                         task.reset(new task_type([raw_func2]() -> result_type\r
213                         {\r
214                                 std::unique_ptr<function_type> func2(raw_func2);\r
215                                 return (*func2)();\r
216                         }));\r
217                 }\r
218                 catch(...)\r
219                 {\r
220                         delete raw_func2;\r
221                         throw;\r
222                 }\r
223                 \r
224                 task->set_wait_callback(std::function<void(task_type&)>([=](task_type& my_task) // The std::function wrapper is required in order to add ::result_type to functor class.\r
225                 {\r
226                         try\r
227                         {\r
228                                 if(is_current())  // Avoids potential deadlock.\r
229                                         my_task();\r
230                         }\r
231                         catch(boost::task_already_started&){}\r
232                 }));\r
233                                 \r
234                 auto future = task->get_future();\r
235 \r
236                 auto raw_task = task.release();\r
237                 priority_function prio_func(priority.value(), [raw_task]\r
238                 {\r
239                         std::unique_ptr<task_type> task(raw_task);\r
240                         try\r
241                         {\r
242                                 (*task)();\r
243                         }\r
244                         catch(boost::task_already_started&){}\r
245                 });\r
246 \r
247                 execution_queue_.push(prio_func);\r
248                 semaphore_.push(0);\r
249                                                         \r
250                 return std::move(future);               \r
251         }\r
252 \r
253         void run() // noexcept\r
254         {\r
255                 win32_exception::install_handler();             \r
256                 while(is_running_)\r
257                 {\r
258                         try\r
259                         {\r
260                                 int dummy;\r
261                                 semaphore_.pop(dummy);\r
262 \r
263                                 priority_function func;\r
264                                 if(execution_queue_.try_pop(func))\r
265                                         func();\r
266                         }\r
267                         catch(...)\r
268                         {\r
269                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
270                         }\r
271                 }\r
272         }       \r
273 };\r
274 \r
275 }