diff --git a/src/kern/event.zig b/src/kern/event.zig new file mode 100644 index 0000000..31e2f84 --- /dev/null +++ b/src/kern/event.zig @@ -0,0 +1,32 @@ +const ke = @import("root").ke; +const ki = ke.private; + +/// Generic waitable Event, exported for use everywhere in the kernel. +pub const Event = struct { + hdr: ki.wait.DispatchHeader, + + const Self = @This(); + + pub const Type = enum { + /// Wake only one waiter. + Synchronization, + /// Wake all waiters. + Notification, + }; + + /// Initialize an event with a specified type. + pub fn init(self: *Self, @"type": Type) void { + self.hdr.init(switch (@"type") { + .Synchronization => .Synchronization, + .Notification => .Notification, + }); + } + + /// Signal an event, waking its waiter(s). + pub fn signal(self: *Self) void { + const ipl = self.hdr.lock.acquire(); + self.hdr.signaled = 1; + ki.wait.satisfy_wait(&self.hdr); + self.hdr.lock.release(ipl); + } +}; diff --git a/src/kern/root.zig b/src/kern/root.zig index ffb7128..1396022 100644 --- a/src/kern/root.zig +++ b/src/kern/root.zig @@ -17,6 +17,7 @@ pub const Console = p.log.Console; pub const CpuLocal = p.cpu.CpuLocal; pub const ExportedCpuLocal = p.cpu.ExportedCpuLocal; pub const CpuMask = p.cpu.CpuMask; +pub const Event = p.event.Event; // === Exported Interfaces === pub const panic = p.panic.panic;