//! Kernel synchronization primitives. const std = @import("std"); const arch = @import("kernel.zig").arch; /// Basic spinlock implementation pub const Spinlock = struct { state: std.atomic.Value(bool) = .{ .raw = false }, const Guard = struct { lock: *Spinlock, irq_mask: bool, /// Releases the `Guard`, restoring the previous IRQ state and releasing the lock used /// to acquire it. pub fn release(self: @This()) void { self.lock.release(); _ = arch.set_interrupt_mask(self.irq_mask); } }; /// Acquires a lock over `self`. Returns `false` if the lock is already held by someone else. pub fn try_lock(self: *@This()) bool { return self.state.cmpxchgStrong(false, true, .acquire, .monotonic) orelse false; } /// Acquires a lock over `self`. Will block until a lock can be acquired. pub fn lock(self: *@This()) void { while (!self.try_lock()) { arch.spin_hint(); } } /// Same as `lock()`, but additionally saves current IRQ state and masks IRQs. pub fn lock_irqsave(self: *@This()) Guard { const irq_mask = arch.set_interrupt_mask(true); self.lock(); return .{ .irq_mask = irq_mask, .lock = self }; } /// Releases a lock over `self`. pub fn release(self: *@This()) void { self.state.store(false, .release); } };