@@ -245,8 +245,9 @@ internal class CoroutineScheduler(
245
245
246
246
private val random = Random ()
247
247
248
- // This is used a "stop signal" for debugging/tests only
249
- private val isTerminated = atomic(false )
248
+ // This is used a "stop signal" for close and shutdown functions
249
+ private val _isTerminated = atomic(0 ) // todo: replace with atomic boolean on new versions of atomicFu
250
+ private val isTerminated: Boolean get() = _isTerminated .value != 0
250
251
251
252
companion object {
252
253
private const val MAX_SPINS = 1000
@@ -295,7 +296,7 @@ internal class CoroutineScheduler(
295
296
// Shuts down current scheduler and waits until all work is done and all threads are stopped.
296
297
fun shutdown (timeout : Long ) {
297
298
// atomically set termination flag which is checked when workers are added or removed
298
- if (! isTerminated .compareAndSet(false , true )) return
299
+ if (! _isTerminated .compareAndSet(0 , 1 )) return
299
300
// make sure we are not waiting for the current thread
300
301
val currentWorker = Thread .currentThread() as ? Worker
301
302
// Capture # of created workers that cannot change anymore (mind the synchronized block!)
@@ -438,7 +439,7 @@ internal class CoroutineScheduler(
438
439
private fun createNewWorker (): Int {
439
440
synchronized(workers) {
440
441
// Make sure we're not trying to resurrect terminated scheduler
441
- if (isTerminated.value ) throw RejectedExecutionException (" $schedulerName was terminated" )
442
+ if (isTerminated) throw RejectedExecutionException (" $schedulerName was terminated" )
442
443
val state = controlState.value
443
444
val created = createdWorkers(state)
444
445
val blocking = blockingWorkers(state)
@@ -693,7 +694,7 @@ internal class CoroutineScheduler(
693
694
694
695
override fun run () {
695
696
var wasIdle = false // local variable to avoid extra idleReset invocations when tasks repeatedly arrive
696
- while (! isTerminated.value && state != WorkerState .TERMINATED ) {
697
+ while (! isTerminated && state != WorkerState .TERMINATED ) {
697
698
val task = findTask()
698
699
if (task == null ) {
699
700
// Wait for a job with potential park
@@ -817,7 +818,7 @@ internal class CoroutineScheduler(
817
818
private fun tryTerminateWorker () {
818
819
synchronized(workers) {
819
820
// Make sure we're not trying race with termination of scheduler
820
- if (isTerminated.value ) return
821
+ if (isTerminated) return
821
822
// Someone else terminated, bail out
822
823
if (createdWorkers <= corePoolSize) return
823
824
// Try to find blocking task before termination
0 commit comments