LLVM API Documentation

Mutex.h

Go to the documentation of this file.
00001 //===- llvm/System/Mutex.h - Mutex Operating System Concept -----*- 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::Mutex class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SYSTEM_MUTEX_H
00015 #define LLVM_SYSTEM_MUTEX_H
00016 
00017 #include "llvm/System/Threading.h"
00018 #include <cassert>
00019 
00020 namespace llvm
00021 {
00022   namespace sys
00023   {
00024     /// @brief Platform agnostic Mutex class.
00025     class MutexImpl
00026     {
00027     /// @name Constructors
00028     /// @{
00029     public:
00030 
00031       /// Initializes the lock but doesn't acquire it. if \p recursive is set
00032       /// to false, the lock will not be recursive which makes it cheaper but
00033       /// also more likely to deadlock (same thread can't acquire more than
00034       /// once).
00035       /// @brief Default Constructor.
00036       explicit MutexImpl(bool recursive = true);
00037 
00038       /// Releases and removes the lock
00039       /// @brief Destructor
00040       ~MutexImpl();
00041 
00042     /// @}
00043     /// @name Methods
00044     /// @{
00045     public:
00046 
00047       /// Attempts to unconditionally acquire the lock. If the lock is held by
00048       /// another thread, this method will wait until it can acquire the lock.
00049       /// @returns false if any kind of error occurs, true otherwise.
00050       /// @brief Unconditionally acquire the lock.
00051       bool acquire();
00052 
00053       /// Attempts to release the lock. If the lock is held by the current
00054       /// thread, the lock is released allowing other threads to acquire the
00055       /// lock.
00056       /// @returns false if any kind of error occurs, true otherwise.
00057       /// @brief Unconditionally release the lock.
00058       bool release();
00059 
00060       /// Attempts to acquire the lock without blocking. If the lock is not
00061       /// available, this function returns false quickly (without blocking). If
00062       /// the lock is available, it is acquired.
00063       /// @returns false if any kind of error occurs or the lock is not
00064       /// available, true otherwise.
00065       /// @brief Try to acquire the lock.
00066       bool tryacquire();
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       MutexImpl(const MutexImpl & original);
00079       void operator=(const MutexImpl &);
00080     /// @}
00081     };
00082     
00083     
00084     /// SmartMutex - A mutex with a compile time constant parameter that 
00085     /// indicates whether this mutex should become a no-op when we're not
00086     /// running in multithreaded mode.
00087     template<bool mt_only>
00088     class SmartMutex : public MutexImpl {
00089       unsigned acquired;
00090       bool recursive;
00091     public:
00092       explicit SmartMutex(bool rec = true) :
00093         MutexImpl(rec), acquired(0), recursive(rec) { }
00094       
00095       bool acquire() {
00096         if (!mt_only || llvm_is_multithreaded())
00097           return MutexImpl::acquire();
00098         
00099         // Single-threaded debugging code.  This would be racy in multithreaded
00100         // mode, but provides not sanity checks in single threaded mode.
00101         assert((recursive || acquired == 0) && "Lock already acquired!!");
00102         ++acquired;
00103         return true;
00104       }
00105 
00106       bool release() {
00107         if (!mt_only || llvm_is_multithreaded())
00108           return MutexImpl::release();
00109         
00110         // Single-threaded debugging code.  This would be racy in multithreaded
00111         // mode, but provides not sanity checks in single threaded mode.
00112         assert(((recursive && acquired) || (acquired == 1)) &&
00113                "Lock not acquired before release!");
00114         --acquired;
00115         return true;
00116       }
00117 
00118       bool tryacquire() {
00119         if (!mt_only || llvm_is_multithreaded())
00120           return MutexImpl::tryacquire();
00121         return true;
00122       }
00123       
00124       private:
00125         SmartMutex(const SmartMutex<mt_only> & original);
00126         void operator=(const SmartMutex<mt_only> &);
00127     };
00128     
00129     /// Mutex - A standard, always enforced mutex.
00130     typedef SmartMutex<false> Mutex;
00131     
00132     template<bool mt_only>
00133     class SmartScopedLock  {
00134       SmartMutex<mt_only>& mtx;
00135       
00136     public:
00137       SmartScopedLock(SmartMutex<mt_only>& m) : mtx(m) {
00138         mtx.acquire();
00139       }
00140       
00141       ~SmartScopedLock() {
00142         mtx.release();
00143       }
00144     };
00145     
00146     typedef SmartScopedLock<false> ScopedLock;
00147   }
00148 }
00149 
00150 #endif