X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=common%2Fconcurrency%2Fexecutor.h;h=31df71fcfc4f50dab9093f314643ed83e53b2131;hb=f8c5ab340ceea14f419e77c408ff2bcf37a61604;hp=87fd167bce85d4fb129cefbcc90dffa500b73417;hpb=789c03c2b410cd7964d544da198f389a3113fb7c;p=casparcg diff --git a/common/concurrency/executor.h b/common/concurrency/executor.h index 87fd167bc..31df71fcf 100644 --- a/common/concurrency/executor.h +++ b/common/concurrency/executor.h @@ -20,13 +20,15 @@ #pragma once #include "../exception/win32_exception.h" -#include "../utility/assert.h" +#include "../utility/string.h" +#include "../utility/move_on_copy.h" #include "../log/log.h" #include #include #include +#include #include #include @@ -76,23 +78,6 @@ enum thread_priority below_normal_priority_class }; -namespace internal -{ - template - struct move_on_copy - { - move_on_copy(const move_on_copy& other) : value(std::move(other.value)){} - move_on_copy(T&& value) : value(std::move(value)){} - mutable T value; - }; - - template - move_on_copy make_move_on_copy(T&& value) - { - return move_on_copy(std::move(value)); - } -} - class executor : boost::noncopyable { const std::string name_; @@ -177,7 +162,7 @@ public: auto begin_invoke(Func&& func, task_priority priority = normal_priority) -> boost::unique_future // noexcept { // Create a move on copy adaptor to avoid copying the functor into the queue, tbb::concurrent_queue does not support move semantics. - auto task_adaptor = internal::make_move_on_copy(create_task(func)); + auto task_adaptor = make_move_on_copy(create_task(func)); auto future = task_adaptor.value.get_future(); @@ -193,28 +178,7 @@ public: return std::move(future); } - - template - auto try_begin_invoke(Func&& func, task_priority priority = normal_priority) -> boost::unique_future // noexcept - { - // Create a move on copy adaptor to avoid copying the functor into the queue, tbb::concurrent_queue does not support move semantics. - auto task_adaptor = internal::make_move_on_copy(create_task(func)); - - auto future = task_adaptor.value.get_future(); - - if(priority == normal_priority || execution_queue_[normal_priority].try_push(nullptr)) - { - execution_queue_[priority].try_push([=] - { - try{task_adaptor.value();} - catch(boost::task_already_started&){} - catch(...){CASPAR_LOG_CURRENT_EXCEPTION();} - }); - } - - return std::move(future); - } - + template auto invoke(Func&& func, task_priority prioriy = normal_priority) -> decltype(func()) // noexcept { @@ -223,16 +187,7 @@ public: return begin_invoke(std::forward(func), prioriy).get(); } - - template - auto try_invoke(Func&& func, task_priority prioriy = normal_priority) -> decltype(func()) // noexcept - { - if(boost::this_thread::get_id() == thread_.get_id()) // Avoids potential deadlock. - return func(); - - return try_begin_invoke(std::forward(func), prioriy).get(); - } - + void yield() // noexcept { if(boost::this_thread::get_id() != thread_.get_id()) // Only yield when calling from execution thread.