AbstractExprEvaluatorPool

abstract class AbstractExprEvaluatorPool<T : AbstractExprEvaluator>(size: Int, val resetEvery: Int) : AbstractEnginePool<T>

Abstract base class for a pool of AbstractExprEvaluator instances.

Each evaluator's context is reset once it has been returned resetEvery times, so engine state such as Kotlin's REPL history cannot grow without bound. Resetting is not free: a fresh Kotlin REPL costs several times an evaluation, so resetting on every return would make the pool slower than a lone evaluator. Between resets, a declaration an expression makes (such as val y = 1 inside run { }) stays visible to the evaluator's later borrowers. Subclasses populate the pool with populate in their init block. AbstractEnginePool describes borrowing and closing.

Parameters

size

the number of evaluator instances in the pool; must be positive

resetEvery

how many times an evaluator is returned between resets of its context; 1 resets on every return

Type Parameters

T

the concrete type of AbstractExprEvaluator managed by this pool

Throws

if size or resetEvery is not positive

Constructors

Link copied to clipboard
constructor(size: Int, resetEvery: Int)
constructor(size: Int)

Creates a pool that resets each evaluator's context every time it is returned.

Properties

Link copied to clipboard

Returns an approximate, point-in-time indication of whether the pool currently has no instances available. Delegates to Channel.isEmpty, which is racy under concurrent borrow/recycle and may return a stale result, so do not rely on it for correctness.

Link copied to clipboard
Link copied to clipboard
val size: Int

Functions

Link copied to clipboard

Evaluates expr by borrowing an evaluator from the pool, blocking the current thread until one is available and recycling it afterwards.

Link copied to clipboard
open override fun close()

Closes the pool and the instances it holds. An instance still borrowed is closed when it is returned, and later borrows throw kotlinx.coroutines.channels.ClosedReceiveChannelException.

Link copied to clipboard
suspend fun eval(expr: String): Boolean

Suspends until an evaluator can be borrowed from the pool, evaluates expr, and recycles the evaluator afterwards. Suspends (rather than blocking) when all size evaluators are in use.