• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_BASE_MUTEX_H_
18 #define ART_RUNTIME_BASE_MUTEX_H_
19 
20 #include <limits.h>  // for INT_MAX
21 #include <pthread.h>
22 #include <stdint.h>
23 #include <unistd.h>  // for pid_t
24 
25 #include <iosfwd>
26 #include <string>
27 
28 #include <android-base/logging.h>
29 
30 #include "base/aborting.h"
31 #include "base/atomic.h"
32 #include "runtime_globals.h"
33 #include "base/macros.h"
34 #include "locks.h"
35 
36 #if defined(__linux__)
37 #define ART_USE_FUTEXES 1
38 #else
39 #define ART_USE_FUTEXES 0
40 #endif
41 
42 // Currently Darwin doesn't support locks with timeouts.
43 #if !defined(__APPLE__)
44 #define HAVE_TIMED_RWLOCK 1
45 #else
46 #define HAVE_TIMED_RWLOCK 0
47 #endif
48 
49 namespace art {
50 
51 class SHARED_LOCKABLE ReaderWriterMutex;
52 class SHARED_LOCKABLE MutatorMutex;
53 class ScopedContentionRecorder;
54 class Thread;
55 class LOCKABLE Mutex;
56 
57 constexpr bool kDebugLocking = kIsDebugBuild;
58 
59 // Record Log contention information, dumpable via SIGQUIT.
60 #if ART_USE_FUTEXES
61 // To enable lock contention logging, set this to true.
62 constexpr bool kLogLockContentions = false;
63 // FUTEX_WAKE first argument:
64 constexpr int kWakeOne = 1;
65 constexpr int kWakeAll = INT_MAX;
66 #else
67 // Keep this false as lock contention logging is supported only with
68 // futex.
69 constexpr bool kLogLockContentions = false;
70 #endif
71 constexpr size_t kContentionLogSize = 4;
72 constexpr size_t kContentionLogDataSize = kLogLockContentions ? 1 : 0;
73 constexpr size_t kAllMutexDataSize = kLogLockContentions ? 1 : 0;
74 
75 // Base class for all Mutex implementations
76 class BaseMutex {
77  public:
GetName()78   const char* GetName() const {
79     return name_;
80   }
81 
IsMutex()82   virtual bool IsMutex() const { return false; }
IsReaderWriterMutex()83   virtual bool IsReaderWriterMutex() const { return false; }
IsMutatorMutex()84   virtual bool IsMutatorMutex() const { return false; }
85 
86   virtual void Dump(std::ostream& os) const = 0;
87 
88   static void DumpAll(std::ostream& os);
89 
ShouldRespondToEmptyCheckpointRequest()90   bool ShouldRespondToEmptyCheckpointRequest() const {
91     return should_respond_to_empty_checkpoint_request_;
92   }
93 
SetShouldRespondToEmptyCheckpointRequest(bool value)94   void SetShouldRespondToEmptyCheckpointRequest(bool value) {
95     should_respond_to_empty_checkpoint_request_ = value;
96   }
97 
98   virtual void WakeupToRespondToEmptyCheckpoint() = 0;
99 
100  protected:
101   friend class ConditionVariable;
102 
103   BaseMutex(const char* name, LockLevel level);
104   virtual ~BaseMutex();
105 
106   // Add this mutex to those owned by self, and perform appropriate checking.
107   // For this call only, self may also be another suspended thread.
108   void RegisterAsLocked(Thread* self);
109   void RegisterAsLockedImpl(Thread* self, LockLevel level);
110 
111   void RegisterAsUnlocked(Thread* self);
112   void RegisterAsUnlockedImpl(Thread* self, LockLevel level);
113 
114   void CheckSafeToWait(Thread* self);
115 
116   friend class ScopedContentionRecorder;
117 
118   void RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t nano_time_blocked);
119   void DumpContention(std::ostream& os) const;
120 
121   const char* const name_;
122 
123   // A log entry that records contention but makes no guarantee that either tid will be held live.
124   struct ContentionLogEntry {
ContentionLogEntryContentionLogEntry125     ContentionLogEntry() : blocked_tid(0), owner_tid(0) {}
126     uint64_t blocked_tid;
127     uint64_t owner_tid;
128     AtomicInteger count;
129   };
130   struct ContentionLogData {
131     ContentionLogEntry contention_log[kContentionLogSize];
132     // The next entry in the contention log to be updated. Value ranges from 0 to
133     // kContentionLogSize - 1.
134     AtomicInteger cur_content_log_entry;
135     // Number of times the Mutex has been contended.
136     AtomicInteger contention_count;
137     // Sum of time waited by all contenders in ns.
138     Atomic<uint64_t> wait_time;
139     void AddToWaitTime(uint64_t value);
ContentionLogDataContentionLogData140     ContentionLogData() : wait_time(0) {}
141   };
142   ContentionLogData contention_log_data_[kContentionLogDataSize];
143 
144   const LockLevel level_;  // Support for lock hierarchy.
145   bool should_respond_to_empty_checkpoint_request_;
146 
147  public:
HasEverContended()148   bool HasEverContended() const {
149     if (kLogLockContentions) {
150       return contention_log_data_->contention_count.load(std::memory_order_seq_cst) > 0;
151     }
152     return false;
153   }
154 };
155 
156 // A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain
157 // exclusive access to what it guards. A Mutex can be in one of two states:
158 // - Free - not owned by any thread,
159 // - Exclusive - owned by a single thread.
160 //
161 // The effect of locking and unlocking operations on the state is:
162 // State     | ExclusiveLock | ExclusiveUnlock
163 // -------------------------------------------
164 // Free      | Exclusive     | error
165 // Exclusive | Block*        | Free
166 // * Mutex is not reentrant unless recursive is true. An attempt to ExclusiveLock on a
167 // recursive=false Mutex on a thread already owning the Mutex results in an error.
168 //
169 // TODO(b/140590186): Remove support for recursive == true.
170 //
171 // Some mutexes, including those associated with Java monitors may be accessed (in particular
172 // acquired) by a thread in suspended state. Suspending all threads does NOT prevent mutex state
173 // from changing.
174 std::ostream& operator<<(std::ostream& os, const Mutex& mu);
175 class LOCKABLE Mutex : public BaseMutex {
176  public:
177   explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false);
178   ~Mutex();
179 
IsMutex()180   bool IsMutex() const override { return true; }
181 
182   // Block until mutex is free then acquire exclusive access.
183   void ExclusiveLock(Thread* self) ACQUIRE();
Lock(Thread * self)184   void Lock(Thread* self) ACQUIRE() {  ExclusiveLock(self); }
185 
186   // Returns true if acquires exclusive access, false otherwise.
187   bool ExclusiveTryLock(Thread* self) TRY_ACQUIRE(true);
TryLock(Thread * self)188   bool TryLock(Thread* self) TRY_ACQUIRE(true) { return ExclusiveTryLock(self); }
189   // Equivalent to ExclusiveTryLock, but retry for a short period before giving up.
190   bool ExclusiveTryLockWithSpinning(Thread* self) TRY_ACQUIRE(true);
191 
192   // Release exclusive access.
193   void ExclusiveUnlock(Thread* self) RELEASE();
Unlock(Thread * self)194   void Unlock(Thread* self) RELEASE() {  ExclusiveUnlock(self); }
195 
196   // Is the current thread the exclusive holder of the Mutex.
197   ALWAYS_INLINE bool IsExclusiveHeld(const Thread* self) const;
198 
199   // Assert that the Mutex is exclusively held by the current thread.
200   ALWAYS_INLINE void AssertExclusiveHeld(const Thread* self) const ASSERT_CAPABILITY(this);
201   ALWAYS_INLINE void AssertHeld(const Thread* self) const ASSERT_CAPABILITY(this);
202 
203   // Assert that the Mutex is not held by the current thread.
AssertNotHeldExclusive(const Thread * self)204   void AssertNotHeldExclusive(const Thread* self) ASSERT_CAPABILITY(!*this) {
205     if (kDebugLocking && (gAborting == 0)) {
206       CHECK(!IsExclusiveHeld(self)) << *this;
207     }
208   }
AssertNotHeld(const Thread * self)209   void AssertNotHeld(const Thread* self) ASSERT_CAPABILITY(!*this) {
210     AssertNotHeldExclusive(self);
211   }
212 
213   // Id associated with exclusive owner. No memory ordering semantics if called from a thread
214   // other than the owner. GetTid() == GetExclusiveOwnerTid() is a reliable way to determine
215   // whether we hold the lock; any other information may be invalidated before we return.
216   pid_t GetExclusiveOwnerTid() const;
217 
218   // Returns how many times this Mutex has been locked, it is typically better to use
219   // AssertHeld/NotHeld. For a simply held mutex this method returns 1. Should only be called
220   // while holding the mutex or threads are suspended.
GetDepth()221   unsigned int GetDepth() const {
222     return recursion_count_;
223   }
224 
225   void Dump(std::ostream& os) const override;
226 
227   void DumpStack(Thread *self, uint64_t wait_start_ms, uint64_t try_times = 1);
228 
229   static bool IsDumpFrequent(Thread *self, uint64_t try_times = 1);
230 
setEnableMonitorTimeout()231   void setEnableMonitorTimeout() {
232     enable_monitor_timeout_ = true;
233   }
234 
setMonitorId(uint32_t monitorId)235   void setMonitorId(uint32_t monitorId) {
236     monitor_id_ = monitorId;
237   }
238 
239   // For negative capabilities in clang annotations.
240   const Mutex& operator!() const { return *this; }
241 
242   void WakeupToRespondToEmptyCheckpoint() override;
243 
244 #if ART_USE_FUTEXES
245   // Acquire the mutex, possibly on behalf of another thread. Acquisition must be
246   // uncontended. New_owner must be current thread or suspended.
247   // Mutex must be at level kMonitorLock.
248   // Not implementable for the pthreads version, so we must avoid calling it there.
249   void ExclusiveLockUncontendedFor(Thread* new_owner);
250 
251   // Undo the effect of the previous calling, setting the mutex back to unheld.
252   // Still assumes no concurrent access.
253   void ExclusiveUnlockUncontended();
254 #endif  // ART_USE_FUTEXES
255 
256  private:
257 #if ART_USE_FUTEXES
258   // Low order bit: 0 is unheld, 1 is held.
259   // High order bits: Number of waiting contenders.
260   AtomicInteger state_and_contenders_;
261 
262   static constexpr int32_t kHeldMask = 1;
263 
264   static constexpr int32_t kContenderShift = 1;
265 
266   static constexpr int32_t kContenderIncrement = 1 << kContenderShift;
267 
increment_contenders()268   void increment_contenders() {
269     state_and_contenders_.fetch_add(kContenderIncrement);
270   }
271 
decrement_contenders()272   void decrement_contenders() {
273     state_and_contenders_.fetch_sub(kContenderIncrement);
274   }
275 
get_contenders()276   int32_t get_contenders() {
277     // Result is guaranteed to include any contention added by this thread; otherwise approximate.
278     // Treat contenders as unsigned because we're concerned about overflow; should never matter.
279     return static_cast<uint32_t>(state_and_contenders_.load(std::memory_order_relaxed))
280         >> kContenderShift;
281   }
282 
283   // Exclusive owner.
284   Atomic<pid_t> exclusive_owner_;
285 #else
286   pthread_mutex_t mutex_;
287   Atomic<pid_t> exclusive_owner_;  // Guarded by mutex_. Asynchronous reads are OK.
288 #endif
289 
290   unsigned int recursion_count_;
291   const bool recursive_;  // Can the lock be recursively held?
292 
293   bool enable_monitor_timeout_ = false;
294 
295   uint32_t monitor_id_;
296 
297   friend class ConditionVariable;
298   DISALLOW_COPY_AND_ASSIGN(Mutex);
299 };
300 
301 // A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex.
302 // Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader)
303 // access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a
304 // condition variable. A ReaderWriterMutex can be in one of three states:
305 // - Free - not owned by any thread,
306 // - Exclusive - owned by a single thread,
307 // - Shared(n) - shared amongst n threads.
308 //
309 // The effect of locking and unlocking operations on the state is:
310 //
311 // State     | ExclusiveLock | ExclusiveUnlock | SharedLock       | SharedUnlock
312 // ----------------------------------------------------------------------------
313 // Free      | Exclusive     | error           | SharedLock(1)    | error
314 // Exclusive | Block         | Free            | Block            | error
315 // Shared(n) | Block         | error           | SharedLock(n+1)* | Shared(n-1) or Free
316 // * for large values of n the SharedLock may block.
317 std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu);
318 class SHARED_LOCKABLE ReaderWriterMutex : public BaseMutex {
319  public:
320   explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel);
321   ~ReaderWriterMutex();
322 
IsReaderWriterMutex()323   bool IsReaderWriterMutex() const override { return true; }
324 
325   // Block until ReaderWriterMutex is free then acquire exclusive access.
326   void ExclusiveLock(Thread* self) ACQUIRE();
WriterLock(Thread * self)327   void WriterLock(Thread* self) ACQUIRE() {  ExclusiveLock(self); }
328 
329   // Release exclusive access.
330   void ExclusiveUnlock(Thread* self) RELEASE();
WriterUnlock(Thread * self)331   void WriterUnlock(Thread* self) RELEASE() {  ExclusiveUnlock(self); }
332 
333   // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success
334   // or false if timeout is reached.
335 #if HAVE_TIMED_RWLOCK
336   bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns)
337       EXCLUSIVE_TRYLOCK_FUNCTION(true);
338 #endif
339 
340   // Block until ReaderWriterMutex is shared or free then acquire a share on the access.
341   void SharedLock(Thread* self) ACQUIRE_SHARED() ALWAYS_INLINE;
ReaderLock(Thread * self)342   void ReaderLock(Thread* self) ACQUIRE_SHARED() { SharedLock(self); }
343 
344   // Try to acquire share of ReaderWriterMutex.
345   bool SharedTryLock(Thread* self) SHARED_TRYLOCK_FUNCTION(true);
346 
347   // Release a share of the access.
348   void SharedUnlock(Thread* self) RELEASE_SHARED() ALWAYS_INLINE;
ReaderUnlock(Thread * self)349   void ReaderUnlock(Thread* self) RELEASE_SHARED() { SharedUnlock(self); }
350 
351   // Is the current thread the exclusive holder of the ReaderWriterMutex.
352   ALWAYS_INLINE bool IsExclusiveHeld(const Thread* self) const;
353 
354   // Assert the current thread has exclusive access to the ReaderWriterMutex.
355   ALWAYS_INLINE void AssertExclusiveHeld(const Thread* self) const ASSERT_CAPABILITY(this);
356   ALWAYS_INLINE void AssertWriterHeld(const Thread* self) const ASSERT_CAPABILITY(this);
357 
358   // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex.
AssertNotExclusiveHeld(const Thread * self)359   void AssertNotExclusiveHeld(const Thread* self) ASSERT_CAPABILITY(!this) {
360     if (kDebugLocking && (gAborting == 0)) {
361       CHECK(!IsExclusiveHeld(self)) << *this;
362     }
363   }
AssertNotWriterHeld(const Thread * self)364   void AssertNotWriterHeld(const Thread* self) ASSERT_CAPABILITY(!this) {
365     AssertNotExclusiveHeld(self);
366   }
367 
368   // Is the current thread a shared holder of the ReaderWriterMutex.
369   bool IsSharedHeld(const Thread* self) const;
370 
371   // Assert the current thread has shared access to the ReaderWriterMutex.
AssertSharedHeld(const Thread * self)372   ALWAYS_INLINE void AssertSharedHeld(const Thread* self) ASSERT_SHARED_CAPABILITY(this) {
373     if (kDebugLocking && (gAborting == 0)) {
374       // TODO: we can only assert this well when self != null.
375       CHECK(IsSharedHeld(self) || self == nullptr) << *this;
376     }
377   }
AssertReaderHeld(const Thread * self)378   ALWAYS_INLINE void AssertReaderHeld(const Thread* self) ASSERT_SHARED_CAPABILITY(this) {
379     AssertSharedHeld(self);
380   }
381 
382   // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive
383   // mode.
AssertNotHeld(const Thread * self)384   ALWAYS_INLINE void AssertNotHeld(const Thread* self) ASSERT_CAPABILITY(!this) {
385     if (kDebugLocking && (gAborting == 0)) {
386       CHECK(!IsExclusiveHeld(self)) << *this;
387       CHECK(!IsSharedHeld(self)) << *this;
388     }
389   }
390 
391   // Id associated with exclusive owner. No memory ordering semantics if called from a thread other
392   // than the owner. Returns 0 if the lock is not held. Returns either 0 or -1 if it is held by
393   // one or more readers.
394   pid_t GetExclusiveOwnerTid() const;
395 
396   void Dump(std::ostream& os) const override;
397 
398   // For negative capabilities in clang annotations.
399   const ReaderWriterMutex& operator!() const { return *this; }
400 
401   void WakeupToRespondToEmptyCheckpoint() override;
402 
403  private:
404 #if ART_USE_FUTEXES
405   // Out-of-inline path for handling contention for a SharedLock.
406   void HandleSharedLockContention(Thread* self, int32_t cur_state);
407 
408   // -1 implies held exclusive, >= 0: shared held by state_ many owners.
409   AtomicInteger state_;
410   // Exclusive owner. Modification guarded by this mutex.
411   Atomic<pid_t> exclusive_owner_;
412   // Number of contenders waiting for either a reader share or exclusive access.  We only maintain
413   // the sum, since we would otherwise need to read both in all unlock operations.
414   // We keep this separate from the state, since futexes are limited to 32 bits, and obvious
415   // approaches to combining with state_ risk overflow.
416   AtomicInteger num_contenders_;
417 #else
418   pthread_rwlock_t rwlock_;
419   Atomic<pid_t> exclusive_owner_;  // Writes guarded by rwlock_. Asynchronous reads are OK.
420 #endif
421   DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex);
422 };
423 
424 // MutatorMutex is a special kind of ReaderWriterMutex created specifically for the
425 // Locks::mutator_lock_ mutex. The behaviour is identical to the ReaderWriterMutex except that
426 // thread state changes also play a part in lock ownership. The mutator_lock_ will not be truly
427 // held by any mutator threads. However, a thread in the kRunnable state is considered to have
428 // shared ownership of the mutator lock and therefore transitions in and out of the kRunnable
429 // state have associated implications on lock ownership. Extra methods to handle the state
430 // transitions have been added to the interface but are only accessible to the methods dealing
431 // with state transitions. The thread state and flags attributes are used to ensure thread state
432 // transitions are consistent with the permitted behaviour of the mutex.
433 //
434 // *) The most important consequence of this behaviour is that all threads must be in one of the
435 // suspended states before exclusive ownership of the mutator mutex is sought.
436 //
437 std::ostream& operator<<(std::ostream& os, const MutatorMutex& mu);
438 class SHARED_LOCKABLE MutatorMutex : public ReaderWriterMutex {
439  public:
440   explicit MutatorMutex(const char* name, LockLevel level = kDefaultMutexLevel)
ReaderWriterMutex(name,level)441     : ReaderWriterMutex(name, level) {}
~MutatorMutex()442   ~MutatorMutex() {}
443 
IsMutatorMutex()444   virtual bool IsMutatorMutex() const { return true; }
445 
446   // For negative capabilities in clang annotations.
447   const MutatorMutex& operator!() const { return *this; }
448 
449  private:
450   friend class Thread;
451   void TransitionFromRunnableToSuspended(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE;
452   void TransitionFromSuspendedToRunnable(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE;
453 
454   DISALLOW_COPY_AND_ASSIGN(MutatorMutex);
455 };
456 
457 // ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually
458 // (Signal) or all at once (Broadcast).
459 class ConditionVariable {
460  public:
461   ConditionVariable(const char* name, Mutex& mutex);
462   ~ConditionVariable();
463 
464   // Requires the mutex to be held.
465   void Broadcast(Thread* self);
466   // Requires the mutex to be held.
467   void Signal(Thread* self);
468   // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their
469   //       pointer copy, thereby defeating annotalysis.
470   void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
471   bool TimedWait(Thread* self, int64_t ms, int32_t ns) NO_THREAD_SAFETY_ANALYSIS;
472   // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held
473   // when waiting.
474   // TODO: remove this.
475   void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
476 
CheckSafeToWait(Thread * self)477   void CheckSafeToWait(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
478     if (kDebugLocking) {
479       guard_.CheckSafeToWait(self);
480     }
481   }
482 
483  private:
484   const char* const name_;
485   // The Mutex being used by waiters. It is an error to mix condition variables between different
486   // Mutexes.
487   Mutex& guard_;
488 #if ART_USE_FUTEXES
489   // A counter that is modified by signals and broadcasts. This ensures that when a waiter gives up
490   // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_
491   // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait
492   // without guard_ held.
493   AtomicInteger sequence_;
494   // Number of threads that have come into to wait, not the length of the waiters on the futex as
495   // waiters may have been requeued onto guard_. Guarded by guard_.
496   int32_t num_waiters_;
497 
498   void RequeueWaiters(int32_t count);
499 #else
500   pthread_cond_t cond_;
501 #endif
502   DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
503 };
504 
505 // Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it
506 // upon destruction.
507 class SCOPED_CAPABILITY MutexLock {
508  public:
MutexLock(Thread * self,Mutex & mu)509   MutexLock(Thread* self, Mutex& mu) ACQUIRE(mu) : self_(self), mu_(mu) {
510     mu_.ExclusiveLock(self_);
511   }
512 
RELEASE()513   ~MutexLock() RELEASE() {
514     mu_.ExclusiveUnlock(self_);
515   }
516 
517  private:
518   Thread* const self_;
519   Mutex& mu_;
520   DISALLOW_COPY_AND_ASSIGN(MutexLock);
521 };
522 
523 // Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon
524 // construction and releases it upon destruction.
525 class SCOPED_CAPABILITY ReaderMutexLock {
526  public:
527   ALWAYS_INLINE ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) ACQUIRE(mu);
528 
529   ALWAYS_INLINE ~ReaderMutexLock() RELEASE();
530 
531  private:
532   Thread* const self_;
533   ReaderWriterMutex& mu_;
534   DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock);
535 };
536 
537 // Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon
538 // construction and releases it upon destruction.
539 class SCOPED_CAPABILITY WriterMutexLock {
540  public:
WriterMutexLock(Thread * self,ReaderWriterMutex & mu)541   WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) :
542       self_(self), mu_(mu) {
543     mu_.ExclusiveLock(self_);
544   }
545 
UNLOCK_FUNCTION()546   ~WriterMutexLock() UNLOCK_FUNCTION() {
547     mu_.ExclusiveUnlock(self_);
548   }
549 
550  private:
551   Thread* const self_;
552   ReaderWriterMutex& mu_;
553   DISALLOW_COPY_AND_ASSIGN(WriterMutexLock);
554 };
555 
556 }  // namespace art
557 
558 #endif  // ART_RUNTIME_BASE_MUTEX_H_
559