Skip to content

Commit bd27a14

Browse files
committed
[Concurrency] Fix cooperative executor to return only after all jobs run.
We were terminating after the first set of jobs; if one of them scheduled another job, and there were no timers running, we would terminate, which was wrong.
1 parent c866fa0 commit bd27a14

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

stdlib/public/Concurrency/CooperativeExecutor.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,15 @@ extension CooperativeExecutor: RunLoopExecutor {
265265
_sleep(seconds: toWait.seconds,
266266
nanoseconds: toWait.nanoseconds)
267267
}
268-
} else {
268+
} else if runQueue.isEmpty {
269269
// Stop if no more jobs are available
270270
break
271271
}
272272
#else // $Embedded || SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
273-
// Stop if no more jobs are available
274-
break
273+
if runQueue.isEmpty {
274+
// Stop if no more jobs are available
275+
break
276+
}
275277
#endif
276278
}
277279
}

stdlib/public/Concurrency/PriorityQueue.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ struct PriorityQueue<T> {
6060
upHeap(ndx: storage.count - 1)
6161
}
6262

63+
/// Return `true` if the queue is empty
64+
var isEmpty: Bool {
65+
return storage.isEmpty
66+
}
67+
6368
/// The highest priority item from the queue, or `nil` if none.
6469
var top: T? {
6570
if storage.isEmpty {

0 commit comments

Comments
 (0)