Skip to content

semaphore

stable

Concurrency primitives including a counting Semaphore, a non-reentrant Mutex, and a bounded Channel for producer-consumer messaging.

use plugin semaphore::{Semaphore.new, acquire, try_acquire, …}
19 functions Utilities
/ filter jk navigate Esc clear
Functions (19)
  1. Semaphore.new Create a semaphore with a permit count
  2. acquire Acquire one permit (non-blocking)
  3. try_acquire Acquire N permits atomically
  4. release Release one permit
  5. available Count available permits
  6. max_permits Return the maximum permit count
  7. is_available Check if at least one permit is free
  8. reset Restore all permits to maximum
  9. Mutex.new Create an unlocked mutex
  10. lock Acquire the mutex lock
  11. unlock Release the mutex lock
  12. is_locked Check if the mutex is currently locked
  13. Channel.new Create a bounded message channel
  14. send Send a value into the channel
  15. recv Receive the next value from the channel
  16. len Count pending messages
  17. is_empty Check if the channel has no messages
  18. is_full Check if the channel is at capacity
  19. capacity Return the channel capacity

Create a semaphore with a permit count

Creates a counting semaphore initialized with max_permits available permits.

use plugin semaphore::{Semaphore}

let sem = Semaphore.new(3)
print(sem.available())  // 3

Acquire one permit (non-blocking)

Attempts to acquire one permit. Returns true if a permit was taken, false if none are available. Non-blocking.

use plugin semaphore::{Semaphore}

let sem = Semaphore.new(2)
print(sem.acquire())  // true
print(sem.acquire())  // true
print(sem.acquire())  // false (exhausted)
sem.release()
print(sem.acquire())  // true

Acquire N permits atomically

Attempts to acquire count permits atomically. Returns true only if all count permits are available at once; otherwise returns false without consuming any.

use plugin semaphore::{Semaphore}

let sem = Semaphore.new(5)
print(sem.try_acquire(3))  // true  (5 -> 2)
print(sem.try_acquire(3))  // false (only 2 left)

Release one permit

Returns one permit to the semaphore. Will not exceed max_permits.

use plugin semaphore::{Semaphore}

let sem = Semaphore.new(1)
sem.acquire()
print(sem.available())  // 0
sem.release()
print(sem.available())  // 1

Count available permits

Returns the number of permits currently available.

Return the maximum permit count

Returns the maximum number of permits this semaphore was created with.

use plugin semaphore::{Semaphore}

let sem = Semaphore.new(10)
sem.acquire()
print(sem.available())    // 9
print(sem.max_permits())  // 10

Check if at least one permit is free

Returns true if at least one permit is available.

use plugin semaphore::{Semaphore}

let sem = Semaphore.new(1)
print(sem.is_available())  // true
sem.acquire()
print(sem.is_available())  // false

Restore all permits to maximum

Resets all permits back to max_permits, regardless of how many have been acquired.

use plugin semaphore::{Semaphore}

let sem = Semaphore.new(4)
sem.acquire()
sem.acquire()
sem.reset()
print(sem.available())  // 4

Create an unlocked mutex

Creates a new unlocked mutex. Use a mutex to guard exclusive access to a shared resource.

use plugin semaphore::{Mutex}

let mtx = Mutex.new()
print(mtx.is_locked())  // false

Acquire the mutex lock

Attempts to acquire the mutex. Returns true if the lock was acquired, false if it is already held.

use plugin semaphore::{Mutex}

let mtx = Mutex.new()
print(mtx.lock())      // true
print(mtx.lock())      // false (already locked)
mtx.unlock()
print(mtx.lock())      // true

Release the mutex lock

Releases the mutex. Always succeeds, even if the mutex was not locked.

Check if the mutex is currently locked

Returns true if the mutex is currently held.

use plugin semaphore::{Mutex}

let mtx = Mutex.new()
mtx.lock()
print(mtx.is_locked())  // true
mtx.unlock()
print(mtx.is_locked())  // false

Create a bounded message channel

Creates a bounded FIFO channel that can hold up to capacity values. When full, send returns false without blocking.

use plugin semaphore::{Channel}

let ch = Channel.new(5)
ch.send("task 1")
ch.send("task 2")
print(ch.len())  // 2

Send a value into the channel

Sends a value into the channel. Returns true on success, false if the channel is full.

use plugin semaphore::{Channel}

let ch = Channel.new(2)
print(ch.send("a"))  // true
print(ch.send("b"))  // true
print(ch.send("c"))  // false (full)

Receive the next value from the channel

Removes and returns the oldest value from the channel. Returns nil if the channel is empty.

use plugin semaphore::{Channel}

let ch = Channel.new(3)
ch.send(10)
ch.send(20)
print(ch.recv())  // 10
print(ch.recv())  // 20
print(ch.recv())  // nil

Count pending messages

Returns the number of values currently in the channel buffer.

Check if the channel has no messages

Returns true if the channel has no pending values.

Check if the channel is at capacity

Returns true if the number of buffered values equals the capacity.

Return the channel capacity

Returns the maximum number of values the channel can buffer.

use plugin semaphore::{Channel}

let ch = Channel.new(4)
ch.send(1)
ch.send(2)
print(ch.capacity())  // 4
print(ch.is_full())   // false
print(ch.is_empty())  // false
enespt-br