threadpool: active-wait during shutdown
Instead of waiting for the workers to finish processing tasks, help them actively inside Stop(). This speeds up the JSON-RPC and REST server shutdown procedure, and results in a faster node shutdown when many requests remain unhandled
This commit is contained in:
1 file changed
+6
-2
@@ -139,6 +139,9 @@ public:
|
|||||||
threads_to_join.swap(m_workers);
|
threads_to_join.swap(m_workers);
|
||||||
}
|
}
|
||||||
m_cv.notify_all();
|
m_cv.notify_all();
|
||||||
|
// Help draining queue
|
||||||
|
while (ProcessTask()) {}
|
||||||
|
// Free resources
|
||||||
for (auto& worker : threads_to_join) worker.join();
|
for (auto& worker : threads_to_join) worker.join();
|
||||||
|
|
||||||
// Since we currently wait for tasks completion, sanity-check empty queue
|
// Since we currently wait for tasks completion, sanity-check empty queue
|
||||||
@@ -187,18 +190,19 @@ public:
|
|||||||
* @brief Execute a single queued task synchronously.
|
* @brief Execute a single queued task synchronously.
|
||||||
* Removes one task from the queue and executes it on the calling thread.
|
* Removes one task from the queue and executes it on the calling thread.
|
||||||
*/
|
*/
|
||||||
void ProcessTask() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
bool ProcessTask() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
||||||
{
|
{
|
||||||
std::packaged_task<void()> task;
|
std::packaged_task<void()> task;
|
||||||
{
|
{
|
||||||
LOCK(m_mutex);
|
LOCK(m_mutex);
|
||||||
if (m_work_queue.empty()) return;
|
if (m_work_queue.empty()) return false;
|
||||||
|
|
||||||
// Pop the task
|
// Pop the task
|
||||||
task = std::move(m_work_queue.front());
|
task = std::move(m_work_queue.front());
|
||||||
m_work_queue.pop();
|
m_work_queue.pop();
|
||||||
}
|
}
|
||||||
task();
|
task();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user