mutex.h revision 6093a5c277e54bcd949dd6fac7b3856e5f371d06
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 <pthread.h>
21#include <stdint.h>
22
23#include <iosfwd>
24#include <string>
25
26#include "atomic.h"
27#include "base/logging.h"
28#include "base/macros.h"
29#include "globals.h"
30
31#if defined(__APPLE__)
32#define ART_USE_FUTEXES 0
33#else
34#define ART_USE_FUTEXES 1
35#endif
36
37// Currently Darwin doesn't support locks with timeouts.
38#if !defined(__APPLE__)
39#define HAVE_TIMED_RWLOCK 1
40#else
41#define HAVE_TIMED_RWLOCK 0
42#endif
43
44namespace art {
45
46class LOCKABLE ReaderWriterMutex;
47class ScopedContentionRecorder;
48class Thread;
49
50// LockLevel is used to impose a lock hierarchy [1] where acquisition of a Mutex at a higher or
51// equal level to a lock a thread holds is invalid. The lock hierarchy achieves a cycle free
52// partial ordering and thereby cause deadlock situations to fail checks.
53//
54// [1] http://www.drdobbs.com/parallel/use-lock-hierarchies-to-avoid-deadlock/204801163
55enum LockLevel {
56  kLoggingLock = 0,
57  kMemMapsLock,
58  kSwapMutexesLock,
59  kUnexpectedSignalLock,
60  kThreadSuspendCountLock,
61  kAbortLock,
62  kJdwpSocketLock,
63  kRosAllocGlobalLock,
64  kRosAllocBracketLock,
65  kRosAllocBulkFreeLock,
66  kAllocSpaceLock,
67  kReferenceProcessorLock,
68  kDexFileMethodInlinerLock,
69  kDexFileToMethodInlinerMapLock,
70  kMarkSweepMarkStackLock,
71  kTransactionLogLock,
72  kInternTableLock,
73  kDefaultMutexLevel,
74  kMarkSweepLargeObjectLock,
75  kPinTableLock,
76  kLoadLibraryLock,
77  kJdwpObjectRegistryLock,
78  kModifyLdtLock,
79  kAllocatedThreadIdsLock,
80  kMonitorPoolLock,
81  kClassLinkerClassesLock,
82  kBreakpointLock,
83  kMonitorLock,
84  kMonitorListLock,
85  kThreadListLock,
86  kBreakpointInvokeLock,
87  kDeoptimizationLock,
88  kTraceLock,
89  kProfilerLock,
90  kJdwpEventListLock,
91  kJdwpAttachLock,
92  kJdwpStartLock,
93  kRuntimeShutdownLock,
94  kHeapBitmapLock,
95  kMutatorLock,
96  kThreadListSuspendThreadLock,
97  kZygoteCreationLock,
98
99  kLockLevelCount  // Must come last.
100};
101std::ostream& operator<<(std::ostream& os, const LockLevel& rhs);
102
103const bool kDebugLocking = kIsDebugBuild;
104
105// Record Log contention information, dumpable via SIGQUIT.
106#ifdef ART_USE_FUTEXES
107// To enable lock contention logging, set this to true.
108const bool kLogLockContentions = false;
109#else
110// Keep this false as lock contention logging is supported only with
111// futex.
112const bool kLogLockContentions = false;
113#endif
114const size_t kContentionLogSize = 4;
115const size_t kContentionLogDataSize = kLogLockContentions ? 1 : 0;
116const size_t kAllMutexDataSize = kLogLockContentions ? 1 : 0;
117
118// Base class for all Mutex implementations
119class BaseMutex {
120 public:
121  const char* GetName() const {
122    return name_;
123  }
124
125  virtual bool IsMutex() const { return false; }
126  virtual bool IsReaderWriterMutex() const { return false; }
127
128  virtual void Dump(std::ostream& os) const = 0;
129
130  static void DumpAll(std::ostream& os);
131
132 protected:
133  friend class ConditionVariable;
134
135  BaseMutex(const char* name, LockLevel level);
136  virtual ~BaseMutex();
137  void RegisterAsLocked(Thread* self);
138  void RegisterAsUnlocked(Thread* self);
139  void CheckSafeToWait(Thread* self);
140
141  friend class ScopedContentionRecorder;
142
143  void RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t nano_time_blocked);
144  void DumpContention(std::ostream& os) const;
145
146  const LockLevel level_;  // Support for lock hierarchy.
147  const char* const name_;
148
149  // A log entry that records contention but makes no guarantee that either tid will be held live.
150  struct ContentionLogEntry {
151    ContentionLogEntry() : blocked_tid(0), owner_tid(0) {}
152    uint64_t blocked_tid;
153    uint64_t owner_tid;
154    AtomicInteger count;
155  };
156  struct ContentionLogData {
157    ContentionLogEntry contention_log[kContentionLogSize];
158    // The next entry in the contention log to be updated. Value ranges from 0 to
159    // kContentionLogSize - 1.
160    AtomicInteger cur_content_log_entry;
161    // Number of times the Mutex has been contended.
162    AtomicInteger contention_count;
163    // Sum of time waited by all contenders in ns.
164    volatile uint64_t wait_time;
165    void AddToWaitTime(uint64_t value);
166    ContentionLogData() : wait_time(0) {}
167  };
168  ContentionLogData contention_log_data_[kContentionLogDataSize];
169
170 public:
171  bool HasEverContended() const {
172    if (kLogLockContentions) {
173      return contention_log_data_->contention_count.LoadSequentiallyConsistent() > 0;
174    }
175    return false;
176  }
177};
178
179// A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain
180// exclusive access to what it guards. A Mutex can be in one of two states:
181// - Free - not owned by any thread,
182// - Exclusive - owned by a single thread.
183//
184// The effect of locking and unlocking operations on the state is:
185// State     | ExclusiveLock | ExclusiveUnlock
186// -------------------------------------------
187// Free      | Exclusive     | error
188// Exclusive | Block*        | Free
189// * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in
190//   an error. Being non-reentrant simplifies Waiting on ConditionVariables.
191std::ostream& operator<<(std::ostream& os, const Mutex& mu);
192class LOCKABLE Mutex : public BaseMutex {
193 public:
194  explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false);
195  ~Mutex();
196
197  virtual bool IsMutex() const { return true; }
198
199  // Block until mutex is free then acquire exclusive access.
200  void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION();
201  void Lock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() {  ExclusiveLock(self); }
202
203  // Returns true if acquires exclusive access, false otherwise.
204  bool ExclusiveTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true);
205  bool TryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true) { return ExclusiveTryLock(self); }
206
207  // Release exclusive access.
208  void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION();
209  void Unlock(Thread* self) UNLOCK_FUNCTION() {  ExclusiveUnlock(self); }
210
211  // Is the current thread the exclusive holder of the Mutex.
212  bool IsExclusiveHeld(const Thread* self) const;
213
214  // Assert that the Mutex is exclusively held by the current thread.
215  void AssertExclusiveHeld(const Thread* self) {
216    if (kDebugLocking && (gAborting == 0)) {
217      CHECK(IsExclusiveHeld(self)) << *this;
218    }
219  }
220  void AssertHeld(const Thread* self) { AssertExclusiveHeld(self); }
221
222  // Assert that the Mutex is not held by the current thread.
223  void AssertNotHeldExclusive(const Thread* self) {
224    if (kDebugLocking && (gAborting == 0)) {
225      CHECK(!IsExclusiveHeld(self)) << *this;
226    }
227  }
228  void AssertNotHeld(const Thread* self) { AssertNotHeldExclusive(self); }
229
230  // Id associated with exclusive owner. No memory ordering semantics if called from a thread other
231  // than the owner.
232  uint64_t GetExclusiveOwnerTid() const;
233
234  // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld.
235  unsigned int GetDepth() const {
236    return recursion_count_;
237  }
238
239  virtual void Dump(std::ostream& os) const;
240
241 private:
242#if ART_USE_FUTEXES
243  // 0 is unheld, 1 is held.
244  AtomicInteger state_;
245  // Exclusive owner.
246  volatile uint64_t exclusive_owner_;
247  // Number of waiting contenders.
248  AtomicInteger num_contenders_;
249#else
250  pthread_mutex_t mutex_;
251  volatile uint64_t exclusive_owner_;  // Guarded by mutex_.
252#endif
253  const bool recursive_;  // Can the lock be recursively held?
254  unsigned int recursion_count_;
255  friend class ConditionVariable;
256  DISALLOW_COPY_AND_ASSIGN(Mutex);
257};
258
259// A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex.
260// Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader)
261// access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a
262// condition variable. A ReaderWriterMutex can be in one of three states:
263// - Free - not owned by any thread,
264// - Exclusive - owned by a single thread,
265// - Shared(n) - shared amongst n threads.
266//
267// The effect of locking and unlocking operations on the state is:
268//
269// State     | ExclusiveLock | ExclusiveUnlock | SharedLock       | SharedUnlock
270// ----------------------------------------------------------------------------
271// Free      | Exclusive     | error           | SharedLock(1)    | error
272// Exclusive | Block         | Free            | Block            | error
273// Shared(n) | Block         | error           | SharedLock(n+1)* | Shared(n-1) or Free
274// * for large values of n the SharedLock may block.
275std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu);
276class LOCKABLE ReaderWriterMutex : public BaseMutex {
277 public:
278  explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel);
279  ~ReaderWriterMutex();
280
281  virtual bool IsReaderWriterMutex() const { return true; }
282
283  // Block until ReaderWriterMutex is free then acquire exclusive access.
284  void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION();
285  void WriterLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() {  ExclusiveLock(self); }
286
287  // Release exclusive access.
288  void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION();
289  void WriterUnlock(Thread* self) UNLOCK_FUNCTION() {  ExclusiveUnlock(self); }
290
291  // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success
292  // or false if timeout is reached.
293#if HAVE_TIMED_RWLOCK
294  bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns)
295      EXCLUSIVE_TRYLOCK_FUNCTION(true);
296#endif
297
298  // Block until ReaderWriterMutex is shared or free then acquire a share on the access.
299  void SharedLock(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE;
300  void ReaderLock(Thread* self) SHARED_LOCK_FUNCTION() { SharedLock(self); }
301
302  // Try to acquire share of ReaderWriterMutex.
303  bool SharedTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true);
304
305  // Release a share of the access.
306  void SharedUnlock(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE;
307  void ReaderUnlock(Thread* self) UNLOCK_FUNCTION() { SharedUnlock(self); }
308
309  // Is the current thread the exclusive holder of the ReaderWriterMutex.
310  bool IsExclusiveHeld(const Thread* self) const;
311
312  // Assert the current thread has exclusive access to the ReaderWriterMutex.
313  void AssertExclusiveHeld(const Thread* self) {
314    if (kDebugLocking && (gAborting == 0)) {
315      CHECK(IsExclusiveHeld(self)) << *this;
316    }
317  }
318  void AssertWriterHeld(const Thread* self) { AssertExclusiveHeld(self); }
319
320  // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex.
321  void AssertNotExclusiveHeld(const Thread* self) {
322    if (kDebugLocking && (gAborting == 0)) {
323      CHECK(!IsExclusiveHeld(self)) << *this;
324    }
325  }
326  void AssertNotWriterHeld(const Thread* self) { AssertNotExclusiveHeld(self); }
327
328  // Is the current thread a shared holder of the ReaderWriterMutex.
329  bool IsSharedHeld(const Thread* self) const;
330
331  // Assert the current thread has shared access to the ReaderWriterMutex.
332  void AssertSharedHeld(const Thread* self) {
333    if (kDebugLocking && (gAborting == 0)) {
334      // TODO: we can only assert this well when self != NULL.
335      CHECK(IsSharedHeld(self) || self == NULL) << *this;
336    }
337  }
338  void AssertReaderHeld(const Thread* self) { AssertSharedHeld(self); }
339
340  // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive
341  // mode.
342  void AssertNotHeld(const Thread* self) {
343    if (kDebugLocking && (gAborting == 0)) {
344      CHECK(!IsSharedHeld(self)) << *this;
345    }
346  }
347
348  // Id associated with exclusive owner. No memory ordering semantics if called from a thread other
349  // than the owner.
350  uint64_t GetExclusiveOwnerTid() const;
351
352  virtual void Dump(std::ostream& os) const;
353
354 private:
355#if ART_USE_FUTEXES
356  // -1 implies held exclusive, +ve shared held by state_ many owners.
357  AtomicInteger state_;
358  // Exclusive owner. Modification guarded by this mutex.
359  volatile uint64_t exclusive_owner_;
360  // Number of contenders waiting for a reader share.
361  AtomicInteger num_pending_readers_;
362  // Number of contenders waiting to be the writer.
363  AtomicInteger num_pending_writers_;
364#else
365  pthread_rwlock_t rwlock_;
366  volatile uint64_t exclusive_owner_;  // Guarded by rwlock_.
367#endif
368  DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex);
369};
370
371// ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually
372// (Signal) or all at once (Broadcast).
373class ConditionVariable {
374 public:
375  explicit ConditionVariable(const char* name, Mutex& mutex);
376  ~ConditionVariable();
377
378  void Broadcast(Thread* self);
379  void Signal(Thread* self);
380  // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their
381  //       pointer copy, thereby defeating annotalysis.
382  void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
383  void TimedWait(Thread* self, int64_t ms, int32_t ns) NO_THREAD_SAFETY_ANALYSIS;
384  // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held
385  // when waiting.
386  // TODO: remove this.
387  void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
388
389 private:
390  const char* const name_;
391  // The Mutex being used by waiters. It is an error to mix condition variables between different
392  // Mutexes.
393  Mutex& guard_;
394#if ART_USE_FUTEXES
395  // A counter that is modified by signals and broadcasts. This ensures that when a waiter gives up
396  // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_
397  // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait
398  // without guard_ held.
399  AtomicInteger sequence_;
400  // Number of threads that have come into to wait, not the length of the waiters on the futex as
401  // waiters may have been requeued onto guard_. Guarded by guard_.
402  volatile int32_t num_waiters_;
403#else
404  pthread_cond_t cond_;
405#endif
406  DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
407};
408
409// Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it
410// upon destruction.
411class SCOPED_LOCKABLE MutexLock {
412 public:
413  explicit MutexLock(Thread* self, Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : self_(self), mu_(mu) {
414    mu_.ExclusiveLock(self_);
415  }
416
417  ~MutexLock() UNLOCK_FUNCTION() {
418    mu_.ExclusiveUnlock(self_);
419  }
420
421 private:
422  Thread* const self_;
423  Mutex& mu_;
424  DISALLOW_COPY_AND_ASSIGN(MutexLock);
425};
426// Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)".
427#define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_declaration_missing_variable_name)
428
429// Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon
430// construction and releases it upon destruction.
431class SCOPED_LOCKABLE ReaderMutexLock {
432 public:
433  explicit ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) :
434      self_(self), mu_(mu) {
435    mu_.SharedLock(self_);
436  }
437
438  ~ReaderMutexLock() UNLOCK_FUNCTION() {
439    mu_.SharedUnlock(self_);
440  }
441
442 private:
443  Thread* const self_;
444  ReaderWriterMutex& mu_;
445  DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock);
446};
447// Catch bug where variable name is omitted. "ReaderMutexLock (lock);" instead of
448// "ReaderMutexLock mu(lock)".
449#define ReaderMutexLock(x) COMPILE_ASSERT(0, reader_mutex_lock_declaration_missing_variable_name)
450
451// Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon
452// construction and releases it upon destruction.
453class SCOPED_LOCKABLE WriterMutexLock {
454 public:
455  explicit WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) :
456      self_(self), mu_(mu) {
457    mu_.ExclusiveLock(self_);
458  }
459
460  ~WriterMutexLock() UNLOCK_FUNCTION() {
461    mu_.ExclusiveUnlock(self_);
462  }
463
464 private:
465  Thread* const self_;
466  ReaderWriterMutex& mu_;
467  DISALLOW_COPY_AND_ASSIGN(WriterMutexLock);
468};
469// Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of
470// "WriterMutexLock mu(lock)".
471#define WriterMutexLock(x) COMPILE_ASSERT(0, writer_mutex_lock_declaration_missing_variable_name)
472
473// Global mutexes corresponding to the levels above.
474class Locks {
475 public:
476  static void Init();
477
478  // There's a potential race for two threads to try to suspend each other and for both of them
479  // to succeed and get blocked becoming runnable. This lock ensures that only one thread is
480  // requesting suspension of another at any time. As the the thread list suspend thread logic
481  // transitions to runnable, if the current thread were tried to be suspended then this thread
482  // would block holding this lock until it could safely request thread suspension of the other
483  // thread without that thread having a suspension request against this thread. This avoids a
484  // potential deadlock cycle.
485  static Mutex* thread_list_suspend_thread_lock_;
486
487  // The mutator_lock_ is used to allow mutators to execute in a shared (reader) mode or to block
488  // mutators by having an exclusive (writer) owner. In normal execution each mutator thread holds
489  // a share on the mutator_lock_. The garbage collector may also execute with shared access but
490  // at times requires exclusive access to the heap (not to be confused with the heap meta-data
491  // guarded by the heap_lock_ below). When the garbage collector requires exclusive access it asks
492  // the mutators to suspend themselves which also involves usage of the thread_suspend_count_lock_
493  // to cover weaknesses in using ReaderWriterMutexes with ConditionVariables. We use a condition
494  // variable to wait upon in the suspension logic as releasing and then re-acquiring a share on
495  // the mutator lock doesn't necessarily allow the exclusive user (e.g the garbage collector)
496  // chance to acquire the lock.
497  //
498  // Thread suspension:
499  // Shared users                                  | Exclusive user
500  // (holding mutator lock and in kRunnable state) |   .. running ..
501  //   .. running ..                               | Request thread suspension by:
502  //   .. running ..                               |   - acquiring thread_suspend_count_lock_
503  //   .. running ..                               |   - incrementing Thread::suspend_count_ on
504  //   .. running ..                               |     all mutator threads
505  //   .. running ..                               |   - releasing thread_suspend_count_lock_
506  //   .. running ..                               | Block trying to acquire exclusive mutator lock
507  // Poll Thread::suspend_count_ and enter full    |   .. blocked ..
508  // suspend code.                                 |   .. blocked ..
509  // Change state to kSuspended                    |   .. blocked ..
510  // x: Release share on mutator_lock_             | Carry out exclusive access
511  // Acquire thread_suspend_count_lock_            |   .. exclusive ..
512  // while Thread::suspend_count_ > 0              |   .. exclusive ..
513  //   - wait on Thread::resume_cond_              |   .. exclusive ..
514  //     (releases thread_suspend_count_lock_)     |   .. exclusive ..
515  //   .. waiting ..                               | Release mutator_lock_
516  //   .. waiting ..                               | Request thread resumption by:
517  //   .. waiting ..                               |   - acquiring thread_suspend_count_lock_
518  //   .. waiting ..                               |   - decrementing Thread::suspend_count_ on
519  //   .. waiting ..                               |     all mutator threads
520  //   .. waiting ..                               |   - notifying on Thread::resume_cond_
521  //    - re-acquire thread_suspend_count_lock_    |   - releasing thread_suspend_count_lock_
522  // Release thread_suspend_count_lock_            |  .. running ..
523  // Acquire share on mutator_lock_                |  .. running ..
524  //  - This could block but the thread still      |  .. running ..
525  //    has a state of kSuspended and so this      |  .. running ..
526  //    isn't an issue.                            |  .. running ..
527  // Acquire thread_suspend_count_lock_            |  .. running ..
528  //  - we poll here as we're transitioning into   |  .. running ..
529  //    kRunnable and an individual thread suspend |  .. running ..
530  //    request (e.g for debugging) won't try      |  .. running ..
531  //    to acquire the mutator lock (which would   |  .. running ..
532  //    block as we hold the mutator lock). This   |  .. running ..
533  //    poll ensures that if the suspender thought |  .. running ..
534  //    we were suspended by incrementing our      |  .. running ..
535  //    Thread::suspend_count_ and then reading    |  .. running ..
536  //    our state we go back to waiting on         |  .. running ..
537  //    Thread::resume_cond_.                      |  .. running ..
538  // can_go_runnable = Thread::suspend_count_ == 0 |  .. running ..
539  // Release thread_suspend_count_lock_            |  .. running ..
540  // if can_go_runnable                            |  .. running ..
541  //   Change state to kRunnable                   |  .. running ..
542  // else                                          |  .. running ..
543  //   Goto x                                      |  .. running ..
544  //  .. running ..                                |  .. running ..
545  static ReaderWriterMutex* mutator_lock_ ACQUIRED_AFTER(thread_list_suspend_thread_lock_);
546
547  // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap.
548  static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_);
549
550  // Guards shutdown of the runtime.
551  static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_);
552
553  // Guards background profiler global state.
554  static Mutex* profiler_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_);
555
556  // Guards trace (ie traceview) requests.
557  static Mutex* trace_lock_ ACQUIRED_AFTER(profiler_lock_);
558
559  // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads
560  // attaching and detaching.
561  static Mutex* thread_list_lock_ ACQUIRED_AFTER(trace_lock_);
562
563  // Guards breakpoints.
564  static Mutex* breakpoint_lock_ ACQUIRED_AFTER(thread_list_lock_);
565
566  // Guards lists of classes within the class linker.
567  static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(breakpoint_lock_);
568
569  // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code
570  // doesn't try to hold a higher level Mutex.
571  #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(Locks::classlinker_classes_lock_)
572
573  static Mutex* allocated_monitor_ids_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
574
575  // Guard the allocation/deallocation of thread ids.
576  static Mutex* allocated_thread_ids_lock_ ACQUIRED_AFTER(allocated_monitor_ids_lock_);
577
578  // Guards modification of the LDT on x86.
579  static Mutex* modify_ldt_lock_ ACQUIRED_AFTER(allocated_thread_ids_lock_);
580
581  // Guards intern table.
582  static Mutex* intern_table_lock_ ACQUIRED_AFTER(modify_ldt_lock_);
583
584  // Have an exclusive aborting thread.
585  static Mutex* abort_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
586
587  // Allow mutual exclusion when manipulating Thread::suspend_count_.
588  // TODO: Does the trade-off of a per-thread lock make sense?
589  static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_);
590
591  // One unexpected signal at a time lock.
592  static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_);
593
594  // Guards the maps in mem_map.
595  static Mutex* mem_maps_lock_ ACQUIRED_AFTER(unexpected_signal_lock_);
596
597  // Have an exclusive logging thread.
598  static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_);
599};
600
601}  // namespace art
602
603#endif  // ART_RUNTIME_BASE_MUTEX_H_
604