2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
25 #include "enum_class.h"
28 #include <tbb/atomic.h>
29 #include <tbb/concurrent_priority_queue.h>
30 #include <tbb/concurrent_queue.h>
32 #include <boost/thread.hpp>
38 struct task_priority_def
50 typedef enum_class<task_priority_def> task_priority;
54 struct priority_function
57 std::function<void()> func;
64 priority_function(int priority, F&& func)
66 , func(std::forward<F>(func))
75 bool operator<(const priority_function& other) const
77 return priority < other.priority;
81 executor(const executor&);
82 executor& operator=(const executor&);
84 typedef tbb::concurrent_priority_queue<priority_function> function_queue_t;
86 const std::wstring name_;
87 tbb::atomic<bool> is_running_;
88 boost::thread thread_;
89 function_queue_t execution_queue_;
90 tbb::concurrent_bounded_queue<int> semaphore_;
93 executor(const std::wstring& name)
98 thread_ = boost::thread([this]{run();});
105 internal_begin_invoke([=]
112 CASPAR_LOG_CURRENT_EXCEPTION();
118 template<typename Func>
119 auto begin_invoke(Func&& func, task_priority priority = task_priority::normal_priority) -> boost::unique_future<decltype(func())> // noexcept
122 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("executor not running.") << source_info(name_));
124 return internal_begin_invoke(std::forward<Func>(func), priority);
127 template<typename Func>
128 auto invoke(Func&& func, task_priority prioriy = task_priority::normal_priority) -> decltype(func()) // noexcept
130 if(is_current()) // Avoids potential deadlock.
133 return begin_invoke(std::forward<Func>(func), prioriy).get();
139 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Executor can only yield inside of thread context.") << source_info(name_));
142 if(!semaphore_.try_pop(dummy))
145 priority_function func;
146 if(execution_queue_.try_pop(func))
150 void set_capacity(std::size_t capacity)
152 semaphore_.set_capacity(capacity);
155 std::size_t capacity() const
157 return semaphore_.capacity();
162 priority_function func;
163 while(execution_queue_.try_pop(func));
176 invoke([]{}, task_priority::lowest_priority);
179 function_queue_t::size_type size() const
181 return execution_queue_.size();
184 bool is_running() const
189 bool is_current() const
191 return boost::this_thread::get_id() == thread_.get_id();
196 std::wstring print() const
198 return L"executor[" + name_ + L"]";
201 template<typename Func>
202 auto internal_begin_invoke(Func&& func, task_priority priority = task_priority::normal_priority) -> boost::unique_future<decltype(func())> // noexcept
204 typedef typename std::remove_reference<Func>::type function_type;
205 typedef decltype(func()) result_type;
206 typedef boost::packaged_task<result_type> task_type;
208 std::unique_ptr<task_type> task;
210 // Use pointers since the boost thread library doesn't fully support move semantics.
212 auto raw_func2 = new function_type(std::forward<Func>(func));
215 task.reset(new task_type([raw_func2]() -> result_type
217 std::unique_ptr<function_type> func2(raw_func2);
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.
231 if(is_current()) // Avoids potential deadlock.
234 catch(boost::task_already_started&){}
237 auto future = task->get_future();
239 auto raw_task = task.release();
240 priority_function prio_func(priority.value(), [raw_task]
242 std::unique_ptr<task_type> task(raw_task);
247 catch(boost::task_already_started&){}
250 execution_queue_.push(prio_func);
252 if(!semaphore_.try_push(0))
254 CASPAR_LOG(debug) << print() << L" Overflow. Blocking caller.";
257 return std::move(future);
260 void run() // noexcept
262 win32_exception::install_handler();
268 semaphore_.pop(dummy);
270 priority_function func;
271 if(execution_queue_.try_pop(func))
276 CASPAR_LOG_CURRENT_EXCEPTION();