Scoop API

scoop:scoop.concurrent:0.1.0

src/executor_builder.scoop

Selected production source · src/executor_builder.scoop

1package scoop.concurrent
3import scoop.platform.system.logicalProcessorCount
5/** Configuration shared by executor and I/O runtime builders. */
6public class BlockingPoolBuilder public constructor() {
7 /** Number of managed blocking workers; must be positive. */
8 public var threads: Int = 4
9}
11/** Configures a fixed coroutine executor and its owned or borrowed blocking pool. */
12public 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>? = None
18 /** 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 -> None
53 }
54 return ExecutorConfig(workers, pool, drivers)
55 }
58internal enum BlockingPoolConfig {
59 Owned(Int), Borrowed(BlockingTaskPool)
62internal struct ExecutorConfig(
63 val workers: Int,
64 val pool: BlockingPoolConfig,
65 val drivers: Array<CoroutineWorkerDriver>?
68private fun defaultWorkerCount(): Int {
69 val count = logicalProcessorCount
70 if (count < 1L)
71 return 1
72 if (count > 2147483647L)
73 return 2147483647
74 return count.toInt()
77/** Creates workers immediately from a single immutable snapshot of the receiver builder. */
78public 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 error
95 }
96 }
97 }