From 1c6487c1e79cbe0d59a39b483af8ec44b59c586e Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Fri, 3 Apr 2026 18:11:00 +0200 Subject: added async-io quiz --- exercises/085_async.zig | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 exercises/085_async.zig (limited to 'exercises/085_async.zig') diff --git a/exercises/085_async.zig b/exercises/085_async.zig new file mode 100644 index 0000000..48bda2b --- /dev/null +++ b/exercises/085_async.zig @@ -0,0 +1,48 @@ +// +// In previous versions of Zig, async/await used special keywords +// like 'suspend', 'resume', and 'async' that operated on stackframes +// directly. Those keywords no longer exist! +// +// Zig 0.16 replaced them with a unified I/O interface: std.Io. +// This interface uses a VTable pattern - a struct of function pointers - +// to abstract over different concurrency backends: +// +// * Threaded - classic thread-pool based I/O +// * Uring - Linux io_uring +// * Kqueue - BSD/macOS +// * Dispatch - macOS Grand Central Dispatch +// +// The Io struct itself is tiny: +// +// const Io = struct { +// userdata: ?*anyopaque, // opaque state of the backend +// vtable: *const VTable, // table of function pointers +// }; +// +// Your code receives an Io value and calls methods on it. +// The backend is chosen at initialization time - your code doesn't +// need to know which one it is! +// +// In Zig 0.16, main() receives a std.process.Init struct to opt +// into I/O and concurrency support: +// +// pub fn main(init: std.process.Init) !void { +// const io = init.io; +// // ... use io ... +// } +// +// Let's start simple. Fix the main function to extract the Io +// interface from init, then use it to get the current time. +// +const std = @import("std"); + +pub fn main(init: std.process.Init) !void { + const io = init.???; + + // Get the current wall-clock time using the Io interface. + // Hint: Timestamp.now() takes an Io and a Clock type (.real = wall clock). + const timestamp = std.Io.Timestamp.now(io, .real); + + // Print the timestamp in seconds since the Unix epoch. + std.debug.print("Current time: {}s since epoch\n", .{timestamp.toSeconds()}); +} -- cgit v1.2.3 From 58f8df66d57fec4b0d8d69df7ac26624194b86ad Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Sat, 4 Apr 2026 16:05:35 +0200 Subject: improvements for async-io --- exercises/085_async.zig | 9 +++++---- patches/patches/085_async.patch | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'exercises/085_async.zig') diff --git a/exercises/085_async.zig b/exercises/085_async.zig index 48bda2b..1d885a5 100644 --- a/exercises/085_async.zig +++ b/exercises/085_async.zig @@ -7,10 +7,11 @@ // This interface uses a VTable pattern - a struct of function pointers - // to abstract over different concurrency backends: // -// * Threaded - classic thread-pool based I/O -// * Uring - Linux io_uring -// * Kqueue - BSD/macOS -// * Dispatch - macOS Grand Central Dispatch +// * Threaded - thread-pool based I/O +// * Evented - chooses the best event-loop backend for your OS: +// * Uring on Linux (io_uring) +// * Kqueue on BSD/macOS +// * Dispatch on macOS (Grand Central Dispatch) // // The Io struct itself is tiny: // diff --git a/patches/patches/085_async.patch b/patches/patches/085_async.patch index ca8b102..108eae1 100644 --- a/patches/patches/085_async.patch +++ b/patches/patches/085_async.patch @@ -1,6 +1,6 @@ ---- exercises/085_async.zig 2026-04-01 20:40:08.904999609 +0200 -+++ answers/085_async.zig 2026-04-01 20:40:05.641933231 +0200 -@@ -37,7 +37,7 @@ +--- exercises/085_async.zig 2026-04-04 16:01:01.509555724 +0200 ++++ answers/085_async.zig 2026-04-04 16:00:58.541495688 +0200 +@@ -38,7 +38,7 @@ const std = @import("std"); pub fn main(init: std.process.Init) !void { -- cgit v1.2.3