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