mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 06:09:50 -06:00
Common: Add AsyncWorkThread.
This commit is contained in:
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <future>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@ -13,8 +14,6 @@
|
|||||||
#include "Common/SPSCQueue.h"
|
#include "Common/SPSCQueue.h"
|
||||||
#include "Common/Thread.h"
|
#include "Common/Thread.h"
|
||||||
|
|
||||||
// A thread that executes the given function for every item placed into its queue.
|
|
||||||
|
|
||||||
namespace Common
|
namespace Common
|
||||||
{
|
{
|
||||||
namespace detail
|
namespace detail
|
||||||
@ -158,6 +157,38 @@ private:
|
|||||||
using ProducerMutex = std::conditional_t<IsSingleProducer, DummyMutex, std::recursive_mutex>;
|
using ProducerMutex = std::conditional_t<IsSingleProducer, DummyMutex, std::recursive_mutex>;
|
||||||
ProducerMutex m_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
|
} // namespace detail
|
||||||
|
|
||||||
// Multiple threads may use the public interface.
|
// Multiple threads may use the public interface.
|
||||||
@ -169,4 +200,7 @@ using WorkQueueThread = detail::WorkQueueThreadBase<T, false>;
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
using WorkQueueThreadSP = detail::WorkQueueThreadBase<T, true>;
|
using WorkQueueThreadSP = detail::WorkQueueThreadBase<T, true>;
|
||||||
|
|
||||||
|
using AsyncWorkThread = detail::AsyncWorkThreadBase<WorkQueueThread>;
|
||||||
|
using AsyncWorkThreadSP = detail::AsyncWorkThreadBase<WorkQueueThreadSP>;
|
||||||
|
|
||||||
} // namespace Common
|
} // namespace Common
|
||||||
|
Reference in New Issue
Block a user