|
| 1 | +# std/sync |
| 2 | + |
| 3 | +The `std/sync` module provides synchronization primitives for multithreading, including mutexes, condition variables, reader-writer locks, one-time initialization, semaphores, and barriers. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```zc |
| 8 | +import "std/sync.zc" |
| 9 | +
|
| 10 | +fn main() { |
| 11 | + let m = Mutex::new(); |
| 12 | + m.lock(); |
| 13 | + // Critical section |
| 14 | + m.unlock(); |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +## Struct Definitions |
| 19 | + |
| 20 | +### `Mutex` |
| 21 | +A mutual exclusion lock. Implements `Drop` for automatic resource cleanup. |
| 22 | + |
| 23 | +### `CondVar` |
| 24 | +A condition variable for thread signaling. Implements `Drop`. |
| 25 | + |
| 26 | +### `RwLock` |
| 27 | +A reader-writer lock. Implements `Drop`. |
| 28 | + |
| 29 | +### `Once` |
| 30 | +A primitive for one-time initialization. Implements `Drop`. |
| 31 | + |
| 32 | +### `Semaphore` |
| 33 | +A counting semaphore. Implements `Drop`. |
| 34 | + |
| 35 | +### `Barrier` |
| 36 | +A synchronization barrier. Implements `Drop`. |
| 37 | + |
| 38 | +## Methods |
| 39 | + |
| 40 | +### `Mutex` Methods |
| 41 | +| Method | Signature | Description | |
| 42 | +| :--- | :--- | :--- | |
| 43 | +| **new** | `Mutex::new() -> Mutex` | Creates a new mutex. | |
| 44 | +| **lock** | `lock(self)` | Acquires the lock (blocking). | |
| 45 | +| **try_lock** | `try_lock(self) -> bool` | Attempts to acquire the lock without blocking. Returns `true` if successful. | |
| 46 | +| **unlock** | `unlock(self)` | Releases the lock. | |
| 47 | +| **free** | `free(self)` | Manually destroys the mutex. | |
| 48 | + |
| 49 | +### `CondVar` Methods |
| 50 | +| Method | Signature | Description | |
| 51 | +| :--- | :--- | :--- | |
| 52 | +| **new** | `CondVar::new() -> CondVar` | Creates a new condition variable. | |
| 53 | +| **wait** | `wait(self, mutex: Mutex*)` | Waits on the condition variable, releasing the mutex. | |
| 54 | +| **signal** | `signal(self)` | Signals one waiting thread. | |
| 55 | +| **broadcast**| `broadcast(self)` | Signals all waiting threads. | |
| 56 | +| **free** | `free(self)` | Manually destroys the condition variable. | |
| 57 | + |
| 58 | +### `RwLock` Methods |
| 59 | +| Method | Signature | Description | |
| 60 | +| :--- | :--- | :--- | |
| 61 | +| **new** | `RwLock::new() -> RwLock` | Creates a new reader-writer lock. | |
| 62 | +| **rdlock** | `rdlock(self)` | Acquires the read lock (blocking). | |
| 63 | +| **try_rdlock**| `try_rdlock(self) -> bool` | Attempts to acquire the read lock without blocking. | |
| 64 | +| **wrlock** | `wrlock(self)` | Acquires the write lock (blocking). | |
| 65 | +| **try_wrlock**| `try_wrlock(self) -> bool` | Attempts to acquire the write lock without blocking. | |
| 66 | +| **unlock** | `unlock(self)` | Releases the lock. | |
| 67 | +| **free** | `free(self)` | Manually destroys the lock. | |
| 68 | + |
| 69 | +### `Once` Methods |
| 70 | +| Method | Signature | Description | |
| 71 | +| :--- | :--- | :--- | |
| 72 | +| **new** | `Once::new() -> Once` | Creates a new one-time initialization handle. | |
| 73 | +| **call** | `call(self, func: fn())` | Executes `func` exactly once across all threads. | |
| 74 | +| **free** | `free(self)` | Manually destroys the handle. | |
| 75 | + |
| 76 | +### `Semaphore` Methods |
| 77 | +| Method | Signature | Description | |
| 78 | +| :--- | :--- | :--- | |
| 79 | +| **new** | `Semaphore::new(value: int) -> Semaphore` | Creates a new semaphore with initial `value`. | |
| 80 | +| **wait** | `wait(self)` | Decrements the semaphore (blocking if 0). | |
| 81 | +| **try_wait** | `try_wait(self) -> bool` | Attempts to decrement without blocking. Returns `true` if successful. | |
| 82 | +| **post** | `post(self)` | Increments the semaphore. | |
| 83 | +| **value** | `value(self) -> int` | Returns the current semaphore value. | |
| 84 | +| **free** | `free(self)` | Manually destroys the semaphore. | |
| 85 | + |
| 86 | +### `Barrier` Methods |
| 87 | +| Method | Signature | Description | |
| 88 | +| :--- | :--- | :--- | |
| 89 | +| **new** | `Barrier::new(count: int) -> Barrier` | Creates a new barrier for `count` threads. | |
| 90 | +| **wait**| `wait(self) -> bool` | Waits at the barrier. Returns `true` if this thread was the serial thread leader. | |
| 91 | +| **free** | `free(self)` | Manually destroys the barrier. | |
0 commit comments