StmStateMachine

STM-based state machine with typed states and atomic transitions.

Provides composable, transactional state transitions that can be combined with other STM operations in a single atomic block.

Basic usage:

enum class State { IDLE, RUNNING, STOPPED }

val sm = StmStateMachine.create(State.IDLE)
atomically { with(sm) { transitionIf(State.IDLE, State.RUNNING) } } // true
println(sm.currentState()) // RUNNING

Composing state transitions with counters:

val sm = StmStateMachine.create(State.IDLE)
val counter = StmCounter.create(0L)

atomically {
val transitioned = with(sm) { transitionIf(State.IDLE, State.RUNNING) }
if (transitioned) {
with(counter) { increment() }
}
}

Types

Link copied to clipboard
object Companion

Factory for StmStateMachine.

Functions

Link copied to clipboard
fun STM.current(): S

Reads the current state within an STM transaction.

Link copied to clipboard
suspend fun currentState(): S

Reads the current state by running an atomic transaction.

Link copied to clipboard
fun STM.transition(to: S)

Unconditionally transitions to the to state within an STM transaction.

Link copied to clipboard
fun STM.transitionIf(from: S, to: S): Boolean

Transitions from from to to only if the current state equals from. Returns true if the transition occurred, false otherwise.