scoop:scoop.asyncio:0.1.0
src/runtime_reactor.scoop
Selected production source · src/runtime_reactor.scoop
1
package scoop.asyncio3
import scoop.concurrent.BlockingTaskPool4
import scoop.concurrent.CoroutineWorkerDriver6
internal class RuntimeReactor(7
private val runtime: RuntimeEventLoop,8
private val index: Int9
): CoroutineWorkerDriver {10
val owner = LoopOwner(Some(runtime), index)11
private var native: Ptr<Unit>? = None12
private var recordedFailure: Throwable? = None13
val failure: Throwable? get() = recordedFailure15
fun open(pool: BlockingTaskPool) {16
native = Some(owner.open(runtime, LoopBlockingConfig.Shared(pool)))17
}19
public override fun enter() {20
RuntimeWorkerContext.enter(runtime.identity, index)21
}23
public override fun poll() {24
owner.pump(native!!)25
}27
public override fun park() {28
if (!owner.hasQueuedWork())29
owner.poll(native!!, true)30
}32
public override fun wake() {33
owner.wakeExecution()34
}36
public override fun close() {37
when (native) {38
case Some(val pointer) -> {39
try {40
owner.finishRoot(None)41
owner.drive(pointer)42
} catch (error: Throwable) {43
recordedFailure = Some(error)44
} finally {45
native = None46
RuntimeWorkerContext.leave(runtime.identity)47
}48
}49
case None -> {}50
}51
}52
}54
@ThreadLocal55
private var physicalRuntime: Long = 0L57
@ThreadLocal58
private var physicalReactor: Int = -160
// Only scalar identities live in TLS; moving managed references remain in the runtime graph.61
internal object RuntimeWorkerContext {62
private val identities = AtomicLong(1L)64
fun nextIdentity(): Long = identities.fetchAdd(1L)66
fun enter(runtime: Long, reactor: Int) {67
@Unsafe68
{69
physicalRuntime = runtime70
physicalReactor = reactor71
}72
}74
fun leave(runtime: Long) {75
@Unsafe76
{77
if (physicalRuntime == runtime) {78
physicalRuntime = 0L79
physicalReactor = -180
}81
}82
}84
fun current(runtime: Long): Int {85
@Unsafe86
{87
return if (physicalRuntime == runtime)88
physicalReactor89
else90
-191
}92
}93
}