WorkQueueThread: Rework without Flags/Events

This commit is contained in:
Robin Kertels 2023-01-29 17:07:36 +01:00 committed by Scott Mansell
parent 9affbfe683
commit 94a0c50bf8
1 changed files with 80 additions and 62 deletions

View File

@ -25,8 +25,8 @@ public:
void Reset(std::function<void(T)> function) void Reset(std::function<void(T)> function)
{ {
Shutdown(); Shutdown();
m_shutdown.Clear(); std::lock_guard lg(m_lock);
m_cancelled.Clear(); m_cancelled = false;
m_function = std::move(function); m_function = std::move(function);
m_thread = std::thread(&WorkQueueThread::ThreadLoop, this); m_thread = std::thread(&WorkQueueThread::ThreadLoop, this);
} }
@ -34,85 +34,107 @@ public:
template <typename... Args> template <typename... Args>
void EmplaceItem(Args&&... args) void EmplaceItem(Args&&... args)
{ {
if (!m_cancelled.IsSet()) std::lock_guard lg(m_lock);
if (m_shutdown)
{ {
std::lock_guard lg(m_lock); return;
m_items.emplace(std::forward<Args>(args)...);
} }
m_wakeup.Set(); m_items.emplace(std::forward<Args>(args)...);
m_idle = false;
m_worker_cond_var.notify_one();
} }
void Push(T&& item) void Push(T&& item)
{ {
if (!m_cancelled.IsSet()) std::lock_guard lg(m_lock);
if (m_cancelled)
{ {
std::lock_guard lg(m_lock); return;
m_items.push(item);
} }
m_wakeup.Set(); m_items.push(item);
m_idle = false;
m_worker_cond_var.notify_one();
} }
void Push(const T& item) void Push(const T& item)
{ {
if (!m_cancelled.IsSet())
{
std::lock_guard lg(m_lock); std::lock_guard lg(m_lock);
if (m_cancelled)
{
return;
}
m_items.push(item); m_items.push(item);
} m_idle = false;
m_wakeup.Set(); m_worker_cond_var.notify_one();
} }
void Clear() void Clear()
{ {
{ std::lock_guard lg(m_lock);
std::lock_guard lg(m_lock); m_items = std::queue<T>();
m_items = std::queue<T>(); m_worker_cond_var.notify_one();
}
m_wakeup.Set();
} }
void Cancel() void Cancel()
{ {
m_cancelled.Set(); if (!m_thread.joinable())
Clear(); {
Shutdown(); return;
} }
bool IsCancelled() const { return m_cancelled.IsSet(); } {
std::unique_lock lg(m_lock);
m_items = std::queue<T>();
m_cancelled = true;
m_shutdown = true;
m_worker_cond_var.notify_one();
}
m_thread.join();
}
void Shutdown() void Shutdown()
{ {
if (m_thread.joinable()) if (!m_thread.joinable())
{ {
m_shutdown.Set(); return;
m_wakeup.Set();
m_thread.join();
} }
{
std::unique_lock lg(m_lock);
m_shutdown = true;
m_worker_cond_var.notify_one();
}
m_thread.join();
} }
// Doesn't return until the most recent function invocation has finished. // Doesn't return until the most recent function invocation has finished.
void FlushOne() void ClearAndFlush()
{ {
if (m_thread.joinable()) if (!m_thread.joinable())
{ {
m_flush.Set(); return;
Clear();
m_flushed.Wait();
} }
std::unique_lock lg(m_lock);
m_items = std::queue<T>();
m_wait_cond_var.wait(lg, [&] {
return m_idle;
});
} }
// Doesn't return until the queue is empty. // Doesn't return until the most recent function invocation has finished.
void Flush() void Flush()
{ {
if (m_thread.joinable()) if (!m_thread.joinable())
{ {
m_flush.Set(); return;
m_wakeup.Set();
m_flushed.Wait();
} }
}
bool IsFlushing() const { return m_flush.IsSet() || m_shutdown.IsSet(); } std::unique_lock lg(m_lock);
m_wait_cond_var.wait(lg, [&] {
return m_idle;
});
}
private: private:
void ThreadLoop() void ThreadLoop()
@ -121,41 +143,37 @@ private:
while (true) while (true)
{ {
m_wakeup.Wait(); std::unique_lock lg(m_lock);
if (m_items.empty())
while (true)
{ {
std::unique_lock lg(m_lock); m_idle = true;
if (m_items.empty()) m_wait_cond_var.notify_all();
m_worker_cond_var.wait(lg, [&] {
return m_shutdown || !m_items.empty();
});
if (m_shutdown)
{ {
if (m_flush.IsSet())
{
m_flush.Clear();
m_flushed.Set();
}
break; break;
} }
T item{std::move(m_items.front())}; continue;
m_items.pop();
lg.unlock();
m_function(std::move(item));
} }
T item{std::move(m_items.front())};
m_items.pop();
lg.unlock();
if (m_shutdown.IsSet()) m_function(std::move(item));
break;
} }
} }
std::function<void(T)> m_function; std::function<void(T)> m_function;
std::thread m_thread; std::thread m_thread;
Common::Event m_wakeup;
Common::Flag m_shutdown;
Common::Flag m_cancelled;
Common::Flag m_flush;
Common::Event m_flushed;
std::mutex m_lock; std::mutex m_lock;
std::queue<T> m_items; std::queue<T> m_items;
std::condition_variable m_wait_cond_var;
std::condition_variable m_worker_cond_var;
bool m_idle = true;
bool m_shutdown = false;
bool m_cancelled = false;
}; };
} // namespace Common } // namespace Common