Scoop API

scoop:scoop.asyncio:0.1.0

src/loop/loop_operation.scoop

Selected production source · src/loop/loop_operation.scoop

1package scoop.asyncio
3internal enum OperationChoice {
4 Pending, Stopped(Throwable?), Completed
5}
7private enum OperationDelivery {
8 Submit, Finish
9}
11internal abstract class LoopOperation internal constructor(
12 val loop: LoopOwner,
13 val id: Long,
14 val retained: Boolean = false
15) {
16 // The native owner alone changes completion registration.
17 var registered: Boolean = false
18 abstract fun run()
19 abstract fun begin(pointer: Ptr<Unit>)
20 abstract fun complete(event: NativeEvent)
21 abstract fun cancelNative(pointer: Ptr<Unit>)
22 abstract fun selectFailure(error: Throwable): Boolean
23 abstract fun requestFailure(error: Throwable)
26internal abstract class PendingOperation internal constructor(
27 loop: LoopOwner,
28 id: Long
29): LoopOperation(loop, id) {
30 private val lock: IoLock = IoLock()
31 private var choice: OperationChoice = OperationChoice.Pending
32 private var subscription: Disposable? = None
33 // Only the loop owner changes the delivery phase.
34 private var delivery = OperationDelivery.Submit
36 protected abstract fun start(pointer: Ptr<Unit>): IoStart
37 protected abstract fun recordCompletion(event: NativeEvent, failure: Throwable?)
38 protected abstract fun finish()
40 public final override fun run() {
41 when (delivery) {
42 case Submit -> loop.beginOperation(this)
43 case Finish -> finish()
44 }
45 }
47 fun subscribe(job: Job) {
48 val registration = job.invokeOnCancellation { requestFailure(CancellationException()) }
49 // Completion cannot run until this operation has been submitted.
50 subscription = Some(registration)
51 }
53 public final override fun selectFailure(error: Throwable): Boolean = selectStop(Some(error))
55 private fun selectStop(error: Throwable?): Boolean =
56 lock.withLock {
57 when (choice) {
58 case Pending -> {
59 choice = OperationChoice.Stopped(error);
60 true
61 }
62 case _ -> false
63 }
64 }
66 public final override fun requestFailure(error: Throwable) {
67 if (selectFailure(error))
68 loop.post { loop.cancel(id) }
69 }
71 // Ordinary timer disposal needs native retirement, not an exceptional coroutine result.
72 fun requestStop() {
73 if (selectStop(None))
74 loop.post { loop.cancel(id) }
75 }
77 public final override fun begin(pointer: Ptr<Unit>) {
78 // Control operations must observe earlier byte submissions, including close/shutdown.
79 loop.flushTransfers()
80 val cancelled =
81 lock.withLock {
82 when (choice) {
83 case Stopped(_) -> true;
84 case _ -> false
85 }
86 }
87 if (cancelled) {
88 loop.complete(NativeEvent(id, IoResult.Done));
89 return
90 }
91 val result =
92 try {
93 start(pointer)
94 }
95 catch (error: Throwable) {
96 selectFailure(error);
97 IoStart.Finished(NativeEvent(id, IoResult.Done))
98 }
99 when (result) {
100 case Pending -> {}
101 case Finished(val event) -> loop.complete(event)
102 }
103 loop.dispatchBlocking(pointer)
104 }
106 protected fun unsubscribe() {
107 when (subscription) {
108 case Some(val value) -> value.dispose()
109 case None -> {}
110 }
111 subscription = None
112 }
114 public final override fun complete(event: NativeEvent) {
115 val failure: Throwable? =
116 lock.withLock {
117 val previous = choice
118 choice = OperationChoice.Completed
119 when (previous) {
120 case Stopped(val error) -> error
121 case Pending -> None
122 case Completed -> throw IllegalStateException(Some("operation completed twice"))
123 }
124 }
125 unsubscribe()
126 recordCompletion(event, failure)
127 delivery = OperationDelivery.Finish
128 loop.enqueueReady(this)
129 }
132private struct CallbackCompletion(val event: NativeEvent, val failure: Throwable?)
134internal class CallbackOperation(
135 loop: LoopOwner,
136 id: Long,
137 private val onStart: (Ptr<Unit>, Long) -> IoStart,
138 private val onCancel: (Ptr<Unit>, Long) -> Unit,
139 private val onFinish: (NativeEvent, Throwable?) -> Unit
140): PendingOperation(loop, id) {
141 private var completion: CallbackCompletion? = None
143 protected override fun start(pointer: Ptr<Unit>): IoStart = onStart(pointer, id)
144 public override fun cancelNative(pointer: Ptr<Unit>) {
145 onCancel(pointer, id)
146 }
147 protected override fun recordCompletion(event: NativeEvent, failure: Throwable?) {
148 completion = Some(CallbackCompletion(event, failure))
149 }
150 protected override fun finish() {
151 val result = completion!!
152 completion = None
153 onFinish(result.event, result.failure)
154 }