LLVM API Documentation

RWMutex.h

Go to the documentation of this file.
00001 //===- RWMutex.h - Reader/Writer Mutual Exclusion Lock ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the llvm::sys::RWMutex class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SYSTEM_RWMUTEX_H
00015 #define LLVM_SYSTEM_RWMUTEX_H
00016 
00017 #include "llvm/System/Threading.h"
00018 #include <cassert>
00019 
00020 namespace llvm
00021 {
00022   namespace sys
00023   {
00024     /// @brief Platform agnostic RWMutex class.
00025     class RWMutexImpl
00026     {
00027     /// @name Constructors
00028     /// @{
00029     public:
00030 
00031       /// Initializes the lock but doesn't acquire it.
00032       /// @brief Default Constructor.
00033       explicit RWMutexImpl();
00034 
00035       /// Releases and removes the lock
00036       /// @brief Destructor
00037       ~RWMutexImpl();
00038 
00039     /// @}
00040     /// @name Methods
00041     /// @{
00042     public:
00043 
00044       /// Attempts to unconditionally acquire the lock in reader mode. If the
00045       /// lock is held by a writer, this method will wait until it can acquire
00046       /// the lock. 
00047       /// @returns false if any kind of error occurs, true otherwise.
00048       /// @brief Unconditionally acquire the lock in reader mode.
00049       bool reader_acquire();
00050 
00051       /// Attempts to release the lock in reader mode.
00052       /// @returns false if any kind of error occurs, true otherwise.
00053       /// @brief Unconditionally release the lock in reader mode.
00054       bool reader_release();
00055 
00056       /// Attempts to unconditionally acquire the lock in reader mode. If the
00057       /// lock is held by any readers, this method will wait until it can
00058       /// acquire the lock. 
00059       /// @returns false if any kind of error occurs, true otherwise.
00060       /// @brief Unconditionally acquire the lock in writer mode.
00061       bool writer_acquire();
00062 
00063       /// Attempts to release the lock in writer mode.
00064       /// @returns false if any kind of error occurs, true otherwise.
00065       /// @brief Unconditionally release the lock in write mode.
00066       bool writer_release();
00067 
00068     //@}
00069     /// @name Platform Dependent Data
00070     /// @{
00071     private:
00072       void* data_; ///< We don't know what the data will be
00073 
00074     /// @}
00075     /// @name Do Not Implement
00076     /// @{
00077     private:
00078       RWMutexImpl(const RWMutexImpl & original);
00079       void operator=(const RWMutexImpl &);
00080     /// @}
00081     };
00082     
00083     /// SmartMutex - An R/W mutex with a compile time constant parameter that 
00084     /// indicates whether this mutex should become a no-op when we're not
00085     /// running in multithreaded mode.
00086     template<bool mt_only>
00087     class SmartRWMutex : public RWMutexImpl {
00088       unsigned readers, writers;
00089     public:
00090       explicit SmartRWMutex() : RWMutexImpl(), readers(0), writers(0) { }
00091       
00092       bool reader_acquire() {
00093         if (!mt_only || llvm_is_multithreaded())
00094           return RWMutexImpl::reader_acquire();
00095         
00096         // Single-threaded debugging code.  This would be racy in multithreaded
00097         // mode, but provides not sanity checks in single threaded mode.
00098         ++readers;
00099         return true;
00100       }
00101       
00102       bool reader_release() {
00103         if (!mt_only || llvm_is_multithreaded())
00104           return RWMutexImpl::reader_release();
00105         
00106         // Single-threaded debugging code.  This would be racy in multithreaded
00107         // mode, but provides not sanity checks in single threaded mode.
00108         assert(readers > 0 && "Reader lock not acquired before release!");
00109         --readers;
00110         return true;
00111       }
00112       
00113       bool writer_acquire() {
00114         if (!mt_only || llvm_is_multithreaded())
00115           return RWMutexImpl::writer_acquire();
00116         
00117         // Single-threaded debugging code.  This would be racy in multithreaded
00118         // mode, but provides not sanity checks in single threaded mode.
00119         assert(writers == 0 && "Writer lock already acquired!");
00120         ++writers;
00121         return true;
00122       }
00123       
00124       bool writer_release() {
00125         if (!mt_only || llvm_is_multithreaded())
00126           return RWMutexImpl::writer_release();
00127         
00128         // Single-threaded debugging code.  This would be racy in multithreaded
00129         // mode, but provides not sanity checks in single threaded mode.
00130         assert(writers == 1 && "Writer lock not acquired before release!");
00131         --writers;
00132         return true;
00133       }
00134       
00135     private:
00136       SmartRWMutex(const SmartRWMutex<mt_only> & original);
00137       void operator=(const SmartRWMutex<mt_only> &);
00138     };
00139     typedef SmartRWMutex<false> RWMutex;
00140     
00141     /// ScopedReader - RAII acquisition of a reader lock
00142     template<bool mt_only>
00143     struct SmartScopedReader {
00144       SmartRWMutex<mt_only>& mutex;
00145       
00146       explicit SmartScopedReader(SmartRWMutex<mt_only>& m) : mutex(m) {
00147         mutex.reader_acquire();
00148       }
00149       
00150       ~SmartScopedReader() {
00151         mutex.reader_release();
00152       }
00153     };
00154     typedef SmartScopedReader<false> ScopedReader;
00155     
00156     /// ScopedWriter - RAII acquisition of a writer lock
00157     template<bool mt_only>
00158     struct SmartScopedWriter {
00159       SmartRWMutex<mt_only>& mutex;
00160       
00161       explicit SmartScopedWriter(SmartRWMutex<mt_only>& m) : mutex(m) {
00162         mutex.writer_acquire();
00163       }
00164       
00165       ~SmartScopedWriter() {
00166         mutex.writer_release();
00167       }
00168     };
00169     typedef SmartScopedWriter<false> ScopedWriter;
00170   }
00171 }
00172 
00173 #endif