Module: sync.mutex

Source: ./sync/mutex.reef


Overview

sync/mutex - Mutual exclusion primitives

Provides mutex (mutual exclusion lock) support by wrapping the runtime's reef_lock_t. Use these primitives for low-level synchronization when Active Objects are not suitable.

IMPORTANT: Prefer Active Objects over manual mutex usage. Active Objects provide automatic locking via exclusive methods and are safer.

Usage: import sync.mutex let m = mutex_create() mutex_lock(m) // ... critical section ... mutex_unlock(m) mutex_destroy(m)

For safer scoped locking, consider using guard patterns: let guard = mutex_guard_create(m) // ... critical section ... mutex_guard_release(guard)


Types

Mutex

Mutex wraps the runtime's reef_lock_t

Fields:

Name Type
lock_ptr pointer
valid bool

MutexGuard

Guard that automatically locks on create, must be released manually (Reef doesn't have destructors, so manual release is required)

Fields:

Name Type
mutex Mutex
locked bool

Functions

fn lock_level_user(): int

Lock level for user-level mutexes (highest level, locks last)

fn mutex_create(): Mutex

Create a new mutex

fn mutex_is_valid(m: Mutex): bool

Check if mutex is valid (was created successfully)

fn mutex_guard_create(m: Mutex): MutexGuard

Create a guard that locks the mutex

fn mutex_guard_is_locked(g: MutexGuard): bool

Check if guard is holding the lock


Procedures

proc mutex_destroy(m: Mutex)

Destroy a mutex (releases resources) WARNING: Ensure no threads hold the mutex when destroying

proc mutex_lock(m: Mutex)

Acquire the mutex (blocking) Will block until the mutex is available

proc mutex_unlock(m: Mutex)

Release the mutex WARNING: Only call if you hold the lock

proc mutex_guard_release(g: MutexGuard)

Release the guard (unlocks the mutex)


Generated by reefc doc