summaryrefslogtreecommitdiff
path: root/exercises
diff options
context:
space:
mode:
Diffstat (limited to 'exercises')
-rw-r--r--exercises/084_interfaces.zig (renamed from exercises/095_interfaces.zig)0
-rw-r--r--exercises/085_async.zig (renamed from exercises/084_async.zig)0
-rw-r--r--exercises/086_async2.zig (renamed from exercises/085_async2.zig)0
-rw-r--r--exercises/087_async3.zig (renamed from exercises/086_async3.zig)0
-rw-r--r--exercises/088_async4.zig (renamed from exercises/087_async4.zig)0
-rw-r--r--exercises/089_async5.zig (renamed from exercises/088_async5.zig)0
-rw-r--r--exercises/090_async6.zig (renamed from exercises/089_async6.zig)0
-rw-r--r--exercises/091_async7.zig (renamed from exercises/090_async7.zig)0
-rw-r--r--exercises/092_async8.zig (renamed from exercises/091_async8.zig)0
-rw-r--r--exercises/093_async9.zig (renamed from exercises/092_async9.zig)0
-rw-r--r--exercises/094_async10.zig (renamed from exercises/093_async10.zig)0
-rw-r--r--exercises/095_quiz_async.zig186
12 files changed, 186 insertions, 0 deletions
diff --git a/exercises/095_interfaces.zig b/exercises/084_interfaces.zig
index 7775dd5..7775dd5 100644
--- a/exercises/095_interfaces.zig
+++ b/exercises/084_interfaces.zig
diff --git a/exercises/084_async.zig b/exercises/085_async.zig
index 48bda2b..48bda2b 100644
--- a/exercises/084_async.zig
+++ b/exercises/085_async.zig
diff --git a/exercises/085_async2.zig b/exercises/086_async2.zig
index 1f1c4c8..1f1c4c8 100644
--- a/exercises/085_async2.zig
+++ b/exercises/086_async2.zig
diff --git a/exercises/086_async3.zig b/exercises/087_async3.zig
index 07221e9..07221e9 100644
--- a/exercises/086_async3.zig
+++ b/exercises/087_async3.zig
diff --git a/exercises/087_async4.zig b/exercises/088_async4.zig
index 50829fc..50829fc 100644
--- a/exercises/087_async4.zig
+++ b/exercises/088_async4.zig
diff --git a/exercises/088_async5.zig b/exercises/089_async5.zig
index 4fb8d76..4fb8d76 100644
--- a/exercises/088_async5.zig
+++ b/exercises/089_async5.zig
diff --git a/exercises/089_async6.zig b/exercises/090_async6.zig
index eab03c9..eab03c9 100644
--- a/exercises/089_async6.zig
+++ b/exercises/090_async6.zig
diff --git a/exercises/090_async7.zig b/exercises/091_async7.zig
index bfe6ffd..bfe6ffd 100644
--- a/exercises/090_async7.zig
+++ b/exercises/091_async7.zig
diff --git a/exercises/091_async8.zig b/exercises/092_async8.zig
index 10921c3..10921c3 100644
--- a/exercises/091_async8.zig
+++ b/exercises/092_async8.zig
diff --git a/exercises/092_async9.zig b/exercises/093_async9.zig
index ad30dcf..ad30dcf 100644
--- a/exercises/092_async9.zig
+++ b/exercises/093_async9.zig
diff --git a/exercises/093_async10.zig b/exercises/094_async10.zig
index 6ed229d..6ed229d 100644
--- a/exercises/093_async10.zig
+++ b/exercises/094_async10.zig
diff --git a/exercises/095_quiz_async.zig b/exercises/095_quiz_async.zig
new file mode 100644
index 0000000..fb78e7b
--- /dev/null
+++ b/exercises/095_quiz_async.zig
@@ -0,0 +1,186 @@
+//
+// Quiz Time — Async I/O!
+//
+// Doctor Zoraptera's insect simulation is going well, but she
+// realized that her virtual garden needs weather data! Insects
+// behave differently depending on temperature, humidity, and
+// wind conditions.
+//
+// She has set up three weather sensors around the garden that
+// measure conditions in parallel and report their readings
+// through a shared data channel. A collector task gathers the
+// readings, and after all sensors have reported, a garden
+// report is printed.
+//
+// But Doctor Z rushed through the code (she was being chased
+// by a grasshopper) and left several bugs. Can you fix them?
+//
+// Here's what the program should do:
+// 1. Three sensor tasks run concurrently, each sending
+// exactly 3 readings through a Queue
+// 2. A collector task receives readings, protected by a Mutex
+// 3. After all sensors finish, the queue is closed
+// 4. The final report is written in a cancel-protected section
+//
+// *************************************************************
+// * A NOTE ABOUT THIS EXERCISE *
+// * *
+// * This quiz uses concepts from exercises 084-093. *
+// * There are 6 bugs to fix — look for the ???s! *
+// * *
+// *************************************************************
+//
+const std = @import("std");
+const print = std.debug.print;
+
+const SensorType = enum { thermometer, hygrometer, anemometer };
+
+const Reading = struct {
+ sensor_type: SensorType,
+ value: i32,
+};
+
+const GardenWeather = struct {
+ temperature: i32 = 0,
+ humidity: i32 = 0,
+ wind: i32 = 0,
+ readings_count: u32 = 0,
+ mutex: std.Io.Mutex = .init,
+
+ fn addReading(self: *GardenWeather, io: std.Io, reading: Reading) void {
+ // Bug 1: The collector needs to lock before modifying
+ // shared state. What Mutex method acquires the lock?
+ self.mutex.lock(io) catch return;
+ self.mutex.???(io) catch return;
+
+ switch (reading.sensor_type) {
+ .thermometer => self.temperature = reading.value,
+ .hygrometer => self.humidity = reading.value,
+ .anemometer => self.wind = reading.value,
+ }
+ self.readings_count += 1;
+ }
+};
+
+pub fn main(init: std.process.Init) !void {
+ const io = init.io;
+
+ var weather = GardenWeather{};
+
+ var reading_buf: [8]Reading = undefined;
+ var queue: std.Io.Queue(Reading) = .init(&reading_buf);
+
+ // Sensor group: runs all three sensors to completion.
+ var sensors: std.Io.Group = .init;
+
+ // Start three sensor tasks. They need GUARANTEED concurrency
+ // since they each simulate real-time measurement.
+ //
+ // Bug 2: io.async doesn't guarantee a separate thread.
+ // Which Io method guarantees true concurrency?
+ // (Don't forget: it can fail, so you need 'try'!)
+ try sensors.???(io, sensor, .{ io, &queue, .thermometer, 20 });
+ try sensors.???(io, sensor, .{ io, &queue, .hygrometer, 60 });
+ try sensors.???(io, sensor, .{ io, &queue, .anemometer, 10 });
+
+ // Collector group: processes readings from the queue.
+ var collectors: std.Io.Group = .init;
+ collectors.async(io, collector, .{ io, &queue, &weather });
+
+ // Bug 3: Wait for ALL sensors to finish sending their readings.
+ // What Group method blocks until all tasks complete?
+ try sensors.await(io);
+ // try sensors.???(io);
+
+ // All sensors done — close the queue so the collector knows
+ // there's no more data coming.
+ queue.close(io);
+
+ // Wait for the collector to drain the queue.
+ try collectors.await(io);
+
+ // Now write the garden report. This is critical — it must
+ // NOT be interrupted, even if something tries to cancel us!
+ //
+ // Bug 4: Protect this section from cancellation.
+ // What Io method swaps the cancel protection state?
+ const old_protection = io.???(.blocked);
+ defer _ = io.???(old_protection);
+
+ printGardenReport(&weather);
+}
+
+fn sensor(
+ io: std.Io,
+ queue: *std.Io.Queue(Reading),
+ sensor_type: SensorType,
+ base_value: i32,
+) void {
+ // Each sensor takes exactly 3 measurements.
+ for (1..4) |i| {
+ io.sleep(std.Io.Duration.fromMilliseconds(100), .awake) catch return;
+
+ const reading = Reading{
+ .sensor_type = sensor_type,
+ .value = base_value + @as(i32, @intCast(i)),
+ };
+
+ // Bug 5: Send the reading into the queue.
+ // What Queue method sends a single element?
+ queue.???(io, reading) catch return;
+ }
+}
+
+fn collector(
+ io: std.Io,
+ queue: *std.Io.Queue(Reading),
+ weather: *GardenWeather,
+) void {
+ while (true) {
+ const reading = queue.getOne(io) catch |err| switch (err) {
+ error.Closed => break,
+ error.Canceled => return,
+ };
+ weather.addReading(io, reading);
+ }
+}
+
+fn printGardenReport(weather: *GardenWeather) void {
+ print("=== Doctor Zoraptera's Garden Report ===\n", .{});
+ print("Temperature : {}C\n", .{weather.temperature});
+ print("Humidity : {}%\n", .{weather.humidity});
+ print("Wind : {} km/h\n", .{weather.wind});
+ print("Readings : {}\n", .{weather.readings_count});
+
+ if (weather.temperature > 20 and weather.wind < 15) {
+ print("Bee-friendly conditions! Expect high pollination.\n", .{});
+ } else {
+ print("Grasshoppers will be grumpy today.\n", .{});
+ }
+}
+
+// Further reading for the curious:
+//
+// This quiz covered the main async I/O primitives:
+// io.async() - launch a task (may run inline)
+// io.concurrent() - launch with guaranteed parallelism
+// Group.concurrent() - concurrent tasks in a group
+// Future.await/cancel - collect or cancel a single task
+// Group.async/await/cancel - manage fire-and-forget tasks
+// Select.async/await - race tasks, act on first completion
+// Queue - bounded channel between tasks
+// Mutex - protect shared state
+// CancelProtection - shield critical sections
+//
+// There are more synchronization primitives we didn't cover:
+// Condition - wait for a condition to become true
+// RwLock - multiple readers OR one writer
+// Semaphore - limit concurrent access to a resource
+// Futex - low-level wait/wake on a memory address
+// Batch - submit multiple I/O operations at once
+//
+// The key insight: all of these work through the Io VTable,
+// so your code is portable across backends (Threaded, Uring,
+// Kqueue, Dispatch) without any changes!
+//
+// Doctor Zoraptera approves.