From cc40f077b018e62e2e6f5dc279e3be84779569ce Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Sat, 20 Jun 2009 00:27:21 +0000 Subject: Add debugging code to test for various locking faux-pas's, when running in single threaded mode. This should help improve testing coverage for threading support, without having extensive actually concurrent clients yet. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73803 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/System/Mutex.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'include/llvm/System/Mutex.h') diff --git a/include/llvm/System/Mutex.h b/include/llvm/System/Mutex.h index 0003ef881c..d2c457dbc9 100644 --- a/include/llvm/System/Mutex.h +++ b/include/llvm/System/Mutex.h @@ -15,6 +15,7 @@ #define LLVM_SYSTEM_MUTEX_H #include "llvm/System/Threading.h" +#include namespace llvm { @@ -85,18 +86,32 @@ namespace llvm /// running in multithreaded mode. template class SmartMutex : public MutexImpl { + unsigned acquired; + bool recursive; public: - explicit SmartMutex(bool recursive = true) : MutexImpl(recursive) { } + explicit SmartMutex(bool rec = true) : + MutexImpl(rec), acquired(0), recursive(rec) { } bool acquire() { if (!mt_only || llvm_is_multithreaded()) return MutexImpl::acquire(); + + // Single-threaded debugging code. This would be racy in multithreaded + // mode, but provides not sanity checks in single threaded mode. + assert((recursive || acquired == 0) && "Lock already acquired!!"); + ++acquired; return true; } bool release() { if (!mt_only || llvm_is_multithreaded()) return MutexImpl::release(); + + // Single-threaded debugging code. This would be racy in multithreaded + // mode, but provides not sanity checks in single threaded mode. + assert(((recursive && acquired) || (acquired == 1)) && + "Lock not acquired before release!"); + --acquired; return true; } -- cgit v1.2.3