Skip to content

Instantly share code, notes, and snippets.

@wangyung
Created March 5, 2020 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangyung/16bb22443874b707570ba349390fd6e9 to your computer and use it in GitHub Desktop.
Save wangyung/16bb22443874b707570ba349390fd6e9 to your computer and use it in GitHub Desktop.
The dispatch function in CoroutineScheduler.kt
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