2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
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
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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
25 #include "enum_class.h"
\r
28 #include <tbb/atomic.h>
\r
29 #include <tbb/concurrent_priority_queue.h>
\r
30 #include <tbb/concurrent_queue.h>
\r
32 #include <boost/thread.hpp>
\r
34 #include <functional>
\r
38 struct task_priority_def
\r
42 lowest_priority = 0,
\r
50 typedef enum_class<task_priority_def> task_priority;
\r
52 class executor sealed
\r
54 struct priority_function
\r
57 std::function<void()> func;
\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
75 bool operator<(const priority_function& other) const
\r
77 return priority < other.priority;
\r
81 executor(const executor&);
\r
82 executor& operator=(const executor&);
\r
84 typedef tbb::concurrent_priority_queue<priority_function> function_queue_t;
\r
86 const std::wstring name_;
\r
87 tbb::atomic<bool> is_running_;
\r
88 boost::thread thread_;
\r
89 function_queue_t execution_queue_;
\r
90 tbb::concurrent_bounded_queue<int> semaphore_;
\r
93 executor(const std::wstring& name)
\r
97 thread_ = boost::thread([this]{run();});
\r
104 internal_begin_invoke([=]
\r
106 is_running_ = false;
\r
111 CASPAR_LOG_CURRENT_EXCEPTION();
\r
114 is_running_ = false;
\r
115 semaphore_.try_push(0);
\r
120 template<typename Func>
\r
121 auto begin_invoke(Func&& func, task_priority priority = task_priority::normal_priority) -> boost::unique_future<decltype(func())> // noexcept
\r
123 if(execution_queue_.size() > 128)
\r
124 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("executor overflow.") << source_info(name_));
\r
127 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("executor not running.") << source_info(name_));
\r
129 return internal_begin_invoke(std::forward<Func>(func), priority);
\r
132 template<typename Func>
\r
133 auto invoke(Func&& func, task_priority prioriy = task_priority::normal_priority) -> decltype(func()) // noexcept
\r
135 if(is_current()) // Avoids potential deadlock.
\r
138 return begin_invoke(std::forward<Func>(func), prioriy).get();
\r
144 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Executor can only yield inside of thread context.") << source_info(name_));
\r
147 if(!semaphore_.try_pop(dummy))
\r
150 priority_function func;
\r
151 if(execution_queue_.try_pop(func))
\r
155 void set_capacity(std::size_t capacity)
\r
157 semaphore_.set_capacity(capacity);
\r
160 std::size_t capacity() const
\r
162 return semaphore_.capacity();
\r
167 priority_function func;
\r
168 while(execution_queue_.try_pop(func));
\r
175 is_running_ = false;
\r
181 invoke([]{}, task_priority::lowest_priority);
\r
184 function_queue_t::size_type size() const
\r
186 return execution_queue_.size();
\r
189 bool is_running() const
\r
191 return is_running_;
\r
194 bool is_current() const
\r
196 return boost::this_thread::get_id() == thread_.get_id();
\r
201 template<typename Func>
\r
202 auto internal_begin_invoke(Func&& func, task_priority priority = task_priority::normal_priority) -> boost::unique_future<decltype(func())> // noexcept
\r
204 typedef typename std::remove_reference<Func>::type function_type;
\r
205 typedef decltype(func()) result_type;
\r
206 typedef boost::packaged_task<result_type> task_type;
\r
208 std::unique_ptr<task_type> task;
\r
210 // Use pointers since the boost thread library doesn't fully support move semantics.
\r
212 auto raw_func2 = new function_type(std::forward<Func>(func));
\r
215 task.reset(new task_type([raw_func2]() -> result_type
\r
217 std::unique_ptr<function_type> func2(raw_func2);
\r
227 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
231 if(is_current()) // Avoids potential deadlock.
\r
234 catch(boost::task_already_started&){}
\r
237 auto future = task->get_future();
\r
239 auto raw_task = task.release();
\r
240 priority_function prio_func(priority.value(), [raw_task]
\r
242 std::unique_ptr<task_type> task(raw_task);
\r
247 catch(boost::task_already_started&){}
\r
250 execution_queue_.push(prio_func);
\r
251 semaphore_.push(0);
\r
253 return std::move(future);
\r
256 void run() // noexcept
\r
258 win32_exception::install_handler();
\r
264 semaphore_.pop(dummy);
\r
266 priority_function func;
\r
267 if(execution_queue_.try_pop(func))
\r
272 CASPAR_LOG_CURRENT_EXCEPTION();
\r