Skip to content

Commit

Permalink
add assertLocked to std.debug.SafetyLock
Browse files Browse the repository at this point in the history
  • Loading branch information
scottredig authored and andrewrk committed Nov 22, 2024
1 parent 73dcd19 commit b2c62bc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 16 additions & 2 deletions lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/std/hash_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit b2c62bc

Please sign in to comment.