scoop:scoop.asyncio:0.1.0
src/async_runtime_builder.scoop
Selected production source · src/async_runtime_builder.scoop
1
package scoop.asyncio3
import scoop.concurrent.BlockingPoolBuilder4
import scoop.platform.system.logicalProcessorCount6
/** Configures one I/O runtime without starting any threads. */7
public 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 = 412
/** 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.threads17
}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
}24
}26
internal struct AsyncRuntimeConfig(val workers: Int, val blockingThreads: Int)28
private fun defaultRuntimeWorkers(): Int {29
val count = logicalProcessorCount30
if (count < 1L)31
return 132
if (count > 2147483647L)33
return 214748364734
return count.toInt()35
}37
/** Creates a single-use runtime from a configuration snapshot. */38
public fun asyncRuntime(configure: AsyncRuntimeBuilder.() -> Unit = {}): AsyncRuntime {39
val builder = AsyncRuntimeBuilder()40
configure(builder)41
return AsyncRuntime(builder.snapshot())42
}