Users of the llvm global mutex must now acquire it manually.
[oota-llvm.git] / include / llvm / Support / Mutex.h
1 //===- llvm/Support/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_SUPPORT_MUTEX_H
15 #define LLVM_SUPPORT_MUTEX_H
16
17 #include "llvm/Support/Compiler.h"
18 #include <cassert>
19
20 namespace llvm
21 {
22   // Forward declare.
23   bool llvm_is_multithreaded();
24
25   namespace sys
26   {
27     /// @brief Platform agnostic Mutex class.
28     class MutexImpl
29     {
30     /// @name Constructors
31     /// @{
32     public:
33
34       /// Initializes the lock but doesn't acquire it. if \p recursive is set
35       /// to false, the lock will not be recursive which makes it cheaper but
36       /// also more likely to deadlock (same thread can't acquire more than
37       /// once).
38       /// @brief Default Constructor.
39       explicit MutexImpl(bool recursive = true);
40
41       /// Releases and removes the lock
42       /// @brief Destructor
43       ~MutexImpl();
44
45     /// @}
46     /// @name Methods
47     /// @{
48     public:
49
50       /// Attempts to unconditionally acquire the lock. If the lock is held by
51       /// another thread, this method will wait until it can acquire the lock.
52       /// @returns false if any kind of error occurs, true otherwise.
53       /// @brief Unconditionally acquire the lock.
54       bool acquire();
55
56       /// Attempts to release the lock. If the lock is held by the current
57       /// thread, the lock is released allowing other threads to acquire the
58       /// lock.
59       /// @returns false if any kind of error occurs, true otherwise.
60       /// @brief Unconditionally release the lock.
61       bool release();
62
63       /// Attempts to acquire the lock without blocking. If the lock is not
64       /// available, this function returns false quickly (without blocking). If
65       /// the lock is available, it is acquired.
66       /// @returns false if any kind of error occurs or the lock is not
67       /// available, true otherwise.
68       /// @brief Try to acquire the lock.
69       bool tryacquire();
70
71     //@}
72     /// @name Platform Dependent Data
73     /// @{
74     private:
75       void* data_; ///< We don't know what the data will be
76
77     /// @}
78     /// @name Do Not Implement
79     /// @{
80     private:
81       MutexImpl(const MutexImpl &) LLVM_DELETED_FUNCTION;
82       void operator=(const MutexImpl &) LLVM_DELETED_FUNCTION;
83     /// @}
84     };
85
86
87     /// SmartMutex - A mutex with a compile time constant parameter that
88     /// indicates whether this mutex should become a no-op when we're not
89     /// running in multithreaded mode.
90     template<bool mt_only>
91     class SmartMutex : public MutexImpl {
92       unsigned acquired;
93       bool recursive;
94     public:
95       explicit SmartMutex(bool rec = true) :
96         MutexImpl(rec), acquired(0), recursive(rec) { }
97
98       bool acquire() {
99         if (!mt_only || llvm_is_multithreaded()) {
100           return MutexImpl::acquire();
101         } else {
102           // Single-threaded debugging code.  This would be racy in
103           // multithreaded mode, but provides not sanity checks in single
104           // threaded mode.
105           assert((recursive || acquired == 0) && "Lock already acquired!!");
106           ++acquired;
107           return true;
108         }
109       }
110
111       bool release() {
112         if (!mt_only || llvm_is_multithreaded()) {
113           return MutexImpl::release();
114         } else {
115           // Single-threaded debugging code.  This would be racy in
116           // multithreaded mode, but provides not sanity checks in single
117           // threaded mode.
118           assert(((recursive && acquired) || (acquired == 1)) &&
119                  "Lock not acquired before release!");
120           --acquired;
121           return true;
122         }
123       }
124
125       bool tryacquire() {
126         if (!mt_only || llvm_is_multithreaded())
127           return MutexImpl::tryacquire();
128         else return true;
129       }
130
131       private:
132         SmartMutex(const SmartMutex<mt_only> & original);
133         void operator=(const SmartMutex<mt_only> &);
134     };
135
136     /// Mutex - A standard, always enforced mutex.
137     typedef SmartMutex<false> Mutex;
138
139     template<bool mt_only>
140     class SmartScopedLock  {
141       SmartMutex<mt_only>& mtx;
142
143     public:
144       SmartScopedLock(SmartMutex<mt_only>& m) : mtx(m) {
145         mtx.acquire();
146       }
147
148       ~SmartScopedLock() {
149         mtx.release();
150       }
151     };
152
153     typedef SmartScopedLock<false> ScopedLock;
154   }
155 }
156
157 #endif