llvm.org GIT mirror llvm / d930a30
Support: make LLVM Mutexes STL-compatible Use lock/unlock() convention instead of acquire/release(). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216336 91177308-0d34-0410-b5e6-96231b3b80d8 Dylan Noblesmith 6 years ago
7 changed file(s) with 42 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
110110
111111 void clear() { Map.clear(); }
112112
113 /// Return 1 if the specified key is in the map, 0 otherwise.
113 /// Return 1 if the specified key is in the map, 0 otherwise.
114114 size_type count(const KeyT &Val) const {
115115 return Map.find_as(Val) == Map.end() ? 0 : 1;
116116 }
216216 ValueMapCallbackVH Copy(*this);
217217 typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
218218 if (M)
219 M->acquire();
219 M->lock();
220220 Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this.
221221 Copy.Map->Map.erase(Copy); // Definitely destroys *this.
222222 if (M)
223 M->release();
223 M->unlock();
224224 }
225225 void allUsesReplacedWith(Value *new_key) override {
226226 assert(isa(new_key) &&
229229 ValueMapCallbackVH Copy(*this);
230230 typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
231231 if (M)
232 M->acquire();
232 M->lock();
233233
234234 KeyT typed_new_key = cast(new_key);
235235 // Can destroy *this:
245245 }
246246 }
247247 if (M)
248 M->release();
248 M->unlock();
249249 }
250250 };
251251
8585 /// indicates whether this mutex should become a no-op when we're not
8686 /// running in multithreaded mode.
8787 template
88 class SmartMutex : public MutexImpl {
88 class SmartMutex {
89 MutexImpl impl;
8990 unsigned acquired;
9091 bool recursive;
9192 public:
9293 explicit SmartMutex(bool rec = true) :
93 MutexImpl(rec), acquired(0), recursive(rec) { }
94 impl(rec), acquired(0), recursive(rec) { }
9495
95 bool acquire() {
96 bool lock() {
9697 if (!mt_only || llvm_is_multithreaded()) {
97 return MutexImpl::acquire();
98 return impl.acquire();
9899 } else {
99100 // Single-threaded debugging code. This would be racy in
100101 // multithreaded mode, but provides not sanity checks in single
105106 }
106107 }
107108
108 bool release() {
109 bool unlock() {
109110 if (!mt_only || llvm_is_multithreaded()) {
110 return MutexImpl::release();
111 return impl.release();
111112 } else {
112113 // Single-threaded debugging code. This would be racy in
113114 // multithreaded mode, but provides not sanity checks in single
119120 }
120121 }
121122
122 bool tryacquire() {
123 bool try_lock() {
123124 if (!mt_only || llvm_is_multithreaded())
124 return MutexImpl::tryacquire();
125 return impl.tryacquire();
125126 else return true;
126127 }
127128
139140
140141 public:
141142 SmartScopedLock(SmartMutex& m) : mtx(m) {
142 mtx.acquire();
143 mtx.lock();
143144 }
144145
145146 ~SmartScopedLock() {
146 mtx.release();
147 mtx.unlock();
147148 }
148149 };
149150
2828 MutexGuard(const MutexGuard &) LLVM_DELETED_FUNCTION;
2929 void operator=(const MutexGuard &) LLVM_DELETED_FUNCTION;
3030 public:
31 MutexGuard(sys::Mutex &m) : M(m) { M.acquire(); }
32 ~MutexGuard() { M.release(); }
31 MutexGuard(sys::Mutex &m) : M(m) { M.lock(); }
32 ~MutexGuard() { M.unlock(); }
3333 /// holds - Returns true if this locker instance holds the specified lock.
3434 /// This is mostly used in assertions to validate that the correct mutex
3535 /// is held.
8484 /// indicates whether this mutex should become a no-op when we're not
8585 /// running in multithreaded mode.
8686 template
87 class SmartRWMutex : public RWMutexImpl {
87 class SmartRWMutex {
88 RWMutexImpl impl;
8889 unsigned readers, writers;
8990 public:
90 explicit SmartRWMutex() : RWMutexImpl(), readers(0), writers(0) { }
91 explicit SmartRWMutex() : impl(), readers(0), writers(0) { }
9192
92 bool reader_acquire() {
93 bool lock_shared() {
9394 if (!mt_only || llvm_is_multithreaded())
94 return RWMutexImpl::reader_acquire();
95 return impl.reader_acquire();
9596
9697 // Single-threaded debugging code. This would be racy in multithreaded
9798 // mode, but provides not sanity checks in single threaded mode.
99100 return true;
100101 }
101102
102 bool reader_release() {
103 bool unlock_shared() {
103104 if (!mt_only || llvm_is_multithreaded())
104 return RWMutexImpl::reader_release();
105 return impl.reader_release();
105106
106107 // Single-threaded debugging code. This would be racy in multithreaded
107108 // mode, but provides not sanity checks in single threaded mode.
110111 return true;
111112 }
112113
113 bool writer_acquire() {
114 bool lock() {
114115 if (!mt_only || llvm_is_multithreaded())
115 return RWMutexImpl::writer_acquire();
116 return impl.writer_acquire();
116117
117118 // Single-threaded debugging code. This would be racy in multithreaded
118119 // mode, but provides not sanity checks in single threaded mode.
121122 return true;
122123 }
123124
124 bool writer_release() {
125 bool unlock() {
125126 if (!mt_only || llvm_is_multithreaded())
126 return RWMutexImpl::writer_release();
127 return impl.writer_release();
127128
128129 // Single-threaded debugging code. This would be racy in multithreaded
129130 // mode, but provides not sanity checks in single threaded mode.
144145 SmartRWMutex& mutex;
145146
146147 explicit SmartScopedReader(SmartRWMutex& m) : mutex(m) {
147 mutex.reader_acquire();
148 mutex.lock_shared();
148149 }
149150
150151 ~SmartScopedReader() {
151 mutex.reader_release();
152 mutex.unlock_shared();
152153 }
153154 };
154155 typedef SmartScopedReader ScopedReader;
159160 SmartRWMutex& mutex;
160161
161162 explicit SmartScopedWriter(SmartRWMutex& m) : mutex(m) {
162 mutex.writer_acquire();
163 mutex.lock();
163164 }
164165
165166 ~SmartScopedWriter() {
166 mutex.writer_release();
167 mutex.unlock();
167168 }
168169 };
169170 typedef SmartScopedWriter ScopedWriter;
247247 const std::vector &ArgVals) {
248248 TheInterpreter = this;
249249
250 FunctionsLock->acquire();
250 FunctionsLock->lock();
251251
252252 // Do a lookup to see if the function is in our cache... this should just be a
253253 // deferred annotation!
254254 std::map::iterator FI = ExportedFunctions->find(F);
255255 if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F)
256256 : FI->second) {
257 FunctionsLock->release();
257 FunctionsLock->unlock();
258258 return Fn(F->getFunctionType(), ArgVals);
259259 }
260260
272272 RawFn = RF->second;
273273 }
274274
275 FunctionsLock->release();
275 FunctionsLock->unlock();
276276
277277 GenericValue Result;
278278 if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals, getDataLayout(), Result))
161161 sigfillset(&SigMask);
162162 sigprocmask(SIG_UNBLOCK, &SigMask, nullptr);
163163
164 SignalsMutex.acquire();
164 SignalsMutex.lock();
165165 RemoveFilesToRemove();
166166
167167 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) {
168168 if (InterruptFunction) {
169169 void (*IF)() = InterruptFunction;
170 SignalsMutex.release();
170 SignalsMutex.unlock();
171171 InterruptFunction = nullptr;
172172 IF(); // run the interrupt function.
173173 return;
174174 }
175175
176 SignalsMutex.release();
176 SignalsMutex.unlock();
177177 raise(Sig); // Execute the default handler.
178178 return;
179179 }
180180
181 SignalsMutex.release();
181 SignalsMutex.unlock();
182182
183183 // Otherwise if it is a fault (like SEGV) run any handler.
184184 for (unsigned i = 0, e = CallBacksToRun.size(); i != e; ++i)
185185 };
186186 static void onRAUW(const ExtraData &Data, KeyT Old, KeyT New) {
187187 *Data.CalledRAUW = true;
188 EXPECT_FALSE(Data.M->tryacquire()) << "Mutex should already be locked.";
188 EXPECT_FALSE(Data.M->try_lock()) << "Mutex should already be locked.";
189189 }
190190 static void onDelete(const ExtraData &Data, KeyT Old) {
191191 *Data.CalledDeleted = true;
192 EXPECT_FALSE(Data.M->tryacquire()) << "Mutex should already be locked.";
192 EXPECT_FALSE(Data.M->try_lock()) << "Mutex should already be locked.";
193193 }
194194 static MutexT *getMutex(const ExtraData &Data) { return Data.M; }
195195 };