[llvm-commits] [llvm] r73710 - in /llvm/trunk: include/llvm/System/RWMutex.h lib/System/RWMutex.cpp lib/System/Unix/RWMutex.inc lib/System/Win32/RWMutex.inc
Owen Anderson
resistor at mac.com
Thu Jun 18 11:26:19 PDT 2009
Author: resistor
Date: Thu Jun 18 13:26:15 2009
New Revision: 73710
URL: http://llvm.org/viewvc/llvm-project?rev=73710&view=rev
Log:
Give RWMutex the SmartRWMutex treatment too.
Modified:
llvm/trunk/include/llvm/System/RWMutex.h
llvm/trunk/lib/System/RWMutex.cpp
llvm/trunk/lib/System/Unix/RWMutex.inc
llvm/trunk/lib/System/Win32/RWMutex.inc
Modified: llvm/trunk/include/llvm/System/RWMutex.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/System/RWMutex.h?rev=73710&r1=73709&r2=73710&view=diff
==============================================================================
--- llvm/trunk/include/llvm/System/RWMutex.h (original)
+++ llvm/trunk/include/llvm/System/RWMutex.h Thu Jun 18 13:26:15 2009
@@ -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 @@
/// 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 @@
/// @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;
}
}
Modified: llvm/trunk/lib/System/RWMutex.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/RWMutex.cpp?rev=73710&r1=73709&r2=73710&view=diff
==============================================================================
--- llvm/trunk/lib/System/RWMutex.cpp (original)
+++ llvm/trunk/lib/System/RWMutex.cpp Thu Jun 18 13:26:15 2009
@@ -23,12 +23,12 @@
// Define all methods as no-ops if threading is explicitly disabled
namespace llvm {
using namespace sys;
-RWMutex::RWMutex() { }
-RWMutex::~RWMutex() { }
-bool RWMutex::reader_acquire() { return true; }
-bool RWMutex::reader_release() { return true; }
-bool RWMutex::writer_acquire() { return true; }
-bool RWMutex::writer_release() { return true; }
+RWMutexImpl::RWMutexImpl() { }
+RWMutexImpl::~RWMutexImpl() { }
+bool RWMutexImpl::reader_acquire() { return true; }
+bool RWMutexImpl::reader_release() { return true; }
+bool RWMutexImpl::writer_acquire() { return true; }
+bool RWMutexImpl::writer_release() { return true; }
}
#else
@@ -56,7 +56,7 @@
static const bool pthread_enabled = true;
// Construct a RWMutex using pthread calls
-RWMutex::RWMutex()
+RWMutexImpl::RWMutexImpl()
: data_(0)
{
if (pthread_enabled)
@@ -89,7 +89,7 @@
}
// Destruct a RWMutex
-RWMutex::~RWMutex()
+RWMutexImpl::~RWMutexImpl()
{
if (pthread_enabled)
{
@@ -101,7 +101,7 @@
}
bool
-RWMutex::reader_acquire()
+RWMutexImpl::reader_acquire()
{
if (pthread_enabled)
{
@@ -115,7 +115,7 @@
}
bool
-RWMutex::reader_release()
+RWMutexImpl::reader_release()
{
if (pthread_enabled)
{
@@ -129,7 +129,7 @@
}
bool
-RWMutex::writer_acquire()
+RWMutexImpl::writer_acquire()
{
if (pthread_enabled)
{
@@ -143,7 +143,7 @@
}
bool
-RWMutex::writer_release()
+RWMutexImpl::writer_release()
{
if (pthread_enabled)
{
Modified: llvm/trunk/lib/System/Unix/RWMutex.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Unix/RWMutex.inc?rev=73710&r1=73709&r2=73710&view=diff
==============================================================================
--- llvm/trunk/lib/System/Unix/RWMutex.inc (original)
+++ llvm/trunk/lib/System/Unix/RWMutex.inc Thu Jun 18 13:26:15 2009
@@ -20,23 +20,23 @@
using namespace sys;
-RWMutex::RWMutex() { }
+RWMutexImpl::RWMutexImpl() { }
-RWMutex::~RWMutex() { }
+RWMutexImpl::~RWMutexImpl() { }
-bool RWMutex::reader_acquire() {
+bool RWMutexImpl::reader_acquire() {
return true;
}
-bool RWMutex::reader_release() {
+bool RWMutexImpl::reader_release() {
return true;
}
-bool RWMutex::writer_acquire() {
+bool RWMutexImpl::writer_acquire() {
return true;
}
-bool RWMutex::writer_release() {
+bool RWMutexImpl::writer_release() {
return true;
}
Modified: llvm/trunk/lib/System/Win32/RWMutex.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Win32/RWMutex.inc?rev=73710&r1=73709&r2=73710&view=diff
==============================================================================
--- llvm/trunk/lib/System/Win32/RWMutex.inc (original)
+++ llvm/trunk/lib/System/Win32/RWMutex.inc Thu Jun 18 13:26:15 2009
@@ -24,32 +24,32 @@
namespace llvm {
using namespace sys;
-RWMutex::RWMutex() {
+RWMutexImpl::RWMutexImpl() {
data_ = calloc(1, sizeof(CRITICAL_SECTION));
InitializeCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
}
-RWMutex::~RWMutex() {
+RWMutexImpl::~RWMutexImpl() {
DeleteCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
free(data_);
}
-bool RWMutex::reader_acquire() {
+bool RWMutexImpl::reader_acquire() {
EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
return true;
}
-bool RWMutex::reader_release() {
+bool RWMutexImpl::reader_release() {
LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
return true;
}
-bool RWMutex::writer_acquire() {
+bool RWMutexImpl::writer_acquire() {
EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
return true;
}
-bool RWMutex::writer_release() {
+bool RWMutexImpl::writer_release() {
LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
return true;
}
More information about the llvm-commits
mailing list