Mutable

class Mutable(permits: Int, acquiredPermits: Int = 0) : SmartSemaphore(source)

Mutable variant of SmartSemaphore. With that variant you may lock and unlock. Besides, you may create Immutable variant of this instance with immutable factory

Parameters

locked

Preset state of freePermits and its internal _freePermitsStateFlow

Constructors

Link copied to clipboard
constructor(permits: Int, acquiredPermits: Int = 0)

Properties

Link copied to clipboard
open val freePermits: Int
Link copied to clipboard
open override val permitsStateFlow: StateFlow<Int>

Functions

Link copied to clipboard
suspend fun acquire(permits: Int = 1)

Holds call until this SmartSemaphore will be re-locked. That means that current method will

Link copied to clipboard
suspend fun acquireByOne(permits: Int = 1)

Holds call until this SmartSemaphore will be re-locked. That means that while freePermits == true, holds will wait for freePermits == false and then try to lock

Link copied to clipboard
Link copied to clipboard
suspend fun release(permits: Int = 1): Boolean

If freePermits == true - will change it to false and return true. If current call will not unlock this SmartSemaphore - false

Link copied to clipboard
suspend fun tryAcquire(permits: Int = 1): Boolean

Will try to lock this SmartSemaphore immediataly

Link copied to clipboard
suspend fun SmartSemaphore.waitRelease(permits: Int = 1): Int

Will wait until the SmartSemaphore.permitsStateFlow of this instance will have permits count free permits.

Link copied to clipboard
inline suspend fun <T> SmartSemaphore.Mutable.withAcquire(permits: Int = 1, action: () -> T): T

Will call SmartSemaphore.Mutable.lock, then execute action and return the result after SmartSemaphore.Mutable.unlock