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 DispatchedContinuation<in T>( | |
@JvmField val dispatcher: CoroutineDispatcher, | |
@JvmField val continuation: Continuation<T> | |
) : DispatchedTask<T>(MODE_ATOMIC_DEFAULT), CoroutineStackFrame, Continuation<T> by continuation { | |
// Use delegate to store the current object | |
override val delegate: Continuation<T> | |
get() = this | |
// ATOMIC boot mode |
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
@InternalCoroutinesApi | |
public abstract class AbstractCoroutine<in T>( | |
parentContext: CoroutineContext, | |
initParentJob: Boolean, | |
active: Boolean | |
) : JobSupport(active), Job, Continuation<T>, CoroutineScope { | |
// ... | |
/** |
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 BaseContinuationImpl (...) { | |
// Implement resumeWith of Continuation | |
// It is final and cannot be overridden! | |
public final override fun resumeWith (result: Result<Any?>) { | |
// ... | |
val outcome = invokeSuspend(param) | |
// ... | |
} | |
// For implementation | |
protected abstract fun invokeSuspend (result: Result<Any?>) : Any? |
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
public interface CoroutineScope { | |
public val coroutineContext: CoroutineContext | |
} |
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
public final override fun resumeWith(result: Result<Any?>) { | |
// This loop unrolls recursion in current.resumeWith(param) to make saner and shorter stack traces on resume | |
var current = this | |
var param = result | |
while (true) { | |
// Invoke "resume" debug probe on every resumed continuation, so that a debugging library infrastructure | |
// can precisely track what part of suspended callstack was already resumed | |
probeCoroutineResumed(current) | |
with(current) { | |
val completion = completion!! // fail fast when trying to resume continuation without completion |
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
inline fun resumeCancellableWith( | |
result: Result<T>, | |
noinline onCancellation: ((cause: Throwable) -> Unit)? | |
) { | |
val state = result.toState(onCancellation) | |
if (dispatcher.isDispatchNeeded(context)) { | |
_state = state | |
resumeMode = MODE_CANCELLABLE | |
dispatcher.dispatch(context, this) | |
} else { |
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
public abstract class CoroutineDispatcher : | |
AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor { | |
public abstract fun dispatch(context: CoroutineContext, block: Runnable) | |
public open fun isDispatchNeeded(context: CoroutineContext): Boolean = true | |
} |
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 Task( | |
@JvmField var submissionTime: Long, | |
@JvmField var taskContext: TaskContext | |
) : Runnable { | |
...... | |
} |
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 actual typealias SchedulerTask = Task |