Give RWMutex the SmartRWMutex treatment too.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73710 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Owen Anderson 2009-06-18 18:26:15 +00:00
parent b849a4dd4b
commit b65e9ed106
4 changed files with 83 additions and 39 deletions

View File

@ -14,12 +14,14 @@
#ifndef LLVM_SYSTEM_RWMUTEX_H #ifndef LLVM_SYSTEM_RWMUTEX_H
#define LLVM_SYSTEM_RWMUTEX_H #define LLVM_SYSTEM_RWMUTEX_H
#include "llvm/System/Threading.h"
namespace llvm namespace llvm
{ {
namespace sys namespace sys
{ {
/// @brief Platform agnostic Mutex class. /// @brief Platform agnostic RWMutex class.
class RWMutex class RWMutexImpl
{ {
/// @name Constructors /// @name Constructors
/// @{ /// @{
@ -27,11 +29,11 @@ namespace llvm
/// Initializes the lock but doesn't acquire it. /// Initializes the lock but doesn't acquire it.
/// @brief Default Constructor. /// @brief Default Constructor.
explicit RWMutex(); explicit RWMutexImpl();
/// Releases and removes the lock /// Releases and removes the lock
/// @brief Destructor /// @brief Destructor
~RWMutex(); ~RWMutexImpl();
/// @} /// @}
/// @name Methods /// @name Methods
@ -74,38 +76,80 @@ namespace llvm
/// @name Do Not Implement /// @name Do Not Implement
/// @{ /// @{
private: private:
RWMutex(const RWMutex & original); RWMutexImpl(const RWMutexImpl & original);
void operator=(const RWMutex &); void operator=(const RWMutexImpl &);
/// @} /// @}
}; };
/// ScopedReader - RAII acquisition of a reader lock /// SmartMutex - An R/W mutex with a compile time constant parameter that
struct ScopedReader { /// indicates whether this mutex should become a no-op when we're not
RWMutex* mutex; /// running in multithreaded mode.
template<bool mt_only>
class SmartRWMutex : RWMutexImpl {
public:
explicit SmartRWMutex() : RWMutexImpl() { }
explicit ScopedReader(RWMutex* m) { 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
template<bool mt_only>
struct SmartScopedReader {
SmartRWMutex<mt_only>* mutex;
explicit SmartScopedReader(SmartRWMutex<mt_only>* m) {
mutex = m; mutex = m;
mutex->reader_acquire(); mutex->reader_acquire();
} }
~ScopedReader() { ~SmartScopedReader() {
mutex->reader_release(); mutex->reader_release();
} }
}; };
typedef SmartScopedReader<false> ScopedReader;
/// ScopedWriter - RAII acquisition of a writer lock /// ScopedWriter - RAII acquisition of a writer lock
struct ScopedWriter { template<bool mt_only>
RWMutex* mutex; struct SmartScopedWriter {
SmartRWMutex<mt_only>* mutex;
explicit ScopedWriter(RWMutex* m) { explicit SmartScopedWriter(SmartRWMutex<mt_only>* m) {
mutex = m; mutex = m;
mutex->writer_acquire(); mutex->writer_acquire();
} }
~ScopedWriter() { ~SmartScopedWriter() {
mutex->writer_release(); mutex->writer_release();
} }
}; };
typedef SmartScopedWriter<false> ScopedWriter;
} }
} }

View File

@ -23,12 +23,12 @@
// Define all methods as no-ops if threading is explicitly disabled // Define all methods as no-ops if threading is explicitly disabled
namespace llvm { namespace llvm {
using namespace sys; using namespace sys;
RWMutex::RWMutex() { } RWMutexImpl::RWMutexImpl() { }
RWMutex::~RWMutex() { } RWMutexImpl::~RWMutexImpl() { }
bool RWMutex::reader_acquire() { return true; } bool RWMutexImpl::reader_acquire() { return true; }
bool RWMutex::reader_release() { return true; } bool RWMutexImpl::reader_release() { return true; }
bool RWMutex::writer_acquire() { return true; } bool RWMutexImpl::writer_acquire() { return true; }
bool RWMutex::writer_release() { return true; } bool RWMutexImpl::writer_release() { return true; }
} }
#else #else
@ -56,7 +56,7 @@ using namespace sys;
static const bool pthread_enabled = true; static const bool pthread_enabled = true;
// Construct a RWMutex using pthread calls // Construct a RWMutex using pthread calls
RWMutex::RWMutex() RWMutexImpl::RWMutexImpl()
: data_(0) : data_(0)
{ {
if (pthread_enabled) if (pthread_enabled)
@ -89,7 +89,7 @@ RWMutex::RWMutex()
} }
// Destruct a RWMutex // Destruct a RWMutex
RWMutex::~RWMutex() RWMutexImpl::~RWMutexImpl()
{ {
if (pthread_enabled) if (pthread_enabled)
{ {
@ -101,7 +101,7 @@ RWMutex::~RWMutex()
} }
bool bool
RWMutex::reader_acquire() RWMutexImpl::reader_acquire()
{ {
if (pthread_enabled) if (pthread_enabled)
{ {
@ -115,7 +115,7 @@ RWMutex::reader_acquire()
} }
bool bool
RWMutex::reader_release() RWMutexImpl::reader_release()
{ {
if (pthread_enabled) if (pthread_enabled)
{ {
@ -129,7 +129,7 @@ RWMutex::reader_release()
} }
bool bool
RWMutex::writer_acquire() RWMutexImpl::writer_acquire()
{ {
if (pthread_enabled) if (pthread_enabled)
{ {
@ -143,7 +143,7 @@ RWMutex::writer_acquire()
} }
bool bool
RWMutex::writer_release() RWMutexImpl::writer_release()
{ {
if (pthread_enabled) if (pthread_enabled)
{ {

View File

@ -20,23 +20,23 @@ namespace llvm {
using namespace sys; using namespace sys;
RWMutex::RWMutex() { } RWMutexImpl::RWMutexImpl() { }
RWMutex::~RWMutex() { } RWMutexImpl::~RWMutexImpl() { }
bool RWMutex::reader_acquire() { bool RWMutexImpl::reader_acquire() {
return true; return true;
} }
bool RWMutex::reader_release() { bool RWMutexImpl::reader_release() {
return true; return true;
} }
bool RWMutex::writer_acquire() { bool RWMutexImpl::writer_acquire() {
return true; return true;
} }
bool RWMutex::writer_release() { bool RWMutexImpl::writer_release() {
return true; return true;
} }

View File

@ -24,32 +24,32 @@
namespace llvm { namespace llvm {
using namespace sys; using namespace sys;
RWMutex::RWMutex() { RWMutexImpl::RWMutexImpl() {
data_ = calloc(1, sizeof(CRITICAL_SECTION)); data_ = calloc(1, sizeof(CRITICAL_SECTION));
InitializeCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); InitializeCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
} }
RWMutex::~RWMutex() { RWMutexImpl::~RWMutexImpl() {
DeleteCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); DeleteCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
free(data_); free(data_);
} }
bool RWMutex::reader_acquire() { bool RWMutexImpl::reader_acquire() {
EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
return true; return true;
} }
bool RWMutex::reader_release() { bool RWMutexImpl::reader_release() {
LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
return true; return true;
} }
bool RWMutex::writer_acquire() { bool RWMutexImpl::writer_acquire() {
EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
return true; return true;
} }
bool RWMutex::writer_release() { bool RWMutexImpl::writer_release() {
LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
return true; return true;
} }