mutex.h revision c5f17732d8144491c642776b6b48c85dfadf4b52
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,
58719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kUnexpectedSignalLock,
59719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kThreadSuspendCountLock,
60719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kAbortLock,
61719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpSocketLock,
62719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kRosAllocGlobalLock,
63719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kRosAllocBracketLock,
64719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kRosAllocBulkFreeLock,
65719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kAllocSpaceLock,
662d1ab0a7bf7639d1af0c453415f9625110c34f6dMathieu Chartier  kReferenceProcessorLock,
67719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kDexFileMethodInlinerLock,
68719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kDexFileToMethodInlinerMapLock,
69719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMarkSweepMarkStackLock,
70719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kTransactionLogLock,
71719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kInternTableLock,
72719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMonitorPoolLock,
73719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kDefaultMutexLevel,
74719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMarkSweepLargeObjectLock,
75719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kPinTableLock,
76719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kLoadLibraryLock,
77719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpObjectRegistryLock,
789e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  kModifyLdtLock,
799e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  kAllocatedThreadIdsLock,
80719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kClassLinkerClassesLock,
81719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kBreakpointLock,
82719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMonitorLock,
83440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier  kMonitorListLock,
84719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kThreadListLock,
85719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kBreakpointInvokeLock,
86719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kDeoptimizationLock,
87719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kTraceLock,
88719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kProfilerLock,
89719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpEventListLock,
90719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpAttachLock,
91719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpStartLock,
92719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kRuntimeShutdownLock,
93719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kHeapBitmapLock,
94719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMutatorLock,
95719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kZygoteCreationLock,
96719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
97719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kLockLevelCount  // Must come last.
98719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers};
99719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstd::ostream& operator<<(std::ostream& os, const LockLevel& rhs);
100719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
1012e250c826b3c405d675017efe79e5db3651c9ee6Brian Carlstromconst bool kDebugLocking = kIsDebugBuild;
10225fd14b87cced64a179dee885573113be5e11944Ian Rogers
1031afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Record Log contention information, dumpable via SIGQUIT.
1041afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#ifdef ART_USE_FUTEXES
10508f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Hao// To enable lock contention logging, set this to true.
10608f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Haoconst bool kLogLockContentions = false;
1071afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#else
1081afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Keep this false as lock contention logging is supported only with
1091afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// futex.
1101afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst bool kLogLockContentions = false;
1111afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#endif
112d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersconst size_t kContentionLogSize = 4;
1131afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kContentionLogDataSize = kLogLockContentions ? 1 : 0;
1141afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kAllMutexDataSize = kLogLockContentions ? 1 : 0;
1151afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi
11600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Base class for all Mutex implementations
11700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass BaseMutex {
11800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
119bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers  const char* GetName() const {
12000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return name_;
12100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
12200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
12300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool IsMutex() const { return false; }
12400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool IsReaderWriterMutex() const { return false; }
12500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
12656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  virtual void Dump(std::ostream& os) const = 0;
12756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
12856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  static void DumpAll(std::ostream& os);
12956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
13000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers protected:
13100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  friend class ConditionVariable;
13200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
13381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  BaseMutex(const char* name, LockLevel level);
13456edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  virtual ~BaseMutex();
13581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void RegisterAsLocked(Thread* self);
13681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void RegisterAsUnlocked(Thread* self);
13781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void CheckSafeToWait(Thread* self);
13800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
13956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  friend class ScopedContentionRecorder;
14056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
1411afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  void RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t nano_time_blocked);
14256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  void DumpContention(std::ostream& os) const;
14356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
14481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  const LockLevel level_;  // Support for lock hierarchy.
145bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers  const char* const name_;
1461afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi
14756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  // A log entry that records contention but makes no guarantee that either tid will be held live.
14856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  struct ContentionLogEntry {
14956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers    ContentionLogEntry() : blocked_tid(0), owner_tid(0) {}
15056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers    uint64_t blocked_tid;
15156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers    uint64_t owner_tid;
15256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers    AtomicInteger count;
15356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  };
1541afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  struct ContentionLogData {
1551afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    ContentionLogEntry contention_log[kContentionLogSize];
1561afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    // The next entry in the contention log to be updated. Value ranges from 0 to
1571afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    // kContentionLogSize - 1.
1581afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    AtomicInteger cur_content_log_entry;
1591afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    // Number of times the Mutex has been contended.
1601afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    AtomicInteger contention_count;
1611afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    // Sum of time waited by all contenders in ns.
1621afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    volatile uint64_t wait_time;
1631afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    void AddToWaitTime(uint64_t value);
1641afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    ContentionLogData() : wait_time(0) {}
1651afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  };
1663e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers  ContentionLogData contention_log_data_[kContentionLogDataSize];
1671afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi
1681afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi public:
1691afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  bool HasEverContended() const {
1701afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    if (kLogLockContentions) {
1713e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers      return contention_log_data_->contention_count.LoadSequentiallyConsistent() > 0;
1721afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    }
1731afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    return false;
1741afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  }
17500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers};
17600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
17700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain
17800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// exclusive access to what it guards. A Mutex can be in one of two states:
17900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread,
18000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread.
18100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers//
18200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is:
18300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State     | ExclusiveLock | ExclusiveUnlock
18400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// -------------------------------------------
18500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free      | Exclusive     | error
18600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block*        | Free
18700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in
18800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers//   an error. Being non-reentrant simplifies Waiting on ConditionVariables.
18901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const Mutex& mu);
19000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE Mutex : public BaseMutex {
19100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
19281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false);
19300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~Mutex();
19400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
19500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool IsMutex() const { return true; }
19600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
19700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Block until mutex is free then acquire exclusive access.
19881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION();
19981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void Lock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() {  ExclusiveLock(self); }
20000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
20100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Returns true if acquires exclusive access, false otherwise.
20281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool ExclusiveTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true);
20381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool TryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true) { return ExclusiveTryLock(self); }
20400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
20500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Release exclusive access.
20681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION();
20781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void Unlock(Thread* self) UNLOCK_FUNCTION() {  ExclusiveUnlock(self); }
20800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
20900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Is the current thread the exclusive holder of the Mutex.
21081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool IsExclusiveHeld(const Thread* self) const;
2118daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
21200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert that the Mutex is exclusively held by the current thread.
21381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertExclusiveHeld(const Thread* self) {
2143c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum    if (kDebugLocking && (gAborting == 0)) {
21501ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers      CHECK(IsExclusiveHeld(self)) << *this;
21600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
21700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
21881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertHeld(const Thread* self) { AssertExclusiveHeld(self); }
21900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
22000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert that the Mutex is not held by the current thread.
22181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertNotHeldExclusive(const Thread* self) {
2223c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum    if (kDebugLocking && (gAborting == 0)) {
22301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers      CHECK(!IsExclusiveHeld(self)) << *this;
22400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
22500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
22681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertNotHeld(const Thread* self) { AssertNotHeldExclusive(self); }
22700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
22800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Id associated with exclusive owner.
22900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  uint64_t GetExclusiveOwnerTid() const;
23000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
23100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld.
23200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  unsigned int GetDepth() const {
23300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return recursion_count_;
23400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
23500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
23656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  virtual void Dump(std::ostream& os) const;
23701ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers
23800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private:
239c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES
240c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // 0 is unheld, 1 is held.
241c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  volatile int32_t state_;
242c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // Exclusive owner.
243c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  volatile uint64_t exclusive_owner_;
244c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // Number of waiting contenders.
245b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers  AtomicInteger num_contenders_;
246c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else
24700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  pthread_mutex_t mutex_;
248c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers  volatile uint64_t exclusive_owner_;  // Guarded by mutex_.
249c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif
25000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  const bool recursive_;  // Can the lock be recursively held?
25100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  unsigned int recursion_count_;
252f1498437b0d6beb9f4f91980b98cbeb0b5c773ceElliott Hughes  friend class ConditionVariable;
2538daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(Mutex);
2548daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes};
2558daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
25600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex.
25700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader)
25800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a
25900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// condition variable. A ReaderWriterMutex can be in one of three states:
26000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread,
26100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread,
26200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Shared(n) - shared amongst n threads.
26300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers//
26400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is:
26500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers//
26600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State     | ExclusiveLock | ExclusiveUnlock | SharedLock       | SharedUnlock
26700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ----------------------------------------------------------------------------
26800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free      | Exclusive     | error           | SharedLock(1)    | error
26900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block         | Free            | Block            | error
27000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Shared(n) | Block         | error           | SharedLock(n+1)* | Shared(n-1) or Free
27100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * for large values of n the SharedLock may block.
27201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu);
27300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE ReaderWriterMutex : public BaseMutex {
2748daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes public:
27581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel);
27600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~ReaderWriterMutex();
27700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
27800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool IsReaderWriterMutex() const { return true; }
27900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
28000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Block until ReaderWriterMutex is free then acquire exclusive access.
28181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION();
28281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void WriterLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() {  ExclusiveLock(self); }
28300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
28400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Release exclusive access.
28581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION();
28681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void WriterUnlock(Thread* self) UNLOCK_FUNCTION() {  ExclusiveUnlock(self); }
28700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
28800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success
28900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // or false if timeout is reached.
29066aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#if HAVE_TIMED_RWLOCK
291c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns)
29281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers      EXCLUSIVE_TRYLOCK_FUNCTION(true);
29366aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#endif
29400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
29500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Block until ReaderWriterMutex is shared or free then acquire a share on the access.
2961ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers  void SharedLock(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE;
29781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void ReaderLock(Thread* self) SHARED_LOCK_FUNCTION() { SharedLock(self); }
29800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
29900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Try to acquire share of ReaderWriterMutex.
30081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool SharedTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true);
30100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
30200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Release a share of the access.
3031ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers  void SharedUnlock(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE;
30481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void ReaderUnlock(Thread* self) UNLOCK_FUNCTION() { SharedUnlock(self); }
30500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
30600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Is the current thread the exclusive holder of the ReaderWriterMutex.
30781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool IsExclusiveHeld(const Thread* self) const;
30800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
30900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert the current thread has exclusive access to the ReaderWriterMutex.
31081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertExclusiveHeld(const Thread* self) {
311702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz    if (kDebugLocking && (gAborting == 0)) {
31201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers      CHECK(IsExclusiveHeld(self)) << *this;
31300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
3148daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
31581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertWriterHeld(const Thread* self) { AssertExclusiveHeld(self); }
3168daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
31700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex.
31881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertNotExclusiveHeld(const Thread* self) {
319702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz    if (kDebugLocking && (gAborting == 0)) {
320e3359f7ad7671c5816f17145ca3a01516512e8d6Ian Rogers      CHECK(!IsExclusiveHeld(self)) << *this;
32100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
32200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
32381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertNotWriterHeld(const Thread* self) { AssertNotExclusiveHeld(self); }
32400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
32500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Is the current thread a shared holder of the ReaderWriterMutex.
32681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool IsSharedHeld(const Thread* self) const;
32700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
32800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert the current thread has shared access to the ReaderWriterMutex.
32981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertSharedHeld(const Thread* self) {
330702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz    if (kDebugLocking && (gAborting == 0)) {
33123055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers      // TODO: we can only assert this well when self != NULL.
33223055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers      CHECK(IsSharedHeld(self) || self == NULL) << *this;
33300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
3348daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
33581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertReaderHeld(const Thread* self) { AssertSharedHeld(self); }
3368daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
33700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive
33800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // mode.
33981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void AssertNotHeld(const Thread* self) {
3403c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum    if (kDebugLocking && (gAborting == 0)) {
34101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers      CHECK(!IsSharedHeld(self)) << *this;
34200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
34300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
34400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
34500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Id associated with exclusive owner.
34600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  uint64_t GetExclusiveOwnerTid() const;
34781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
34856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  virtual void Dump(std::ostream& os) const;
34901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers
3508daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes private:
35181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#if ART_USE_FUTEXES
35281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  // -1 implies held exclusive, +ve shared held by state_ many owners.
35381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  volatile int32_t state_;
35481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  // Exclusive owner.
35581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  volatile uint64_t exclusive_owner_;
35681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  // Pending readers.
35781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  volatile int32_t num_pending_readers_;
35881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  // Pending writers.
359b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers  AtomicInteger num_pending_writers_;
36081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#else
36100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  pthread_rwlock_t rwlock_;
362c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers  volatile uint64_t exclusive_owner_;  // Guarded by rwlock_.
36381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#endif
36400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex);
3658daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes};
3668daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
36700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually
36800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// (Signal) or all at once (Broadcast).
3695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesclass ConditionVariable {
3705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes public:
37123055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers  explicit ConditionVariable(const char* name, Mutex& mutex);
3725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  ~ConditionVariable();
3735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
374c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  void Broadcast(Thread* self);
375c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  void Signal(Thread* self);
376c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their
377c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  //       pointer copy, thereby defeating annotalysis.
378c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
379c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  void TimedWait(Thread* self, int64_t ms, int32_t ns) NO_THREAD_SAFETY_ANALYSIS;
3801d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held
3811d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  // when waiting.
3821d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  // TODO: remove this.
3831d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
3845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
3855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes private:
38623055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers  const char* const name_;
387c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // The Mutex being used by waiters. It is an error to mix condition variables between different
388c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // Mutexes.
389c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  Mutex& guard_;
390c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES
391c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // A counter that is modified by signals and broadcasts. This ensures that when a waiter gives up
392d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers  // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_
393d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers  // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait
394d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers  // without guard_ held.
395b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers  AtomicInteger sequence_;
396c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // Number of threads that have come into to wait, not the length of the waiters on the futex as
3975bd97c483c1de1eb97afe76123b1b9ab53095edfIan Rogers  // waiters may have been requeued onto guard_. Guarded by guard_.
398c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  volatile int32_t num_waiters_;
399c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else
400c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  pthread_cond_t cond_;
401c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif
4025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
4035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes};
4045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
40500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it
40600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// upon destruction.
40700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE MutexLock {
40800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
40981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  explicit MutexLock(Thread* self, Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : self_(self), mu_(mu) {
41081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.ExclusiveLock(self_);
41181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  }
41281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
41300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~MutexLock() UNLOCK_FUNCTION() {
41481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.ExclusiveUnlock(self_);
41500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
41600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
41700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private:
41881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  Thread* const self_;
41900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Mutex& mu_;
42000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  DISALLOW_COPY_AND_ASSIGN(MutexLock);
42100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers};
42200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)".
42300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_declaration_missing_variable_name)
42400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
42500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon
42600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction.
42700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE ReaderMutexLock {
42800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
42981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  explicit ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) :
43081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers      self_(self), mu_(mu) {
43181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.SharedLock(self_);
43281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  }
43381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
43400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~ReaderMutexLock() UNLOCK_FUNCTION() {
43581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.SharedUnlock(self_);
43600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
43700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
43800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private:
43981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  Thread* const self_;
44000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ReaderWriterMutex& mu_;
44100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock);
44200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers};
44300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "ReaderMutexLock (lock);" instead of
44400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "ReaderMutexLock mu(lock)".
44500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define ReaderMutexLock(x) COMPILE_ASSERT(0, reader_mutex_lock_declaration_missing_variable_name)
44600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
44700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon
44800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction.
44900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE WriterMutexLock {
45000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
45181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  explicit WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) :
45281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers      self_(self), mu_(mu) {
45381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.ExclusiveLock(self_);
45481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  }
45581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
45600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~WriterMutexLock() UNLOCK_FUNCTION() {
45781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.ExclusiveUnlock(self_);
45800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
45900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
46000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private:
46150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* const self_;
46200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ReaderWriterMutex& mu_;
46300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  DISALLOW_COPY_AND_ASSIGN(WriterMutexLock);
46400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers};
46500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of
46600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "WriterMutexLock mu(lock)".
46700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define WriterMutexLock(x) COMPILE_ASSERT(0, writer_mutex_lock_declaration_missing_variable_name)
46800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
469719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Global mutexes corresponding to the levels above.
470719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersclass Locks {
471719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers public:
472719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static void Init();
473719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
474719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // The mutator_lock_ is used to allow mutators to execute in a shared (reader) mode or to block
475719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // mutators by having an exclusive (writer) owner. In normal execution each mutator thread holds
476719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // a share on the mutator_lock_. The garbage collector may also execute with shared access but
477719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // at times requires exclusive access to the heap (not to be confused with the heap meta-data
478719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // guarded by the heap_lock_ below). When the garbage collector requires exclusive access it asks
479719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // the mutators to suspend themselves which also involves usage of the thread_suspend_count_lock_
480719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // to cover weaknesses in using ReaderWriterMutexes with ConditionVariables. We use a condition
481719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // variable to wait upon in the suspension logic as releasing and then re-acquiring a share on
482719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // the mutator lock doesn't necessarily allow the exclusive user (e.g the garbage collector)
483719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // chance to acquire the lock.
484719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //
485719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Thread suspension:
486719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Shared users                                  | Exclusive user
487719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // (holding mutator lock and in kRunnable state) |   .. running ..
488719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               | Request thread suspension by:
489719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               |   - acquiring thread_suspend_count_lock_
490719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               |   - incrementing Thread::suspend_count_ on
491719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               |     all mutator threads
492719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               |   - releasing thread_suspend_count_lock_
493719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               | Block trying to acquire exclusive mutator lock
494719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Poll Thread::suspend_count_ and enter full    |   .. blocked ..
495719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // suspend code.                                 |   .. blocked ..
496719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Change state to kSuspended                    |   .. blocked ..
497719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // x: Release share on mutator_lock_             | Carry out exclusive access
498719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Acquire thread_suspend_count_lock_            |   .. exclusive ..
499719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // while Thread::suspend_count_ > 0              |   .. exclusive ..
500719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   - wait on Thread::resume_cond_              |   .. exclusive ..
501719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //     (releases thread_suspend_count_lock_)     |   .. exclusive ..
502719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               | Release mutator_lock_
503719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               | Request thread resumption by:
504719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               |   - acquiring thread_suspend_count_lock_
505719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               |   - decrementing Thread::suspend_count_ on
506719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               |     all mutator threads
507719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               |   - notifying on Thread::resume_cond_
508719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    - re-acquire thread_suspend_count_lock_    |   - releasing thread_suspend_count_lock_
509719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Release thread_suspend_count_lock_            |  .. running ..
510719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Acquire share on mutator_lock_                |  .. running ..
511719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //  - This could block but the thread still      |  .. running ..
512719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    has a state of kSuspended and so this      |  .. running ..
513719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    isn't an issue.                            |  .. running ..
514719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Acquire thread_suspend_count_lock_            |  .. running ..
515719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //  - we poll here as we're transitioning into   |  .. running ..
516719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    kRunnable and an individual thread suspend |  .. running ..
517719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    request (e.g for debugging) won't try      |  .. running ..
518719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    to acquire the mutator lock (which would   |  .. running ..
519719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    block as we hold the mutator lock). This   |  .. running ..
520719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    poll ensures that if the suspender thought |  .. running ..
521719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    we were suspended by incrementing our      |  .. running ..
522719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    Thread::suspend_count_ and then reading    |  .. running ..
523719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    our state we go back to waiting on         |  .. running ..
524719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    Thread::resume_cond_.                      |  .. running ..
525719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // can_go_runnable = Thread::suspend_count_ == 0 |  .. running ..
526719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Release thread_suspend_count_lock_            |  .. running ..
527719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // if can_go_runnable                            |  .. running ..
528719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   Change state to kRunnable                   |  .. running ..
529719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // else                                          |  .. running ..
530719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   Goto x                                      |  .. running ..
531719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //  .. running ..                                |  .. running ..
532719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static ReaderWriterMutex* mutator_lock_;
533719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
534719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap.
535719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_);
536719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
537719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Guards shutdown of the runtime.
538719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_);
539719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
5409e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  // Guards background profiler global state.
5419e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* profiler_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_);
5429e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu
5439e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  // Guards trace (ie traceview) requests.
5449e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* trace_lock_ ACQUIRED_AFTER(profiler_lock_);
5459e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu
546719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads
547719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // attaching and detaching.
5489e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* thread_list_lock_ ACQUIRED_AFTER(trace_lock_);
549719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
550719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Guards breakpoints.
551719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* breakpoint_lock_ ACQUIRED_AFTER(thread_list_lock_);
552719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
553719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Guards lists of classes within the class linker.
5549e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(breakpoint_lock_);
555719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
556719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code
557719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // doesn't try to hold a higher level Mutex.
558719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(Locks::classlinker_classes_lock_)
559719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
5609e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  // Guard the allocation/deallocation of thread ids.
5619e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* allocated_thread_ids_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
5629e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu
5639e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  // Guards modification of the LDT on x86.
5649e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* modify_ldt_lock_ ACQUIRED_AFTER(allocated_thread_ids_lock_);
5659e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu
566719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Guards intern table.
5679e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* intern_table_lock_ ACQUIRED_AFTER(modify_ldt_lock_);
568719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
569719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Have an exclusive aborting thread.
570719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* abort_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
571719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
572719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Allow mutual exclusion when manipulating Thread::suspend_count_.
573719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // TODO: Does the trade-off of a per-thread lock make sense?
574719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_);
575719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
576719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // One unexpected signal at a time lock.
577719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_);
578719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
5793eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi  // Guards the maps in mem_map.
5803eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi  static Mutex* mem_maps_lock_ ACQUIRED_AFTER(unexpected_signal_lock_);
5813eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi
582719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Have an exclusive logging thread.
583719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_);
584719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers};
585719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
5868daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}  // namespace art
5878daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
588fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_BASE_MUTEX_H_
589