std/sync
Mutex, channel, rwlock, and atomic primitives for thread synchronization.
from "std/sync" import { Mutex, Channel, RwLock, AtomicI64, AtomicBool }Types
Mutex
struct Mutex {
_handle: *u8,
}OS-level mutex. Must be destroyed after use to avoid resource leaks.
Channel
struct Channel {
_ptr: *u8,
}Bounded FIFO channel for sending i64 values between threads. Blocks on send when full, blocks on recv when empty.
RwLock
struct RwLock {
_handle: *u8,
}Reader-writer lock: multiple concurrent readers OR one exclusive writer.
AtomicI64
struct AtomicI64 {
_ptr: *u8,
}Lock-free 64-bit atomic integer. All operations use sequential consistency.
AtomicBool
struct AtomicBool {
_ptr: *u8,
}Lock-free atomic boolean. All operations use sequential consistency.
Mutex Methods
Mutex.new
fn Mutex.new(): Result<Mutex>Create a new unlocked mutex.
m.lock
fn lock(self: &Mutex): Result<i32>Acquire the lock. Blocks if another thread holds it.
m.unlock
fn unlock(self: &Mutex): Result<i32>Release the lock.
m.withLock
fn withLock(self: &Mutex, f: () => void): Result<i32>Scoped locking — acquires, runs closure, unlocks. Guarantees unlock.
m.destroy
fn destroy(self: &Mutex): voidFree the underlying OS mutex resource.
Channel Methods
Channel.new
fn Channel.new(capacity: i64): Result<Channel>Create a bounded channel with the given capacity.
ch.send
fn send(self: &Channel, val: i64): Result<i32>Send a value into the channel. Blocks if full.
ch.recv
fn recv(self: &Channel): Result<i64>Receive a value from the channel. Blocks if empty.
ch.trySend
fn trySend(self: &Channel, val: i64): boolNon-blocking send. Returns true if sent, false if full.
ch.tryRecv
fn tryRecv(self: &Channel): Option<i64>Non-blocking receive. Returns Option.None if empty.
ch.len
fn len(self: &Channel): i64Current number of items in the channel.
ch.destroy
fn destroy(self: &Channel): voidFree the underlying channel resource.
RwLock Methods
RwLock.new
fn RwLock.new(): Result<RwLock>Create a new reader-writer lock.
r.read
fn read(self: &RwLock): Result<i32>Acquire a read lock. Multiple readers allowed simultaneously.
r.write
fn write(self: &RwLock): Result<i32>Acquire an exclusive write lock.
r.unlock
fn unlock(self: &RwLock): Result<i32>Release the lock.
r.withReadLock
fn withReadLock(self: &RwLock, f: () => void): Result<i32>Scoped read lock — acquires, runs closure, unlocks.
r.withWriteLock
fn withWriteLock(self: &RwLock, f: () => void): Result<i32>Scoped write lock — acquires, runs closure, unlocks.
r.destroy
fn destroy(self: &RwLock): voidFree the underlying rwlock resource.
AtomicI64 Methods
AtomicI64.new
fn AtomicI64.new(v: i64): AtomicI64Create an atomic integer with initial value.
a.load
fn load(self: &AtomicI64): i64Atomic read.
a.store
fn store(self: &AtomicI64, v: i64): voidAtomic write.
a.add
fn add(self: &AtomicI64, v: i64): i64Atomic add. Returns old value.
a.sub
fn sub(self: &AtomicI64, v: i64): i64Atomic subtract. Returns old value.
a.cas
fn cas(self: &AtomicI64, expected: i64, desired: i64): i64Compare-and-swap. Returns old value.
a.destroy
fn destroy(self: &AtomicI64): voidFree the atomic resource.
AtomicBool Methods
AtomicBool.new
fn AtomicBool.new(v: bool): AtomicBoolCreate an atomic boolean with initial value.
a.load
fn load(self: &AtomicBool): boolAtomic read.
a.store
fn store(self: &AtomicBool, v: bool): voidAtomic write.
a.swap
fn swap(self: &AtomicBool, v: bool): boolAtomic swap. Returns old value.
a.destroy
fn destroy(self: &AtomicBool): voidFree the atomic resource.
Example: Producer-Consumer
from "std/thread" import { Thread }
from "std/sync" import { Channel }
fn main(): i32 {
let ch = Channel.new(8)!
let t = Thread.spawn(move (): void => {
ch.send(10)!
ch.send(20)!
ch.send(30)!
ch.send(0)!
})!
while true {
let val = ch.recv()!
if val == 0 {
break
}
print(val)
}
t.join()!
ch.destroy()
print("done")
return 0
}