File tree Expand file tree Collapse file tree 2 files changed +15
-14
lines changed
Expand file tree Collapse file tree 2 files changed +15
-14
lines changed Original file line number Diff line number Diff line change @@ -42,28 +42,29 @@ public final class ActorQueue: Sendable {
4242
4343 streamTask = Task . detached ( priority: priority) {
4444 actor ActorExecutor {
45- func seriallyExecute ( _ task: @escaping @Sendable ( ) async -> Void ) async {
45+ func suspendUntilStarted ( _ task: @escaping @Sendable ( ) async -> Void ) async {
4646 let semaphore = Semaphore ( )
47- Task {
48- await self . execute ( task, afterSignaling: semaphore)
49- }
50- // Wait for the task to start.
47+ executeWithoutWaiting ( task, afterSignaling: semaphore)
48+ // Suspend the calling code until our enqueued task starts.
5149 await semaphore. wait ( )
5250 }
5351
54- private func execute (
52+ private func executeWithoutWaiting (
5553 _ task: @escaping @Sendable ( ) async -> Void ,
56- afterSignaling semaphore: Semaphore
57- ) async {
58- // Signal that the task has started.
59- await semaphore. signal ( )
60- await task ( )
54+ afterSignaling semaphore: Semaphore )
55+ {
56+ // Utilize the serial (but not FIFO) Actor context to execute the task without requiring the calling method to wait for the task to complete.
57+ Task {
58+ // Now that we're back within the serial Actor context, signal that the task has started.
59+ await semaphore. signal ( )
60+ await task ( )
61+ }
6162 }
6263 }
6364
6465 let executor = ActorExecutor ( )
6566 for await task in taskStream {
66- await executor. seriallyExecute ( task)
67+ await executor. suspendUntilStarted ( task)
6768 }
6869 }
6970 }
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ actor Semaphore {
2929 return
3030 }
3131
32- await withCheckedContinuation { continuation in
32+ await withUnsafeContinuation { continuation in
3333 continuations. append ( continuation)
3434 }
3535 }
@@ -52,6 +52,6 @@ actor Semaphore {
5252 count < 0
5353 }
5454
55- private var continuations = [ CheckedContinuation < Void , Never > ] ( )
55+ private var continuations = [ UnsafeContinuation < Void , Never > ] ( )
5656 private var count = 0
5757}
You can’t perform that action at this time.
0 commit comments