Scoop API

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 provide
7 * scheduling or structured cancellation.
8 */
9public interface Continuation<T> {
10 /**
11 * Completes the suspended computation with a value. A continuation must not be completed
12 * twice.
13 */
14 public fun resume(value: T)
15 /**
16 * Completes the suspended computation exceptionally. A continuation must not be completed
17 * twice.
18 */
19 public fun resumeWithException(exception: Throwable)
22/**
23 * An object-based suspending computation accepted by the coroutine start intrinsic.
24 */
25public interface SuspendTask<T> {
26 /**
27 * Runs the computation and produces its eventual result.
28 */
29 public suspend fun run(): T
32/**
33 * Registers completion of an externally driven suspension.
34 */
35public interface SuspendRegistration<T> {
36 /**
37 * Receives the continuation to complete when the external operation finishes.
38 */
39 public fun register(continuation: Continuation<T>)
42/**
43 * Adapts a suspending function value to the object-based coroutine task protocol.
44 */
45public 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()
52/**
53 * Adapts a continuation-registration function to the object-based suspension protocol.
54 */
55public struct FunctionSuspendRegistration<T>(
56 val registration: (Continuation<T>) -> Unit
57): 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 }
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 */
70public fun <T> startCoroutine(task: SuspendTask<T>, completion: Continuation<T>) {
71 val execution = prepareCoroutineExecution(currentCoroutineExecutorOrNone(), None)
72 startCoroutineTask(execution, false, task, completion)
75internal 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 )
89@Intrinsic("coroutine_start")
90private fun <T> startCoroutineExecution(
91 execution: CoroutineExecutionDriver,
92 dispatched: Boolean,
93 task: SuspendTask<T>,
94 completion: Continuation<T>
97/** Starts a task by submitting its first drive to the selected executor. */
98public 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)
107/** Starts a suspending function by submitting its first drive to the selected executor. */
108public fun <T> startDispatchedCoroutine(
109 executor: CoroutineExecutor,
110 task: suspend () -> T,
111 completion: Continuation<T>
112) {
113 startDispatchedCoroutine(executor, FunctionSuspendTask(task), completion)
116/**
117 * Suspends until the supplied registration completes its continuation. Use the cancellable form
118 * when registration must follow the current job.
119 */
120@Intrinsic("coroutine_suspend")
121public suspend fun <T> suspendCoroutine(registration: SuspendRegistration<T>): T
123/**
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 */
127public fun <T> startCoroutine(task: suspend () -> T, completion: Continuation<T>) {
128 startCoroutine(FunctionSuspendTask(task), completion)
131/**
132 * Suspends until the supplied registration completes its continuation. Use the cancellable form
133 * when registration must follow the current job.
134 */
135public suspend fun <T> suspendCoroutine(registration: (Continuation<T>) -> Unit): T =
136 suspendCoroutine(FunctionSuspendRegistration(registration))