Repository navigation
Refactor coroutine result handling with FutureResult - #14
Conversation
Changes: - Add FutureResult(T): thread-safe result container with atomic state - Uses CAS to ensure results are set exactly once - get() returns ?T based on atomic state check - Update Coroutine: - Store result_ptr: ?*anyopaque instead of AnyCoroutineResult - init() now takes comptime Result: type and *FutureResult(Result) - Wrapper calls FutureResult.set() with function result - Remove CoroutineData.result field - Refactor Task(T): - Now contains any_task, future_result, and runtime pointer - Simplified API: join() waits and returns result - deinit() uses runtime.releaseTask() with ref counting - Update AnyTask: - Add destroy_fn pointer for type-erased destruction - Runtime uses @fieldParentPtr to get typed Task from AnyTask - Runtime.spawn() now returns *Task(T) directly All tests pass
|
Warning Rate limit exceeded@lalinsky has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 6 minutes and 47 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (6)
WalkthroughCoroutines now write results into external FutureResult storage; internal AnyCoroutineResult and CoroutineResult helpers were removed. Runtime and Task types embed FutureResult, spawn returns a pointer to Task, and task lifecycle uses reference-counted release with a destroy callback. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant U as User
participant R as Runtime
participant T as Task(T)
participant A as AnyTask
participant C as Coroutine
participant F as FutureResult(T)
U->>R: spawn(func, args, options)
R->>T: allocate Task(T) { any_task, future_result = F }
R->>A: init AnyTask (destroy_fn, coro)
R->>C: init coro with Result=T and result_ptr=&F
R->>R: enqueue &A into ready_queue
R-->>U: return *Task(T)
Note over C,F: coroutine stores result externally via FutureResult
A->>C: run()
C->>F: set(value) %% CAS not_set->setting->set
U->>T: join()
T->>F: get()
alt result not set
T->>R: wait(A.id)
R-->>T: signaled
T->>F: get()
end
T-->>U: value
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
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 |
- Fix race condition in FutureResult.set() by using three-state (not_set -> setting -> set) with proper release/acquire ordering - Remove unused AnyCoroutineResult and CoroutineResult(T) types - Remove dead Coroutine.getResult() and Runtime.getResult() methods
Replace direct allocator.destroy(task) calls with releaseTask() in Runtime.deinit and cleanup_queue processing to ensure proper ref counting and use of task-specific destroy_fn, preventing memory corruption from freeing wrong-sized allocations.
Change task arrays from Task(T) to *Task(T) to match the new spawn() return type, and update defer loops to iterate over pointers directly.
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 (2)
examples/mutex_demo.zig(1 hunks)examples/producer_consumer.zig(1 hunks)
🔇 Additional comments (4)
examples/producer_consumer.zig (3)
93-94: LGTM! Type declarations updated correctly.The arrays now hold task pointers, aligning with the new
Runtime.spawn()API that returns*Task(T).
96-99: Task spawning integrates correctly with pointer return type.The assignments correctly store the
*Task(void)pointers returned byruntime.spawn().
101-104: LGTM! Task teardown properly handles pointer-based tasks.The defer block correctly deinitializes each task pointer, ensuring proper cleanup with the new reference-counted lifecycle.
examples/mutex_demo.zig (1)
36-36: LGTM! Array type updated to match pointer-based Task API.The change from value-based to pointer-based task storage correctly aligns with the refactored
Runtime.spawn()API that now returns*Task(T).
Track task_count/producer_count/consumer_count and only deinit the slice of tasks that were actually initialized, preventing deinit on uninitialized entries if spawn() fails partway through the loop.
Add JoinHandle(T) wrapper type with union(enum) { coro: *Task(T) }
to abstract task implementation details and prepare for future
spawnBlocking() support. Task methods are now private, with public
API exposed through JoinHandle. Update all examples to use the new
JoinHandle type.
4bb1a60 Fix stack unwinding on aarch64 and riscv64 (#18) ae64534 Update CI to use Zig master cf32ccd Update for Zig 0.16 compatibility c2f6ee8 Add a test for capturing stack trace (#17) 3d4c6de Test using DebugAllocator in coroutine (#14) 6536658 Fix the test b6eec4f Build tests in release mode REVERT: eab0a7b Merge remote-tracking branch 'origin/main' into zig-0.16 REVERT: 7579996 Expose StackPoolConfig REVERT: 7fa7b4f Update CI to use Zig master REVERT: cbabd36 Update for Zig 0.16 compatibility git-subtree-dir: src/lib/coro git-subtree-split: 4bb1a60186952636e6d3e409d62d9e8cf50b1560
Modified backend tick() functions to return a boolean indicating whether the wait timed out. This allows the event loop to only re-check timers when we actually timed out, avoiding unnecessary clock_gettime() syscalls and timer heap iterations when the loop was woken by I/O events. Changes: - poll.zig: Return true when poll() returns 0 (timeout) - epoll.zig: Return true when epoll_wait() returns 0 (timeout) - kqueue.zig: Return true when kevent() returns 0 (timeout) - loop.zig: Only call checkTimers() again if backend timed out This optimization reduces overhead on every tick that processes I/O events.
Changes:
Add FutureResult(T): thread-safe result container with atomic state
Update Coroutine:
Refactor Task(T):
Update AnyTask:
Runtime.spawn() now returns *Task(T) directly
All tests pass
Summary by CodeRabbit
New Features
Refactor
Chores