13#include <condition_variable>
36 static std::shared_ptr<GpuExecutionOwner>
shared() {
41 static const auto* owner =
42 new std::shared_ptr<GpuExecutionOwner>(std::make_shared<GpuExecutionOwner>());
61 std::lock_guard lock(mutex_);
80 template <
class F>
auto invoke(F&& fn) -> std::invoke_result_t<F> {
81 if (std::this_thread::get_id() == worker_.get_id())
83 using R = std::invoke_result_t<F>;
84 auto task = std::make_shared<std::packaged_task<R()>>(std::forward<F>(fn));
85 auto result = task->get_future();
87 std::lock_guard lock(mutex_);
88 queue_.emplace_back([task] { (*task)(); });
104 std::function<void()> task;
106 std::unique_lock lock(mutex_);
107 ready_.wait(lock, [
this] {
return stopping_ || !queue_.empty(); });
110 task = std::move(queue_.front());
117 std::condition_variable ready_;
118 std::deque<std::function<void()>> queue_;
119 bool stopping_ =
false;
static std::shared_ptr< GpuExecutionOwner > shared()
Acquire the process-resident controlling thread for matched GPU plugins.
Definition GpuExecutionOwner.hpp:36
GpuExecutionOwner(const GpuExecutionOwner &)=delete
~GpuExecutionOwner()
Stop and join a nonresident execution owner after queued work drains.
Definition GpuExecutionOwner.hpp:59
GpuExecutionOwner()
Start a dedicated synchronous-dispatch worker.
Definition GpuExecutionOwner.hpp:51
auto invoke(F &&fn) -> std::invoke_result_t< F >
Execute one callable on the controlling thread and wait for completion.
Definition GpuExecutionOwner.hpp:80
Definition ExchangeLaw.hpp:60