Skip to content

Commit b2c62bc

Browse files
scottredigandrewrk
authored andcommitted
add assertLocked to std.debug.SafetyLock
1 parent 73dcd19 commit b2c62bc

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/std/debug.zig

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,9 +1531,9 @@ pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize
15311531
}
15321532

15331533
pub const SafetyLock = struct {
1534-
state: State = .unlocked,
1534+
state: State = if (runtime_safety) .unlocked else .unknown,
15351535

1536-
pub const State = if (runtime_safety) enum { unlocked, locked } else enum { unlocked };
1536+
pub const State = if (runtime_safety) enum { unlocked, locked } else enum { unknown };
15371537

15381538
pub fn lock(l: *SafetyLock) void {
15391539
if (!runtime_safety) return;
@@ -1551,8 +1551,22 @@ pub const SafetyLock = struct {
15511551
if (!runtime_safety) return;
15521552
assert(l.state == .unlocked);
15531553
}
1554+
1555+
pub fn assertLocked(l: SafetyLock) void {
1556+
if (!runtime_safety) return;
1557+
assert(l.state == .locked);
1558+
}
15541559
};
15551560

1561+
test SafetyLock {
1562+
var safety_lock: SafetyLock = .{};
1563+
safety_lock.assertUnlocked();
1564+
safety_lock.lock();
1565+
safety_lock.assertLocked();
1566+
safety_lock.unlock();
1567+
safety_lock.assertUnlocked();
1568+
}
1569+
15561570
/// Detect whether the program is being executed in the Valgrind virtual machine.
15571571
///
15581572
/// When Valgrind integrations are disabled, this returns comptime-known false.

lib/std/hash_map.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1692,7 +1692,7 @@ pub fn HashMapUnmanaged(
16921692
}
16931693

16941694
self.size = 0;
1695-
self.pointer_stability = .{ .state = .unlocked };
1695+
self.pointer_stability = .{};
16961696
std.mem.swap(Self, self, &map);
16971697
map.deinit(allocator);
16981698
}

0 commit comments

Comments
 (0)