-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
coro: add ThreadPool for mixing blocking code
- Loading branch information
Showing
6 changed files
with
227 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# CORO API | ||
|
||
## Mixing blocking code | ||
|
||
Sometimes it's not feasible to rewrite blocking code so that it plays nice with the `coro.Scheduler`. | ||
In that kind of scenario it is possible to use `coro.ThreadPool` to allow tasks to yield until blocking code | ||
finishes on a worker thread. | ||
|
||
### Example | ||
|
||
```zig | ||
fn blockingCode() u32 { | ||
std.time.sleep(1 * std.time.ns_per_s); | ||
return 69; | ||
} | ||
fn task(pool: *ThreadPool) !void { | ||
const ret = try pool.yieldForCompletition(blockingCode, .{}); | ||
try std.testing.expectEqual(69, ret); | ||
} | ||
var pool: ThreadPool = .{}; | ||
defer pool.deinit(); // pool must always be destroyed before scheduler | ||
try pool.start(std.testing.allocator, 0); | ||
var scheduler = try Scheduler.init(std.testing.allocator, .{}); | ||
defer scheduler.deinit(); | ||
_ = try scheduler.spawn(task, .{&pool}, .{}); | ||
try scheduler.run(); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.