mutex.h revision c7190697f8665e706f6ebb4ae36fa63c46a32cd5
18daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes/*
28daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Copyright (C) 2011 The Android Open Source Project
38daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes *
48daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
58daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * you may not use this file except in compliance with the License.
68daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * You may obtain a copy of the License at
78daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes *
88daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
98daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes *
108daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Unless required by applicable law or agreed to in writing, software
118daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
128daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * See the License for the specific language governing permissions and
148daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * limitations under the License.
158daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes */
168daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_BASE_MUTEX_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_BASE_MUTEX_H_
198daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
208daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include <pthread.h>
21cd74c4b3a6893c876c6e03fd99a1264249653d80Brian Carlstrom#include <stdint.h>
22ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes
23ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes#include <iosfwd>
248daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include <string>
258daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
26ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers#include "atomic.h"
2707ed66b5ae659c452cbe1ab20c3dbf1d6f546461Elliott Hughes#include "base/logging.h"
28761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/macros.h"
2900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "globals.h"
3081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
31ab47016374d340011680e9cd970ee7d6225d6704Ian Rogers#if defined(__APPLE__)
3281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#define ART_USE_FUTEXES 0
33ab47016374d340011680e9cd970ee7d6225d6704Ian Rogers#else
34c01417898a6e4f8a5663d6c0556982488c133cdfChris Dearman#define ART_USE_FUTEXES 1
35ab47016374d340011680e9cd970ee7d6225d6704Ian Rogers#endif
368daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
3766aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers// Currently Darwin doesn't support locks with timeouts.
3866aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#if !defined(__APPLE__)
3966aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#define HAVE_TIMED_RWLOCK 1
4066aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#else
4166aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#define HAVE_TIMED_RWLOCK 0
4266aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#endif
4366aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers
448daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesnamespace art {
458daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
46719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersclass LOCKABLE ReaderWriterMutex;
4756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogersclass ScopedContentionRecorder;
4850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogersclass Thread;
4950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers
50719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// LockLevel is used to impose a lock hierarchy [1] where acquisition of a Mutex at a higher or
51719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// equal level to a lock a thread holds is invalid. The lock hierarchy achieves a cycle free
52719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// partial ordering and thereby cause deadlock situations to fail checks.
53719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers//
54719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// [1] http://www.drdobbs.com/parallel/use-lock-hierarchies-to-avoid-deadlock/204801163
55719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersenum LockLevel {
56719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kLoggingLock = 0,
573eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi  kMemMapsLock,
587de77dd4f2d3cbb0615ee001589eb99ae82c3dccRaghu Gandham  kSwapMutexesLock,
59719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kUnexpectedSignalLock,
60719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kThreadSuspendCountLock,
61719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kAbortLock,
62719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpSocketLock,
63719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kRosAllocGlobalLock,
64719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kRosAllocBracketLock,
65719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kRosAllocBulkFreeLock,
66719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kAllocSpaceLock,
672d1ab0a7bf7639d1af0c453415f9625110c34f6dMathieu Chartier  kReferenceProcessorLock,
68719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kDexFileMethodInlinerLock,
69719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kDexFileToMethodInlinerMapLock,
70719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMarkSweepMarkStackLock,
71719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kTransactionLogLock,
72719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kInternTableLock,
73719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMonitorPoolLock,
74719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kDefaultMutexLevel,
75719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMarkSweepLargeObjectLock,
76719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kPinTableLock,
77719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kLoadLibraryLock,
78719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpObjectRegistryLock,
799e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  kModifyLdtLock,
809e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  kAllocatedThreadIdsLock,
81719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kClassLinkerClassesLock,
82719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kBreakpointLock,
83719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMonitorLock,
84440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier  kMonitorListLock,
85719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kThreadListLock,
86719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kBreakpointInvokeLock,
87719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kDeoptimizationLock,
88719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kTraceLock,
89719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kProfilerLock,
90719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpEventListLock,
91719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpAttachLock,
92719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpStartLock,
93719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kRuntimeShutdownLock,
94719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kHeapBitmapLock,
95719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMutatorLock,
96719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kZygoteCreationLock,
97719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
98719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kLockLevelCount  // Must come last.
99719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers};
100719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstd::ostream& operator<<(std::ostream& os, const LockLevel& rhs);
101719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
1022e250c826b3c405d675017efe79e5db3651c9ee6Brian Carlstromconst bool kDebugLocking = kIsDebugBuild;
10325fd14b87cced64a179dee885573113be5e11944Ian Rogers
1041afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Record Log contention information, dumpable via SIGQUIT.
1051afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#ifdef ART_USE_FUTEXES
10608f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Hao// To enable lock contention logging, set this to true.
10708f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Haoconst bool kLogLockContentions = false;
1081afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#else
1091afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Keep this false as lock contention logging is supported only with
1101afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// futex.
1111afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst bool kLogLockContentions = false;
1121afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#endif
113d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersconst size_t kContentionLogSize = 4;
1141afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kContentionLogDataSize = kLogLockContentions ? 1 : 0;
1151afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kAllMutexDataSize = kLogLockContentions ? 1 : 0;
1161afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi
11700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Base class for all Mutex implementations
11800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass BaseMutex {
11900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
120bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers  const char* GetName() const {
12100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return name_;
12200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
12300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
12400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool IsMutex() const { return false; }
12500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool IsReaderWriterMutex() const { return false; }
12600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
12756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  virtual void Dump(std::ostream& os) const = 0;
12856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
12956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  static void DumpAll(std::ostream& os);
13056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
13100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers protected:
13200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  friend class ConditionVariable;
13300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
13481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  BaseMutex(const char* name, LockLevel level);
13556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  virtual ~BaseMutex();
13681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void RegisterAsLocked(Thread* self);
13781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void RegisterAsUnlocked(Thread* self);
13881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void CheckSafeToWait(Thread* self);
13900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
14056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  friend class ScopedContentionRecorder;
14156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
1421afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  void RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t nano_time_blocked);
14356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  void DumpContention(std::ostream& os) const;
14456edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
14581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  const LockLevel level_;  // Support for lock hierarchy.
146bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers  const char* const name_;
1471afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi
14856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  // A log entry that records contention but makes no guarantee that either tid will be held live.
14956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  struct ContentionLogEntry {
15056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers    ContentionLogEntry() : blocked_tid(0), owner_tid(0) {}
15156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers    uint64_t blocked_tid;
15256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers    uint64_t owner_tid;
15356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers    AtomicInteger count;
15456edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  };
1551afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  struct ContentionLogData {
1561afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    ContentionLogEntry contention_log[kContentionLogSize];
1571afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    // The next entry in the contention log to be updated. Value ranges from 0 to
1581afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    // kContentionLogSize - 1.
1591afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    AtomicInteger cur_content_log_entry;
1601afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    // Number of times the Mutex has been contended.
1611afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    AtomicInteger contention_count;
1621afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    // Sum of time waited by all contenders in ns.
1631afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    volatile uint64_t wait_time;
1641afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    void AddToWaitTime(uint64_t value);
1651afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    ContentionLogData() : wait_time(0) {}
1661afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  };
1673e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers  ContentionLogData contention_log_data_[kContentionLogDataSize];
1681afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi
1691afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi public:
1701afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  bool HasEverContended() const {
1711afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    if (kLogLockContentions) {
1723e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers      return contention_log_data_->contention_count.LoadSequentiallyConsistent() > 0;
1731afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    }
1741afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    return false;
1751afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  }
17600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers};
17700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
17800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain
17900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// exclusive access to what it guards. A Mutex can be in one of two states:
18000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread,
18100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread.
18200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers//
18300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is:
18400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State     | ExclusiveLock | ExclusiveUnlock
18500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// -------------------------------------------
18600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free      | Exclusive     | error
18700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block*        | Free
18800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in
18900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers//   an error. Being non-reentrant simplifies Waiting on ConditionVariables.
19001ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const Mutex& mu);
19100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE Mutex : public BaseMutex {
19200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
19381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false);
19400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~Mutex();
19500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
19600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool IsMutex() const { return true; }
19700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
19800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Block until mutex is free then acquire exclusive access.
19981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION();
20081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void Lock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() {  ExclusiveLock(self); }
20100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
20200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Returns true if acquires exclusive access, false otherwise.
20381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool ExclusiveTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true);
20481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool TryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true) { return ExclusiveTryLock(self); }
20500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
20600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Release exclusive access.
20781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION();
20881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void Unlock(Thread* self) UNLOCK_FUNCTION() {  ExclusiveUnlock(self); }
20900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
21000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Is the current thread the exclusive holder of the Mutex.
21181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool IsExclusiveHeld(const Thread* self) const;
2128daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
21300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert that the Mutex is exclusively held by the current thread.
21481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertExclusiveHeld(const Thread* self) {
2153c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum    if (kDebugLocking && (gAborting == 0)) {
21601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers      CHECK(IsExclusiveHeld(self)) << *this;
21700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
21800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
21981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertHeld(const Thread* self) { AssertExclusiveHeld(self); }
22000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
22100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert that the Mutex is not held by the current thread.
22281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertNotHeldExclusive(const Thread* self) {
2233c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum    if (kDebugLocking && (gAborting == 0)) {
22401ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers      CHECK(!IsExclusiveHeld(self)) << *this;
22500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
22600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
22781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertNotHeld(const Thread* self) { AssertNotHeldExclusive(self); }
22800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
229c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // Id associated with exclusive owner. No memory ordering semantics if called from a thread other
230c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // than the owner.
23100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  uint64_t GetExclusiveOwnerTid() const;
23200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
23300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld.
23400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  unsigned int GetDepth() const {
23500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return recursion_count_;
23600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
23700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
23856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  virtual void Dump(std::ostream& os) const;
23901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers
24000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private:
241c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES
242c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // 0 is unheld, 1 is held.
243c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  AtomicInteger state_;
244c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // Exclusive owner.
245c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  volatile uint64_t exclusive_owner_;
246c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // Number of waiting contenders.
247b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers  AtomicInteger num_contenders_;
248c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else
24900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  pthread_mutex_t mutex_;
250c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers  volatile uint64_t exclusive_owner_;  // Guarded by mutex_.
251c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif
25200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  const bool recursive_;  // Can the lock be recursively held?
25300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  unsigned int recursion_count_;
254f1498437b0d6beb9f4f91980b98cbeb0b5c773ceElliott Hughes  friend class ConditionVariable;
2558daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(Mutex);
2568daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes};
2578daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
25800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex.
25900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader)
26000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a
26100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// condition variable. A ReaderWriterMutex can be in one of three states:
26200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread,
26300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread,
26400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Shared(n) - shared amongst n threads.
26500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers//
26600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is:
26700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers//
26800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State     | ExclusiveLock | ExclusiveUnlock | SharedLock       | SharedUnlock
26900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ----------------------------------------------------------------------------
27000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free      | Exclusive     | error           | SharedLock(1)    | error
27100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block         | Free            | Block            | error
27200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Shared(n) | Block         | error           | SharedLock(n+1)* | Shared(n-1) or Free
27300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * for large values of n the SharedLock may block.
27401ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu);
27500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE ReaderWriterMutex : public BaseMutex {
2768daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes public:
27781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel);
27800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~ReaderWriterMutex();
27900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
28000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool IsReaderWriterMutex() const { return true; }
28100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
28200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Block until ReaderWriterMutex is free then acquire exclusive access.
28381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION();
28481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void WriterLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() {  ExclusiveLock(self); }
28500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
28600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Release exclusive access.
28781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION();
28881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void WriterUnlock(Thread* self) UNLOCK_FUNCTION() {  ExclusiveUnlock(self); }
28900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
29000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success
29100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // or false if timeout is reached.
29266aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#if HAVE_TIMED_RWLOCK
293c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns)
29481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers      EXCLUSIVE_TRYLOCK_FUNCTION(true);
29566aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#endif
29600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
29700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Block until ReaderWriterMutex is shared or free then acquire a share on the access.
2981ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers  void SharedLock(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE;
29981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void ReaderLock(Thread* self) SHARED_LOCK_FUNCTION() { SharedLock(self); }
30000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
30100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Try to acquire share of ReaderWriterMutex.
30281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool SharedTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true);
30300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
30400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Release a share of the access.
3051ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers  void SharedUnlock(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE;
30681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void ReaderUnlock(Thread* self) UNLOCK_FUNCTION() { SharedUnlock(self); }
30700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
30800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Is the current thread the exclusive holder of the ReaderWriterMutex.
30981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool IsExclusiveHeld(const Thread* self) const;
31000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
31100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert the current thread has exclusive access to the ReaderWriterMutex.
31281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertExclusiveHeld(const Thread* self) {
313702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz    if (kDebugLocking && (gAborting == 0)) {
31401ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers      CHECK(IsExclusiveHeld(self)) << *this;
31500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
3168daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
31781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertWriterHeld(const Thread* self) { AssertExclusiveHeld(self); }
3188daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
31900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex.
32081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertNotExclusiveHeld(const Thread* self) {
321702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz    if (kDebugLocking && (gAborting == 0)) {
322e3359f7ad7671c5816f17145ca3a01516512e8d6Ian Rogers      CHECK(!IsExclusiveHeld(self)) << *this;
32300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
32400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
32581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertNotWriterHeld(const Thread* self) { AssertNotExclusiveHeld(self); }
32600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
32700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Is the current thread a shared holder of the ReaderWriterMutex.
32881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool IsSharedHeld(const Thread* self) const;
32900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
33000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert the current thread has shared access to the ReaderWriterMutex.
33181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertSharedHeld(const Thread* self) {
332702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz    if (kDebugLocking && (gAborting == 0)) {
33323055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers      // TODO: we can only assert this well when self != NULL.
33423055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers      CHECK(IsSharedHeld(self) || self == NULL) << *this;
33500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
3368daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
33781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertReaderHeld(const Thread* self) { AssertSharedHeld(self); }
3388daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
33900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive
34000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // mode.
34181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertNotHeld(const Thread* self) {
3423c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum    if (kDebugLocking && (gAborting == 0)) {
34301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers      CHECK(!IsSharedHeld(self)) << *this;
34400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
34500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
34600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
347c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // Id associated with exclusive owner. No memory ordering semantics if called from a thread other
348c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // than the owner.
34900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  uint64_t GetExclusiveOwnerTid() const;
35081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
35156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  virtual void Dump(std::ostream& os) const;
35201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers
3538daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes private:
35481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#if ART_USE_FUTEXES
35581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  // -1 implies held exclusive, +ve shared held by state_ many owners.
356c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  AtomicInteger state_;
357c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // Exclusive owner. Modification guarded by this mutex.
35881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  volatile uint64_t exclusive_owner_;
359c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // Number of contenders waiting for a reader share.
360c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  AtomicInteger num_pending_readers_;
361c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // Number of contenders waiting to be the writer.
362b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers  AtomicInteger num_pending_writers_;
36381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#else
36400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  pthread_rwlock_t rwlock_;
365c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers  volatile uint64_t exclusive_owner_;  // Guarded by rwlock_.
36681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#endif
36700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex);
3688daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes};
3698daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
37000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually
37100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// (Signal) or all at once (Broadcast).
3725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesclass ConditionVariable {
3735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes public:
37423055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers  explicit ConditionVariable(const char* name, Mutex& mutex);
3755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  ~ConditionVariable();
3765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
377c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  void Broadcast(Thread* self);
378c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  void Signal(Thread* self);
379c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their
380c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  //       pointer copy, thereby defeating annotalysis.
381c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
382c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  void TimedWait(Thread* self, int64_t ms, int32_t ns) NO_THREAD_SAFETY_ANALYSIS;
3831d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held
3841d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  // when waiting.
3851d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  // TODO: remove this.
3861d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
3875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
3885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes private:
38923055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers  const char* const name_;
390c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // The Mutex being used by waiters. It is an error to mix condition variables between different
391c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // Mutexes.
392c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  Mutex& guard_;
393c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES
394c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // A counter that is modified by signals and broadcasts. This ensures that when a waiter gives up
395d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers  // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_
396d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers  // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait
397d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers  // without guard_ held.
398b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers  AtomicInteger sequence_;
399c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // Number of threads that have come into to wait, not the length of the waiters on the futex as
4005bd97c483c1de1eb97afe76123b1b9ab53095edfIan Rogers  // waiters may have been requeued onto guard_. Guarded by guard_.
401c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  volatile int32_t num_waiters_;
402c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else
403c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  pthread_cond_t cond_;
404c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif
4055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
4065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes};
4075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
40800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it
40900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// upon destruction.
41000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE MutexLock {
41100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
41281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  explicit MutexLock(Thread* self, Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : self_(self), mu_(mu) {
41381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.ExclusiveLock(self_);
41481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  }
41581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
41600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~MutexLock() UNLOCK_FUNCTION() {
41781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.ExclusiveUnlock(self_);
41800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
41900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
42000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private:
42181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  Thread* const self_;
42200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Mutex& mu_;
42300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  DISALLOW_COPY_AND_ASSIGN(MutexLock);
42400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers};
42500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)".
42600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_declaration_missing_variable_name)
42700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
42800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon
42900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction.
43000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE ReaderMutexLock {
43100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
43281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  explicit ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) :
43381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers      self_(self), mu_(mu) {
43481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.SharedLock(self_);
43581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  }
43681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
43700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~ReaderMutexLock() UNLOCK_FUNCTION() {
43881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.SharedUnlock(self_);
43900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
44000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
44100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private:
44281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  Thread* const self_;
44300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ReaderWriterMutex& mu_;
44400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock);
44500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers};
44600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "ReaderMutexLock (lock);" instead of
44700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "ReaderMutexLock mu(lock)".
44800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define ReaderMutexLock(x) COMPILE_ASSERT(0, reader_mutex_lock_declaration_missing_variable_name)
44900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
45000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon
45100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction.
45200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE WriterMutexLock {
45300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
45481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  explicit WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) :
45581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers      self_(self), mu_(mu) {
45681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.ExclusiveLock(self_);
45781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  }
45881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
45900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~WriterMutexLock() UNLOCK_FUNCTION() {
46081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.ExclusiveUnlock(self_);
46100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
46200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
46300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private:
46450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* const self_;
46500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ReaderWriterMutex& mu_;
46600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  DISALLOW_COPY_AND_ASSIGN(WriterMutexLock);
46700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers};
46800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of
46900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "WriterMutexLock mu(lock)".
47000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define WriterMutexLock(x) COMPILE_ASSERT(0, writer_mutex_lock_declaration_missing_variable_name)
47100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
472719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Global mutexes corresponding to the levels above.
473719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersclass Locks {
474719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers public:
475719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static void Init();
476719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
477719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // The mutator_lock_ is used to allow mutators to execute in a shared (reader) mode or to block
478719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // mutators by having an exclusive (writer) owner. In normal execution each mutator thread holds
479719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // a share on the mutator_lock_. The garbage collector may also execute with shared access but
480719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // at times requires exclusive access to the heap (not to be confused with the heap meta-data
481719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // guarded by the heap_lock_ below). When the garbage collector requires exclusive access it asks
482719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // the mutators to suspend themselves which also involves usage of the thread_suspend_count_lock_
483719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // to cover weaknesses in using ReaderWriterMutexes with ConditionVariables. We use a condition
484719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // variable to wait upon in the suspension logic as releasing and then re-acquiring a share on
485719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // the mutator lock doesn't necessarily allow the exclusive user (e.g the garbage collector)
486719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // chance to acquire the lock.
487719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //
488719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Thread suspension:
489719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Shared users                                  | Exclusive user
490719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // (holding mutator lock and in kRunnable state) |   .. running ..
491719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               | Request thread suspension by:
492719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               |   - acquiring thread_suspend_count_lock_
493719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               |   - incrementing Thread::suspend_count_ on
494719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               |     all mutator threads
495719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               |   - releasing thread_suspend_count_lock_
496719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               | Block trying to acquire exclusive mutator lock
497719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Poll Thread::suspend_count_ and enter full    |   .. blocked ..
498719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // suspend code.                                 |   .. blocked ..
499719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Change state to kSuspended                    |   .. blocked ..
500719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // x: Release share on mutator_lock_             | Carry out exclusive access
501719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Acquire thread_suspend_count_lock_            |   .. exclusive ..
502719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // while Thread::suspend_count_ > 0              |   .. exclusive ..
503719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   - wait on Thread::resume_cond_              |   .. exclusive ..
504719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //     (releases thread_suspend_count_lock_)     |   .. exclusive ..
505719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               | Release mutator_lock_
506719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               | Request thread resumption by:
507719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               |   - acquiring thread_suspend_count_lock_
508719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               |   - decrementing Thread::suspend_count_ on
509719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               |     all mutator threads
510719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               |   - notifying on Thread::resume_cond_
511719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    - re-acquire thread_suspend_count_lock_    |   - releasing thread_suspend_count_lock_
512719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Release thread_suspend_count_lock_            |  .. running ..
513719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Acquire share on mutator_lock_                |  .. running ..
514719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //  - This could block but the thread still      |  .. running ..
515719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    has a state of kSuspended and so this      |  .. running ..
516719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    isn't an issue.                            |  .. running ..
517719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Acquire thread_suspend_count_lock_            |  .. running ..
518719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //  - we poll here as we're transitioning into   |  .. running ..
519719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    kRunnable and an individual thread suspend |  .. running ..
520719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    request (e.g for debugging) won't try      |  .. running ..
521719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    to acquire the mutator lock (which would   |  .. running ..
522719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    block as we hold the mutator lock). This   |  .. running ..
523719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    poll ensures that if the suspender thought |  .. running ..
524719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    we were suspended by incrementing our      |  .. running ..
525719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    Thread::suspend_count_ and then reading    |  .. running ..
526719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    our state we go back to waiting on         |  .. running ..
527719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    Thread::resume_cond_.                      |  .. running ..
528719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // can_go_runnable = Thread::suspend_count_ == 0 |  .. running ..
529719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Release thread_suspend_count_lock_            |  .. running ..
530719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // if can_go_runnable                            |  .. running ..
531719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   Change state to kRunnable                   |  .. running ..
532719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // else                                          |  .. running ..
533719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   Goto x                                      |  .. running ..
534719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //  .. running ..                                |  .. running ..
535719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static ReaderWriterMutex* mutator_lock_;
536719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
537719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap.
538719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_);
539719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
540719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Guards shutdown of the runtime.
541719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_);
542719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
5439e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  // Guards background profiler global state.
5449e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* profiler_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_);
5459e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu
5469e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  // Guards trace (ie traceview) requests.
5479e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* trace_lock_ ACQUIRED_AFTER(profiler_lock_);
5489e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu
549719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads
550719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // attaching and detaching.
5519e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* thread_list_lock_ ACQUIRED_AFTER(trace_lock_);
552719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
553719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Guards breakpoints.
554719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* breakpoint_lock_ ACQUIRED_AFTER(thread_list_lock_);
555719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
556719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Guards lists of classes within the class linker.
5579e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(breakpoint_lock_);
558719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
559719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code
560719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // doesn't try to hold a higher level Mutex.
561719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(Locks::classlinker_classes_lock_)
562719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
5639e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  // Guard the allocation/deallocation of thread ids.
5649e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* allocated_thread_ids_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
5659e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu
5669e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  // Guards modification of the LDT on x86.
5679e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* modify_ldt_lock_ ACQUIRED_AFTER(allocated_thread_ids_lock_);
5689e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu
569719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Guards intern table.
5709e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* intern_table_lock_ ACQUIRED_AFTER(modify_ldt_lock_);
571719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
572719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Have an exclusive aborting thread.
573719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* abort_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
574719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
575719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Allow mutual exclusion when manipulating Thread::suspend_count_.
576719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // TODO: Does the trade-off of a per-thread lock make sense?
577719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_);
578719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
579719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // One unexpected signal at a time lock.
580719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_);
581719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
5823eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi  // Guards the maps in mem_map.
5833eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi  static Mutex* mem_maps_lock_ ACQUIRED_AFTER(unexpected_signal_lock_);
5843eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi
585719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Have an exclusive logging thread.
586719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_);
587719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers};
588719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
5898daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}  // namespace art
5908daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
591fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_BASE_MUTEX_H_
592