scoop:scoop.concurrent:0.1.0
src/blocking_pool.scoop
Selected production source · src/blocking_pool.scoop
1
package scoop.concurrent3
/** A fixed managed thread pool for ordinary blocking work, including GC-managed closures. */4
public class BlockingTaskPool public constructor(threads: Int): Closeable {5
private val workers = WorkerGroup.create(threads)7
/** Whether this pool has stopped accepting work. */8
public val isShutdown: Boolean get() = workers.isShutdown9
/** Whether accepted work has drained and workers have left their execution loops. */10
public val isTerminated: Boolean get() = workers.isTerminated12
/** Registers a disposable ordinary notification, called unlocked or immediately if terminated. */13
public fun invokeOnTermination(handler: () -> Unit): Disposable = workers.onTermination(handler)15
/** Queues one work item; rejects submissions after shutdown was requested. */16
public fun submit(work: BlockingWork) {17
workers.submit { work.run() }18
}20
/** Stops admission and lets all accepted work finish. Does not cancel running work. */21
public fun requestShutdown() {22
workers.requestShutdown()23
}25
/** Waits for workers to leave their work loops; cancellation only ends this wait. */26
public suspend fun awaitTermination() {27
workers.awaitTermination()28
}30
/** Joins the stopped workers. Shutdown must already have been requested. */31
public fun join() {32
workers.join()33
}35
/** Requests shutdown and joins from a thread outside this pool. */36
public override fun close() {37
workers.checkNotWorker()38
requestShutdown()39
join()40
}42
internal fun ensureOpen() {43
workers.ensureOpen()44
}45
internal val isCurrentWorker: Boolean get() = workers.isCurrentWorker46
}