diff --git a/README.md b/README.md index 55a6982..70f3a0e 100644 --- a/README.md +++ b/README.md @@ -281,14 +281,14 @@ Cancellation is [cooperative](https://docs.swift.org/swift-book/documentation/th - The operation decides how to respond. It can check `Task.isCancelled`, or call `Task.checkCancellation()` to throw a `CancellationError`. - An operation that doesn't check for cancellation runs to completion, and the tasks behind it wait for it as usual. -## Checking whether a queue is idle +## Checking whether a queue has work left -A `FIFOQueue` and an `ActorQueue` each have an `isIdle` property: +A `FIFOQueue` has an `isIdle` property, and an `ActorQueue` has a `hasStartedAllWork` property: - A `FIFOQueue` is idle when every task you enqueued on it has finished. A task that is suspended at an `await` hasn't finished. -- An `ActorQueue` is idle when every task you enqueued on it has started running on the actor. The operations of its tasks might not have finished. +- An `ActorQueue` has started all of its work when every task you enqueued on it has started running on the actor. The operations of its tasks might not have finished. -The value can change as soon as you read it: a task you enqueue afterward makes the queue busy again. +Either value can change as soon as you read it: a task you enqueue afterward makes it `false` again. For example, here's how an actor that keeps one `FIFOQueue` per document might remove the queues it no longer needs: @@ -314,9 +314,9 @@ actor DocumentStore { } ``` -In the code above, the actor both enqueues the tasks and reads `isIdle`, so no task can be enqueued between the check and the removal. +In the code above, the actor both enqueues the tasks and reads `isIdle`, so no task can be enqueued between the check and the removal. An actor that keeps `ActorQueue`s can remove them the same way by reading `hasStartedAllWork`. -`CancellableQueue` has no `isIdle` property. To check one, keep a reference to the queue it wraps, and read that queue's `isIdle`. +`CancellableQueue` has neither property. To check one, keep a reference to the queue it wraps, and read that queue's property. ## Installation diff --git a/Sources/AsyncQueue/ActorQueue.swift b/Sources/AsyncQueue/ActorQueue.swift index 0ef21bf..0e5fc71 100644 --- a/Sources/AsyncQueue/ActorQueue.swift +++ b/Sources/AsyncQueue/ActorQueue.swift @@ -79,15 +79,15 @@ public final class ActorQueue: @unchecked Sendable { /// Whether every task you enqueued on this queue has started running on the actor. /// /// A task counts from the moment you enqueue it until it starts running on the actor. - /// So the queue can be idle while the operations of its tasks haven't finished. + /// So this property can be `true` while the operations of its tasks haven't finished. /// A task runs alongside the code that enqueues it, so the task might start running on the actor before that code reads this property. /// If that code is running on the actor, though, the task can't start running there until the code suspends or returns. /// - /// The value can change as soon as you read it: a task you enqueue afterward makes the queue busy again. - /// If one actor enqueues every task on the queue, that actor can read this property and then remove an idle queue + /// The value can change as soon as you read it: a task you enqueue afterward makes it `false` again. + /// If one actor enqueues every task on the queue, that actor can read this property and then remove the queue /// without suspending between the read and the removal. /// No task can be enqueued in between, so every task enqueued on the removed queue has started running on the queue's actor. - public var isIdle: Bool { + public var hasStartedAllWork: Bool { turns.isIdle } diff --git a/Tests/AsyncQueueTests/ActorQueueTests.swift b/Tests/AsyncQueueTests/ActorQueueTests.swift index fc49ca6..1aca871 100644 --- a/Tests/AsyncQueueTests/ActorQueueTests.swift +++ b/Tests/AsyncQueueTests/ActorQueueTests.swift @@ -515,32 +515,32 @@ struct ActorQueueTests { } @Test - func isIdle_onNewQueue_isTrue() { - #expect(systemUnderTest.isIdle) + func hasStartedAllWork_onNewQueue_isTrue() { + #expect(systemUnderTest.hasStartedAllWork) } @Test - func isIdle_afterTaskIsEnqueued_isFalseUntilTaskIsIsolatedToActor() async { + func hasStartedAllWork_afterTaskIsEnqueued_isFalseUntilTaskIsIsolatedToActor() async { let actor = ManuallyExecutedActor() var enqueuedJobs = actor.executor.enqueuedJobs.makeAsyncIterator() let task = Task(on: actor.queue) { actor in - actor.queue.isIdle + actor.queue.hasStartedAllWork } // `Task(on:)` takes the task's place on the queue before it returns, and the task cannot be isolated to the actor // until the test runs the actor's executor. - let isIdleAfterTaskWasEnqueued = actor.queue.isIdle + let hadStartedAllWorkAfterTaskWasEnqueued = actor.queue.hasStartedAllWork // Wait for the task to hop to the actor. It has begun executing, but it is not isolated to the actor yet. _ = await enqueuedJobs.next() - let isIdleAfterTaskHoppedToActor = actor.queue.isIdle + let hadStartedAllWorkAfterTaskHoppedToActor = actor.queue.hasStartedAllWork actor.executor.runUntilIdle() - let isIdleWhileOperationExecuted = await task.value - #expect(!isIdleAfterTaskWasEnqueued) - #expect(!isIdleAfterTaskHoppedToActor) - #expect(isIdleWhileOperationExecuted) + let hadStartedAllWorkWhileOperationExecuted = await task.value + #expect(!hadStartedAllWorkAfterTaskWasEnqueued) + #expect(!hadStartedAllWorkAfterTaskHoppedToActor) + #expect(hadStartedAllWorkWhileOperationExecuted) } @Test - func isIdle_whileLastOperationIsSuspended_isTrue() async { + func hasStartedAllWork_whileLastOperationIsSuspended_isTrue() async { let operationStarted = Semaphore() let operationAllowedToReturn = Semaphore() let task = Task(on: systemUnderTest) { _ in @@ -549,23 +549,23 @@ struct ActorQueueTests { } await operationStarted.wait() // The task gave up its place on the queue once it was isolated to the actor, before its operation began. - let isIdleWhileOperationWasSuspended = systemUnderTest.isIdle + let hadStartedAllWorkWhileOperationWasSuspended = systemUnderTest.hasStartedAllWork await operationAllowedToReturn.signal() await task.value - #expect(isIdleWhileOperationWasSuspended) + #expect(hadStartedAllWorkWhileOperationWasSuspended) } @Test - func isIdle_withSeveralTasks_isFalseUntilLastTaskIsIsolatedToActor() async { + func hasStartedAllWork_withSeveralTasks_isFalseUntilLastTaskIsIsolatedToActor() async { let actor = ManuallyExecutedActor() var enqueuedJobs = actor.executor.enqueuedJobs.makeAsyncIterator() // Each operation reads the property before it can suspend. The actor executes one job at a time, so the last task // cannot be isolated to the actor while the first task's operation executes. let firstTask = Task(on: actor.queue) { actor in - actor.queue.isIdle + actor.queue.hasStartedAllWork } let lastTask = Task(on: actor.queue) { actor in - actor.queue.isIdle + actor.queue.hasStartedAllWork } // Wait for the first task to hop to the actor, then run it. Its operation cannot execute before both tasks are enqueued. _ = await enqueuedJobs.next() @@ -573,32 +573,32 @@ struct ActorQueueTests { // Wait for the last task to hop to the actor, then run it if the previous call did not already. _ = await enqueuedJobs.next() actor.executor.runUntilIdle() - let isIdleWhileFirstOperationExecuted = await firstTask.value - let isIdleWhileLastOperationExecuted = await lastTask.value - #expect(!isIdleWhileFirstOperationExecuted) - #expect(isIdleWhileLastOperationExecuted) + let hadStartedAllWorkWhileFirstOperationExecuted = await firstTask.value + let hadStartedAllWorkWhileLastOperationExecuted = await lastTask.value + #expect(!hadStartedAllWorkWhileFirstOperationExecuted) + #expect(hadStartedAllWorkWhileLastOperationExecuted) } @Test - func isIdle_onMainActorQueue_afterTaskIsEnqueuedWhileCallerHoldsMainActor_isFalseUntilOperationRuns() async { + func hasStartedAllWork_onMainActorQueue_afterTaskIsEnqueuedWhileCallerHoldsMainActor_isFalseUntilOperationRuns() async { // Use a queue of our own rather than `MainActor.queue`, which tests running in parallel share. let mainActorQueue = ActorQueue() mainActorQueue.adoptExecutionContext(of: MainActor.shared) // While this closure holds the main actor, the task cannot be isolated to it. The test cannot wait here for the task // to hop to the main actor, so `mainTask_holdsTurnUntilItIsIsolatedToMainActor` covers when the task's turn ends. - let (task, isIdleAfterTaskWasEnqueued) = await MainActor.run { + let (task, hadStartedAllWorkAfterTaskWasEnqueued) = await MainActor.run { let task = Task(on: mainActorQueue) { - mainActorQueue.isIdle + mainActorQueue.hasStartedAllWork } - return (task, mainActorQueue.isIdle) + return (task, mainActorQueue.hasStartedAllWork) } - let isIdleWhileOperationExecuted = await task.value - #expect(!isIdleAfterTaskWasEnqueued) - #expect(isIdleWhileOperationExecuted) + let hadStartedAllWorkWhileOperationExecuted = await task.value + #expect(!hadStartedAllWorkAfterTaskWasEnqueued) + #expect(hadStartedAllWorkWhileOperationExecuted) } @Test - func isIdle_onMainActorQueue_whileLastOperationIsSuspended_isTrue() async { + func hasStartedAllWork_onMainActorQueue_whileLastOperationIsSuspended_isTrue() async { // Use a queue of our own rather than `MainActor.queue`, which tests running in parallel share. let mainActorQueue = ActorQueue() mainActorQueue.adoptExecutionContext(of: MainActor.shared) @@ -610,10 +610,10 @@ struct ActorQueueTests { } await operationStarted.wait() // The task gave up its place on the queue once it was isolated to the main actor, before its operation began. - let isIdleWhileOperationWasSuspended = mainActorQueue.isIdle + let hadStartedAllWorkWhileOperationWasSuspended = mainActorQueue.hasStartedAllWork await operationAllowedToReturn.signal() await task.value - #expect(isIdleWhileOperationWasSuspended) + #expect(hadStartedAllWorkWhileOperationWasSuspended) } @Test