# 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

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

## Properties

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

## Functions

acquire
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

acquireByOne
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

immutable
Link copied to clipboard
fun immutable (): SmartSemaphore.Immutable
release
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

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

Will try to lock this SmartSemaphore immediataly

waitRelease
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.

waitReleaseAll
Link copied to clipboard
suspend fun SmartSemaphore . waitReleaseAll (): Int
withAcquire
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