StmCounter

STM-based counter that can be atomically incremented/decremented.

Uses Arrow's Software Transactional Memory to provide lock-free, composable atomic operations on a counter value.

Basic usage:

val counter = StmCounter.create(0L)
atomically { with(counter) { increment() } }
println(counter.value()) // 1

Composing with other STM operations:

val counter = StmCounter.create(0L)
val gauge = StmGauge.create(0.0)

atomically {
val count = with(counter) { increment() }
with(gauge) { set(count.toDouble()) }
}

Batch updates inside a single transaction:

atomically {
with(counter) {
add(10)
decrement()
}
}
println(counter.value()) // 9

Types

Link copied to clipboard
object Companion

Factory for StmCounter.

Functions

Link copied to clipboard
fun STM.add(amount: Long): Long

Adds amount to the counter and returns the new value within an STM transaction.

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

Reads the current counter value within an STM transaction.

Link copied to clipboard
fun STM.decrement(): Long

Decrements the counter by 1 and returns the new value within an STM transaction.

Link copied to clipboard
fun STM.increment(): Long

Increments the counter by 1 and returns the new value within an STM transaction.

Link copied to clipboard
fun STM.set(value: Long)

Sets the counter to value within an STM transaction.

Link copied to clipboard
suspend fun value(): Long

Reads the current counter value by running an atomic transaction.