Repository navigation
Refactor sync primitives and add Queue(T) - #21
Conversation
- Refactored Mutex, Condition, and ResetEvent to not store Runtime
- Changed init() from function to constant init: Type = .{}
- All methods now take rt: *Runtime as parameter
- Updated all call sites in src/net.zig and src/tcp.zig
- Implemented Queue(T) bounded FIFO queue with ring buffer
- Coroutine-safe for multiple producers/consumers within single Runtime
- Blocking operations: get() and put()
- Non-blocking operations: tryGet() and tryPut()
- Graceful and immediate close modes
- Helper methods: isEmpty() and isFull()
- Added comprehensive tests for Queue functionality
- Basic put/get operations
- Blocking and non-blocking behavior
- Multiple producers/consumers
- Close behavior (graceful/immediate)
- Ring buffer wrapping
WalkthroughRefactors sync primitives to require an explicit Runtime on operations (initters become constant inits), adds a generic bounded coroutine-safe Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant P as Producer coroutine
participant C as Consumer coroutine
participant Q as Queue(T)
participant RT as Runtime
Note over Q,RT: Bounded FIFO with runtime-parameterized ops
P->>Q: put(item, RT)
alt queue full
Q-->>P: suspend (put) / return QueueFull (tryPut)
Q->>RT: park producer
else space available
Q-->>P: ok
end
C->>Q: get(RT)
alt queue empty
Q-->>C: suspend (get) / return QueueEmpty (tryGet)
Q->>RT: park consumer
else item available
Q-->>C: item
end
rect rgba(230,245,255,0.5)
Note right of Q: close(RT, immediate)\nUnblocks waiters with QueueClosed
end
sequenceDiagram
autonumber
participant T as Task
participant E as ResetEvent
participant RT as Runtime
Note over E,RT: ResetEvent methods now take `*Runtime`
T->>E: wait(RT)
par Another task
Other->>E: set(RT)
end
E->>RT: wake waiting tasks
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
README.md(1 hunks)src/net.zig(6 hunks)src/sync.zig(28 hunks)src/tcp.zig(5 hunks)
🧰 Additional context used
🪛 GitHub Actions: CI
src/sync.zig
[error] 9-9: Mutex is defined as a struct, not a function. Ensure correct initialization (e.g., Mutex{ ... } or Mutex.init(...) if a constructor exists).
- Removed explicit Error enum with all possible errors - Changed get(), tryGet(), put(), tryPut() to use inferred error sets - Each method now only declares the errors it actually returns: - get(): error.QueueClosed - tryGet(): error.QueueClosed, error.QueueEmpty - put(): error.QueueClosed - tryPut(): error.QueueClosed, error.QueueFull
|
@claude address CI build failures |
|
Claude finished @lalinsky's task —— View job ✅ CI Build Failures FixedSummaryFixed all CI build failures by updating example files to use the refactored sync primitives API and added a zero-capacity queue guard as suggested by CodeRabbit. Changes Made
Test Results✅ All 50 tests passed locally
Commit
The changes have been pushed to the |
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]>
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/sync.zig (1)
22-23: Clarify the unused runtime parameter.The
runtimeparameter is accepted but immediately discarded with_ = runtime;. If the runtime is genuinely not needed forlock()(because the coroutine context is obtained viacoroutines.getCurrent()), consider either:
- Adding a doc comment explaining why the parameter exists for API consistency
- Removing the parameter if it's not required
The same pattern appears in
ResetEvent.wait()at line 216.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
examples/mutex_demo.zig(2 hunks)examples/producer_consumer.zig(4 hunks)src/sync.zig(28 hunks)
🔇 Additional comments (7)
examples/mutex_demo.zig (1)
11-12: LGTM! Refactoring correctly applied.The mutex usage has been properly updated to pass the runtime parameter to
lock/unlock, and initialization now uses the constantMutex.initpattern. These changes align with the broader API refactoring described in the PR.Also applies to: 32-32
src/sync.zig (5)
59-160: LGTM! Condition refactoring is correct.The
Conditionimplementation properly threads the runtime parameter through all operations. The timeout handling intimedWaitcorrectly uses the runtime for timer operations and coroutine wake-up.
162-316: ResetEvent refactoring looks correct.The
ResetEventimplementation properly uses the runtime parameter inset()andtimedWait()for coroutine management. The unused parameter inwait()was already noted in the Mutex review.
660-663: Zero-capacity assertion properly addresses previous review.The assertion at line 661 (
std.debug.assert(buffer.len > 0);) correctly guards against zero-capacity queues, addressing the previous review comment. This prevents division-by-zero issues in the ring buffer modulo operations.
640-799: Queue(T) implementation looks solid.The bounded FIFO queue implementation is well-structured:
- Ring buffer semantics are correctly implemented with head/tail pointers and modulo arithmetic
- Blocking operations (
get/put) properly wait on conditions and check for closure- Non-blocking operations (
tryGet/tryPut) correctly return appropriate errors- Close semantics handle both graceful (drain items) and immediate (clear buffer) modes
- Synchronization using mutex and two conditions (not_empty/not_full) follows classic producer-consumer pattern
318-1115: Excellent test coverage for both refactored and new code.The test suite is comprehensive:
- All existing tests for Mutex, Condition, and ResetEvent have been updated to use the new runtime-aware API
- 9 new tests for Queue(T) cover essential scenarios: basic operations, blocking/non-blocking behavior, multiple producers/consumers, close modes, error handling, and ring buffer wrapping
- The tests validate both happy paths and error conditions
examples/producer_consumer.zig (1)
13-21: LGTM! BoundedBuffer correctly updated for runtime-aware API.All changes align with the synchronization primitive refactoring:
- Initialization properly uses constant initializers (Mutex.init, Condition.init)
- The runtime parameter is correctly marked as unused in
init()since it's no longer needed- Method signatures updated to accept runtime parameters
- All synchronization operations properly pass the runtime through
Also applies to: 23-41, 43-63, 69-69, 77-77
) This commit refactors the completion system to store errors separately from result payloads, improving clarity and type safety. Key changes: - Added err and has_result fields to base Completion struct - Changed operation result fields from Error!Payload to Payload - Renamed result fields to result_private_do_not_touch to prevent direct access - Updated setResult/setError to handle cancelation race conditions - Changed getResult API to take comptime operation type parameter - Updated all backends (io_uring, epoll, kqueue, poll) to use new pattern - Updated all tests to use getResult() methods The new pattern requires calling setResult(.operation_type, value) and getResult(.operation_type) instead of directly accessing result fields.
Summary
Mutex,Condition, andResetEventto remove storedRuntimereferenceQueue(T)- a coroutine-safe bounded FIFO queue with ring bufferChanges to existing sync primitives
init(runtime: *Runtime)to constantinit: Type = .{}rt: *Runtimeas parametersrc/net.zigandsrc/tcp.zigQueue(T) implementation
get()andput()tryGet()andtryPut()close(rt, immediate: bool)isEmpty()andisFull()Test plan
Summary by CodeRabbit