Repository navigation
Add spawnBlocking API for thread pool execution - #16
Conversation
WalkthroughAdds blocking task support to the runtime using a thread pool, new awaitable/task kinds, wakeup/completion plumbing, and a spawnBlocking API. Updates Runtime fields and run loop handling, introduces callback/drain functions, and adds a smoke test. Exposes queue_mpsc in vendor libxev as a public constant. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant U as User Code
participant RT as Runtime
participant TP as ThreadPool
participant EV as xev.Async/xev.Loop
participant DR as drainBlockingCompletions
U->>RT: spawnBlocking(func, args)
RT->>TP: enqueue task with threadPoolCallback
Note over TP: Blocking work executes
TP-->>RT: completion enqueued (MPSC)
TP->>EV: async_wakeup.fire()
EV-->>RT: completion ready (async_completion)
RT->>DR: drainBlockingCompletions(...)
DR-->>U: resume waiter(s), deliver result
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
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 |
Implements spawnBlocking() to run blocking operations on a thread pool
without blocking the async runtime:
- New types: AnyBlockingTask, BlockingTask(T) for thread pool tasks
- Thread pool integration via xev.ThreadPool with async completion notifications
- MPSC queue for thread-safe completion handling from worker threads
- Event loop optimization: uses .no_wait mode when work is pending
- Proper ref counting: 1 ref for JoinHandle, 1 ref for runtime completion
- Memory safety: automatic cleanup via destroy_fn callback
- Test coverage: basic spawnBlocking smoke test
Example usage:
```zig
var handle = try runtime.spawnBlocking(blockingWork, .{arg});
defer handle.deinit();
const result = handle.join();
```
4d105ed to
2e6657a
Compare
|
@CodeRabbit review |
✅ Actions performedReview triggered.
|
Summary
Implements
spawnBlocking()to run blocking operations on a thread pool without blocking the async runtime.Implementation Details
AnyBlockingTask,BlockingTask(T)for thread pool tasksxev.ThreadPoolwith async completion notifications viaxev.Async.no_waitmode when work is pending to avoid unnecessary blockingExample Usage
Testing
All tests pass, including the new
spawnBlockingsmoke test.Summary by CodeRabbit