Skip to content

Commit b68e1bc

Browse files
Fix example files to use new sync primitives API
Updates example files to use the refactored sync primitives API where Runtime must be passed as a parameter to methods. Also adds assertion to prevent zero-capacity Queue initialization. Changes: - Update mutex_demo.zig to use Mutex.init constant and pass rt to lock/unlock - Update producer_consumer.zig to use Mutex/Condition.init constants - Pass rt parameter to all lock/unlock/wait/signal calls in examples - Add buffer.len > 0 assertion in Queue.init() to prevent division-by-zero 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Lukáš Lalinský <[email protected]>
1 parent bd3a074 commit b68e1bc

3 files changed

Lines changed: 20 additions & 18 deletions

File tree

‎examples/mutex_demo.zig‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const SharedData = struct {
88

99
fn incrementTask(rt: *zio.Runtime, data: *SharedData, id: u32) void {
1010
for (0..1000) |_| {
11-
data.mutex.lock();
12-
defer data.mutex.unlock();
11+
data.mutex.lock(rt);
12+
defer data.mutex.unlock(rt);
1313

1414
const old = data.counter;
1515
rt.yield(); // Yield to simulate preemption
@@ -29,7 +29,7 @@ pub fn main() !void {
2929
defer runtime.deinit();
3030

3131
var shared_data = SharedData{
32-
.mutex = zio.Mutex.init(&runtime),
32+
.mutex = zio.Mutex.init,
3333
};
3434

3535
// Spawn multiple tasks that increment shared counter

‎examples/producer_consumer.zig‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@ const BoundedBuffer = struct {
1111
not_full: zio.Condition,
1212

1313
fn init(runtime: *zio.Runtime) BoundedBuffer {
14+
_ = runtime;
1415
return .{
1516
.buffer = std.mem.zeroes([8]i32),
16-
.mutex = zio.Mutex.init(runtime),
17-
.not_empty = zio.Condition.init(runtime),
18-
.not_full = zio.Condition.init(runtime),
17+
.mutex = zio.Mutex.init,
18+
.not_empty = zio.Condition.init,
19+
.not_full = zio.Condition.init,
1920
};
2021
}
2122

22-
fn put(self: *BoundedBuffer, item: i32) void {
23-
self.mutex.lock();
24-
defer self.mutex.unlock();
23+
fn put(self: *BoundedBuffer, rt: *zio.Runtime, item: i32) void {
24+
self.mutex.lock(rt);
25+
defer self.mutex.unlock(rt);
2526

2627
// Wait until buffer is not full
2728
while (self.count == self.buffer.len) {
28-
self.not_full.wait(&self.mutex);
29+
self.not_full.wait(rt, &self.mutex);
2930
}
3031

3132
// Add item to buffer
@@ -36,16 +37,16 @@ const BoundedBuffer = struct {
3637
std.log.info("Produced: {} (buffer size: {})", .{ item, self.count });
3738

3839
// Signal that buffer is not empty
39-
self.not_empty.signal();
40+
self.not_empty.signal(rt);
4041
}
4142

42-
fn get(self: *BoundedBuffer) i32 {
43-
self.mutex.lock();
44-
defer self.mutex.unlock();
43+
fn get(self: *BoundedBuffer, rt: *zio.Runtime) i32 {
44+
self.mutex.lock(rt);
45+
defer self.mutex.unlock(rt);
4546

4647
// Wait until buffer is not empty
4748
while (self.count == 0) {
48-
self.not_empty.wait(&self.mutex);
49+
self.not_empty.wait(rt, &self.mutex);
4950
}
5051

5152
// Remove item from buffer
@@ -56,7 +57,7 @@ const BoundedBuffer = struct {
5657
std.log.info("Consumed: {} (buffer size: {})", .{ item, self.count });
5758

5859
// Signal that buffer is not full
59-
self.not_full.signal();
60+
self.not_full.signal(rt);
6061

6162
return item;
6263
}
@@ -65,15 +66,15 @@ const BoundedBuffer = struct {
6566
fn producer(rt: *zio.Runtime, buffer: *BoundedBuffer, id: u32) void {
6667
for (0..5) |i| {
6768
const item = @as(i32, @intCast(id * 100 + i));
68-
buffer.put(item);
69+
buffer.put(rt, item);
6970
rt.sleep(100); // Small delay between productions
7071
}
7172
std.log.info("Producer {} finished", .{id});
7273
}
7374

7475
fn consumer(rt: *zio.Runtime, buffer: *BoundedBuffer, id: u32) void {
7576
for (0..5) |_| {
76-
const item = buffer.get();
77+
const item = buffer.get(rt);
7778
_ = item;
7879
rt.sleep(150); // Small delay between consumptions
7980
}

‎src/sync.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ pub fn Queue(comptime T: type) type {
658658
/// Initialize a queue with the provided buffer.
659659
/// The buffer's length determines the queue capacity.
660660
pub fn init(buffer: []T) Self {
661+
std.debug.assert(buffer.len > 0);
661662
return .{ .buffer = buffer };
662663
}
663664

0 commit comments

Comments
 (0)