From 144de52f27ee8ebc616275fb7147cd54d5a672ac Mon Sep 17 00:00:00 2001 From: cozis Date: Thu, 4 Jun 2026 17:29:34 +0200 Subject: [PATCH] scheduler: Add cancellation flag to Task and .cancel method to set it from the parent --- src/scheduler.zig | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/scheduler.zig b/src/scheduler.zig index 2afa9bf..96d34c8 100644 --- a/src/scheduler.zig +++ b/src/scheduler.zig @@ -45,7 +45,11 @@ const Task = struct { context: ?*const anyopaque, state : State, node : *Node, + + // If this is a subtask, parent_id refers to the parent. + // The cancel flag is set when the parent requests cancellation. parent_id: ?TaskID, + cancel: bool, // These fields are only used when state=.blocked and // are not mutually exclusive. Each represents a different @@ -138,6 +142,7 @@ fn spawnInner( .wakeup_futex = null, .node = node, .parent_id = parent_id, + .cancel = false, }; try self.tasks.append(self.gpa, task); @@ -376,3 +381,13 @@ pub fn wait(self: *Scheduler, ids: []const TaskID) !TaskID { contextSwitch(&task.regs, &self.regs); } } + +pub fn cancel(self: *Scheduler, id: TaskID) !void { + const child = self.findTaskByID(id) + orelse return error.InvalidHandle; + + if (child.parent_id != self.current_id) + return error.InvalidHandle; + + child.cancel = true; +}