Mutual exclusion lock using pthread mutex.
import sync.mutex
let mutex = Mutex.new() // creates real pthread mutex
Important: Mutex.new() must be used (not Mutex() which zero-inits the handle).
| Method | Return | Description |
|---|---|---|
lock() |
Void |
Acquire lock (blocking) |
unlock() |
Void |
Release lock |
tryLock() |
Bool |
Non-blocking lock attempt |
let mutex = Mutex.new()
mutex.lock()
// critical section
sharedCounter = sharedCounter + 1
mutex.unlock()
Read-write lock allowing multiple concurrent readers or one exclusive writer.
import sync.rwlock
| Method | Return | Description |
|---|---|---|
readLock() |
Void |
Acquire read lock |
readUnlock() |
Void |
Release read lock |
writeLock() |
Void |
Acquire write lock |
writeUnlock() |
Void |
Release write lock |
destroy() |
Void |
Free resources |
let rw = RwLock.new()
// Multiple readers
rw.readLock()
let value = sharedData
rw.readUnlock()
// Exclusive writer
rw.writeLock()
sharedData = newValue
rw.writeUnlock()
Thread barrier -- blocks until N threads arrive.
import sync.barrier
let barrier = Barrier.new(4) // 4 threads
| Method | Return | Description |
|---|---|---|
wait() |
Int |
Wait at barrier (returns 0 for serial thread) |
destroy() |
Void |
Free resources |
Lock-free atomic integer operations.
import sync.atomic
let counter = AtomicInt.new(0)
Uses heap-allocated storage via __AtomicAlloc(initial).
| Method | Return | Description |
|---|---|---|
load() |
Int |
Load value |
load_relaxed() |
Int |
Load with relaxed ordering |
load_acquire() |
Int |
Load with acquire ordering |
store(value: Int) |
Void |
Store value |
store_release(value: Int) |
Void |
Store with release ordering |
compareExchange(expected: Int, desired: Int) |
Bool |
CAS operation |
let counter = AtomicInt.new(0)
// In thread 1:
counter.store_release(42)
// In thread 2:
let val = counter.load_acquire()
Message passing between threads using Unix pipes (MPSC).
import sync.channel
let ch = Channel.new() // creates pipe pair
Internally uses __ChannelCreate which packs read_fd << 32 | write_fd into an i64 handle.
| Method | Description |
|---|---|
send(value) |
Send a value |
receive() |
Receive a value (blocking) |
Per-thread storage.
import sync.thread_local
| Method | Return | Description |
|---|---|---|
set(value: Int) |
Void |
Set thread-local value |
get() |
Int |
Get thread-local value |
destroy() |
Void |
Free TLS key |
Multi-producer single-consumer queue (mutex-protected).
import sync.mpsc_queue
| Function | Description |
|---|---|
new_mpsc_queue() |
Create queue |
mpsc_push(queue, value) |
Push value |
mpsc_drain(queue) |
Drain all values |
mpsc_len(queue) |
Get length |
mpsc_destroy(queue) |
Free resources |
Single-producer single-consumer lock-free queue.
import sync.spsc_queue
Lock-free concurrent data structures.
import sync.atomic_queue
import sync.atomic_stack
| Function | Description |
|---|---|
seen_atomic_queue_new() |
Create queue |
seen_atomic_queue_push(q, val) |
Enqueue |
seen_atomic_queue_pop(q) |
Dequeue |
seen_atomic_queue_destroy(q) |
Free |
| Function | Description |
|---|---|
seen_atomic_stack_new() |
Create stack |
seen_atomic_stack_push(s, val) |
Push |
seen_atomic_stack_pop(s) |
Pop |
seen_atomic_stack_destroy(s) |
Free |
import thread.pool
| Function | Description |
|---|---|
seen_ws_pool_new(nworkers) |
Create pool with N workers |
seen_ws_pool_submit(pool, fn_ptr, arg) |
Submit task |
seen_ws_pool_shutdown(pool) |
Shutdown pool |
The ordering module provides constants:
import sync.ordering
