From b995bdaa72b4b6bf6a33534524a3f4d052c739d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Thu, 23 Feb 2017 14:43:22 +0100 Subject: [PATCH] Use thread wait primitives for worker thread shutdown. Avoids possible issues due to tasks interleaving the shutdown on the same thread. --- source/vibe/core/taskpool.d | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/source/vibe/core/taskpool.d b/source/vibe/core/taskpool.d index 7ed86d7..55058f7 100644 --- a/source/vibe/core/taskpool.d +++ b/source/vibe/core/taskpool.d @@ -66,8 +66,22 @@ shared class TaskPool { m_signal.emit(); auto ec = m_signal.emitCount; - while (m_state.lock.threads.length > 0) - ec = m_signal.waitUninterruptible(ec); + while (true) { + WorkerThread th; + with (m_state.lock) + if (threads.length) { + th = threads[0]; + threads = threads[1 .. $]; + } + if (!th) break; + + () @trusted { + try th.join(); + catch (Exception e) { + logWarn("Failed to wait for worker thread exit: %s", e.msg); + } + } (); + } size_t cnt = m_state.lock.queue.length; if (cnt > 0) logWarn("There were still %d worker tasks pending at exit.", cnt); @@ -290,7 +304,6 @@ private class WorkerThread : Thread { if (threads.length > 0 && !queue.empty) logWarn("Worker threads shut down with worker tasks still left in the queue."); } - m_pool.m_signal.emit(); exitEventLoop(); }