win32/ComWorker: fold class COMWorkerThread into class COMWorker

This commit is contained in:
Max Kellermann
2021-03-07 18:21:56 +01:00
parent cf108c389f
commit 48bdd09f64
2 changed files with 20 additions and 24 deletions

View File

@@ -21,7 +21,9 @@
#include "Com.hxx" #include "Com.hxx"
#include "thread/Name.hxx" #include "thread/Name.hxx"
void COMWorker::COMWorkerThread::Work() noexcept { void
COMWorker::Work() noexcept
{
SetThreadName("COM Worker"); SetThreadName("COM Worker");
COM com{true}; COM com{true};
while (true) { while (true) {

View File

@@ -30,27 +30,11 @@
// Worker thread for all COM operation // Worker thread for all COM operation
class COMWorker { class COMWorker {
private: Thread thread{BIND_THIS_METHOD(Work)};
class COMWorkerThread : public Thread {
public:
COMWorkerThread() : Thread{BIND_THIS_METHOD(Work)} {}
private: boost::lockfree::spsc_queue<std::function<void()>> spsc_buffer{32};
friend class COMWorker; std::atomic_flag running_flag = true;
void Work() noexcept; WinEvent event{};
void Finish() noexcept {
running_flag.clear();
event.Set();
}
void Push(const std::function<void()> &function) {
spsc_buffer.push(function);
event.Set();
}
boost::lockfree::spsc_queue<std::function<void()>> spsc_buffer{32};
std::atomic_flag running_flag = true;
WinEvent event{};
};
public: public:
COMWorker() { COMWorker() {
@@ -58,7 +42,7 @@ public:
} }
~COMWorker() noexcept { ~COMWorker() noexcept {
thread.Finish(); Finish();
thread.Join(); thread.Join();
} }
@@ -70,7 +54,7 @@ public:
using R = std::invoke_result_t<std::decay_t<Function>>; using R = std::invoke_result_t<std::decay_t<Function>>;
auto promise = std::make_shared<Promise<R>>(); auto promise = std::make_shared<Promise<R>>();
auto future = promise->get_future(); auto future = promise->get_future();
thread.Push([function = std::forward<Function>(function), Push([function = std::forward<Function>(function),
promise = std::move(promise)]() mutable { promise = std::move(promise)]() mutable {
try { try {
if constexpr (std::is_void_v<R>) { if constexpr (std::is_void_v<R>) {
@@ -87,7 +71,17 @@ public:
} }
private: private:
COMWorkerThread thread; void Finish() noexcept {
running_flag.clear();
event.Set();
}
void Push(const std::function<void()> &function) {
spsc_buffer.push(function);
event.Set();
}
void Work() noexcept;
}; };
#endif #endif