You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: plugins/zrtl_thread/README.md
+129-5Lines changed: 129 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
# zrtl_thread
2
2
3
-
Threadingand synchronization primitives for Zyntax-based languages.
3
+
Threading, synchronization, and message passing for Zyntax-based languages.
4
4
5
5
## Overview
6
6
7
-
Provides thread spawning, atomic operations, and mutex support. Supports both function pointers and ZRTL closures for thread entry points.
7
+
Provides thread spawning, atomic operations, mutex support, and MPSC (multi-producer, single-consumer) channels for inter-thread communication. Supports both function pointers and ZRTL closures for thread entry points.
8
8
9
9
## Exported Symbols
10
10
@@ -47,7 +47,45 @@ Provides thread spawning, atomic operations, and mutex support. Supports both fu
const result = $Thread$join(handle); // result = 42
60
98
99
+
// Get CPU cores for work distribution
100
+
const cores = $Thread$available_parallelism();
101
+
```
102
+
103
+
### Atomics
104
+
105
+
```zig
61
106
// Using atomics for thread-safe counter
62
107
const counter = $Atomic$new(0);
63
108
@@ -67,7 +112,11 @@ $Atomic$add(counter, 1);
67
112
// Read final value
68
113
const count = $Atomic$load(counter);
69
114
$Atomic$free(counter);
115
+
```
70
116
117
+
### Mutex
118
+
119
+
```zig
71
120
// Using mutex for critical sections
72
121
const mutex = $Mutex$new();
73
122
@@ -76,9 +125,76 @@ $Mutex$lock(mutex);
76
125
$Mutex$unlock(mutex);
77
126
78
127
$Mutex$free(mutex);
128
+
```
79
129
80
-
// Get CPU cores for work distribution
81
-
const cores = $Thread$available_parallelism();
130
+
### Channels (Message Passing)
131
+
132
+
```zig
133
+
// Create a channel
134
+
const pair = $Channel$new();
135
+
const sender = pair.sender;
136
+
const receiver = pair.receiver;
137
+
138
+
// Producer thread
139
+
extern fn producer(tx: i64) i64 {
140
+
for (var i = 0; i < 10; i += 1) {
141
+
$Channel$send(tx, i);
142
+
}
143
+
$Channel$close_sender(tx);
144
+
return 10;
145
+
}
146
+
147
+
// Spawn producer
148
+
const prod = $Thread$spawn(producer, sender);
149
+
150
+
// Receive messages in main thread
151
+
while (true) {
152
+
const result = $Channel$try_recv_status(receiver);
153
+
if (result.status == 0) {
154
+
// Got value: result.value
155
+
process(result.value);
156
+
} else if (result.status == -1) {
157
+
// Channel disconnected, all done
158
+
break;
159
+
}
160
+
// status == 1 means empty, keep trying
161
+
}
162
+
163
+
$Thread$join(prod);
164
+
$Channel$close_receiver(receiver);
165
+
```
166
+
167
+
### Multiple Producers
168
+
169
+
```zig
170
+
const pair = $Channel$new();
171
+
const receiver = pair.receiver;
172
+
173
+
// Clone sender for each producer
174
+
const sender1 = pair.sender;
175
+
const sender2 = $Channel$clone_sender(sender1);
176
+
const sender3 = $Channel$clone_sender(sender1);
177
+
178
+
// Spawn multiple producers
179
+
const t1 = $Thread$spawn(producer_fn, sender1);
180
+
const t2 = $Thread$spawn(producer_fn, sender2);
181
+
const t3 = $Thread$spawn(producer_fn, sender3);
182
+
183
+
// Receive from all producers
184
+
while (true) {
185
+
const result = $Channel$recv_timeout_status(receiver, 100);
186
+
if (result.status == 0) {
187
+
// Process message
188
+
} else if (result.status == -1) {
189
+
break; // All senders closed
190
+
}
191
+
}
192
+
193
+
// Wait for all producers
194
+
$Thread$join(t1);
195
+
$Thread$join(t2);
196
+
$Thread$join(t3);
197
+
$Channel$close_receiver(receiver);
82
198
```
83
199
84
200
## Thread Types
@@ -96,6 +212,14 @@ Use `ZrtlClosure` for closures with captured state. The `spawn_closure` variants
96
212
97
213
All atomic operations use `SeqCst` (sequential consistency) ordering for maximum safety. This provides strong guarantees but may have some performance overhead compared to weaker orderings.
98
214
215
+
## Channel Semantics
216
+
217
+
- **MPSC**: Multiple senders, single receiver
218
+
- **Unbounded**: `$Channel$new` creates a channel with unlimited buffer
219
+
- **Bounded**: `$Channel$bounded(n)` blocks senders when n messages are queued
220
+
- **Disconnection**: Closing all senders signals EOF to receiver
221
+
- **Ordering**: Messages are received in FIFO order per sender
0 commit comments