[llvm-commits] [llvm] r73709 - in /llvm/trunk: include/llvm/System/Mutex.h lib/System/Mutex.cpp lib/System/Unix/Mutex.inc lib/System/Win32/Mutex.inc

Owen Anderson resistor at mac.com
Thu Jun 18 10:53:17 PDT 2009


Author: resistor
Date: Thu Jun 18 12:53:17 2009
New Revision: 73709

URL: http://llvm.org/viewvc/llvm-project?rev=73709&view=rev
Log:
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<false>, to preserve source compatibility.

Modified:
    llvm/trunk/include/llvm/System/Mutex.h
    llvm/trunk/lib/System/Mutex.cpp
    llvm/trunk/lib/System/Unix/Mutex.inc
    llvm/trunk/lib/System/Win32/Mutex.inc

Modified: llvm/trunk/include/llvm/System/Mutex.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/System/Mutex.h?rev=73709&r1=73708&r2=73709&view=diff

==============================================================================
--- llvm/trunk/include/llvm/System/Mutex.h (original)
+++ llvm/trunk/include/llvm/System/Mutex.h Thu Jun 18 12:53:17 2009
@@ -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 @@
       /// 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 @@
     /// @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<bool mt_only>
+    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<mt_only>(const SmartMutex<mt_only> & original);
+        void operator=(const SmartMutex<mt_only> &);
+    };
+    
+    /// Mutex - A standard, always enforced mutex.
+    typedef SmartMutex<false> Mutex;
   }
 }
 

Modified: llvm/trunk/lib/System/Mutex.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Mutex.cpp?rev=73709&r1=73708&r2=73709&view=diff

==============================================================================
--- llvm/trunk/lib/System/Mutex.cpp (original)
+++ llvm/trunk/lib/System/Mutex.cpp Thu Jun 18 12:53:17 2009
@@ -23,11 +23,11 @@
 // Define all methods as no-ops if threading is explicitly disabled
 namespace llvm {
 using namespace sys;
-Mutex::Mutex( bool recursive) { }
-Mutex::~Mutex() { }
-bool Mutex::acquire() { return true; }
-bool Mutex::release() { return true; }
-bool Mutex::tryacquire() { return true; }
+MutexImpl::MutexImpl( bool recursive) { }
+MutexImpl::~MutexImpl() { }
+bool MutexImpl::acquire() { return true; }
+bool MutexImpl::release() { return true; }
+bool MutexImpl::tryacquire() { return true; }
 }
 #else
 
@@ -55,7 +55,7 @@
 static const bool pthread_enabled = true;
 
 // Construct a Mutex using pthread calls
-Mutex::Mutex( bool recursive)
+MutexImpl::MutexImpl( bool recursive)
   : data_(0)
 {
   if (pthread_enabled)
@@ -94,7 +94,7 @@
 }
 
 // Destruct a Mutex
-Mutex::~Mutex()
+MutexImpl::~MutexImpl()
 {
   if (pthread_enabled)
   {
@@ -106,7 +106,7 @@
 }
 
 bool
-Mutex::acquire()
+MutexImpl::acquire()
 {
   if (pthread_enabled)
   {
@@ -120,7 +120,7 @@
 }
 
 bool
-Mutex::release()
+MutexImpl::release()
 {
   if (pthread_enabled)
   {
@@ -134,7 +134,7 @@
 }
 
 bool
-Mutex::tryacquire()
+MutexImpl::tryacquire()
 {
   if (pthread_enabled)
   {

Modified: llvm/trunk/lib/System/Unix/Mutex.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Unix/Mutex.inc?rev=73709&r1=73708&r2=73709&view=diff

==============================================================================
--- llvm/trunk/lib/System/Unix/Mutex.inc (original)
+++ llvm/trunk/lib/System/Unix/Mutex.inc Thu Jun 18 12:53:17 2009
@@ -20,28 +20,28 @@
 {
 using namespace sys;
 
-Mutex::Mutex( bool recursive)
+MutexImpl::MutexImpl( bool recursive)
 {
 }
 
-Mutex::~Mutex()
+MutexImpl::~MutexImpl()
 {
 }
 
 bool 
-Mutex::acquire()
+MutexImpl::MutexImpl()
 {
   return true;
 }
 
 bool 
-Mutex::release()
+MutexImpl::release()
 {
   return true;
 }
 
 bool 
-Mutex::tryacquire( void )
+MutexImpl::tryacquire( void )
 {
   return true;
 }

Modified: llvm/trunk/lib/System/Win32/Mutex.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Win32/Mutex.inc?rev=73709&r1=73708&r2=73709&view=diff

==============================================================================
--- llvm/trunk/lib/System/Win32/Mutex.inc (original)
+++ llvm/trunk/lib/System/Win32/Mutex.inc Thu Jun 18 12:53:17 2009
@@ -22,13 +22,13 @@
 namespace llvm {
 using namespace sys;
 
-Mutex::Mutex(bool /*recursive*/)
+MutexImpl::MutexImpl(bool /*recursive*/)
 {
   data_ = new CRITICAL_SECTION;
   InitializeCriticalSection((LPCRITICAL_SECTION)data_);
 }
 
-Mutex::~Mutex()
+MutexImpl::~MutexImpl()
 {
   DeleteCriticalSection((LPCRITICAL_SECTION)data_);
   delete (LPCRITICAL_SECTION)data_;
@@ -36,21 +36,21 @@
 }
 
 bool 
-Mutex::acquire()
+MutexImpl::acquire()
 {
   EnterCriticalSection((LPCRITICAL_SECTION)data_);
   return true;
 }
 
 bool 
-Mutex::release()
+MutexImpl::release()
 {
   LeaveCriticalSection((LPCRITICAL_SECTION)data_);
   return true;
 }
 
 bool 
-Mutex::tryacquire()
+MutexImpl::tryacquire()
 {
   return TryEnterCriticalSection((LPCRITICAL_SECTION)data_);
 }





More information about the llvm-commits mailing list