@@ -384,14 +384,15 @@ 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,
387
+ * [fair ] -- whether this [dispatch] call is fair .
388
+ * If `true` then the task will be dispatched in a FIFO manner and no additional workers will be requested,
389
389
* but only if the current thread is a corresponding worker thread.
390
390
* Note that caller cannot be ensured that it is being executed on worker thread for the following reasons:
391
391
* - [CoroutineStart.UNDISPATCHED]
392
- * - Concurrent [close] that effectively shutdowns the worker thread
392
+ * - Concurrent [close] that effectively shutdowns the worker thread.
393
+ * Used for [yield].
393
394
*/
394
- fun dispatch (block : Runnable , taskContext : TaskContext = NonBlockingContext , tailDispatch : Boolean = false) {
395
+ fun dispatch (block : Runnable , taskContext : TaskContext = NonBlockingContext , fair : Boolean = false) {
395
396
trackTask() // this is needed for virtual time support
396
397
val task = createTask(block, taskContext)
397
398
val isBlockingTask = task.isBlocking
@@ -400,14 +401,14 @@ internal class CoroutineScheduler(
400
401
val stateSnapshot = if (isBlockingTask) incrementBlockingTasks() else 0
401
402
// try to submit the task to the local queue and act depending on the result
402
403
val currentWorker = currentWorker()
403
- val notAdded = currentWorker.submitToLocalQueue(task, tailDispatch )
404
+ val notAdded = currentWorker.submitToLocalQueue(task, fair )
404
405
if (notAdded != null ) {
405
406
if (! addToGlobalQueue(notAdded)) {
406
407
// Global queue is closed in the last step of close/shutdown -- no more tasks should be accepted
407
408
throw RejectedExecutionException (" $schedulerName was terminated" )
408
409
}
409
410
}
410
- val skipUnpark = tailDispatch && currentWorker != null
411
+ val skipUnpark = fair && currentWorker != null
411
412
// Checking 'task' instead of 'notAdded' is completely okay
412
413
if (isBlockingTask) {
413
414
// Use state snapshot to better estimate the number of running threads
@@ -506,7 +507,7 @@ internal class CoroutineScheduler(
506
507
* Returns `null` if task was successfully added or an instance of the
507
508
* task that was not added or replaced (thus should be added to global queue).
508
509
*/
509
- private fun Worker?.submitToLocalQueue (task : Task , tailDispatch : Boolean ): Task ? {
510
+ private fun Worker?.submitToLocalQueue (task : Task , fair : Boolean ): Task ? {
510
511
if (this == null ) return task
511
512
/*
512
513
* This worker could have been already terminated from this thread by close/shutdown and it should not
@@ -518,7 +519,7 @@ internal class CoroutineScheduler(
518
519
return task
519
520
}
520
521
mayHaveLocalTasks = true
521
- return localQueue.add(task, fair = tailDispatch )
522
+ return localQueue.add(task, fair = fair )
522
523
}
523
524
524
525
private fun currentWorker (): Worker ? = (Thread .currentThread() as ? Worker )?.takeIf { it.scheduler == this }
0 commit comments