Scoop API

scoop:scoop.asyncio:0.1.0

src/async_runtime_builder.scoop

Selected production source · src/async_runtime_builder.scoop

1package scoop.asyncio
3import scoop.concurrent.BlockingPoolBuilder
4import scoop.platform.system.logicalProcessorCount
6/** Configures one I/O runtime without starting any threads. */
7public class AsyncRuntimeBuilder public constructor() {
8 /** Number of coroutine workers; initially the logical processor count. */
9 public var workers: Int = defaultRuntimeWorkers()
10 private var blockingThreads: Int = 4
12 /** Replaces the configuration of the pool shared by coroutine and I/O work. */
13 public fun blocking(configure: BlockingPoolBuilder.() -> Unit) {
14 val builder = BlockingPoolBuilder()
15 configure(builder)
16 blockingThreads = builder.threads
17 }
19 internal fun snapshot(): AsyncRuntimeConfig {
20 if (workers <= 0 || blockingThreads <= 0)
21 throw IllegalArgumentException(Some("runtime worker counts must be positive"))
22 return AsyncRuntimeConfig(workers, blockingThreads)
23 }
26internal struct AsyncRuntimeConfig(val workers: Int, val blockingThreads: Int)
28private fun defaultRuntimeWorkers(): Int {
29 val count = logicalProcessorCount
30 if (count < 1L)
31 return 1
32 if (count > 2147483647L)
33 return 2147483647
34 return count.toInt()
37/** Creates a single-use runtime from a configuration snapshot. */
38public fun asyncRuntime(configure: AsyncRuntimeBuilder.() -> Unit = {}): AsyncRuntime {
39 val builder = AsyncRuntimeBuilder()
40 configure(builder)
41 return AsyncRuntime(builder.snapshot())