From 876eddecbf1b26b103f10f1d6ee75bd63209edc8 Mon Sep 17 00:00:00 2001 From: songhuaixu <965991028@qq.com> Date: Thu, 27 Nov 2025 23:39:54 +0800 Subject: [PATCH 1/4] queue task add name --- Sources/AsyncQueue/ActorQueue.swift | 5 +++-- Sources/AsyncQueue/FIFOQueue.swift | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Sources/AsyncQueue/ActorQueue.swift b/Sources/AsyncQueue/ActorQueue.swift index 575310a..79948d4 100644 --- a/Sources/AsyncQueue/ActorQueue.swift +++ b/Sources/AsyncQueue/ActorQueue.swift @@ -55,11 +55,12 @@ public final class ActorQueue: @unchecked Sendable { // MARK: Initialization /// Instantiates an actor queue. - public init() { + /// - Parameter name: Human readable name of the task. + public init(name: String? = nil) { let (taskStream, taskStreamContinuation) = AsyncStream.makeStream() self.taskStreamContinuation = taskStreamContinuation - Task { + Task(name: name) { // In an ideal world, we would isolate this `for await` loop to the `ActorType`. // However, there's no good way to do that without retaining the actor and creating a cycle. for await actorTask in taskStream { diff --git a/Sources/AsyncQueue/FIFOQueue.swift b/Sources/AsyncQueue/FIFOQueue.swift index 36dfff7..d83a5e8 100644 --- a/Sources/AsyncQueue/FIFOQueue.swift +++ b/Sources/AsyncQueue/FIFOQueue.swift @@ -27,12 +27,13 @@ public final class FIFOQueue: Sendable { // MARK: Initialization /// Instantiates a FIFO queue. + /// - Parameter name: Human readable name of the task. /// - Parameter priority: The baseline priority of the tasks added to the asynchronous queue. - public init(priority: TaskPriority? = nil) { + public init(name: String? = nil, priority: TaskPriority? = nil) { let (taskStream, taskStreamContinuation) = AsyncStream.makeStream() self.taskStreamContinuation = taskStreamContinuation - Task.detached(priority: priority) { + Task.detached(name: name, priority: priority) { for await fifoTask in taskStream { await fifoTask.task() } From 5eaad4ff4e404238ee03fd16e2172dc92d4bb1cc Mon Sep 17 00:00:00 2001 From: songhuaixu <965991028@qq.com> Date: Fri, 28 Nov 2025 10:15:13 +0800 Subject: [PATCH 2/4] Update Sources/AsyncQueue/FIFOQueue.swift Co-authored-by: Dan Federman --- Sources/AsyncQueue/FIFOQueue.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/AsyncQueue/FIFOQueue.swift b/Sources/AsyncQueue/FIFOQueue.swift index d83a5e8..606dbd0 100644 --- a/Sources/AsyncQueue/FIFOQueue.swift +++ b/Sources/AsyncQueue/FIFOQueue.swift @@ -27,7 +27,7 @@ public final class FIFOQueue: Sendable { // MARK: Initialization /// Instantiates a FIFO queue. - /// - Parameter name: Human readable name of the task. + /// - Parameter name: Human readable name of the queue. /// - Parameter priority: The baseline priority of the tasks added to the asynchronous queue. public init(name: String? = nil, priority: TaskPriority? = nil) { let (taskStream, taskStreamContinuation) = AsyncStream.makeStream() From d4c451c6d8d92d5435f5a57c449f7d431a026af6 Mon Sep 17 00:00:00 2001 From: songhuaixu <965991028@qq.com> Date: Fri, 28 Nov 2025 10:15:19 +0800 Subject: [PATCH 3/4] Update Sources/AsyncQueue/ActorQueue.swift Co-authored-by: Dan Federman --- Sources/AsyncQueue/ActorQueue.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/AsyncQueue/ActorQueue.swift b/Sources/AsyncQueue/ActorQueue.swift index 79948d4..2cfc744 100644 --- a/Sources/AsyncQueue/ActorQueue.swift +++ b/Sources/AsyncQueue/ActorQueue.swift @@ -55,7 +55,7 @@ public final class ActorQueue: @unchecked Sendable { // MARK: Initialization /// Instantiates an actor queue. - /// - Parameter name: Human readable name of the task. + /// - Parameter name: Human readable name of the queue. public init(name: String? = nil) { let (taskStream, taskStreamContinuation) = AsyncStream.makeStream() self.taskStreamContinuation = taskStreamContinuation From 23179dfe2f91e2e32af761ce5a1446ec9bdd1347 Mon Sep 17 00:00:00 2001 From: songhuaixu <965991028@qq.com> Date: Fri, 28 Nov 2025 10:32:19 +0800 Subject: [PATCH 4/4] task add name --- Sources/AsyncQueue/ActorQueue.swift | 25 ++++++++++++++------- Sources/AsyncQueue/FIFOQueue.swift | 25 ++++++++++++++------- Sources/AsyncQueue/Utilities/Delivery.swift | 3 ++- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Sources/AsyncQueue/ActorQueue.swift b/Sources/AsyncQueue/ActorQueue.swift index 2cfc744..0f2d1d5 100644 --- a/Sources/AsyncQueue/ActorQueue.swift +++ b/Sources/AsyncQueue/ActorQueue.swift @@ -147,10 +147,13 @@ extension Task { /// it only makes it impossible for you to explicitly cancel the task. /// /// - Parameters: + /// - name: Human readable name of the task. + /// - priority: The priority of the operation task. /// - actorQueue: The queue on which to enqueue the task. /// - operation: The operation to perform. @discardableResult public init( + name: String? = nil, priority: TaskPriority? = nil, on actorQueue: ActorQueue, operation: @Sendable @escaping (isolated ActorType) async -> Success @@ -163,11 +166,11 @@ extension Task { await semaphore.wait() delivery.execute({ @Sendable executionContext in await delivery.sendValue(operation(executionContext)) - }, in: executionContext, priority: priority) + }, in: executionContext, name: name, priority: priority) } ) actorQueue.taskStreamContinuation.yield(task) - self.init(priority: priority) { + self.init(name: name, priority: priority) { await withTaskCancellationHandler( operation: { await semaphore.signal() @@ -200,12 +203,14 @@ extension Task { /// it only makes it impossible for you to explicitly cancel the task. /// /// - Parameters: + /// - name: Human readable name of the task. /// - priority: The priority of the task. /// Pass `nil` to use the priority from `Task.currentPriority`. /// - actorQueue: The queue on which to enqueue the task. /// - operation: The operation to perform. @discardableResult public init( + name: String? = nil, priority: TaskPriority? = nil, on actorQueue: ActorQueue, operation: @escaping @Sendable (isolated ActorType) async throws -> Success @@ -222,11 +227,11 @@ extension Task { } catch { await delivery.sendFailure(error) } - }, in: executionContext, priority: priority) + }, in: executionContext, name: name, priority: priority) } ) actorQueue.taskStreamContinuation.yield(task) - self.init(priority: priority) { + self.init(name: name, priority: priority) { try await withTaskCancellationHandler( operation: { await semaphore.signal() @@ -259,12 +264,14 @@ extension Task { /// it only makes it impossible for you to explicitly cancel the task. /// /// - Parameters: + /// - name: Human readable name of the task. /// - priority: The priority of the task. /// Pass `nil` to use the priority from `Task.currentPriority`. /// - actorQueue: The queue on which to enqueue the task. /// - operation: The operation to perform. @discardableResult public init( + name: String? = nil, priority: TaskPriority? = nil, on actorQueue: ActorQueue, operation: @MainActor @escaping () async -> Success @@ -277,11 +284,11 @@ extension Task { await semaphore.wait() delivery.execute({ @Sendable executionContext in await delivery.sendValue(operation()) - }, in: executionContext, priority: priority) + }, in: executionContext, name: name, priority: priority) } ) actorQueue.taskStreamContinuation.yield(task) - self.init(priority: priority) { + self.init(name: name, priority: priority) { await withTaskCancellationHandler( operation: { await semaphore.signal() @@ -314,12 +321,14 @@ extension Task { /// it only makes it impossible for you to explicitly cancel the task. /// /// - Parameters: + /// - name: Human readable name of the task. /// - priority: The priority of the task. /// Pass `nil` to use the priority from `Task.currentPriority`. /// - actorQueue: The queue on which to enqueue the task. /// - operation: The operation to perform. @discardableResult public init( + name: String? = nil, priority: TaskPriority? = nil, on actorQueue: ActorQueue, operation: @escaping @MainActor () async throws -> Success @@ -336,11 +345,11 @@ extension Task { } catch { await delivery.sendFailure(error) } - }, in: executionContext, priority: priority) + }, in: executionContext, name: name, priority: priority) } ) actorQueue.taskStreamContinuation.yield(task) - self.init(priority: priority) { + self.init(name: name, priority: priority) { try await withTaskCancellationHandler( operation: { await semaphore.signal() diff --git a/Sources/AsyncQueue/FIFOQueue.swift b/Sources/AsyncQueue/FIFOQueue.swift index 606dbd0..8dd41ea 100644 --- a/Sources/AsyncQueue/FIFOQueue.swift +++ b/Sources/AsyncQueue/FIFOQueue.swift @@ -80,10 +80,12 @@ extension Task { /// it only makes it impossible for you to explicitly cancel the task. /// /// - Parameters: + /// - name: Human readable name of the task. /// - fifoQueue: The queue on which to enqueue the task. /// - operation: The operation to perform. @discardableResult public init( + name: String? = nil, on fifoQueue: FIFOQueue, @_inheritActorContext @_implicitSelfCapture operation: sending @escaping @isolated(any) () async -> Success ) where Failure == Never { @@ -94,10 +96,10 @@ extension Task { await semaphore.wait() await delivery.execute({ @Sendable delivery in await delivery.sendValue(executeOnce.operation()) - }, in: delivery).value + }, in: delivery, name: name).value } fifoQueue.taskStreamContinuation.yield(task) - self.init { + self.init(name: name) { await withTaskCancellationHandler( operation: { await semaphore.signal() @@ -130,10 +132,12 @@ extension Task { /// it only makes it impossible for you to explicitly cancel the task. /// /// - Parameters: + /// - name: Human readable name of the task. /// - fifoQueue: The queue on which to enqueue the task. /// - operation: The operation to perform. @discardableResult public init( + name: String? = nil, on fifoQueue: FIFOQueue, @_inheritActorContext @_implicitSelfCapture operation: sending @escaping @isolated(any) () async throws -> Success ) where Failure == any Error { @@ -148,10 +152,10 @@ extension Task { } catch { delivery.sendFailure(error) } - }, in: delivery).value + }, in: delivery, name: name).value } fifoQueue.taskStreamContinuation.yield(task) - self.init { + self.init(name: name) { try await withTaskCancellationHandler( operation: { await semaphore.signal() @@ -184,11 +188,14 @@ extension Task { /// it only makes it impossible for you to explicitly cancel the task. /// /// - Parameters: + /// - name: Human readable name of the task. + /// - priority: The priority of the operation task. /// - fifoQueue: The queue on which to enqueue the task. /// - isolatedActor: The actor to which the operation is isolated. /// - operation: The operation to perform. @discardableResult public init( + name: String? = nil, priority: TaskPriority? = nil, on fifoQueue: FIFOQueue, isolatedTo isolatedActor: ActorType, @@ -200,10 +207,10 @@ extension Task { await semaphore.wait() await delivery.execute({ @Sendable isolatedActor in await delivery.sendValue(operation(isolatedActor)) - }, in: isolatedActor, priority: priority).value + }, in: isolatedActor, name: name, priority: priority).value } fifoQueue.taskStreamContinuation.yield(task) - self.init { + self.init(name: name) { await withTaskCancellationHandler( operation: { await semaphore.signal() @@ -236,6 +243,7 @@ extension Task { /// it only makes it impossible for you to explicitly cancel the task. /// /// - Parameters: + /// - name: Human readable name of the task. /// - priority: The priority of the queue. /// Pass `nil` to use the priority from `Task.currentPriority`. /// - fifoQueue: The queue on which to enqueue the task. @@ -243,6 +251,7 @@ extension Task { /// - operation: The operation to perform. @discardableResult public init( + name: String? = nil, priority: TaskPriority? = nil, on fifoQueue: FIFOQueue, isolatedTo isolatedActor: ActorType, @@ -258,10 +267,10 @@ extension Task { } catch { await delivery.sendFailure(error) } - }, in: isolatedActor, priority: priority).value + }, in: isolatedActor, name: name, priority: priority).value } fifoQueue.taskStreamContinuation.yield(task) - self.init(priority: priority) { + self.init(name: name, priority: priority) { try await withTaskCancellationHandler( operation: { await semaphore.signal() diff --git a/Sources/AsyncQueue/Utilities/Delivery.swift b/Sources/AsyncQueue/Utilities/Delivery.swift index 3ce6d4a..9e23558 100644 --- a/Sources/AsyncQueue/Utilities/Delivery.swift +++ b/Sources/AsyncQueue/Utilities/Delivery.swift @@ -43,11 +43,12 @@ actor Delivery { func execute( _ operation: sending @escaping (isolated ActorType) async -> Void, in context: isolated ActorType, + name: String? = nil, priority: TaskPriority? = nil ) -> Task { // In Swift 6, a `Task` enqueued from an actor begins executing immediately on that actor. // Since we're running on our actor's context already, we can just dispatch a Task to get first-enqueued-first-start task execution. - let task = Task(priority: priority) { + let task = Task(name: name, priority: priority) { await operation(context) } taskContainer.withLock {