Skip to content

Commit 3b910a0

Browse files
author
Zuhaitz Méndez Fernández de Aránguiz
committed
'std/sync'
1 parent eb13a9a commit 3b910a0

2 files changed

Lines changed: 93 additions & 10 deletions

File tree

std/sync.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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. |

std/thread.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,9 @@ struct Thread {
2929
}
3030
```
3131

32-
### `Mutex`
32+
## Synchronization
3333

34-
A mutual exclusion primitive for protecting shared data.
35-
36-
```zc
37-
struct Mutex {
38-
// Internal sync primitives
39-
}
40-
```
41-
42-
## Methods
34+
For synchronization primitives such as `Mutex`, `CondVar`, `RwLock`, `Once`, and `Semaphore`, see the [std/sync](sync.md) module.
4335

4436
### `Thread` Methods
4537

0 commit comments

Comments
 (0)