concurrent-coroutines / org.apache.tuweni.concurrent.coroutines / CoroutineLatch

CoroutineLatch

class CoroutineLatch (source)

A co-routine synchronization aid that allows co-routines to wait until a set of operations being performed has completed.

The latch is initialized with a given count. If the latch count is greater than zero, the await() method will suspend until the count reaches zero due to invocations of the countDown() method, at which point all suspended co-routines will be resumed.

Unlike the Java CountDownLatch, this latch allows the count to be increased via invocation of the countUp() method. Increasing the count from zero will result in calls to await() suspending again. Note that the count may be negative, requiring multiple calls to countUp() before calls to await() suspend.

Parameters

initial - The initial count of the latch, which may be positive, zero, or negative.

Constructors

<init>

CoroutineLatch(initial: Int)

A latch.

Properties

count

val count: Int

The current latch count.

isOpen

val isOpen: Boolean

Indicates if the latch is open (count <= 0).

Functions

await

suspend fun await(): Unit

Await the latch opening. If already open, return without suspending.

countDown

fun countDown(): Boolean

Decrease the latch count, potentially opening the latch and awakening suspending co-routines.

countUp

fun countUp(): Boolean

Increase the latch count, potentially closing the latch.