@@ -384,14 +384,14 @@ internal class CoroutineScheduler(
384
384
* this [block] may execute blocking operations (IO, system calls, locking primitives etc.)
385
385
*
386
386
* [taskContext] -- concurrency context of given [block].
387
- * [tailDispatch] -- whether this [dispatch] call is the last action the (presumably) worker thread does in its current task.
388
- * If `true`, then the task will be dispatched in a FIFO manner and no additional workers will be requested,
389
- * but only if the current thread is a corresponding worker thread.
387
+ * [fair] -- whether this [dispatch] call is fair.
388
+ * If `true` then the task will be dispatched in a FIFO manner.
390
389
* Note that caller cannot be ensured that it is being executed on worker thread for the following reasons:
391
390
* - [CoroutineStart.UNDISPATCHED]
392
- * - Concurrent [close] that effectively shutdowns the worker thread
391
+ * - Concurrent [close] that effectively shutdowns the worker thread.
392
+ * Used for [yield].
393
393
*/
394
- fun dispatch (block : Runnable , taskContext : TaskContext = NonBlockingContext , tailDispatch : Boolean = false) {
394
+ fun dispatch (block : Runnable , taskContext : TaskContext = NonBlockingContext , fair : Boolean = false) {
395
395
trackTask() // this is needed for virtual time support
396
396
val task = createTask(block, taskContext)
397
397
val isBlockingTask = task.isBlocking
@@ -400,20 +400,18 @@ internal class CoroutineScheduler(
400
400
val stateSnapshot = if (isBlockingTask) incrementBlockingTasks() else 0
401
401
// try to submit the task to the local queue and act depending on the result
402
402
val currentWorker = currentWorker()
403
- val notAdded = currentWorker.submitToLocalQueue(task, tailDispatch )
403
+ val notAdded = currentWorker.submitToLocalQueue(task, fair )
404
404
if (notAdded != null ) {
405
405
if (! addToGlobalQueue(notAdded)) {
406
406
// Global queue is closed in the last step of close/shutdown -- no more tasks should be accepted
407
407
throw RejectedExecutionException (" $schedulerName was terminated" )
408
408
}
409
409
}
410
- val skipUnpark = tailDispatch && currentWorker != null
411
410
// Checking 'task' instead of 'notAdded' is completely okay
412
411
if (isBlockingTask) {
413
412
// Use state snapshot to better estimate the number of running threads
414
- signalBlockingWork(stateSnapshot, skipUnpark = skipUnpark )
413
+ signalBlockingWork(stateSnapshot)
415
414
} else {
416
- if (skipUnpark) return
417
415
signalCpuWork()
418
416
}
419
417
}
@@ -429,8 +427,7 @@ internal class CoroutineScheduler(
429
427
}
430
428
431
429
// NB: should only be called from 'dispatch' method due to blocking tasks increment
432
- private fun signalBlockingWork (stateSnapshot : Long , skipUnpark : Boolean ) {
433
- if (skipUnpark) return
430
+ private fun signalBlockingWork (stateSnapshot : Long ) {
434
431
if (tryUnpark()) return
435
432
// Use state snapshot to avoid accidental thread overprovision
436
433
if (tryCreateWorker(stateSnapshot)) return
@@ -506,7 +503,7 @@ internal class CoroutineScheduler(
506
503
* Returns `null` if task was successfully added or an instance of the
507
504
* task that was not added or replaced (thus should be added to global queue).
508
505
*/
509
- private fun Worker?.submitToLocalQueue (task : Task , tailDispatch : Boolean ): Task ? {
506
+ private fun Worker?.submitToLocalQueue (task : Task , fair : Boolean ): Task ? {
510
507
if (this == null ) return task
511
508
/*
512
509
* This worker could have been already terminated from this thread by close/shutdown and it should not
@@ -518,7 +515,7 @@ internal class CoroutineScheduler(
518
515
return task
519
516
}
520
517
mayHaveLocalTasks = true
521
- return localQueue.add(task, fair = tailDispatch )
518
+ return localQueue.add(task, fair = fair )
522
519
}
523
520
524
521
private fun currentWorker (): Worker ? = (Thread .currentThread() as ? Worker )?.takeIf { it.scheduler == this }
0 commit comments