Mutex deadlock detection in debug

Add a debug implementation to Mutex that detects deadlocks caused by calling lock twice in a single thread.
This commit is contained in:
IntegratedQuantum
2023-01-19 15:57:29 +01:00
committed by GitHub
parent 116b770809
commit f38fd388f8
2 changed files with 64 additions and 22 deletions

View File

@@ -161,12 +161,20 @@ const WindowsImpl = struct {
}
}
if (comptime builtin.mode == .Debug) {
// The internal state of the DebugMutex needs to be handled here as well.
mutex.impl.locking_thread.store(0, .Unordered);
}
const rc = os.windows.kernel32.SleepConditionVariableSRW(
&self.condition,
&mutex.impl.srwlock,
if (comptime builtin.mode == .Debug) &mutex.impl.impl.srwlock else &mutex.impl.srwlock,
timeout_ms,
0, // the srwlock was assumed to acquired in exclusive mode not shared
);
if (comptime builtin.mode == .Debug) {
// The internal state of the DebugMutex needs to be handled here as well.
mutex.impl.locking_thread.store(std.Thread.getCurrentId(), .Unordered);
}
// Return error.Timeout if we know the timeout elapsed correctly.
if (rc == os.windows.FALSE) {