diff options
| author | Admiral H. Curtiss <pikachu025@gmail.com> | 2025-05-04 18:45:14 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-04 18:45:14 +0200 |
| commit | d2db9d95906de73d704bd0f32bd1ee79f4a442f6 (patch) | |
| tree | a7e8a05356f7786c810acc181b84177db2e67ce2 /Source/Core/Common/WorkQueueThread.h | |
| parent | 2a3580fda5bdbcc163b1736663e29cb8356e3a20 (diff) | |
| parent | 4e736d60db295c0700fa1391b473544a9cb5ed72 (diff) | |
Merge pull request #13608 from jordan-woyak/async-work-thread
Common: Add AsyncWorkThread.
Diffstat (limited to 'Source/Core/Common/WorkQueueThread.h')
| -rw-r--r-- | Source/Core/Common/WorkQueueThread.h | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/Source/Core/Common/WorkQueueThread.h b/Source/Core/Common/WorkQueueThread.h index 190afcabf2..9ad7c4f471 100644 --- a/Source/Core/Common/WorkQueueThread.h +++ b/Source/Core/Common/WorkQueueThread.h @@ -5,6 +5,7 @@ #include <atomic> #include <functional> +#include <future> #include <mutex> #include <string> #include <thread> @@ -13,8 +14,6 @@ #include "Common/SPSCQueue.h" #include "Common/Thread.h" -// A thread that executes the given function for every item placed into its queue. - namespace Common { namespace detail @@ -158,6 +157,38 @@ private: using ProducerMutex = std::conditional_t<IsSingleProducer, DummyMutex, std::recursive_mutex>; ProducerMutex m_mutex; }; + +// A WorkQueueThread-like class that takes functions to invoke. +template <template <typename> typename WorkThread> +class AsyncWorkThreadBase +{ +public: + using FuncType = std::function<void()>; + + AsyncWorkThreadBase() = default; + explicit AsyncWorkThreadBase(std::string thread_name) { Reset(std::move(thread_name)); } + + void Reset(std::string thread_name) + { + m_worker.Reset(std::move(thread_name), std::invoke<FuncType>); + } + + void Push(FuncType func) { m_worker.Push(std::move(func)); } + + auto PushBlocking(FuncType func) + { + std::packaged_task task{std::move(func)}; + m_worker.EmplaceItem([&] { task(); }); + return task.get_future().get(); + } + + void Cancel() { m_worker.Cancel(); } + void Shutdown() { m_worker.Shutdown(); } + void WaitForCompletion() { m_worker.WaitForCompletion(); } + +private: + WorkThread<FuncType> m_worker; +}; } // namespace detail // Multiple threads may use the public interface. @@ -169,4 +200,7 @@ using WorkQueueThread = detail::WorkQueueThreadBase<T, false>; template <typename T> using WorkQueueThreadSP = detail::WorkQueueThreadBase<T, true>; +using AsyncWorkThread = detail::AsyncWorkThreadBase<WorkQueueThread>; +using AsyncWorkThreadSP = detail::AsyncWorkThreadBase<WorkQueueThreadSP>; + } // namespace Common |
