scoop:scoop.asyncio:0.1.0
src/loop/event_loop_session.scoop
Selected production source · src/loop/event_loop_session.scoop
1
package scoop.asyncio3
import scoop.asyncio.host.*5
/** An attached structured scope. The host must keep running until onClosed is delivered. */6
public class EventLoopSession internal constructor(7
/** The I/O service installed in the session root context. */8
public val loop: EventLoop,9
private val mailbox: LoopMailbox10
) {11
/** True after I/O cleanup and host watch removal, before onClosed is invoked. */12
public val isClosed: Boolean get() = mailbox.isClosed13
/** 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
}19
}21
internal class LoopAttachment(22
private val owner: LoopOwner,23
private val native: Ptr<Unit>,24
private val mailbox: LoopMailbox,25
private val onClosed: (Throwable?) -> Unit26
) {27
private var watch: EventLoopWatch? = None28
private var detaching: Boolean = false30
fun install(host: EventLoopHost) {31
@Unsafe32
{33
watch = Some(host.watch(loopNotification(native), { pump() }))34
}35
}37
private fun pump() {38
if (detaching || !owner.pump(native))39
return40
if (!owner.stopIfDrained(native))41
return42
detaching = true43
watch!!.remove {44
val failure = owner.destroy(native)45
mailbox.didClose()46
onClosed(failure)47
}48
watch = None49
}50
}