mutex.h revision b486a98aadc95d80548953410cf23edba62259fa
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
4690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SHARED_LOCKABLE ReaderWriterMutex;
4790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SHARED_LOCKABLE MutatorMutex;
4856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogersclass ScopedContentionRecorder;
4950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogersclass Thread;
5050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers
51719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// LockLevel is used to impose a lock hierarchy [1] where acquisition of a Mutex at a higher or
52719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// equal level to a lock a thread holds is invalid. The lock hierarchy achieves a cycle free
53719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// partial ordering and thereby cause deadlock situations to fail checks.
54719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers//
55719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// [1] http://www.drdobbs.com/parallel/use-lock-hierarchies-to-avoid-deadlock/204801163
56719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersenum LockLevel {
57719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kLoggingLock = 0,
587de77dd4f2d3cbb0615ee001589eb99ae82c3dccRaghu Gandham  kSwapMutexesLock,
59719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kUnexpectedSignalLock,
60719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kThreadSuspendCountLock,
61719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kAbortLock,
62d0a160d722e696b1a3ffb91dcfde61ee11bfb3e0Tao Wu  kJdwpAdbStateLock,
63719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpSocketLock,
642cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchi  kRegionSpaceRegionLock,
65f169e27100acbec024f699de9ef842a27c0b2036Mathieu Chartier  kMarkSweepMarkStackLock,
662994605707219f67af8340ddd9379ce9bd86d74cMathieu Chartier  kRosAllocGlobalLock,
672994605707219f67af8340ddd9379ce9bd86d74cMathieu Chartier  kRosAllocBracketLock,
682994605707219f67af8340ddd9379ce9bd86d74cMathieu Chartier  kRosAllocBulkFreeLock,
69f169e27100acbec024f699de9ef842a27c0b2036Mathieu Chartier  kTaggingLockLevel,
70bc4d218ce2ceef0c4f308d4ff42f7ec1ec43c40eAndreas Gampe  kTransactionLogLock,
71c8089540ccf0f1c43d8db3828f21d489b28a4013Andreas Gampe  kJniFunctionTableLock,
72673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier  kJniWeakGlobalsLock,
7305a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe  kJniGlobalsLock,
74a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  kReferenceQueueSoftReferencesLock,
75a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  kReferenceQueuePhantomReferencesLock,
76a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  kReferenceQueueFinalizerReferencesLock,
77a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  kReferenceQueueWeakReferencesLock,
78a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  kReferenceQueueClearedReferencesLock,
79a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  kReferenceProcessorLock,
805cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky  kJitDebugInterfaceLock,
81719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kAllocSpaceLock,
822cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchi  kBumpPointerSpaceBlockLock,
83c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier  kArenaPoolLock,
84719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kInternTableLock,
853f5838d7d0b9fc63db0ccc35c2ea05ed29264986Vladimir Marko  kOatFileSecondaryLookupLock,
86a206c745dbb64b14f05c87891d425475c2f6d63aRichard Uhler  kHostDlOpenHandlesLock,
87ca3c8c33501bf199d6fd0a5db30a27d8e010cb23David Brazdil  kVerifierDepsLock,
88f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  kOatFileManagerLock,
897526d783ab68ed1dd53c763c75895cb432532b0fAndreas Gampe  kTracingUniqueMethodsLock,
907526d783ab68ed1dd53c763c75895cb432532b0fAndreas Gampe  kTracingStreamingLock,
91b8aa1e4c10dcdc7fef96634f87e259dfee83a1cfMathieu Chartier  kDeoptimizedMethodsLock,
92b8aa1e4c10dcdc7fef96634f87e259dfee83a1cfMathieu Chartier  kClassLoaderClassesLock,
93719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kDefaultMutexLevel,
946c60d8420e51fda65ad247ae04b5a823c88c26b6Mathieu Chartier  kDexLock,
95719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMarkSweepLargeObjectLock,
96719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpObjectRegistryLock,
979e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  kModifyLdtLock,
989e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  kAllocatedThreadIdsLock,
9974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  kMonitorPoolLock,
100b8aa1e4c10dcdc7fef96634f87e259dfee83a1cfMathieu Chartier  kClassLinkerClassesLock,  // TODO rename.
10165975776f807d55c83af6cca1e447f8daa794413Mathieu Chartier  kJitCodeCacheLock,
102063fc772b5b8aed7d769cd7cccb6ddc7619326eeMingyao Yang  kCHALock,
103719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kBreakpointLock,
104719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMonitorLock,
105440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier  kMonitorListLock,
10668d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  kJniLoadLibraryLock,
107719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kThreadListLock,
108306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom  kAllocTrackerLock,
109719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kDeoptimizationLock,
110719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kProfilerLock,
1114e5b20863898006ec6c9d120cda167d38dda6e60Sebastien Hertz  kJdwpShutdownLock,
112719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpEventListLock,
113719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpAttachLock,
114719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kJdwpStartLock,
115719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kRuntimeShutdownLock,
11669dbec6d9d55eeb2867949c2791d01dc9aa916c8Jeff Hao  kTraceLock,
117719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kHeapBitmapLock,
118719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kMutatorLock,
1199ef78b59da51080882e47505896b420977fd79aeMathieu Chartier  kInstrumentEntrypointsLock,
120719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kZygoteCreationLock,
121719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
122719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  kLockLevelCount  // Must come last.
123719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers};
124719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstd::ostream& operator<<(std::ostream& os, const LockLevel& rhs);
125719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
1262e250c826b3c405d675017efe79e5db3651c9ee6Brian Carlstromconst bool kDebugLocking = kIsDebugBuild;
12725fd14b87cced64a179dee885573113be5e11944Ian Rogers
1281afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Record Log contention information, dumpable via SIGQUIT.
1291afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#ifdef ART_USE_FUTEXES
13008f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Hao// To enable lock contention logging, set this to true.
13108f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Haoconst bool kLogLockContentions = false;
1321afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#else
1331afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Keep this false as lock contention logging is supported only with
1341afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// futex.
1351afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst bool kLogLockContentions = false;
1361afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#endif
137d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersconst size_t kContentionLogSize = 4;
1381afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kContentionLogDataSize = kLogLockContentions ? 1 : 0;
1391afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kAllMutexDataSize = kLogLockContentions ? 1 : 0;
1401afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi
14100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Base class for all Mutex implementations
14200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass BaseMutex {
14300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
144bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers  const char* GetName() const {
14500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return name_;
14600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
14700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
14800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool IsMutex() const { return false; }
14900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool IsReaderWriterMutex() const { return false; }
150eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  virtual bool IsMutatorMutex() const { return false; }
15100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
15256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  virtual void Dump(std::ostream& os) const = 0;
15356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
15456edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  static void DumpAll(std::ostream& os);
15556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
156a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  bool ShouldRespondToEmptyCheckpointRequest() const {
157a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi    return should_respond_to_empty_checkpoint_request_;
158a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  }
159a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi
160a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  void SetShouldRespondToEmptyCheckpointRequest(bool value) {
161a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi    should_respond_to_empty_checkpoint_request_ = value;
162a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  }
163a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi
164a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  virtual void WakeupToRespondToEmptyCheckpoint() = 0;
165a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi
16600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers protected:
16700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  friend class ConditionVariable;
16800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
16981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  BaseMutex(const char* name, LockLevel level);
17056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  virtual ~BaseMutex();
17181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void RegisterAsLocked(Thread* self);
17281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void RegisterAsUnlocked(Thread* self);
17381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  void CheckSafeToWait(Thread* self);
17400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
17556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  friend class ScopedContentionRecorder;
17656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
1771afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  void RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t nano_time_blocked);
17856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  void DumpContention(std::ostream& os) const;
17956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
18081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  const LockLevel level_;  // Support for lock hierarchy.
181bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers  const char* const name_;
182a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  bool should_respond_to_empty_checkpoint_request_;
1831afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi
18456edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  // A log entry that records contention but makes no guarantee that either tid will be held live.
18556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  struct ContentionLogEntry {
18656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers    ContentionLogEntry() : blocked_tid(0), owner_tid(0) {}
18756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers    uint64_t blocked_tid;
18856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers    uint64_t owner_tid;
18956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers    AtomicInteger count;
19056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  };
1911afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  struct ContentionLogData {
1921afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    ContentionLogEntry contention_log[kContentionLogSize];
1931afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    // The next entry in the contention log to be updated. Value ranges from 0 to
1941afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    // kContentionLogSize - 1.
1951afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    AtomicInteger cur_content_log_entry;
1961afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    // Number of times the Mutex has been contended.
1971afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    AtomicInteger contention_count;
1981afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    // Sum of time waited by all contenders in ns.
19937f3c968ecd04e77802fe17bb82dabc07de21ca1Ian Rogers    Atomic<uint64_t> wait_time;
2001afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    void AddToWaitTime(uint64_t value);
2011afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    ContentionLogData() : wait_time(0) {}
2021afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  };
2033e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers  ContentionLogData contention_log_data_[kContentionLogDataSize];
2041afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi
2051afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi public:
2061afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  bool HasEverContended() const {
2071afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    if (kLogLockContentions) {
2083e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers      return contention_log_data_->contention_count.LoadSequentiallyConsistent() > 0;
2091afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    }
2101afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi    return false;
2111afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi  }
21200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers};
21300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
21400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain
21500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// exclusive access to what it guards. A Mutex can be in one of two states:
21600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread,
21700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread.
21800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers//
21900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is:
22000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State     | ExclusiveLock | ExclusiveUnlock
22100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// -------------------------------------------
22200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free      | Exclusive     | error
22300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block*        | Free
22400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in
22500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers//   an error. Being non-reentrant simplifies Waiting on ConditionVariables.
22601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const Mutex& mu);
22700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE Mutex : public BaseMutex {
22800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
22981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false);
23000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~Mutex();
23100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
23200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool IsMutex() const { return true; }
23300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
23400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Block until mutex is free then acquire exclusive access.
23590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void ExclusiveLock(Thread* self) ACQUIRE();
23690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void Lock(Thread* self) ACQUIRE() {  ExclusiveLock(self); }
23700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
23800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Returns true if acquires exclusive access, false otherwise.
23990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool ExclusiveTryLock(Thread* self) TRY_ACQUIRE(true);
24090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool TryLock(Thread* self) TRY_ACQUIRE(true) { return ExclusiveTryLock(self); }
24100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
24200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Release exclusive access.
24390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void ExclusiveUnlock(Thread* self) RELEASE();
24490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void Unlock(Thread* self) RELEASE() {  ExclusiveUnlock(self); }
24500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
24600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Is the current thread the exclusive holder of the Mutex.
247b486a98aadc95d80548953410cf23edba62259faAndreas Gampe  ALWAYS_INLINE bool IsExclusiveHeld(const Thread* self) const;
2488daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
24900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert that the Mutex is exclusively held by the current thread.
250b486a98aadc95d80548953410cf23edba62259faAndreas Gampe  ALWAYS_INLINE void AssertExclusiveHeld(const Thread* self) const ASSERT_CAPABILITY(this);
251b486a98aadc95d80548953410cf23edba62259faAndreas Gampe  ALWAYS_INLINE void AssertHeld(const Thread* self) const ASSERT_CAPABILITY(this);
25200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
25300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert that the Mutex is not held by the current thread.
25490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void AssertNotHeldExclusive(const Thread* self) ASSERT_CAPABILITY(!*this) {
255db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray    if (kDebugLocking && (gAborting == 0)) {
25601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers      CHECK(!IsExclusiveHeld(self)) << *this;
25700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
25800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
25990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void AssertNotHeld(const Thread* self) ASSERT_CAPABILITY(!*this) {
26090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier    AssertNotHeldExclusive(self);
26190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  }
26200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
263c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // Id associated with exclusive owner. No memory ordering semantics if called from a thread other
264c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // than the owner.
26500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  uint64_t GetExclusiveOwnerTid() const;
26600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
26700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld.
26800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  unsigned int GetDepth() const {
26900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return recursion_count_;
27000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
27100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
27256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  virtual void Dump(std::ostream& os) const;
27301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers
27490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  // For negative capabilities in clang annotations.
27590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  const Mutex& operator!() const { return *this; }
27690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier
277a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  void WakeupToRespondToEmptyCheckpoint() OVERRIDE;
278a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi
27900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private:
280c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES
281c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // 0 is unheld, 1 is held.
282c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  AtomicInteger state_;
283c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // Exclusive owner.
284c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  volatile uint64_t exclusive_owner_;
285c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // Number of waiting contenders.
286b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers  AtomicInteger num_contenders_;
287c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else
28800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  pthread_mutex_t mutex_;
289c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers  volatile uint64_t exclusive_owner_;  // Guarded by mutex_.
290c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif
29100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  const bool recursive_;  // Can the lock be recursively held?
29200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  unsigned int recursion_count_;
293f1498437b0d6beb9f4f91980b98cbeb0b5c773ceElliott Hughes  friend class ConditionVariable;
2948daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(Mutex);
2958daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes};
2968daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
29700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex.
29800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader)
29900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a
30000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// condition variable. A ReaderWriterMutex can be in one of three states:
30100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread,
30200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread,
30300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Shared(n) - shared amongst n threads.
30400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers//
30500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is:
30600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers//
30700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State     | ExclusiveLock | ExclusiveUnlock | SharedLock       | SharedUnlock
30800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ----------------------------------------------------------------------------
30900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free      | Exclusive     | error           | SharedLock(1)    | error
31000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block         | Free            | Block            | error
31100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Shared(n) | Block         | error           | SharedLock(n+1)* | Shared(n-1) or Free
31200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * for large values of n the SharedLock may block.
31301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu);
31490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SHARED_LOCKABLE ReaderWriterMutex : public BaseMutex {
3158daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes public:
31681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel);
31700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~ReaderWriterMutex();
31800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
31900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool IsReaderWriterMutex() const { return true; }
32000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
32100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Block until ReaderWriterMutex is free then acquire exclusive access.
32290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void ExclusiveLock(Thread* self) ACQUIRE();
32390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void WriterLock(Thread* self) ACQUIRE() {  ExclusiveLock(self); }
32400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
32500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Release exclusive access.
32690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void ExclusiveUnlock(Thread* self) RELEASE();
32790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void WriterUnlock(Thread* self) RELEASE() {  ExclusiveUnlock(self); }
32800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
32900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success
33000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // or false if timeout is reached.
33166aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#if HAVE_TIMED_RWLOCK
332c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns)
33381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers      EXCLUSIVE_TRYLOCK_FUNCTION(true);
33466aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#endif
33500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
33600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Block until ReaderWriterMutex is shared or free then acquire a share on the access.
33790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void SharedLock(Thread* self) ACQUIRE_SHARED() ALWAYS_INLINE;
33890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void ReaderLock(Thread* self) ACQUIRE_SHARED() { SharedLock(self); }
33900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
34000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Try to acquire share of ReaderWriterMutex.
34190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool SharedTryLock(Thread* self) SHARED_TRYLOCK_FUNCTION(true);
34200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
34300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Release a share of the access.
34490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void SharedUnlock(Thread* self) RELEASE_SHARED() ALWAYS_INLINE;
34590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void ReaderUnlock(Thread* self) RELEASE_SHARED() { SharedUnlock(self); }
34600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
34700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Is the current thread the exclusive holder of the ReaderWriterMutex.
348b486a98aadc95d80548953410cf23edba62259faAndreas Gampe  ALWAYS_INLINE bool IsExclusiveHeld(const Thread* self) const;
34900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
35000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert the current thread has exclusive access to the ReaderWriterMutex.
351b486a98aadc95d80548953410cf23edba62259faAndreas Gampe  ALWAYS_INLINE void AssertExclusiveHeld(const Thread* self) const ASSERT_CAPABILITY(this);
352b486a98aadc95d80548953410cf23edba62259faAndreas Gampe  ALWAYS_INLINE void AssertWriterHeld(const Thread* self) const ASSERT_CAPABILITY(this);
3538daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
35400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex.
35590ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier  void AssertNotExclusiveHeld(const Thread* self) ASSERT_CAPABILITY(!this) {
356db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray    if (kDebugLocking && (gAborting == 0)) {
357e3359f7ad7671c5816f17145ca3a01516512e8d6Ian Rogers      CHECK(!IsExclusiveHeld(self)) << *this;
35800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
35900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
36090ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier  void AssertNotWriterHeld(const Thread* self) ASSERT_CAPABILITY(!this) {
36190ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier    AssertNotExclusiveHeld(self);
36290ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier  }
36300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
36400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Is the current thread a shared holder of the ReaderWriterMutex.
36581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  bool IsSharedHeld(const Thread* self) const;
36600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
36700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert the current thread has shared access to the ReaderWriterMutex.
3683768adec2f819cb506577618fdde158ba659ddd4Mathieu Chartier  ALWAYS_INLINE void AssertSharedHeld(const Thread* self) ASSERT_SHARED_CAPABILITY(this) {
369db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray    if (kDebugLocking && (gAborting == 0)) {
3702cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      // TODO: we can only assert this well when self != null.
3712cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      CHECK(IsSharedHeld(self) || self == nullptr) << *this;
37200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
3738daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
3743768adec2f819cb506577618fdde158ba659ddd4Mathieu Chartier  ALWAYS_INLINE void AssertReaderHeld(const Thread* self) ASSERT_SHARED_CAPABILITY(this) {
37590ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier    AssertSharedHeld(self);
37690ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier  }
3778daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
37800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive
37900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // mode.
3803768adec2f819cb506577618fdde158ba659ddd4Mathieu Chartier  ALWAYS_INLINE void AssertNotHeld(const Thread* self) ASSERT_SHARED_CAPABILITY(!this) {
381db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray    if (kDebugLocking && (gAborting == 0)) {
38201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers      CHECK(!IsSharedHeld(self)) << *this;
38300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
38400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
38500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
386c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // Id associated with exclusive owner. No memory ordering semantics if called from a thread other
387c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // than the owner.
38800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  uint64_t GetExclusiveOwnerTid() const;
38981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
39056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers  virtual void Dump(std::ostream& os) const;
39101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers
39290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  // For negative capabilities in clang annotations.
39390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  const ReaderWriterMutex& operator!() const { return *this; }
39490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier
395a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  void WakeupToRespondToEmptyCheckpoint() OVERRIDE;
396a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi
3978daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes private:
39851d212ef31945743abe8a469707aaa25bab95357Ian Rogers#if ART_USE_FUTEXES
399cf7f19135f0e273f7b0136315633c2abfc715343Ian Rogers  // Out-of-inline path for handling contention for a SharedLock.
400cf7f19135f0e273f7b0136315633c2abfc715343Ian Rogers  void HandleSharedLockContention(Thread* self, int32_t cur_state);
401cf7f19135f0e273f7b0136315633c2abfc715343Ian Rogers
40281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  // -1 implies held exclusive, +ve shared held by state_ many owners.
403c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  AtomicInteger state_;
404c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // Exclusive owner. Modification guarded by this mutex.
40581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  volatile uint64_t exclusive_owner_;
406c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // Number of contenders waiting for a reader share.
407c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  AtomicInteger num_pending_readers_;
408c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers  // Number of contenders waiting to be the writer.
409b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers  AtomicInteger num_pending_writers_;
41081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#else
41100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  pthread_rwlock_t rwlock_;
412c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers  volatile uint64_t exclusive_owner_;  // Guarded by rwlock_.
41381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#endif
41400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex);
4158daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes};
4168daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
417eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// MutatorMutex is a special kind of ReaderWriterMutex created specifically for the
418eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// Locks::mutator_lock_ mutex. The behaviour is identical to the ReaderWriterMutex except that
419eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// thread state changes also play a part in lock ownership. The mutator_lock_ will not be truly
420eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// held by any mutator threads. However, a thread in the kRunnable state is considered to have
421eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// shared ownership of the mutator lock and therefore transitions in and out of the kRunnable
422eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// state have associated implications on lock ownership. Extra methods to handle the state
423eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// transitions have been added to the interface but are only accessible to the methods dealing
424eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// with state transitions. The thread state and flags attributes are used to ensure thread state
425eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// transitions are consistent with the permitted behaviour of the mutex.
426eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li//
427eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// *) The most important consequence of this behaviour is that all threads must be in one of the
428eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// suspended states before exclusive ownership of the mutator mutex is sought.
429eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li//
430eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Listd::ostream& operator<<(std::ostream& os, const MutatorMutex& mu);
43190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SHARED_LOCKABLE MutatorMutex : public ReaderWriterMutex {
432eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li public:
433eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  explicit MutatorMutex(const char* name, LockLevel level = kDefaultMutexLevel)
434eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li    : ReaderWriterMutex(name, level) {}
435eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  ~MutatorMutex() {}
436eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li
437eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  virtual bool IsMutatorMutex() const { return true; }
438eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li
43990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  // For negative capabilities in clang annotations.
44090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  const MutatorMutex& operator!() const { return *this; }
44190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier
442eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li private:
443eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  friend class Thread;
444eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  void TransitionFromRunnableToSuspended(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE;
445eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  void TransitionFromSuspendedToRunnable(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE;
446eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li
447eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  DISALLOW_COPY_AND_ASSIGN(MutatorMutex);
448eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li};
449eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li
45000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually
45100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// (Signal) or all at once (Broadcast).
4525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesclass ConditionVariable {
4535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes public:
4543887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain  ConditionVariable(const char* name, Mutex& mutex);
4555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  ~ConditionVariable();
4565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
457c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  void Broadcast(Thread* self);
458c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  void Signal(Thread* self);
459c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their
460c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  //       pointer copy, thereby defeating annotalysis.
461c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
4627b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  bool TimedWait(Thread* self, int64_t ms, int32_t ns) NO_THREAD_SAFETY_ANALYSIS;
4631d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held
4641d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  // when waiting.
4651d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  // TODO: remove this.
4661d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
4675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
4685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes private:
46923055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers  const char* const name_;
470c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // The Mutex being used by waiters. It is an error to mix condition variables between different
471c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // Mutexes.
472c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  Mutex& guard_;
473c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES
474c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // A counter that is modified by signals and broadcasts. This ensures that when a waiter gives up
475d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers  // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_
476d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers  // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait
477d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers  // without guard_ held.
478b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers  AtomicInteger sequence_;
479c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  // Number of threads that have come into to wait, not the length of the waiters on the futex as
4805bd97c483c1de1eb97afe76123b1b9ab53095edfIan Rogers  // waiters may have been requeued onto guard_. Guarded by guard_.
481c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  volatile int32_t num_waiters_;
482c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else
483c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers  pthread_cond_t cond_;
484c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif
4855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
4865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes};
4875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
48800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it
48900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// upon destruction.
49090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SCOPED_CAPABILITY MutexLock {
49100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
4923887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain  MutexLock(Thread* self, Mutex& mu) ACQUIRE(mu) : self_(self), mu_(mu) {
49381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.ExclusiveLock(self_);
49481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  }
49581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
4964e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier  ~MutexLock() RELEASE() {
49781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.ExclusiveUnlock(self_);
49800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
49900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
50000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private:
50181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  Thread* const self_;
50200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Mutex& mu_;
50300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  DISALLOW_COPY_AND_ASSIGN(MutexLock);
50400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers};
50500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)".
506575e78c41ece0dec969d31f46be563d4eb7ae43bAndreas Gampe#define MutexLock(x) static_assert(0, "MutexLock declaration missing variable name")
50700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
50800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon
50900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction.
51090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SCOPED_CAPABILITY ReaderMutexLock {
51100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
512b486a98aadc95d80548953410cf23edba62259faAndreas Gampe  ALWAYS_INLINE ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) ACQUIRE(mu);
51381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
514b486a98aadc95d80548953410cf23edba62259faAndreas Gampe  ALWAYS_INLINE ~ReaderMutexLock() RELEASE();
51500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
51600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private:
51781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  Thread* const self_;
51800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ReaderWriterMutex& mu_;
51900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock);
52000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers};
52100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
52200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon
52300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction.
52490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SCOPED_CAPABILITY WriterMutexLock {
52500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public:
5263887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain  WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) :
52781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers      self_(self), mu_(mu) {
52881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.ExclusiveLock(self_);
52981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  }
53081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers
53100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ~WriterMutexLock() UNLOCK_FUNCTION() {
53281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers    mu_.ExclusiveUnlock(self_);
53300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
53400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
53500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private:
53650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* const self_;
53700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ReaderWriterMutex& mu_;
53800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  DISALLOW_COPY_AND_ASSIGN(WriterMutexLock);
53900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers};
54000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of
54100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "WriterMutexLock mu(lock)".
542575e78c41ece0dec969d31f46be563d4eb7ae43bAndreas Gampe#define WriterMutexLock(x) static_assert(0, "WriterMutexLock declaration missing variable name")
54300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
5444e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier// For StartNoThreadSuspension and EndNoThreadSuspension.
5454e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartierclass CAPABILITY("role") Role {
5464e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier public:
5474e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier  void Acquire() ACQUIRE() {}
5484e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier  void Release() RELEASE() {}
5494e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier  const Role& operator!() const { return *this; }
5504e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier};
5514e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier
5524e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartierclass Uninterruptible : public Role {
5534e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier};
5544e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier
555719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Global mutexes corresponding to the levels above.
556719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersclass Locks {
557719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers public:
558719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static void Init();
55991e56692c6bd9fa1d41951ee7dc311f19461f4beMathieu Chartier  static void InitConditions() NO_THREAD_SAFETY_ANALYSIS;  // Condition variables.
560f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr
561f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr  // Destroying various lock types can emit errors that vary depending upon
562f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr  // whether the client (art::Runtime) is currently active.  Allow the client
563f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr  // to set a callback that is used to check when it is acceptable to call
564f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr  // Abort.  The default behavior is that the client *is not* able to call
565f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr  // Abort if no callback is established.
566f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr  using ClientCallback = bool();
567f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr  static void SetClientCallback(ClientCallback* is_safe_to_call_abort_cb) NO_THREAD_SAFETY_ANALYSIS;
568f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr  // Checks for whether it is safe to call Abort() without using locks.
569f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr  static bool IsSafeToCallAbortRacy() NO_THREAD_SAFETY_ANALYSIS;
570f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr
5718a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi  // Add a mutex to expected_mutexes_on_weak_ref_access_.
5728a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi  static void AddToExpectedMutexesOnWeakRefAccess(BaseMutex* mutex, bool need_lock = true);
5738a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi  // Remove a mutex from expected_mutexes_on_weak_ref_access_.
5748a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi  static void RemoveFromExpectedMutexesOnWeakRefAccess(BaseMutex* mutex, bool need_lock = true);
5758a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi  // Check if the given mutex is in expected_mutexes_on_weak_ref_access_.
5768a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi  static bool IsExpectedOnWeakRefAccess(BaseMutex* mutex);
577f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr
5789ef78b59da51080882e47505896b420977fd79aeMathieu Chartier  // Guards allocation entrypoint instrumenting.
5794ad5cd3e7d519484559ef778d96fb3f0be8919faIan Rogers  static Mutex* instrument_entrypoints_lock_;
5809ef78b59da51080882e47505896b420977fd79aeMathieu Chartier
581eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  // A barrier is used to synchronize the GC/Debugger thread with mutator threads. When GC/Debugger
582eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  // thread wants to suspend all mutator threads, it needs to wait for all mutator threads to pass
583eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  // a barrier. Threads that are already suspended will get their barrier passed by the GC/Debugger
584eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  // thread; threads in the runnable state will pass the barrier when they transit to the suspended
585eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  // state. GC/Debugger thread will be woken up when all mutator threads are suspended.
586719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //
587719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Thread suspension:
588eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  // mutator thread                                | GC/Debugger
589eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  //   .. running ..                               |   .. running ..
590719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               | Request thread suspension by:
591719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               |   - acquiring thread_suspend_count_lock_
592719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               |   - incrementing Thread::suspend_count_ on
593719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               |     all mutator threads
594719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. running ..                               |   - releasing thread_suspend_count_lock_
595eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  //   .. running ..                               | Block wait for all threads to pass a barrier
596719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Poll Thread::suspend_count_ and enter full    |   .. blocked ..
597719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // suspend code.                                 |   .. blocked ..
598eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  // Change state to kSuspended (pass the barrier) | Wake up when all threads pass the barrier
599eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  // x: Acquire thread_suspend_count_lock_         |   .. running ..
600eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  // while Thread::suspend_count_ > 0              |   .. running ..
601eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  //   - wait on Thread::resume_cond_              |   .. running ..
602eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  //     (releases thread_suspend_count_lock_)     |   .. running ..
603719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               | Request thread resumption by:
604719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               |   - acquiring thread_suspend_count_lock_
605719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               |   - decrementing Thread::suspend_count_ on
606719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               |     all mutator threads
607719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //   .. waiting ..                               |   - notifying on Thread::resume_cond_
608719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //    - re-acquire thread_suspend_count_lock_    |   - releasing thread_suspend_count_lock_
609719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Release thread_suspend_count_lock_            |  .. running ..
610eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  // Change to kRunnable                           |  .. running ..
611eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  //  - this uses a CAS operation to ensure the    |  .. running ..
612eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  //    suspend request flag isn't raised as the   |  .. running ..
613eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  //    state is changed                           |  .. running ..
614eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  //  - if the CAS operation fails then goto x     |  .. running ..
615719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  //  .. running ..                                |  .. running ..
616eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li  static MutatorMutex* mutator_lock_ ACQUIRED_AFTER(instrument_entrypoints_lock_);
617719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
618719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap.
619719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_);
620719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
621719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Guards shutdown of the runtime.
622719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_);
623719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
6249e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  // Guards background profiler global state.
625b139b6d3833cd6a90f345204202ec57ff277c088Hiroshi Yamauchi  static Mutex* profiler_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_);
6269e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu
6279e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  // Guards trace (ie traceview) requests.
6289e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* trace_lock_ ACQUIRED_AFTER(profiler_lock_);
6299e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu
630306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom  // Guards debugger recent allocation records.
631306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom  static Mutex* alloc_tracker_lock_ ACQUIRED_AFTER(trace_lock_);
632306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom
633306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom  // Guards updates to instrumentation to ensure mutual exclusion of
634306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom  // events like deoptimization requests.
635306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom  // TODO: improve name, perhaps instrumentation_update_lock_.
636306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom  static Mutex* deoptimization_lock_ ACQUIRED_AFTER(alloc_tracker_lock_);
637306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom
638063fc772b5b8aed7d769cd7cccb6ddc7619326eeMingyao Yang  // Guards Class Hierarchy Analysis (CHA).
639063fc772b5b8aed7d769cd7cccb6ddc7619326eeMingyao Yang  static Mutex* cha_lock_ ACQUIRED_AFTER(deoptimization_lock_);
640063fc772b5b8aed7d769cd7cccb6ddc7619326eeMingyao Yang
641719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads
642719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // attaching and detaching.
643063fc772b5b8aed7d769cd7cccb6ddc7619326eeMingyao Yang  static Mutex* thread_list_lock_ ACQUIRED_AFTER(cha_lock_);
644719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
64591e56692c6bd9fa1d41951ee7dc311f19461f4beMathieu Chartier  // Signaled when threads terminate. Used to determine when all non-daemons have terminated.
64691e56692c6bd9fa1d41951ee7dc311f19461f4beMathieu Chartier  static ConditionVariable* thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_);
64791e56692c6bd9fa1d41951ee7dc311f19461f4beMathieu Chartier
64868d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  // Guards maintaining loading library data structures.
64968d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  static Mutex* jni_libraries_lock_ ACQUIRED_AFTER(thread_list_lock_);
65068d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers
651719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Guards breakpoints.
652ed2be1725fb79075892b1a9103487c9d9a95b350Sebastien Hertz  static ReaderWriterMutex* breakpoint_lock_ ACQUIRED_AFTER(jni_libraries_lock_);
653719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
654719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Guards lists of classes within the class linker.
6559e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(breakpoint_lock_);
656719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
657719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code
658719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // doesn't try to hold a higher level Mutex.
65912d625f87bcd6c4059a205bb39007a255f57f382Mathieu Chartier  #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(Locks::classlinker_classes_lock_)
660719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
66174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  static Mutex* allocated_monitor_ids_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
66274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
6639e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  // Guard the allocation/deallocation of thread ids.
66474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  static Mutex* allocated_thread_ids_lock_ ACQUIRED_AFTER(allocated_monitor_ids_lock_);
6659e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu
6669e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  // Guards modification of the LDT on x86.
6679e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu  static Mutex* modify_ldt_lock_ ACQUIRED_AFTER(allocated_thread_ids_lock_);
6689e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu
669cc1b5357f83f0b787d51fbfde3fe870c8a2fa050Andreas Gampe  static ReaderWriterMutex* dex_lock_ ACQUIRED_AFTER(modify_ldt_lock_);
670cc1b5357f83f0b787d51fbfde3fe870c8a2fa050Andreas Gampe
671f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Guards opened oat files in OatFileManager.
672cc1b5357f83f0b787d51fbfde3fe870c8a2fa050Andreas Gampe  static ReaderWriterMutex* oat_file_manager_lock_ ACQUIRED_AFTER(dex_lock_);
673f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
674340dafabc8e88378e395cda9027cf17726910e91Nicolas Geoffray  // Guards extra string entries for VerifierDeps.
675340dafabc8e88378e395cda9027cf17726910e91Nicolas Geoffray  static ReaderWriterMutex* verifier_deps_lock_ ACQUIRED_AFTER(oat_file_manager_lock_);
676ca3c8c33501bf199d6fd0a5db30a27d8e010cb23David Brazdil
677a206c745dbb64b14f05c87891d425475c2f6d63aRichard Uhler  // Guards dlopen_handles_ in DlOpenOatFile.
678ca3c8c33501bf199d6fd0a5db30a27d8e010cb23David Brazdil  static Mutex* host_dlopen_handles_lock_ ACQUIRED_AFTER(verifier_deps_lock_);
679e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier
680719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Guards intern table.
681a206c745dbb64b14f05c87891d425475c2f6d63aRichard Uhler  static Mutex* intern_table_lock_ ACQUIRED_AFTER(host_dlopen_handles_lock_);
682719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
683a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  // Guards reference processor.
684a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  static Mutex* reference_processor_lock_ ACQUIRED_AFTER(intern_table_lock_);
685a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier
686a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  // Guards cleared references queue.
687a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  static Mutex* reference_queue_cleared_references_lock_ ACQUIRED_AFTER(reference_processor_lock_);
688a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier
689a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  // Guards weak references queue.
690a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  static Mutex* reference_queue_weak_references_lock_ ACQUIRED_AFTER(reference_queue_cleared_references_lock_);
691a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier
692a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  // Guards finalizer references queue.
693a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  static Mutex* reference_queue_finalizer_references_lock_ ACQUIRED_AFTER(reference_queue_weak_references_lock_);
694a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier
695a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  // Guards phantom references queue.
696a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  static Mutex* reference_queue_phantom_references_lock_ ACQUIRED_AFTER(reference_queue_finalizer_references_lock_);
697a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier
698a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  // Guards soft references queue.
699a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier  static Mutex* reference_queue_soft_references_lock_ ACQUIRED_AFTER(reference_queue_phantom_references_lock_);
700a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier
70105a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe  // Guard accesses to the JNI Global Reference table.
70205a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe  static ReaderWriterMutex* jni_globals_lock_ ACQUIRED_AFTER(reference_queue_soft_references_lock_);
70305a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe
70405a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe  // Guard accesses to the JNI Weak Global Reference table.
70505a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe  static Mutex* jni_weak_globals_lock_ ACQUIRED_AFTER(jni_globals_lock_);
70605a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe
707c8089540ccf0f1c43d8db3828f21d489b28a4013Andreas Gampe  // Guard accesses to the JNI function table override.
708c8089540ccf0f1c43d8db3828f21d489b28a4013Andreas Gampe  static Mutex* jni_function_table_lock_ ACQUIRED_AFTER(jni_weak_globals_lock_);
709c8089540ccf0f1c43d8db3828f21d489b28a4013Andreas Gampe
710719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Have an exclusive aborting thread.
711c8089540ccf0f1c43d8db3828f21d489b28a4013Andreas Gampe  static Mutex* abort_lock_ ACQUIRED_AFTER(jni_function_table_lock_);
712719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
713719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Allow mutual exclusion when manipulating Thread::suspend_count_.
714719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // TODO: Does the trade-off of a per-thread lock make sense?
715719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_);
716719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
717719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // One unexpected signal at a time lock.
718719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_);
719719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
720719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  // Have an exclusive logging thread.
721719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_);
722a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi
723a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  // List of mutexes that we expect a thread may hold when accessing weak refs. This is used to
724a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  // avoid a deadlock in the empty checkpoint while weak ref access is disabled (b/34964016). If we
725a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  // encounter an unexpected mutex on accessing weak refs,
726a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  // Thread::CheckEmptyCheckpointFromWeakRefAccess will detect it.
727a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi  static std::vector<BaseMutex*> expected_mutexes_on_weak_ref_access_;
7288a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi  static Atomic<const BaseMutex*> expected_mutexes_on_weak_ref_access_guard_;
7298a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi  class ScopedExpectedMutexesOnWeakRefAccessLock;
730719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers};
731719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
7324e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartierclass Roles {
7334e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier public:
73490ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier  // Uninterruptible means that the thread may not become suspended.
7354e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier  static Uninterruptible uninterruptible_;
7364e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier};
7374e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier
7388daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}  // namespace art
7398daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
740fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_BASE_MUTEX_H_
741