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"
27 #include "blocking_bounded_queue_adapter.h"
28 #include "blocking_priority_queue.h"
31 #include <tbb/atomic.h>
32 #include <tbb/concurrent_priority_queue.h>
34 #include <boost/thread.hpp>
35 #include <boost/optional.hpp>
42 struct task_priority_def
54 typedef enum_class<task_priority_def> task_priority;
58 executor(const executor&);
59 executor& operator=(const executor&);
61 typedef blocking_priority_queue<std::function<void()>, task_priority> function_queue_t;
63 const std::wstring name_;
64 tbb::atomic<bool> is_running_;
65 boost::thread thread_;
66 function_queue_t execution_queue_;
69 executor(const std::wstring& name)
71 , execution_queue_(512, std::vector<task_priority> {
72 task_priority::lowest_priority,
73 task_priority::lower_priority,
74 task_priority::low_priority,
75 task_priority::normal_priority,
76 task_priority::high_priority,
77 task_priority::higher_priority
81 thread_ = boost::thread([this]{run();});
88 internal_begin_invoke([=]
95 CASPAR_LOG_CURRENT_EXCEPTION();
101 template<typename Func>
102 auto begin_invoke(Func&& func, task_priority priority = task_priority::normal_priority) -> std::future<decltype(func())> // noexcept
105 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("executor not running.") << source_info(name_));
107 return internal_begin_invoke(std::forward<Func>(func), priority);
110 template<typename Func>
111 auto invoke(Func&& func, task_priority prioriy = task_priority::normal_priority) -> decltype(func()) // noexcept
113 if(is_current()) // Avoids potential deadlock.
116 return begin_invoke(std::forward<Func>(func), prioriy).get();
119 void yield(task_priority minimum_priority)
122 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Executor can only yield inside of thread context.") << source_info(name_));
124 std::function<void ()> func;
126 while (execution_queue_.try_pop(func, minimum_priority))
130 void set_capacity(function_queue_t::size_type capacity)
132 execution_queue_.set_capacity(capacity);
135 function_queue_t::size_type capacity() const
137 return execution_queue_.capacity();
142 return execution_queue_.space_available() == 0;
147 std::function<void ()> func;
148 while(execution_queue_.try_pop(func));
161 invoke([]{}, task_priority::lowest_priority);
164 function_queue_t::size_type size() const
166 return execution_queue_.size();
169 bool is_running() const
174 bool is_current() const
176 return boost::this_thread::get_id() == thread_.get_id();
181 std::wstring print() const
183 return L"executor[" + name_ + L"]";
186 template<typename Func>
187 auto internal_begin_invoke(
189 task_priority priority = task_priority::normal_priority) -> std::future<decltype(func())> // noexcept
191 typedef typename std::remove_reference<Func>::type function_type;
192 typedef decltype(func()) result_type;
193 typedef std::packaged_task<result_type()> task_type;
195 std::shared_ptr<task_type> task;
197 // Use pointers since the boost thread library doesn't fully support move semantics.
199 auto raw_func2 = new function_type(std::forward<Func>(func));
202 task.reset(new task_type([raw_func2]() -> result_type
204 std::unique_ptr<function_type> func2(raw_func2);
214 auto future = task->get_future().share();
215 auto function = [task]
221 catch(std::future_error&){}
224 if (!execution_queue_.try_push(priority, function))
226 CASPAR_LOG(debug) << print() << L" Overflow. Blocking caller.";
227 execution_queue_.push(priority, function);
230 return std::async(std::launch::deferred, [=]() mutable -> result_type
232 if (!is_ready(future) && is_current()) // Avoids potential deadlock.
241 void run() // noexcept
243 win32_exception::install_handler();
248 std::function<void ()> func;
249 execution_queue_.pop(func);
254 CASPAR_LOG_CURRENT_EXCEPTION();