scoop:scoop.asyncio:0.1.0
src/loop/event_loop_builder.scoop
Selected production source · src/loop/event_loop_builder.scoop
1
package scoop.asyncio3
import scoop.concurrent.BlockingPoolBuilder4
import scoop.concurrent.BlockingTaskPool6
/** Configures the managed blocking workers used by a single event-loop lifetime. */7
public class EventLoopBuilder public constructor() {8
private var pool = LoopBlockingConfig.Owned(4)10
/** Selects an owned pool; its threads are created only when run or attach is accepted. */11
public fun blocking(configure: BlockingPoolBuilder.() -> Unit) {12
val builder = BlockingPoolBuilder()13
configure(builder)14
pool = LoopBlockingConfig.Owned(builder.threads)15
}17
/** Borrows a pool whose owner keeps it open until the loop has completely closed. */18
public fun blocking(pool: BlockingTaskPool) {19
this.pool = LoopBlockingConfig.Shared(pool)20
}22
internal fun snapshot(): LoopBlockingConfig {23
when (pool) {24
case Owned(val threads) -> if (threads <= 0)25
throw IllegalArgumentException(Some("blocking thread count must be positive"))26
case Shared(_) -> {}27
}28
return pool29
}30
}32
internal enum LoopBlockingConfig {33
Owned(Int), Shared(BlockingTaskPool)34
}36
/** Creates a single-use I/O owner from a configuration snapshot, without starting threads. */37
public fun eventLoop(configure: EventLoopBuilder.() -> Unit = {}): PlatformEventLoop {38
val builder = EventLoopBuilder()39
configure(builder)40
return PlatformEventLoop(builder.snapshot())41
}