diff options
| author | Owen Anderson <resistor@mac.com> | 2009-06-20 00:27:21 +0000 | 
|---|---|---|
| committer | Owen Anderson <resistor@mac.com> | 2009-06-20 00:27:21 +0000 | 
| commit | 4ce341cc75257aa76486bc3d2608af03cabe1a5a (patch) | |
| tree | 784ab495f700edbd0e0912f32acda72db37416fe | |
| parent | 2cda7d74bc24ee3c40cf7a0d8ba5effe069b14e7 (diff) | |
| download | bcm5719-llvm-4ce341cc75257aa76486bc3d2608af03cabe1a5a.tar.gz bcm5719-llvm-4ce341cc75257aa76486bc3d2608af03cabe1a5a.zip  | |
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.
llvm-svn: 73803
| -rw-r--r-- | llvm/include/llvm/System/Mutex.h | 17 | ||||
| -rw-r--r-- | llvm/include/llvm/System/RWMutex.h | 23 | 
2 files changed, 38 insertions, 2 deletions
diff --git a/llvm/include/llvm/System/Mutex.h b/llvm/include/llvm/System/Mutex.h index 0003ef881c8..d2c457dbc91 100644 --- a/llvm/include/llvm/System/Mutex.h +++ b/llvm/include/llvm/System/Mutex.h @@ -15,6 +15,7 @@  #define LLVM_SYSTEM_MUTEX_H  #include "llvm/System/Threading.h" +#include <cassert>  namespace llvm  { @@ -85,18 +86,32 @@ namespace llvm      /// running in multithreaded mode.      template<bool mt_only>      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;        } diff --git a/llvm/include/llvm/System/RWMutex.h b/llvm/include/llvm/System/RWMutex.h index aa5b0b946aa..e577d457afb 100644 --- a/llvm/include/llvm/System/RWMutex.h +++ b/llvm/include/llvm/System/RWMutex.h @@ -15,6 +15,7 @@  #define LLVM_SYSTEM_RWMUTEX_H  #include "llvm/System/Threading.h" +#include <cassert>  namespace llvm  { @@ -84,30 +85,50 @@ namespace llvm      /// running in multithreaded mode.      template<bool mt_only>      class SmartRWMutex : public RWMutexImpl { +      unsigned readers, writers;      public: -      explicit SmartRWMutex() : RWMutexImpl() { } +      explicit SmartRWMutex() : RWMutexImpl(), readers(0), writers(0) { }        bool reader_acquire() {          if (!mt_only || llvm_is_multithreaded())            return RWMutexImpl::reader_acquire(); +         +        // Single-threaded debugging code.  This would be racy in multithreaded +        // mode, but provides not sanity checks in single threaded mode. +        ++readers;          return true;        }        bool reader_release() {          if (!mt_only || llvm_is_multithreaded())            return RWMutexImpl::reader_release(); +         +        // Single-threaded debugging code.  This would be racy in multithreaded +        // mode, but provides not sanity checks in single threaded mode. +        assert(readers > 0 && "Reader lock not acquired before release!"); +        --readers;          return true;        }        bool writer_acquire() {          if (!mt_only || llvm_is_multithreaded())            return RWMutexImpl::writer_acquire(); +         +        // Single-threaded debugging code.  This would be racy in multithreaded +        // mode, but provides not sanity checks in single threaded mode. +        assert(writers == 0 && "Writer lock already acquired!"); +        ++writers;          return true;        }        bool writer_release() {          if (!mt_only || llvm_is_multithreaded())            return RWMutexImpl::writer_release(); +         +        // Single-threaded debugging code.  This would be racy in multithreaded +        // mode, but provides not sanity checks in single threaded mode. +        assert(writers == 1 && "Writer lock not acquired before release!"); +        --writers;          return true;        }  | 

