Fix a major typo.
[oota-llvm.git] / include / llvm / System / Mutex.h
1 //===- llvm/System/Mutex.h - Mutex Operating System Concept -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the llvm::sys::Mutex class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SYSTEM_MUTEX_H
15 #define LLVM_SYSTEM_MUTEX_H
16
17 #include "llvm/System/Threading.h"
18
19 namespace llvm
20 {
21   namespace sys
22   {
23     /// @brief Platform agnostic Mutex class.
24     class MutexImpl
25     {
26     /// @name Constructors
27     /// @{
28     public:
29
30       /// Initializes the lock but doesn't acquire it. if \p recursive is set
31       /// to false, the lock will not be recursive which makes it cheaper but
32       /// also more likely to deadlock (same thread can't acquire more than
33       /// once).
34       /// @brief Default Constructor.
35       explicit MutexImpl(bool recursive = true);
36
37       /// Releases and removes the lock
38       /// @brief Destructor
39       ~MutexImpl();
40
41     /// @}
42     /// @name Methods
43     /// @{
44     public:
45
46       /// Attempts to unconditionally acquire the lock. If the lock is held by
47       /// another thread, this method will wait until it can acquire the lock.
48       /// @returns false if any kind of error occurs, true otherwise.
49       /// @brief Unconditionally acquire the lock.
50       bool acquire();
51
52       /// Attempts to release the lock. If the lock is held by the current
53       /// thread, the lock is released allowing other threads to acquire the
54       /// lock.
55       /// @returns false if any kind of error occurs, true otherwise.
56       /// @brief Unconditionally release the lock.
57       bool release();
58
59       /// Attempts to acquire the lock without blocking. If the lock is not
60       /// available, this function returns false quickly (without blocking). If
61       /// the lock is available, it is acquired.
62       /// @returns false if any kind of error occurs or the lock is not
63       /// available, true otherwise.
64       /// @brief Try to acquire the lock.
65       bool tryacquire();
66
67     //@}
68     /// @name Platform Dependent Data
69     /// @{
70     private:
71       void* data_; ///< We don't know what the data will be
72
73     /// @}
74     /// @name Do Not Implement
75     /// @{
76     private:
77       MutexImpl(const MutexImpl & original);
78       void operator=(const MutexImpl &);
79     /// @}
80     };
81     
82     
83     /// SmartMutex - A mutex with a compile time constant parameter that 
84     /// indicates whether this mutex should become a no-op when we're not
85     /// running in multithreaded mode.
86     template<bool mt_only>
87     class SmartMutex : public MutexImpl {
88     public:
89       explicit SmartMutex(bool recursive = true) : MutexImpl(recursive) { }
90       
91       bool acquire() {
92         if (!mt_only || llvm_is_multithreaded())
93           return MutexImpl::acquire();
94         return true;
95       }
96
97       bool release() {
98         if (!mt_only || llvm_is_multithreaded())
99           return MutexImpl::release();
100         return true;
101       }
102
103       bool tryacquire() {
104         if (!mt_only || llvm_is_multithreaded())
105           return MutexImpl::tryacquire();
106         return true;
107       }
108       
109       private:
110         SmartMutex(const SmartMutex<mt_only> & original);
111         void operator=(const SmartMutex<mt_only> &);
112     };
113     
114     /// Mutex - A standard, always enforced mutex.
115     typedef SmartMutex<false> Mutex;
116     
117     template<bool mt_only>
118     class SmartScopedLock  {
119       SmartMutex<mt_only>* mtx;
120       
121     public:
122       SmartScopedLock(SmartMutex<mt_only>* m) : mtx(m) {
123         mtx->acquire();
124       }
125       
126       ~SmartScopedLock() {
127         mtx->release();
128       }
129     };
130     
131     typedef SmartScopedLock<false> ScopedLock;
132   }
133 }
134
135 #endif