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
internal inner class Worker private constructor() : Thread() { | |
... | |
private fun executeTask(task: Task) { | |
val taskMode = task.mode | |
idleReset(taskMode) | |
beforeTask(taskMode) | |
runSafely(task) | |
afterTask(taskMode) | |
} | |
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
internal inner class Worker private constructor() : Thread() { | |
... | |
override fun run() = runWorker() | |
private fun runWorker() { | |
... | |
while (!isTerminated && state != WorkerState.TERMINATED) { | |
val task = findTask(mayHaveLocalTasks) | |
if (task != null) { | |
... |
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
internal class CoroutineScheduler( | |
@JvmField val corePoolSize: Int, | |
@JvmField val maxPoolSize: Int, | |
@JvmField val idleWorkerKeepAliveNs: Long = IDLE_WORKER_KEEP_ALIVE_NS, | |
@JvmField val schedulerName: String = DEFAULT_SCHEDULER_NAME | |
) : Executor, Closeable { | |
... | |
override fun execute(command: Runnable) = dispatch(command) |
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
@JvmStatic | |
public actual val Default: CoroutineDispatcher = DefaultScheduler | |
internal object DefaultScheduler : SchedulerCoroutineDispatcher( | |
CORE_POOL_SIZE, MAX_POOL_SIZE, | |
IDLE_WORKER_KEEP_ALIVE_NS, DEFAULT_SCHEDULER_NAME | |
) { | |
... | |
} |
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
/** | |
* Resumes the execution of the corresponding coroutine passing [value] as the return value of the last suspension point. | |
*/ | |
@SinceKotlin("1.3") | |
@InlineOnly | |
public inline fun <T> Continuation<T>.resume(value: T): Unit = | |
resumeWith(Result.success(value)) | |
/** | |
* Resumes the execution of the corresponding coroutine so that the [exception] is re-thrown right after the |
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
internal abstract class DispatchedTask<in T>( | |
@JvmField public var resumeMode: Int | |
) : SchedulerTask() { | |
public final override fun run() { | |
// ... | |
try { | |
val delegate = delegate as DispatchedContinuation<T> | |
val continuation = delegate.continuation | |
withContinuationContext(continuation, delegate.countOrElement) { |
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
@FunctionalInterface | |
public interface Runnable { | |
void run(); | |
} |
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
private inline fun runSafely(completion: Continuation<*>, block: () -> Unit) { | |
try { | |
block() | |
} catch (e: Throwable) { | |
completion.resumeWith(Result.failure(e)) | |
} | |
} |
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
// The initial state of the state machine | |
int label = 0 | |
A a = null | |
B b = null | |
void resumeWith(Object result) { | |
if (label == 0) goto L0 | |
if (label == 1) goto L1 | |
if (label == 2) goto L2 | |
else throw IllegalStateException("call to 'resume' before 'invoke' with coroutine") |
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
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
lifecycleScope.launch { | |
val randomNum = getRandomNum() // suspension point #1 | |
val sqrt = getSqrt(randomNum.toDouble()) // suspension point #2 | |
log(sqrt.toString()) | |
} |
NewerOlder