From b2c62bcbf64a3ffa2af6a8e441f1c12e10c598ec Mon Sep 17 00:00:00 2001 From: Scott Redig Date: Mon, 7 Oct 2024 01:16:20 -0700 Subject: [PATCH] add assertLocked to std.debug.SafetyLock --- lib/std/debug.zig | 18 ++++++++++++++++-- lib/std/hash_map.zig | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/std/debug.zig b/lib/std/debug.zig index 982e71bc35..0756e456ff 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -1531,9 +1531,9 @@ pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize } pub const SafetyLock = struct { - state: State = .unlocked, + state: State = if (runtime_safety) .unlocked else .unknown, - pub const State = if (runtime_safety) enum { unlocked, locked } else enum { unlocked }; + pub const State = if (runtime_safety) enum { unlocked, locked } else enum { unknown }; pub fn lock(l: *SafetyLock) void { if (!runtime_safety) return; @@ -1551,8 +1551,22 @@ pub const SafetyLock = struct { if (!runtime_safety) return; assert(l.state == .unlocked); } + + pub fn assertLocked(l: SafetyLock) void { + if (!runtime_safety) return; + assert(l.state == .locked); + } }; +test SafetyLock { + var safety_lock: SafetyLock = .{}; + safety_lock.assertUnlocked(); + safety_lock.lock(); + safety_lock.assertLocked(); + safety_lock.unlock(); + safety_lock.assertUnlocked(); +} + /// Detect whether the program is being executed in the Valgrind virtual machine. /// /// When Valgrind integrations are disabled, this returns comptime-known false. diff --git a/lib/std/hash_map.zig b/lib/std/hash_map.zig index 9c436320b7..e6f51dc648 100644 --- a/lib/std/hash_map.zig +++ b/lib/std/hash_map.zig @@ -1692,7 +1692,7 @@ pub fn HashMapUnmanaged( } self.size = 0; - self.pointer_stability = .{ .state = .unlocked }; + self.pointer_stability = .{}; std.mem.swap(Self, self, &map); map.deinit(allocator); }