From b849a4dd4bee9ad17e295691087ce09e8d77d685 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Thu, 18 Jun 2009 17:53:17 +0000 Subject: Insert a SmartMutex templated class into the class hierarchy, which takes a template parameter specifying whether this mutex should become a no-op when not running in multithreaded mode. Make sys::Mutex a typedef of SmartMutex, to preserve source compatibility. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73709 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/System/Mutex.h | 48 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/llvm/System/Mutex.h b/include/llvm/System/Mutex.h index 4f38493..f749c5d 100644 --- a/include/llvm/System/Mutex.h +++ b/include/llvm/System/Mutex.h @@ -14,12 +14,14 @@ #ifndef LLVM_SYSTEM_MUTEX_H #define LLVM_SYSTEM_MUTEX_H +#include "llvm/System/Threading.h" + namespace llvm { namespace sys { /// @brief Platform agnostic Mutex class. - class Mutex + class MutexImpl { /// @name Constructors /// @{ @@ -30,11 +32,11 @@ namespace llvm /// also more likely to deadlock (same thread can't acquire more than /// once). /// @brief Default Constructor. - explicit Mutex(bool recursive = true); + explicit MutexImpl(bool recursive = true); /// Releases and removes the lock /// @brief Destructor - ~Mutex(); + ~MutexImpl(); /// @} /// @name Methods @@ -74,10 +76,46 @@ namespace llvm /// @name Do Not Implement /// @{ private: - Mutex(const Mutex & original); - void operator=(const Mutex &); + MutexImpl(const MutexImpl & original); + void operator=(const MutexImpl &); /// @} }; + + + /// SmartMutex - A 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 + class SmartMutex { + MutexImpl mtx; + public: + explicit SmartMutex(bool recursive = true) : mtx(recursive) { } + + bool acquire() { + if (!mt_only || (mt_only && llvm_is_multithreaded())) + return mtx.acquire(); + return true; + } + + bool release() { + if (!mt_only || (mt_only && llvm_is_multithreaded())) + return mtx.release(); + return true; + } + + bool tryacquire() { + if (!mt_only || (mt_only && llvm_is_multithreaded())) + return mtx.tryacquire(); + return true; + } + + private: + SmartMutex(const SmartMutex & original); + void operator=(const SmartMutex &); + }; + + /// Mutex - A standard, always enforced mutex. + typedef SmartMutex Mutex; } } -- cgit v1.1