scoop:scoop.core:0.1.0
src/coroutine.scoop
Selected production source · src/coroutine.scoop
1
// Minimal coroutine protocol (spec 11.9, M10 DESIGN section 1.1).2
// State machines, frames, and continuation adapters are compiler-generated;3
// these declarations provide the source-level typed boundary.5
/**6
* Receives exactly one terminal value or exception from a coroutine. It does not itself provide7
* scheduling or structured cancellation.8
*/9
public interface Continuation<T> {10
/**11
* Completes the suspended computation with a value. A continuation must not be completed12
* twice.13
*/14
public fun resume(value: T)15
/**16
* Completes the suspended computation exceptionally. A continuation must not be completed17
* twice.18
*/19
public fun resumeWithException(exception: Throwable)20
}22
/**23
* An object-based suspending computation accepted by the coroutine start intrinsic.24
*/25
public interface SuspendTask<T> {26
/**27
* Runs the computation and produces its eventual result.28
*/29
public suspend fun run(): T30
}32
/**33
* Registers completion of an externally driven suspension.34
*/35
public interface SuspendRegistration<T> {36
/**37
* Receives the continuation to complete when the external operation finishes.38
*/39
public fun register(continuation: Continuation<T>)40
}42
/**43
* Adapts a suspending function value to the object-based coroutine task protocol.44
*/45
public struct FunctionSuspendTask<T>(val task: suspend () -> T): SuspendTask<T> {46
/**47
* Invokes the wrapped suspending function.48
*/49
public final override suspend fun run(): T = task.invoke()50
}52
/**53
* Adapts a continuation-registration function to the object-based suspension protocol.54
*/55
public struct FunctionSuspendRegistration<T>(56
val registration: (Continuation<T>) -> Unit57
): SuspendRegistration<T> {58
/**59
* Passes the continuation to the wrapped registration function.60
*/61
public final override fun register(continuation: Continuation<T>) {62
registration.invoke(continuation)63
}64
}66
/**67
* Starts a suspending computation and delivers its eventual value or exception to `completion`.68
* This primitive does not create an event loop or a structured child job.69
*/70
public fun <T> startCoroutine(task: SuspendTask<T>, completion: Continuation<T>) {71
val execution = prepareCoroutineExecution(currentCoroutineExecutorOrNone(), None)72
startCoroutineTask(execution, false, task, completion)73
}75
internal fun <T> startCoroutineTask(76
execution: CoroutineExecution,77
dispatched: Boolean,78
task: SuspendTask<T>,79
completion: Continuation<T>80
) {81
startCoroutineExecution(82
execution,83
dispatched,84
RootSuspendTask(Some(task), completion),85
ExecutionCompletion(execution)86
)87
}89
@Intrinsic("coroutine_start")90
private fun <T> startCoroutineExecution(91
execution: CoroutineExecutionDriver,92
dispatched: Boolean,93
task: SuspendTask<T>,94
completion: Continuation<T>95
)97
/** Starts a task by submitting its first drive to the selected executor. */98
public fun <T> startDispatchedCoroutine(99
executor: CoroutineExecutor,100
task: SuspendTask<T>,101
completion: Continuation<T>102
) {103
val execution = prepareCoroutineExecution(Some(executor), None)104
startCoroutineTask(execution, true, task, completion)105
}107
/** Starts a suspending function by submitting its first drive to the selected executor. */108
public fun <T> startDispatchedCoroutine(109
executor: CoroutineExecutor,110
task: suspend () -> T,111
completion: Continuation<T>112
) {113
startDispatchedCoroutine(executor, FunctionSuspendTask(task), completion)114
}116
/**117
* Suspends until the supplied registration completes its continuation. Use the cancellable form118
* when registration must follow the current job.119
*/120
@Intrinsic("coroutine_suspend")121
public suspend fun <T> suspendCoroutine(registration: SuspendRegistration<T>): T123
/**124
* Starts a suspending computation and delivers its eventual value or exception to `completion`.125
* This primitive does not create an event loop or a structured child job.126
*/127
public fun <T> startCoroutine(task: suspend () -> T, completion: Continuation<T>) {128
startCoroutine(FunctionSuspendTask(task), completion)129
}131
/**132
* Suspends until the supplied registration completes its continuation. Use the cancellable form133
* when registration must follow the current job.134
*/135
public suspend fun <T> suspendCoroutine(registration: (Continuation<T>) -> Unit): T =136
suspendCoroutine(FunctionSuspendRegistration(registration))