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
24 #include "../exception/win32_exception.h"
\r
25 #include "../exception/exceptions.h"
\r
26 #include "../utility/string.h"
\r
27 #include "../utility/move_on_copy.h"
\r
28 #include "../log/log.h"
\r
30 #include <tbb/atomic.h>
\r
31 #include <tbb/concurrent_queue.h>
\r
33 #include <boost/thread.hpp>
\r
34 #include <boost/optional.hpp>
\r
35 #include <boost/noncopyable.hpp>
\r
37 #include <functional>
\r
43 typedef struct tagTHREADNAME_INFO
\r
45 DWORD dwType; // must be 0x1000
\r
46 LPCSTR szName; // pointer to name (in user addr space)
\r
47 DWORD dwThreadID; // thread ID (-1=caller thread)
\r
48 DWORD dwFlags; // reserved for future use, must be zero
\r
51 inline void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
\r
53 THREADNAME_INFO info;
\r
55 info.dwType = 0x1000;
\r
56 info.szName = szThreadName;
\r
57 info.dwThreadID = dwThreadID;
\r
62 RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD), (DWORD*)&info );
\r
64 __except (EXCEPTION_CONTINUE_EXECUTION){}
\r
76 enum thread_priority
\r
78 high_priority_class,
\r
79 above_normal_priority_class,
\r
80 normal_priority_class,
\r
81 below_normal_priority_class
\r
84 class executor : boost::noncopyable
\r
86 const std::string name_;
\r
87 boost::thread thread_;
\r
88 tbb::atomic<bool> is_running_;
\r
90 typedef tbb::concurrent_bounded_queue<std::function<void()>> function_queue;
\r
91 function_queue execution_queue_[priority_count];
\r
93 template<typename Func>
\r
94 auto create_task(Func&& func) -> boost::packaged_task<decltype(func())> // noexcept
\r
96 typedef boost::packaged_task<decltype(func())> task_type;
\r
98 auto task = task_type(std::forward<Func>(func));
\r
100 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
104 if(boost::this_thread::get_id() == thread_.get_id()) // Avoids potential deadlock.
\r
107 catch(boost::task_already_started&){}
\r
110 return std::move(task);
\r
115 explicit executor(const std::wstring& name) : name_(narrow(name)) // noexcept
\r
117 is_running_ = true;
\r
118 thread_ = boost::thread([this]{run();});
\r
121 virtual ~executor() // noexcept
\r
127 void set_capacity(size_t capacity) // noexcept
\r
129 execution_queue_[normal_priority].set_capacity(capacity);
\r
132 void set_priority_class(thread_priority p)
\r
136 if(p == high_priority_class)
\r
137 SetThreadPriority(GetCurrentThread(), HIGH_PRIORITY_CLASS);
\r
138 else if(p == above_normal_priority_class)
\r
139 SetThreadPriority(GetCurrentThread(), ABOVE_NORMAL_PRIORITY_CLASS);
\r
140 else if(p == normal_priority_class)
\r
141 SetThreadPriority(GetCurrentThread(), NORMAL_PRIORITY_CLASS);
\r
142 else if(p == below_normal_priority_class)
\r
143 SetThreadPriority(GetCurrentThread(), BELOW_NORMAL_PRIORITY_CLASS);
\r
147 void stop() // noexcept
\r
149 is_running_ = false;
\r
150 execution_queue_[normal_priority].try_push([]{}); // Wake the execution thread.
\r
153 void wait() // noexcept
\r
160 if(boost::this_thread::get_id() != thread_.get_id())
\r
164 template<typename Func>
\r
165 auto begin_invoke(Func&& func, task_priority priority = normal_priority) -> boost::unique_future<decltype(func())> // noexcept
\r
168 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("executor not running."));
\r
170 // Create a move on copy adaptor to avoid copying the functor into the queue, tbb::concurrent_queue does not support move semantics.
\r
171 auto task_adaptor = make_move_on_copy(create_task(func));
\r
173 auto future = task_adaptor.value.get_future();
\r
175 execution_queue_[priority].push([=]
\r
179 task_adaptor.value();
\r
181 catch(boost::task_already_started&)
\r
186 CASPAR_LOG_CURRENT_EXCEPTION();
\r
190 if(priority != normal_priority)
\r
191 execution_queue_[normal_priority].push(nullptr);
\r
193 return std::move(future);
\r
196 template<typename Func>
\r
197 auto invoke(Func&& func, task_priority prioriy = normal_priority) -> decltype(func()) // noexcept
\r
199 if(boost::this_thread::get_id() == thread_.get_id()) // Avoids potential deadlock.
\r
202 return begin_invoke(std::forward<Func>(func), prioriy).get();
\r
205 void yield() // noexcept
\r
207 if(boost::this_thread::get_id() != thread_.get_id()) // Only yield when calling from execution thread.
\r
210 std::function<void()> func;
\r
211 while(execution_queue_[high_priority].try_pop(func))
\r
218 function_queue::size_type capacity() const /*noexcept*/ { return execution_queue_[normal_priority].capacity(); }
\r
219 function_queue::size_type size() const /*noexcept*/ { return execution_queue_[normal_priority].size(); }
\r
220 bool empty() const /*noexcept*/ { return execution_queue_[normal_priority].empty(); }
\r
221 bool is_running() const /*noexcept*/ { return is_running_; }
\r
225 void execute() // noexcept
\r
227 std::function<void()> func;
\r
228 execution_queue_[normal_priority].pop(func);
\r
236 void run() // noexcept
\r
238 win32_exception::install_handler();
\r
239 detail::SetThreadName(GetCurrentThreadId(), name_.c_str());
\r
248 CASPAR_LOG_CURRENT_EXCEPTION();
\r