Sync API Reference

Synchronization primitives: Mutex, RwLock, Channel, atomics, and more

Import

U std/sync

Mutex<T>

Mutual exclusion lock protecting a value.

MethodSignatureDescription
newF new(value: T) -> Mutex<T>Create with initial value
lockF lock(&self) -> MutexGuard<T>Acquire lock (blocking)
try_lockF try_lock(&self) -> MutexGuard<T>?Try non-blocking lock
is_lockedF is_locked(&self) -> i64Check if locked

RwLock<T>

Read-write lock allowing multiple readers or one writer.

MethodSignatureDescription
newF new(value: T) -> RwLock<T>Create with initial value
readF read(&self) -> RwLockReadGuard<T>Acquire read lock
writeF write(&self) -> RwLockWriteGuard<T>Acquire write lock
try_readF try_read(&self) -> RwLockReadGuard<T>?Try non-blocking read lock
try_writeF try_write(&self) -> RwLockWriteGuard<T>?Try non-blocking write lock

Channel<T>

Bounded MPSC message-passing channel.

MethodSignatureDescription
newF new(capacity: i64) -> Channel<T>Create bounded channel
sendF send(&self, value: T) -> i64Send (blocks if full)
recvF recv(&self) -> T?Receive (blocks if empty)
try_sendF try_send(&self, value: T) -> i64Non-blocking send
try_recvF try_recv(&self) -> T?Non-blocking receive
closeF close(&self) -> i64Close channel
is_emptyF is_empty(&self) -> i64Check if empty
lenF len(&self) -> i64Get message count

Sender<T> / Receiver<T>

Channel endpoint handles.

MethodSignatureDescription
newF new(channel: &Channel<T>)Create from channel
sendF send(&self, value: T) -> i64Send message
recvF recv(&self) -> T?Receive message
try_sendF try_send(&self, value: T) -> i64Non-blocking send
try_recvF try_recv(&self) -> T?Non-blocking receive

Channel Functions

FunctionSignatureDescription
channelF channel<T>(capacity: i64) -> (Sender<T>, Receiver<T>)Create channel pair

AtomicI64

Lock-free atomic integer.

MethodSignatureDescription
newF new(value: i64) -> AtomicI64Create atomic
loadF load(&self) -> i64Atomic load
storeF store(&self, value: i64) -> i64Atomic store
swapF swap(&self, value: i64) -> i64Atomic swap, returns old
compare_exchangeF compare_exchange(&self, expected: i64, new_value: i64) -> i64CAS
fetch_addF fetch_add(&self, value: i64) -> i64Atomic add, returns old
fetch_subF fetch_sub(&self, value: i64) -> i64Atomic subtract, returns old
fetch_andF fetch_and(&self, value: i64) -> i64Atomic AND, returns old
fetch_orF fetch_or(&self, value: i64) -> i64Atomic OR, returns old
fetch_xorF fetch_xor(&self, value: i64) -> i64Atomic XOR, returns old

AtomicBool

Lock-free atomic boolean.

MethodSignatureDescription
newF new(value: i64) -> AtomicBoolCreate atomic (0=false, 1=true)
loadF load(&self) -> i64Atomic load
storeF store(&self, value: i64) -> i64Atomic store
swapF swap(&self, value: i64) -> i64Atomic swap, returns old
compare_exchangeF compare_exchange(&self, expected: i64, new_value: i64) -> i64CAS

Condvar

Condition variable for wait/notify.

MethodSignatureDescription
newF new() -> CondvarCreate condvar
waitF wait(&self, mutex_guard: &MutexGuard<i64>) -> i64Wait on condition
wait_timeoutF wait_timeout(&self, mutex_guard: &MutexGuard<i64>, timeout_ms: i64) -> i64Wait with timeout
notify_oneF notify_one(&self) -> i64Wake one waiting thread
notify_allF notify_all(&self) -> i64Wake all waiting threads

Barrier

Synchronization barrier for N threads.

MethodSignatureDescription
newF new(n: i64) -> BarrierCreate barrier for n threads
waitF wait(&self) -> i64Wait at barrier

Semaphore

Counting semaphore.

MethodSignatureDescription
newF new(permits: i64) -> SemaphoreCreate with permit count
acquireF acquire(&self) -> i64Acquire permit (blocking)
try_acquireF try_acquire(&self) -> i64Try non-blocking acquire
releaseF release(&self) -> i64Release permit
availableF available(&self) -> i64Get available permits

Once

One-time initialization.

MethodSignatureDescription
newF new() -> OnceCreate once
call_onceF call_once(&self, fn_ptr: i64) -> i64Call function exactly once
is_completedF is_completed(&self) -> i64Check if completed

SpinLock

Busy-wait lock for short critical sections.

MethodSignatureDescription
newF new() -> SpinLockCreate spinlock
lockF lock(&self) -> SpinLockGuardAcquire lock (spin)
try_lockF try_lock(&self) -> SpinLockGuard?Try non-blocking lock
unlockF unlock(&self) -> i64Release lock

WaitGroup

Wait for a group of tasks to complete.

MethodSignatureDescription
newF new() -> WaitGroupCreate wait group
addF add(&self, delta: i64) -> i64Add delta to counter
doneF done(&self) -> i64Decrement counter by 1
waitF wait(&self) -> i64Wait until counter reaches zero
countF count(&self) -> i64Get current count
freeF free(&self) -> i64Free resources

CancellationTokenSource

Creates and controls cancellation tokens.

MethodSignatureDescription
newF new() -> CancellationTokenSourceCreate token source
tokenF token(&self) -> CancellationTokenGet a token
cancelF cancel(&self) -> i64Cancel all tokens
is_cancelledF is_cancelled(&self) -> i64Check if cancelled
create_linked_sourceF create_linked_source(&self) -> CancellationTokenSourceCreate linked source
token_countF token_count(&self) -> i64Get active token count
freeF free(&self) -> i64Free resources

CancellationToken

Handle to check for cancellation.

MethodSignatureDescription
is_cancelledF is_cancelled(&self) -> i64Check if cancelled
throw_if_cancelledF throw_if_cancelled(&self) -> i64Return -1 if cancelled
wait_for_cancellationF wait_for_cancellation(&self) -> i64Block until cancelled
wait_for_cancellation_timeoutF wait_for_cancellation_timeout(&self, timeout_ms: i64) -> i64Wait with timeout
registerF register(&self, callback: i64) -> CancellationRegistrationRegister callback
noneF none() -> CancellationTokenCreate never-cancelled token
dropF drop(&self) -> i64Drop token reference

Usage

U std/sync

F main() -> i64 {
    m := Mutex::new(0)
    guard := m.lock()
    guard.set(42)
    guard.unlock()
    0
}