|
| 1 | +//! Copyright 2025 The Drift Authors. All rights reserved. |
| 2 | +//! Use of this source code is governed by a BSD-style license that can be |
| 3 | +//! found in the LICENSE file. |
| 4 | + |
| 5 | +const std = @import("std"); |
| 6 | +const assert = @import("assert.zig"); |
| 7 | +const cpu = @import("cpu.zig"); |
| 8 | +const ArchSpinLock = @import("arch/SpinLock.zig"); |
| 9 | + |
| 10 | +const arch = ArchSpinLock.Impl; |
| 11 | + |
| 12 | +/// Options for controlling SpinLock behavior |
| 13 | +pub const SpinLockOptions = packed struct(u3) { |
| 14 | + /// No special options |
| 15 | + none: bool = false, |
| 16 | + |
| 17 | + /// Enable integration with the lockup_detector to monitor spinlock critical sections. |
| 18 | + /// |
| 19 | + /// See //zircon/kernel/lib/lockup_detector/README.md. |
| 20 | + monitored: bool = false, |
| 21 | + |
| 22 | + /// Disable tracing |
| 23 | + trace_disabled: bool = false, |
| 24 | +}; |
| 25 | + |
| 26 | +/// Base spinlock implementation with configurable options |
| 27 | +pub fn SpinLockBase(comptime options: SpinLockOptions) type { |
| 28 | + return struct { |
| 29 | + const Self = @This(); |
| 30 | + |
| 31 | + spinlock: ArchSpinLock = .{ .value = undefined }, |
| 32 | + |
| 33 | + /// Acquire the spinlock |
| 34 | + /// Interrupts must already be disabled |
| 35 | + pub fn acquire(self: *Self) void { |
| 36 | + assert.debug_assert(@src(), !options.monitored); |
| 37 | + //assert.debug_assert(@src(), arch.intsDisabled()); |
| 38 | + assert.debug_assert(@src(), !arch.spinLockHeld(&self.spinlock)); |
| 39 | + |
| 40 | + if (options.trace_disabled) { |
| 41 | + arch.spinLockNonInstrumented(&self.spinlock); |
| 42 | + } else { |
| 43 | + arch.spinLockTraceInstrumented(&self.spinlock, 0); // TODO: Add lock ID |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// Acquire with monitoring |
| 48 | + pub fn acquireMonitored(self: *Self, _: []const u8) void { |
| 49 | + assert.debug_assert(@src(), options.monitored); |
| 50 | + assert.debug_assert(@src(), arch.intsDisabled()); |
| 51 | + assert.debug_assert(@src(), !arch.spinLockHeld(&self.spinlock)); |
| 52 | + |
| 53 | + // TODO: Add lockup detector integration |
| 54 | + if (options.trace_disabled) { |
| 55 | + arch.spinLockNonInstrumented(&self.spinlock); |
| 56 | + } else { |
| 57 | + arch.spinLockTraceInstrumented(&self.spinlock, 0); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Try to acquire the spinlock without waiting |
| 62 | + pub fn tryAcquire(self: *Self) bool { |
| 63 | + assert.debug_assert(@src(), !options.monitored); |
| 64 | + return arch.spinTryLock(&self.spinlock); |
| 65 | + } |
| 66 | + |
| 67 | + /// Try to acquire with monitoring |
| 68 | + pub fn tryAcquireMonitored(self: *Self, _: []const u8) bool { |
| 69 | + assert.debug_assert(@src(), options.monitored); |
| 70 | + const failed = arch.spinTryLock(&self.spinlock); |
| 71 | + if (!failed) { |
| 72 | + // TODO: Add lockup detector integration |
| 73 | + } |
| 74 | + return failed; |
| 75 | + } |
| 76 | + |
| 77 | + /// Release the spinlock |
| 78 | + pub fn release(self: *Self) void { |
| 79 | + arch.spinUnlock(&self.spinlock); |
| 80 | + if (options.monitored) { |
| 81 | + // TODO: Add lockup detector integration |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// Check if held by current CPU |
| 86 | + pub fn isHeld(self: *const Self) bool { |
| 87 | + assert.debug_assert(@src(), arch.intsDisabled()); |
| 88 | + return arch.spinLockHeld(&self.spinlock); |
| 89 | + } |
| 90 | + |
| 91 | + /// Assert that lock is held |
| 92 | + pub fn assertHeld(self: *const Self) void { |
| 93 | + assert(@src(), self.isHeld()); |
| 94 | + } |
| 95 | + |
| 96 | + /// Get CPU holding the lock |
| 97 | + pub fn holderCpu(self: *const Self) u32 { |
| 98 | + return arch.spinLockHolderCpu(&self.spinlock); |
| 99 | + } |
| 100 | + }; |
| 101 | +} |
| 102 | + |
| 103 | +/// Standard spinlock without monitoring |
| 104 | +pub const SpinLock = SpinLockBase(.{ .none = true }); |
| 105 | + |
| 106 | +//Spinlock with lockup detection |
| 107 | +pub const MonitoredSpinLock = SpinLockBase(.{ .monitored = true }); |
| 108 | + |
| 109 | +// Spinlock with tracing disabled |
| 110 | +pub const TraceDisabledSpinLock = SpinLockBase(.{ .trace_disabled = true }); |
0 commit comments