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