my ziglings
0

Configure Feed

Select the types of activity you want to include in your feed.

improvements for async-io

Chris Boesch (Apr 6, 2026, 7:38 PM +0200) 55a4841b 09bae6a7

+10 -9
+6 -5
exercises/094_async10.zig
··· 1 1 // 2 - // In exercise 088, we learned that cancellation happens at 2 + // In exercise 089, we learned that cancellation happens at 3 3 // "cancellation points" — any Io function that can return 4 4 // error.Canceled. 5 5 // ··· 11 11 // 12 12 // const old = io.swapCancelProtection(.blocked); 13 13 // defer _ = io.swapCancelProtection(old); 14 - // 14 + 15 15 // // In this block, NO Io function will return error.Canceled. 16 16 // // The cancel request is held until protection is restored. 17 17 // ··· 36 36 const io = init.io; 37 37 38 38 var future = io.async(importantTask, .{io}); 39 + defer _ = future.cancel(io); 39 40 40 41 // Give the task time to start and enter its critical section. 41 - io.sleep(std.Io.Duration.fromMilliseconds(300), .awake) catch {}; 42 + io.sleep(std.Io.Duration.fromMilliseconds(200), .awake) catch {}; 42 43 43 44 // Cancel while the task is in its protected section. 44 45 const result = future.cancel(io); ··· 50 51 51 52 // Protect this section from cancellation. 52 53 // What method swaps the cancel protection state? 53 - const old = io.???(. blocked); 54 + const old = io.???(.blocked); 54 55 defer _ = io.???(old); 55 56 56 57 // This sleep will NOT return error.Canceled even though 57 58 // we get canceled during it — protection is active! 58 - io.sleep(std.Io.Duration.fromMilliseconds(600), .awake) catch |err| switch (err) { 59 + io.sleep(std.Io.Duration.fromMilliseconds(300), .awake) catch |err| switch (err) { 59 60 error.Canceled => { 60 61 // This should never happen while protected! 61 62 return "ERROR: canceled during critical section!";
+4 -4
patches/patches/094_async10.patch
··· 1 - --- exercises/094_async10.zig 2026-04-03 14:25:16.600025924 +0200 2 - +++ answers/094_async10.zig 2026-04-03 14:24:56.192615893 +0200 3 - @@ -50,8 +50,8 @@ 1 + --- exercises/094_async10.zig 2026-04-06 19:36:59.873966580 +0200 2 + +++ answers/094_async10.zig 2026-04-06 19:37:12.416216872 +0200 3 + @@ -51,8 +51,8 @@ 4 4 5 5 // Protect this section from cancellation. 6 6 // What method swaps the cancel protection state? 7 - - const old = io.???(. blocked); 7 + - const old = io.???(.blocked); 8 8 - defer _ = io.???(old); 9 9 + const old = io.swapCancelProtection(.blocked); 10 10 + defer _ = io.swapCancelProtection(old);