Skip to content

Commit 23179df

Browse files
committed
task add name
1 parent d4c451c commit 23179df

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

Sources/AsyncQueue/ActorQueue.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,13 @@ extension Task {
147147
/// it only makes it impossible for you to explicitly cancel the task.
148148
///
149149
/// - Parameters:
150+
/// - name: Human readable name of the task.
151+
/// - priority: The priority of the operation task.
150152
/// - actorQueue: The queue on which to enqueue the task.
151153
/// - operation: The operation to perform.
152154
@discardableResult
153155
public init<ActorType: Actor>(
156+
name: String? = nil,
154157
priority: TaskPriority? = nil,
155158
on actorQueue: ActorQueue<ActorType>,
156159
operation: @Sendable @escaping (isolated ActorType) async -> Success
@@ -163,11 +166,11 @@ extension Task {
163166
await semaphore.wait()
164167
delivery.execute({ @Sendable executionContext in
165168
await delivery.sendValue(operation(executionContext))
166-
}, in: executionContext, priority: priority)
169+
}, in: executionContext, name: name, priority: priority)
167170
}
168171
)
169172
actorQueue.taskStreamContinuation.yield(task)
170-
self.init(priority: priority) {
173+
self.init(name: name, priority: priority) {
171174
await withTaskCancellationHandler(
172175
operation: {
173176
await semaphore.signal()
@@ -200,12 +203,14 @@ extension Task {
200203
/// it only makes it impossible for you to explicitly cancel the task.
201204
///
202205
/// - Parameters:
206+
/// - name: Human readable name of the task.
203207
/// - priority: The priority of the task.
204208
/// Pass `nil` to use the priority from `Task.currentPriority`.
205209
/// - actorQueue: The queue on which to enqueue the task.
206210
/// - operation: The operation to perform.
207211
@discardableResult
208212
public init<ActorType: Actor>(
213+
name: String? = nil,
209214
priority: TaskPriority? = nil,
210215
on actorQueue: ActorQueue<ActorType>,
211216
operation: @escaping @Sendable (isolated ActorType) async throws -> Success
@@ -222,11 +227,11 @@ extension Task {
222227
} catch {
223228
await delivery.sendFailure(error)
224229
}
225-
}, in: executionContext, priority: priority)
230+
}, in: executionContext, name: name, priority: priority)
226231
}
227232
)
228233
actorQueue.taskStreamContinuation.yield(task)
229-
self.init(priority: priority) {
234+
self.init(name: name, priority: priority) {
230235
try await withTaskCancellationHandler(
231236
operation: {
232237
await semaphore.signal()
@@ -259,12 +264,14 @@ extension Task {
259264
/// it only makes it impossible for you to explicitly cancel the task.
260265
///
261266
/// - Parameters:
267+
/// - name: Human readable name of the task.
262268
/// - priority: The priority of the task.
263269
/// Pass `nil` to use the priority from `Task.currentPriority`.
264270
/// - actorQueue: The queue on which to enqueue the task.
265271
/// - operation: The operation to perform.
266272
@discardableResult
267273
public init(
274+
name: String? = nil,
268275
priority: TaskPriority? = nil,
269276
on actorQueue: ActorQueue<MainActor>,
270277
operation: @MainActor @escaping () async -> Success
@@ -277,11 +284,11 @@ extension Task {
277284
await semaphore.wait()
278285
delivery.execute({ @Sendable executionContext in
279286
await delivery.sendValue(operation())
280-
}, in: executionContext, priority: priority)
287+
}, in: executionContext, name: name, priority: priority)
281288
}
282289
)
283290
actorQueue.taskStreamContinuation.yield(task)
284-
self.init(priority: priority) {
291+
self.init(name: name, priority: priority) {
285292
await withTaskCancellationHandler(
286293
operation: {
287294
await semaphore.signal()
@@ -314,12 +321,14 @@ extension Task {
314321
/// it only makes it impossible for you to explicitly cancel the task.
315322
///
316323
/// - Parameters:
324+
/// - name: Human readable name of the task.
317325
/// - priority: The priority of the task.
318326
/// Pass `nil` to use the priority from `Task.currentPriority`.
319327
/// - actorQueue: The queue on which to enqueue the task.
320328
/// - operation: The operation to perform.
321329
@discardableResult
322330
public init(
331+
name: String? = nil,
323332
priority: TaskPriority? = nil,
324333
on actorQueue: ActorQueue<MainActor>,
325334
operation: @escaping @MainActor () async throws -> Success
@@ -336,11 +345,11 @@ extension Task {
336345
} catch {
337346
await delivery.sendFailure(error)
338347
}
339-
}, in: executionContext, priority: priority)
348+
}, in: executionContext, name: name, priority: priority)
340349
}
341350
)
342351
actorQueue.taskStreamContinuation.yield(task)
343-
self.init(priority: priority) {
352+
self.init(name: name, priority: priority) {
344353
try await withTaskCancellationHandler(
345354
operation: {
346355
await semaphore.signal()

Sources/AsyncQueue/FIFOQueue.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ extension Task {
8080
/// it only makes it impossible for you to explicitly cancel the task.
8181
///
8282
/// - Parameters:
83+
/// - name: Human readable name of the task.
8384
/// - fifoQueue: The queue on which to enqueue the task.
8485
/// - operation: The operation to perform.
8586
@discardableResult
8687
public init(
88+
name: String? = nil,
8789
on fifoQueue: FIFOQueue,
8890
@_inheritActorContext @_implicitSelfCapture operation: sending @escaping @isolated(any) () async -> Success
8991
) where Failure == Never {
@@ -94,10 +96,10 @@ extension Task {
9496
await semaphore.wait()
9597
await delivery.execute({ @Sendable delivery in
9698
await delivery.sendValue(executeOnce.operation())
97-
}, in: delivery).value
99+
}, in: delivery, name: name).value
98100
}
99101
fifoQueue.taskStreamContinuation.yield(task)
100-
self.init {
102+
self.init(name: name) {
101103
await withTaskCancellationHandler(
102104
operation: {
103105
await semaphore.signal()
@@ -130,10 +132,12 @@ extension Task {
130132
/// it only makes it impossible for you to explicitly cancel the task.
131133
///
132134
/// - Parameters:
135+
/// - name: Human readable name of the task.
133136
/// - fifoQueue: The queue on which to enqueue the task.
134137
/// - operation: The operation to perform.
135138
@discardableResult
136139
public init(
140+
name: String? = nil,
137141
on fifoQueue: FIFOQueue,
138142
@_inheritActorContext @_implicitSelfCapture operation: sending @escaping @isolated(any) () async throws -> Success
139143
) where Failure == any Error {
@@ -148,10 +152,10 @@ extension Task {
148152
} catch {
149153
delivery.sendFailure(error)
150154
}
151-
}, in: delivery).value
155+
}, in: delivery, name: name).value
152156
}
153157
fifoQueue.taskStreamContinuation.yield(task)
154-
self.init {
158+
self.init(name: name) {
155159
try await withTaskCancellationHandler(
156160
operation: {
157161
await semaphore.signal()
@@ -184,11 +188,14 @@ extension Task {
184188
/// it only makes it impossible for you to explicitly cancel the task.
185189
///
186190
/// - Parameters:
191+
/// - name: Human readable name of the task.
192+
/// - priority: The priority of the operation task.
187193
/// - fifoQueue: The queue on which to enqueue the task.
188194
/// - isolatedActor: The actor to which the operation is isolated.
189195
/// - operation: The operation to perform.
190196
@discardableResult
191197
public init<ActorType: Actor>(
198+
name: String? = nil,
192199
priority: TaskPriority? = nil,
193200
on fifoQueue: FIFOQueue,
194201
isolatedTo isolatedActor: ActorType,
@@ -200,10 +207,10 @@ extension Task {
200207
await semaphore.wait()
201208
await delivery.execute({ @Sendable isolatedActor in
202209
await delivery.sendValue(operation(isolatedActor))
203-
}, in: isolatedActor, priority: priority).value
210+
}, in: isolatedActor, name: name, priority: priority).value
204211
}
205212
fifoQueue.taskStreamContinuation.yield(task)
206-
self.init {
213+
self.init(name: name) {
207214
await withTaskCancellationHandler(
208215
operation: {
209216
await semaphore.signal()
@@ -236,13 +243,15 @@ extension Task {
236243
/// it only makes it impossible for you to explicitly cancel the task.
237244
///
238245
/// - Parameters:
246+
/// - name: Human readable name of the task.
239247
/// - priority: The priority of the queue.
240248
/// Pass `nil` to use the priority from `Task.currentPriority`.
241249
/// - fifoQueue: The queue on which to enqueue the task.
242250
/// - isolatedActor: The actor to which the operation is isolated.
243251
/// - operation: The operation to perform.
244252
@discardableResult
245253
public init<ActorType: Actor>(
254+
name: String? = nil,
246255
priority: TaskPriority? = nil,
247256
on fifoQueue: FIFOQueue,
248257
isolatedTo isolatedActor: ActorType,
@@ -258,10 +267,10 @@ extension Task {
258267
} catch {
259268
await delivery.sendFailure(error)
260269
}
261-
}, in: isolatedActor, priority: priority).value
270+
}, in: isolatedActor, name: name, priority: priority).value
262271
}
263272
fifoQueue.taskStreamContinuation.yield(task)
264-
self.init(priority: priority) {
273+
self.init(name: name, priority: priority) {
265274
try await withTaskCancellationHandler(
266275
operation: {
267276
await semaphore.signal()

Sources/AsyncQueue/Utilities/Delivery.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ actor Delivery<Success: Sendable, Failure: Error> {
4343
func execute<ActorType: Actor>(
4444
_ operation: sending @escaping (isolated ActorType) async -> Void,
4545
in context: isolated ActorType,
46+
name: String? = nil,
4647
priority: TaskPriority? = nil
4748
) -> Task<Void, Never> {
4849
// In Swift 6, a `Task` enqueued from an actor begins executing immediately on that actor.
4950
// Since we're running on our actor's context already, we can just dispatch a Task to get first-enqueued-first-start task execution.
50-
let task = Task(priority: priority) {
51+
let task = Task(name: name, priority: priority) {
5152
await operation(context)
5253
}
5354
taskContainer.withLock {

0 commit comments

Comments
 (0)