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/094_async10.zig | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 exercises/094_async10.zig (limited to 'exercises/094_async10.zig') diff --git a/exercises/094_async10.zig b/exercises/094_async10.zig new file mode 100644 index 0000000..6ed229d --- /dev/null +++ b/exercises/094_async10.zig @@ -0,0 +1,67 @@ +// +// In exercise 088, we learned that cancellation happens at +// "cancellation points" — any Io function that can return +// error.Canceled. +// +// But sometimes a task has a critical section that MUST NOT +// be interrupted — for example, writing a consistent state +// to disk, or completing a transaction. +// +// Io provides CancelProtection for this: +// +// const old = io.swapCancelProtection(.blocked); +// defer _ = io.swapCancelProtection(old); +// +// // In this block, NO Io function will return error.Canceled. +// // The cancel request is held until protection is restored. +// +// There are two states: +// .unblocked — normal: cancellation points can fire (default) +// .blocked — protected: error.Canceled is never returned +// +// There's also io.checkCancel() — a pure cancellation point +// that does nothing except return error.Canceled if a cancel +// request is pending. Useful in long CPU-bound loops. +// +// And io.recancel() — re-arms a consumed cancel request so +// the NEXT cancellation point will fire again. +// +// Fix this program so the critical section completes even +// when the task is canceled. +// +const std = @import("std"); +const print = std.debug.print; + +pub fn main(init: std.process.Init) !void { + const io = init.io; + + var future = io.async(importantTask, .{io}); + + // Give the task time to start and enter its critical section. + io.sleep(std.Io.Duration.fromMilliseconds(300), .awake) catch {}; + + // Cancel while the task is in its protected section. + const result = future.cancel(io); + print("Task result: {s}\n", .{result}); +} + +fn importantTask(io: std.Io) []const u8 { + print("Starting critical section...\n", .{}); + + // Protect this section from cancellation. + // What method swaps the cancel protection state? + const old = io.???(. blocked); + defer _ = io.???(old); + + // This sleep will NOT return error.Canceled even though + // we get canceled during it — protection is active! + io.sleep(std.Io.Duration.fromMilliseconds(600), .awake) catch |err| switch (err) { + error.Canceled => { + // This should never happen while protected! + return "ERROR: canceled during critical section!"; + }, + }; + + print("Critical section completed safely.\n", .{}); + return "All data saved."; +} -- cgit v1.2.3 From 55a4841b07ac5d20fdd9f272fdbdd1875a7b4431 Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Mon, 6 Apr 2026 19:38:19 +0200 Subject: improvements for async-io --- exercises/094_async10.zig | 11 ++++++----- patches/patches/094_async10.patch | 8 ++++---- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'exercises/094_async10.zig') diff --git a/exercises/094_async10.zig b/exercises/094_async10.zig index 6ed229d..c561d37 100644 --- a/exercises/094_async10.zig +++ b/exercises/094_async10.zig @@ -1,5 +1,5 @@ // -// In exercise 088, we learned that cancellation happens at +// In exercise 089, we learned that cancellation happens at // "cancellation points" — any Io function that can return // error.Canceled. // @@ -11,7 +11,7 @@ // // const old = io.swapCancelProtection(.blocked); // defer _ = io.swapCancelProtection(old); -// + // // In this block, NO Io function will return error.Canceled. // // The cancel request is held until protection is restored. // @@ -36,9 +36,10 @@ pub fn main(init: std.process.Init) !void { const io = init.io; var future = io.async(importantTask, .{io}); + defer _ = future.cancel(io); // Give the task time to start and enter its critical section. - io.sleep(std.Io.Duration.fromMilliseconds(300), .awake) catch {}; + io.sleep(std.Io.Duration.fromMilliseconds(200), .awake) catch {}; // Cancel while the task is in its protected section. const result = future.cancel(io); @@ -50,12 +51,12 @@ fn importantTask(io: std.Io) []const u8 { // Protect this section from cancellation. // What method swaps the cancel protection state? - const old = io.???(. blocked); + const old = io.???(.blocked); defer _ = io.???(old); // This sleep will NOT return error.Canceled even though // we get canceled during it — protection is active! - io.sleep(std.Io.Duration.fromMilliseconds(600), .awake) catch |err| switch (err) { + io.sleep(std.Io.Duration.fromMilliseconds(300), .awake) catch |err| switch (err) { error.Canceled => { // This should never happen while protected! return "ERROR: canceled during critical section!"; diff --git a/patches/patches/094_async10.patch b/patches/patches/094_async10.patch index ae0d26d..e721485 100644 --- a/patches/patches/094_async10.patch +++ b/patches/patches/094_async10.patch @@ -1,10 +1,10 @@ ---- exercises/094_async10.zig 2026-04-03 14:25:16.600025924 +0200 -+++ answers/094_async10.zig 2026-04-03 14:24:56.192615893 +0200 -@@ -50,8 +50,8 @@ +--- exercises/094_async10.zig 2026-04-06 19:36:59.873966580 +0200 ++++ answers/094_async10.zig 2026-04-06 19:37:12.416216872 +0200 +@@ -51,8 +51,8 @@ // Protect this section from cancellation. // What method swaps the cancel protection state? -- const old = io.???(. blocked); +- const old = io.???(.blocked); - defer _ = io.???(old); + const old = io.swapCancelProtection(.blocked); + defer _ = io.swapCancelProtection(old); -- cgit v1.2.3