From fd92a7f555b17751932fa137f2a8e8a0c9117d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sun, 8 Apr 2018 01:30:43 +0200 Subject: [PATCH] Fix Task.running and Task.yield directly after runTask/yieldLock. The status of the task was erroneously reported as not running until the task was actually executed for the first time (which only happens after the yield lock has been lifted). --- source/vibe/core/core.d | 15 +++++++++++++++ source/vibe/core/task.d | 8 +++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/source/vibe/core/core.d b/source/vibe/core/core.d index ccb8ad5..8936f98 100644 --- a/source/vibe/core/core.d +++ b/source/vibe/core/core.d @@ -395,6 +395,21 @@ package Task runTask_internal(alias TFI_SETUP)() return handle; } +unittest { // ensure task.running is true directly after runTask + Task t; + bool hit = false; + { + auto l = yieldLock(); + t = runTask({ hit = true; }); + assert(!hit); + assert(t.running); + } + t.join(); + assert(!t.running); + assert(hit); +} + + /** Runs a new asynchronous task in a worker thread. diff --git a/source/vibe/core/task.d b/source/vibe/core/task.d index cb187ac..0813743 100644 --- a/source/vibe/core/task.d +++ b/source/vibe/core/task.d @@ -67,13 +67,15 @@ struct Task { @property size_t taskCounter() const @safe { return m_taskCounter; } @property inout(Thread) thread() inout @trusted { if (m_fiber) return this.taskFiber.thread; return null; } - /** Determines if the task is still running. + /** Determines if the task is still running or scheduled to be run. */ @property bool running() // FIXME: this is NOT thread safe const @trusted { assert(m_fiber !is null, "Invalid task handle"); try if (this.taskFiber.state == Fiber.State.TERM) return false; catch (Throwable) {} - return this.taskFiber.m_running && this.taskFiber.m_taskCounter == m_taskCounter; + if (this.taskFiber.m_taskCounter != m_taskCounter) + return false; + return this.taskFiber.m_running || this.taskFiber.m_taskFunc.func !is null; } package @property ref ThreadInfo tidInfo() @system { return m_fiber ? taskFiber.tidInfo : s_tidInfo; } // FIXME: this is not thread safe! @@ -465,7 +467,7 @@ final package class TaskFiber : Fiber { void join(bool interruptiple)(size_t task_counter) @trusted { auto cnt = m_onExit.emitCount; - while (m_running && m_taskCounter == task_counter) { + while ((m_running || m_taskFunc.func !is null) && m_taskCounter == task_counter) { static if (interruptiple) cnt = m_onExit.wait(cnt); else