Files
zing/src/sync.zig
T

46 lines
1.4 KiB
Zig

//! 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,
irqMask: 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.setInterruptMask(self.irqMask);
}
};
/// Acquires a lock over `self`. Returns `false` if the lock is already held by someone else.
pub fn tryLock(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.tryLock()) {
arch.spinHint();
}
}
/// Same as `lock()`, but additionally saves current IRQ state and masks IRQs.
pub fn lock_irqsave(self: *@This()) Guard {
const irqMask = arch.setInterruptMask(true);
self.lock();
return .{ .irqMask = irqMask, .lock = self };
}
/// Releases a lock over `self`.
pub fn release(self: *@This()) void {
self.state.store(false, .release);
}
};