Index: include/llvm/Support/RWMutex.h =================================================================== --- include/llvm/Support/RWMutex.h +++ include/llvm/Support/RWMutex.h @@ -142,35 +142,76 @@ }; typedef SmartRWMutex RWMutex; - /// ScopedReader - RAII acquisition of a reader lock - template - struct SmartScopedReader { - SmartRWMutex& mutex; + /// SmartReader - Generic variant that allows customized + /// lock/unlock policy + template + class GenericSmartReader { + SmartRWMutex& Mutex; + bool IsLocked; + + public: + explicit GenericSmartReader(SmartRWMutex& m) : + Mutex(m), IsLocked(false) { + if(auto_lock) lock(); + } - explicit SmartScopedReader(SmartRWMutex& m) : mutex(m) { - mutex.lock_shared(); + void lock() { + if(!IsLocked){ + Mutex.lock_shared(); + IsLocked = true; + } } - ~SmartScopedReader() { - mutex.unlock_shared(); + void unlock() { + if(IsLocked) Mutex.unlock_shared(); + IsLocked = false; } + + ~GenericSmartReader() { unlock(); } }; - typedef SmartScopedReader ScopedReader; + template + using SmartReader = GenericSmartReader; - /// ScopedWriter - RAII acquisition of a writer lock - template - struct SmartScopedWriter { - SmartRWMutex& mutex; + /// SmartWriter - Generic variant that allows customized + /// lock/unlock policy + template + class GenericSmartWriter { + SmartRWMutex& Mutex; + bool IsLocked; - explicit SmartScopedWriter(SmartRWMutex& m) : mutex(m) { - mutex.lock(); + public: + explicit GenericSmartWriter(SmartRWMutex& m) : + Mutex(m), IsLocked(false){ + if(auto_lock) lock(); } - ~SmartScopedWriter() { - mutex.unlock(); + void lock(){ + if(!IsLocked) { + Mutex.lock(); + IsLocked = true; + } } + + void unlock() { + if(IsLocked) Mutex.unlock(); + IsLocked = false; + } + + ~GenericSmartWriter() { unlock(); } }; - typedef SmartScopedWriter ScopedWriter; + template + using SmartWriter = GenericSmartWriter; + + /// ScopedReader - RAII acquisition of a reader lock + template + using SmartScopedReader = GenericSmartReader; + using ScopedReader = SmartScopedReader; + + /// ScopedWriter - RAII acquisition of a writer lock + template + using SmartScopedWriter = GenericSmartWriter; + using ScopedWriter = SmartScopedWriter; + } }