WorkQueueThread: Rework without Flags/Events
This commit is contained in:
parent
9affbfe683
commit
94a0c50bf8
|
@ -25,137 +25,155 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void EmplaceItem(Args&&... args)
|
void EmplaceItem(Args&&... args)
|
||||||
{
|
|
||||||
if (!m_cancelled.IsSet())
|
|
||||||
{
|
{
|
||||||
std::lock_guard lg(m_lock);
|
std::lock_guard lg(m_lock);
|
||||||
m_items.emplace(std::forward<Args>(args)...);
|
if (m_shutdown)
|
||||||
|
{
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
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);
|
std::lock_guard lg(m_lock);
|
||||||
m_items.push(item);
|
if (m_cancelled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
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);
|
||||||
m_items.push(item);
|
if (m_cancelled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
m_wakeup.Set();
|
m_items.push(item);
|
||||||
|
m_idle = false;
|
||||||
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Doesn't return until the queue is empty.
|
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 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()
|
||||||
{
|
{
|
||||||
Common::SetCurrentThreadName("WorkQueueThread");
|
Common::SetCurrentThreadName("WorkQueueThread");
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
m_wakeup.Wait();
|
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
std::unique_lock lg(m_lock);
|
std::unique_lock lg(m_lock);
|
||||||
if (m_items.empty())
|
if (m_items.empty())
|
||||||
{
|
{
|
||||||
if (m_flush.IsSet())
|
m_idle = true;
|
||||||
|
m_wait_cond_var.notify_all();
|
||||||
|
m_worker_cond_var.wait(lg, [&] {
|
||||||
|
return m_shutdown || !m_items.empty();
|
||||||
|
});
|
||||||
|
if (m_shutdown)
|
||||||
{
|
{
|
||||||
m_flush.Clear();
|
|
||||||
m_flushed.Set();
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
T item{std::move(m_items.front())};
|
T item{std::move(m_items.front())};
|
||||||
m_items.pop();
|
m_items.pop();
|
||||||
lg.unlock();
|
lg.unlock();
|
||||||
|
|
||||||
m_function(std::move(item));
|
m_function(std::move(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_shutdown.IsSet())
|
|
||||||
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
|
||||||
|
|
Loading…
Reference in New Issue