Scoop API

scoop:scoop.core:0.1.0

src/coroutine_executor.scoop

Selected production source · src/coroutine_executor.scoop

1/** One managed coroutine drive, invoked exactly once after submission. */
2public interface CoroutineWork {
3 public fun run()
4}
6/** One ordinary managed computation that may block its worker thread. */
7public interface BlockingWork {
8 public fun run()
9}
11/** Owns task admission and the queues used by registered computations. */
12public interface CoroutineExecutor {
13 /** Registers a root task, or a child of an active task on this executor. */
14 public fun registerTask(parent: ExecutorTask?): ExecutorTask
17/** Keeps an executor available until the task and its completion callback have exited. */
18public interface ExecutorTask {
19 public val executor: CoroutineExecutor
21 /** Enqueues one drive; it must never invoke work inline or reject an active task. */
22 public fun dispatch(work: CoroutineWork)
24 /** Resumes a released drive; an executor may run it during a bounded event turn. */
25 public fun dispatchResume(work: CoroutineWork) {
26 dispatch(work)
27 }
29 /** Enqueues one blocking computation with the same lifetime guarantee. */
30 public fun dispatchBlocking(work: BlockingWork)
32 /** Releases this registration exactly once, after all of its work has exited. */
33 public fun complete()
36/** Raised when an executor can no longer admit the requested task. */
37public class ExecutorClosedException public constructor(): Exception(Some("executor is closed"))
39// Compiler-generated work uses this ordinary dispatch boundary. The implementation
40// owns the drive state; continuations retain only its managed interface reference.
41internal interface CoroutineExecutionDriver {
42 public fun start(work: CoroutineWork, dispatched: Boolean)
43 public fun resume(work: CoroutineWork, external: Boolean)
44 public fun task(): ExecutorTask?
47@Intrinsic("coroutine_current_execution")
48internal fun currentCoroutineExecution(): CoroutineExecutionDriver?
50@Intrinsic("coroutine_yield")
51private suspend fun yieldCoroutineDrive()
53/** Checks cancellation and, when an executor is present, yields to its runnable queue. */
54public suspend fun yieldCoroutine() {
55 ensureActive()
56 when (currentExecutorTask()) {
57 case Some(_) -> yieldCoroutineDrive()
58 case None -> {}
59 }
60 ensureActive()
63internal fun currentExecutorTask(): ExecutorTask? =
64 when (currentCoroutineExecution()) {
65 case Some(val execution) -> execution.task()
66 case None -> None
67 }
69internal fun executorParent(executor: CoroutineExecutor, parent: ExecutorTask?): ExecutorTask? =
70 when (parent) {
71 case Some(val parent) -> if (parent.executor === executor)
72 Some(parent)
73 else
74 None
75 case None -> None
76 }
78internal fun prepareCoroutineExecution(
79 executor: CoroutineExecutor?,
80 preferredParent: ExecutorTask?
81): CoroutineExecution {
82 val registration =
83 when (executor) {
84 case Some(val executor) -> {
85 val parent =
86 when (executorParent(executor, preferredParent)) {
87 case Some(val parent) -> Some(parent)
88 case None -> executorParent(executor, currentExecutorTask())
89 }
90 Some(executor.registerTask(parent))
91 }
92 case None -> None
93 }
94 return CoroutineExecution(registration)
97/** Returns the executor selected for the current coroutine, or None for an independent entry. */
98public fun currentCoroutineExecutorOrNone(): CoroutineExecutor? =
99 when (currentExecutorTask()) {
100 case Some(val task) -> Some(task.executor)
101 case None -> None
102 }