@@ -245,8 +245,9 @@ internal class CoroutineScheduler(
245245
246246 private val random = Random ()
247247
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
250251
251252 companion object {
252253 private const val MAX_SPINS = 1000
@@ -295,7 +296,7 @@ internal class CoroutineScheduler(
295296 // Shuts down current scheduler and waits until all work is done and all threads are stopped.
296297 fun shutdown (timeout : Long ) {
297298 // 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
299300 // make sure we are not waiting for the current thread
300301 val currentWorker = Thread .currentThread() as ? Worker
301302 // Capture # of created workers that cannot change anymore (mind the synchronized block!)
@@ -438,7 +439,7 @@ internal class CoroutineScheduler(
438439 private fun createNewWorker (): Int {
439440 synchronized(workers) {
440441 // 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" )
442443 val state = controlState.value
443444 val created = createdWorkers(state)
444445 val blocking = blockingWorkers(state)
@@ -693,7 +694,7 @@ internal class CoroutineScheduler(
693694
694695 override fun run () {
695696 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 ) {
697698 val task = findTask()
698699 if (task == null ) {
699700 // Wait for a job with potential park
@@ -817,7 +818,7 @@ internal class CoroutineScheduler(
817818 private fun tryTerminateWorker () {
818819 synchronized(workers) {
819820 // Make sure we're not trying race with termination of scheduler
820- if (isTerminated.value ) return
821+ if (isTerminated) return
821822 // Someone else terminated, bail out
822823 if (createdWorkers <= corePoolSize) return
823824 // Try to find blocking task before termination
0 commit comments