scoop:scoop.concurrent:0.1.0
src/executor_builder.scoop
Selected production source · src/executor_builder.scoop
1
package scoop.concurrent3
import scoop.platform.system.logicalProcessorCount5
/** Configuration shared by executor and I/O runtime builders. */6
public class BlockingPoolBuilder public constructor() {7
/** Number of managed blocking workers; must be positive. */8
public var threads: Int = 49
}11
/** Configures a fixed coroutine executor and its owned or borrowed blocking pool. */12
public class ExecutorBuilder public constructor() {13
/** Coroutine worker count, initially the platform's logical processor count. */14
public var workers: Int = defaultWorkerCount()15
private var pool: BlockingPoolConfig = BlockingPoolConfig.Owned(4)16
private var eventDrivers: Array<CoroutineWorkerDriver>? = None18
/** Attaches one prepared event driver per worker; the configuration copies the array. */19
public fun drivers(drivers: Array<CoroutineWorkerDriver>) {20
eventDrivers = Some(drivers)21
}23
/** Selects an owned pool, replacing any previous blocking configuration. */24
public fun blocking(configure: BlockingPoolBuilder.() -> Unit) {25
val builder = BlockingPoolBuilder()26
configure(builder)27
pool = BlockingPoolConfig.Owned(builder.threads)28
}30
/** Borrows a pool whose owner must keep it open until this executor has joined. */31
public fun blocking(pool: BlockingTaskPool) {32
this.pool = BlockingPoolConfig.Borrowed(pool)33
}35
internal fun snapshot(): ExecutorConfig {36
if (workers <= 0)37
throw IllegalArgumentException(Some("worker count must be positive"))38
when (pool) {39
case Owned(val threads) -> if (threads <= 0)40
throw IllegalArgumentException(Some("blocking thread count must be positive"))41
case Borrowed(_) -> {}42
}43
val drivers =44
when (eventDrivers) {45
case Some(val values) -> {46
if (values.size != workers.toLong())47
throw IllegalArgumentException(48
Some("event driver count must match workers")49
)50
Some(Array<CoroutineWorkerDriver>(values.size) { index -> values[index] })51
}52
case None -> None53
}54
return ExecutorConfig(workers, pool, drivers)55
}56
}58
internal enum BlockingPoolConfig {59
Owned(Int), Borrowed(BlockingTaskPool)60
}62
internal struct ExecutorConfig(63
val workers: Int,64
val pool: BlockingPoolConfig,65
val drivers: Array<CoroutineWorkerDriver>?66
)68
private fun defaultWorkerCount(): Int {69
val count = logicalProcessorCount70
if (count < 1L)71
return 172
if (count > 2147483647L)73
return 214748364774
return count.toInt()75
}77
/** Creates workers immediately from a single immutable snapshot of the receiver builder. */78
public fun multiThreadExecutor(configure: ExecutorBuilder.() -> Unit = {}): MultiThreadExecutor {79
val builder = ExecutorBuilder()80
configure(builder)81
val configuration = builder.snapshot()82
when (configuration.pool) {83
case Borrowed(val pool) -> {84
pool.ensureOpen()85
return MultiThreadExecutor(configuration.workers, pool, false, configuration.drivers)86
}87
case Owned(val threads) -> {88
val pool = BlockingTaskPool(threads)89
try {90
return MultiThreadExecutor(configuration.workers, pool, true, configuration.drivers)91
}92
catch (error: Throwable) {93
pool.close();94
throw error95
}96
}97
}98
}