Created
March 5, 2020 05:40
-
-
Save wangyung/16bb22443874b707570ba349390fd6e9 to your computer and use it in GitHub Desktop.
The dispatch function in CoroutineScheduler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun dispatch(block: Runnable, taskContext: TaskContext = NonBlockingContext, fair: Boolean = false) { | |
trackTask() // this is needed for virtual time support | |
val task = createTask(block, taskContext) | |
// try to submit the task to the local queue and act depending on the result | |
val notAdded = submitToLocalQueue(task, fair) | |
if (notAdded != null) { | |
if (!addToGlobalQueue(notAdded)) { | |
// Global queue is closed in the last step of close/shutdown -- no more tasks should be accepted | |
throw RejectedExecutionException("$schedulerName was terminated") | |
} | |
} | |
// Checking 'task' instead of 'notAdded' is completely okay | |
if (task.mode == TaskMode.NON_BLOCKING) { | |
signalCpuWork() | |
} else { | |
signalBlockingWork() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment