LLVM  4.0.0
RWMutex.h
Go to the documentation of this file.
1 //===- RWMutex.h - Reader/Writer Mutual Exclusion Lock ----------*- 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::RWMutex class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_RWMUTEX_H
15 #define LLVM_SUPPORT_RWMUTEX_H
16 
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Threading.h"
19 #include <cassert>
20 
21 namespace llvm {
22 namespace sys {
23 
24  /// @brief Platform agnostic RWMutex class.
26  {
27  /// @name Constructors
28  /// @{
29  public:
30 
31  /// Initializes the lock but doesn't acquire it.
32  /// @brief Default Constructor.
33  explicit RWMutexImpl();
34 
35  /// Releases and removes the lock
36  /// @brief Destructor
37  ~RWMutexImpl();
38 
39  /// @}
40  /// @name Methods
41  /// @{
42  public:
43 
44  /// Attempts to unconditionally acquire the lock in reader mode. If the
45  /// lock is held by a writer, this method will wait until it can acquire
46  /// the lock.
47  /// @returns false if any kind of error occurs, true otherwise.
48  /// @brief Unconditionally acquire the lock in reader mode.
49  bool reader_acquire();
50 
51  /// Attempts to release the lock in reader mode.
52  /// @returns false if any kind of error occurs, true otherwise.
53  /// @brief Unconditionally release the lock in reader mode.
54  bool reader_release();
55 
56  /// Attempts to unconditionally acquire the lock in reader mode. If the
57  /// lock is held by any readers, this method will wait until it can
58  /// acquire the lock.
59  /// @returns false if any kind of error occurs, true otherwise.
60  /// @brief Unconditionally acquire the lock in writer mode.
61  bool writer_acquire();
62 
63  /// Attempts to release the lock in writer mode.
64  /// @returns false if any kind of error occurs, true otherwise.
65  /// @brief Unconditionally release the lock in write mode.
66  bool writer_release();
67 
68  //@}
69  /// @name Platform Dependent Data
70  /// @{
71  private:
72 #if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0
73  void* data_; ///< We don't know what the data will be
74 #endif
75 
76  /// @}
77  /// @name Do Not Implement
78  /// @{
79  private:
80  RWMutexImpl(const RWMutexImpl & original) = delete;
81  void operator=(const RWMutexImpl &) = delete;
82  /// @}
83  };
84 
85  /// SmartMutex - An R/W mutex with a compile time constant parameter that
86  /// indicates whether this mutex should become a no-op when we're not
87  /// running in multithreaded mode.
88  template<bool mt_only>
89  class SmartRWMutex {
90  RWMutexImpl impl;
91  unsigned readers = 0;
92  unsigned writers = 0;
93 
94  public:
95  explicit SmartRWMutex() = default;
96 
97  bool lock_shared() {
98  if (!mt_only || llvm_is_multithreaded())
99  return impl.reader_acquire();
100 
101  // Single-threaded debugging code. This would be racy in multithreaded
102  // mode, but provides not sanity checks in single threaded mode.
103  ++readers;
104  return true;
105  }
106 
107  bool unlock_shared() {
108  if (!mt_only || llvm_is_multithreaded())
109  return impl.reader_release();
110 
111  // Single-threaded debugging code. This would be racy in multithreaded
112  // mode, but provides not sanity checks in single threaded mode.
113  assert(readers > 0 && "Reader lock not acquired before release!");
114  --readers;
115  return true;
116  }
117 
118  bool lock() {
119  if (!mt_only || llvm_is_multithreaded())
120  return impl.writer_acquire();
121 
122  // Single-threaded debugging code. This would be racy in multithreaded
123  // mode, but provides not sanity checks in single threaded mode.
124  assert(writers == 0 && "Writer lock already acquired!");
125  ++writers;
126  return true;
127  }
128 
129  bool unlock() {
130  if (!mt_only || llvm_is_multithreaded())
131  return impl.writer_release();
132 
133  // Single-threaded debugging code. This would be racy in multithreaded
134  // mode, but provides not sanity checks in single threaded mode.
135  assert(writers == 1 && "Writer lock not acquired before release!");
136  --writers;
137  return true;
138  }
139 
140  private:
141  SmartRWMutex(const SmartRWMutex<mt_only> & original);
142  void operator=(const SmartRWMutex<mt_only> &);
143  };
144 
146 
147  /// ScopedReader - RAII acquisition of a reader lock
148  template<bool mt_only>
151 
153  mutex.lock_shared();
154  }
155 
157  mutex.unlock_shared();
158  }
159  };
160 
162 
163  /// ScopedWriter - RAII acquisition of a writer lock
164  template<bool mt_only>
167 
169  mutex.lock();
170  }
171 
173  mutex.unlock();
174  }
175  };
176 
178 
179 } // end namespace sys
180 } // end namespace llvm
181 
182 #endif // LLVM_SUPPORT_RWMUTEX_H
SmartRWMutex< mt_only > & mutex
Definition: RWMutex.h:166
~RWMutexImpl()
Releases and removes the lock.
Definition: RWMutex.cpp:68
SmartRWMutex< false > RWMutex
Definition: RWMutex.h:145
SmartMutex - An R/W mutex with a compile time constant parameter that indicates whether this mutex sh...
Definition: RWMutex.h:89
bool writer_release()
Attempts to release the lock in writer mode.
Definition: RWMutex.cpp:107
bool writer_acquire()
Attempts to unconditionally acquire the lock in reader mode.
Definition: RWMutex.cpp:97
ScopedReader - RAII acquisition of a reader lock.
Definition: RWMutex.h:149
ScopedWriter - RAII acquisition of a writer lock.
Definition: RWMutex.h:165
bool reader_acquire()
Attempts to unconditionally acquire the lock in reader mode.
Definition: RWMutex.cpp:77
RWMutexImpl()
Initializes the lock but doesn't acquire it.
Definition: RWMutex.cpp:46
Platform agnostic RWMutex class.
Definition: RWMutex.h:25
bool llvm_is_multithreaded()
Returns true if LLVM is compiled with support for multi-threading, and false otherwise.
Definition: Threading.cpp:25
SmartRWMutex< mt_only > & mutex
Definition: RWMutex.h:150
SmartScopedReader< false > ScopedReader
Definition: RWMutex.h:161
SmartScopedReader(SmartRWMutex< mt_only > &m)
Definition: RWMutex.h:152
SmartScopedWriter< false > ScopedWriter
Definition: RWMutex.h:177
SmartScopedWriter(SmartRWMutex< mt_only > &m)
Definition: RWMutex.h:168
bool reader_release()
Attempts to release the lock in reader mode.
Definition: RWMutex.cpp:87
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())