@@ -299,7 +299,7 @@ internal class CoroutineScheduler(
299
299
// atomically set termination flag which is checked when workers are added or removed
300
300
if (! _isTerminated .compareAndSet(0 , 1 )) return
301
301
// make sure we are not waiting for the current thread
302
- val currentWorker = Thread .currentThread() as ? Worker
302
+ val currentWorker = currentWorker()
303
303
// Capture # of created workers that cannot change anymore (mind the synchronized block!)
304
304
val created = synchronized(workers) { createdWorkers }
305
305
// Shutdown all workers with the only exception of the current thread
@@ -481,9 +481,7 @@ internal class CoroutineScheduler(
481
481
* Returns [ADDED], or [NOT_ADDED], or [ADDED_REQUIRES_HELP].
482
482
*/
483
483
private fun submitToLocalQueue (task : Task , fair : Boolean ): Int {
484
- val worker = Thread .currentThread() as ? Worker
485
- ? : return NOT_ADDED
486
- if (worker.scheduler != = this ) return NOT_ADDED // different scheduler's worker (!!!)
484
+ val worker = currentWorker() ? : return NOT_ADDED
487
485
488
486
/*
489
487
* This worker could have been already terminated from this thread by close/shutdown and it should not
@@ -533,6 +531,11 @@ internal class CoroutineScheduler(
533
531
return ADDED_REQUIRES_HELP
534
532
}
535
533
534
+ private fun currentWorker (): Worker ? {
535
+ val worker = Thread .currentThread() as ? Worker ? : return null
536
+ return if (worker.scheduler == this ) worker else null
537
+ }
538
+
536
539
/* *
537
540
* Returns a string identifying the state of this scheduler for nicer debugging.
538
541
* Note that this method is not atomic and represents rough state of pool.
0 commit comments