Scoop API

scoop:scoop.asyncio:0.1.0

src/loop/event_loop_session.scoop

Selected production source · src/loop/event_loop_session.scoop

1package scoop.asyncio
3import scoop.asyncio.host.*
5/** An attached structured scope. The host must keep running until onClosed is delivered. */
6public class EventLoopSession internal constructor(
7 /** The I/O service installed in the session root context. */
8 public val loop: EventLoop,
9 private val mailbox: LoopMailbox
10) {
11 /** True after I/O cleanup and host watch removal, before onClosed is invoked. */
12 public val isClosed: Boolean get() = mailbox.isClosed
13 /** Queues a callback in the attach context; false means the closure was not retained. */
14 public fun post(block: CoroutineScope.() -> Unit): Boolean = mailbox.post(block)
15 /** Stops accepting callbacks and schedules cancellation, without blocking this thread. */
16 public fun requestClose() {
17 mailbox.requestClose()
18 }
21internal class LoopAttachment(
22 private val owner: LoopOwner,
23 private val native: Ptr<Unit>,
24 private val mailbox: LoopMailbox,
25 private val onClosed: (Throwable?) -> Unit
26) {
27 private var watch: EventLoopWatch? = None
28 private var detaching: Boolean = false
30 fun install(host: EventLoopHost) {
31 @Unsafe
32 {
33 watch = Some(host.watch(loopNotification(native), { pump() }))
34 }
35 }
37 private fun pump() {
38 if (detaching || !owner.pump(native))
39 return
40 if (!owner.stopIfDrained(native))
41 return
42 detaching = true
43 watch!!.remove {
44 val failure = owner.destroy(native)
45 mailbox.didClose()
46 onClosed(failure)
47 }
48 watch = None
49 }