diff options
author | Owen Anderson <resistor@mac.com> | 2009-06-18 18:26:15 +0000 |
---|---|---|
committer | Owen Anderson <resistor@mac.com> | 2009-06-18 18:26:15 +0000 |
commit | b65e9ed10677fe8944822c450b14d7e321f6e6f5 (patch) | |
tree | d7036ada58cde5192fa8b90c10c9913f13fbc956 /include/llvm/System | |
parent | b849a4dd4bee9ad17e295691087ce09e8d77d685 (diff) | |
download | external_llvm-b65e9ed10677fe8944822c450b14d7e321f6e6f5.zip external_llvm-b65e9ed10677fe8944822c450b14d7e321f6e6f5.tar.gz external_llvm-b65e9ed10677fe8944822c450b14d7e321f6e6f5.tar.bz2 |
Give RWMutex the SmartRWMutex treatment too.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73710 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/System')
-rw-r--r-- | include/llvm/System/RWMutex.h | 72 |
1 files changed, 58 insertions, 14 deletions
diff --git a/include/llvm/System/RWMutex.h b/include/llvm/System/RWMutex.h index 0a3932e..6a254b8 100644 --- a/include/llvm/System/RWMutex.h +++ b/include/llvm/System/RWMutex.h @@ -14,12 +14,14 @@ #ifndef LLVM_SYSTEM_RWMUTEX_H #define LLVM_SYSTEM_RWMUTEX_H +#include "llvm/System/Threading.h" + namespace llvm { namespace sys { - /// @brief Platform agnostic Mutex class. - class RWMutex + /// @brief Platform agnostic RWMutex class. + class RWMutexImpl { /// @name Constructors /// @{ @@ -27,11 +29,11 @@ namespace llvm /// Initializes the lock but doesn't acquire it. /// @brief Default Constructor. - explicit RWMutex(); + explicit RWMutexImpl(); /// Releases and removes the lock /// @brief Destructor - ~RWMutex(); + ~RWMutexImpl(); /// @} /// @name Methods @@ -74,38 +76,80 @@ namespace llvm /// @name Do Not Implement /// @{ private: - RWMutex(const RWMutex & original); - void operator=(const RWMutex &); + RWMutexImpl(const RWMutexImpl & original); + void operator=(const RWMutexImpl &); /// @} }; + /// SmartMutex - An R/W mutex with a compile time constant parameter that + /// indicates whether this mutex should become a no-op when we're not + /// running in multithreaded mode. + template<bool mt_only> + class SmartRWMutex : RWMutexImpl { + public: + explicit SmartRWMutex() : RWMutexImpl() { } + + bool reader_acquire() { + if (!mt_only && llvm_is_multithreaded()) + return RWMutexImpl::reader_acquire(); + return true; + } + + bool reader_release() { + if (!mt_only || llvm_is_multithreaded()) + return RWMutexImpl::reader_release(); + return true; + } + + bool writer_acquire() { + if (!mt_only || llvm_is_multithreaded()) + return RWMutexImpl::writer_acquire(); + return true; + } + + bool writer_release() { + if (!mt_only || llvm_is_multithreaded()) + return RWMutexImpl::writer_release(); + return true; + } + + private: + SmartRWMutex(const SmartRWMutex<mt_only> & original); + void operator=(const SmartRWMutex<mt_only> &); + }; + typedef SmartRWMutex<false> RWMutex; + /// ScopedReader - RAII acquisition of a reader lock - struct ScopedReader { - RWMutex* mutex; + template<bool mt_only> + struct SmartScopedReader { + SmartRWMutex<mt_only>* mutex; - explicit ScopedReader(RWMutex* m) { + explicit SmartScopedReader(SmartRWMutex<mt_only>* m) { mutex = m; mutex->reader_acquire(); } - ~ScopedReader() { + ~SmartScopedReader() { mutex->reader_release(); } }; + typedef SmartScopedReader<false> ScopedReader; /// ScopedWriter - RAII acquisition of a writer lock - struct ScopedWriter { - RWMutex* mutex; + template<bool mt_only> + struct SmartScopedWriter { + SmartRWMutex<mt_only>* mutex; - explicit ScopedWriter(RWMutex* m) { + explicit SmartScopedWriter(SmartRWMutex<mt_only>* m) { mutex = m; mutex->writer_acquire(); } - ~ScopedWriter() { + ~SmartScopedWriter() { mutex->writer_release(); } }; + typedef SmartScopedWriter<false> ScopedWriter; } } |