awaitThreshold

suspend fun awaitThreshold(predicate: (L) -> Boolean): L

Suspend until the current result first satisfies predicate, then return that stable snapshot.

If predicate is already satisfied when called, returns immediately without suspending.

The predicate should be monotone: once it returns true for a lattice value v, it should return true for every v' where v' = v.piece(delta) for any delta. A monotone predicate holds permanently once crossed, so the returned snapshot is stable — the caller need not recheck it.

Safe to call from multiple concurrent coroutines; each will receive the first value satisfying its predicate independently.

Return

the first lattice value that satisfies predicate.

Parameters

predicate

a monotone predicate over the lattice value.

Samples

val alice = ReplicaId("alice")
val bob = ReplicaId("bob")

// Contribute is synchronous and thread-safe.
val result = IncrementalResult(GCounter.ZERO)
result.contribute(GCounter.of(alice to 3L))
result.contribute(GCounter.of(bob to 2L))

// The lattice only grows: current value is the join of all contributions so far.
check(result.state.value.value == 5L)

// Duplication is absorbed — same delta twice changes nothing.
result.contribute(GCounter.of(alice to 3L))
check(result.state.value.value == 5L)

// awaitThreshold: in a suspend context, suspends until the predicate is first true.
//   val crossed: GCounter = result.awaitThreshold { it.value >= 5L }
// Returns the first value that satisfies the predicate; the lattice cannot fall below it.