AsyncSemaphore

interface AsyncSemaphore<P>

An interface representing a non-blocking semaphore.

A semaphore maintains a set of permits and each acquire() blocks if necessary until a permit is available, and then takes it. Each release() adds a permit, potentially releasing an acquirer.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
abstract val totalPermits: Int

The total number of permits this semaphore can provide.

Functions

Link copied to clipboard
abstract suspend fun acquire(): P

Acquires a permit from this semaphore, suspending until one is available.

Link copied to clipboard
abstract suspend fun available(): Int

The number of available permits.

Link copied to clipboard
abstract suspend fun release(permit: P)

Releases a permit, returning it to the semaphore.

Link copied to clipboard
abstract suspend fun releaseAll()

Releases all permits back to the semaphore.

Link copied to clipboard
abstract suspend fun tryAcquire(): P?

Tries to acquire a permit from this semaphore. This function is marked as a suspend function because it may perform I/O operations, but it won't suspend in case that no permit is available at the moment.