mutex.h revision e2facc5b18cd756a8b5500fb3d90da69c9ee0fb7
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; 47eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Liclass 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, 583eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi kMemMapsLock, 597de77dd4f2d3cbb0615ee001589eb99ae82c3dccRaghu Gandham kSwapMutexesLock, 60719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kUnexpectedSignalLock, 61719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kThreadSuspendCountLock, 62719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kAbortLock, 63e2facc5b18cd756a8b5500fb3d90da69c9ee0fb7Igor Murashkin kLambdaTableLock, 64719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpSocketLock, 652cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchi kRegionSpaceRegionLock, 66310008008c90fea246efd00cb99ee7ded97c5209Mathieu Chartier kTransactionLogLock, 67a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier kReferenceQueueSoftReferencesLock, 68a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier kReferenceQueuePhantomReferencesLock, 69a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier kReferenceQueueFinalizerReferencesLock, 70a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier kReferenceQueueWeakReferencesLock, 71a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier kReferenceQueueClearedReferencesLock, 72a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier kReferenceProcessorLock, 73e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier kJitCodeCacheLock, 74719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRosAllocGlobalLock, 75719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRosAllocBracketLock, 76719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRosAllocBulkFreeLock, 77719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kAllocSpaceLock, 782cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchi kBumpPointerSpaceBlockLock, 79c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier kArenaPoolLock, 80719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDexFileMethodInlinerLock, 81719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDexFileToMethodInlinerMapLock, 82719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMarkSweepMarkStackLock, 83719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kInternTableLock, 843f5838d7d0b9fc63db0ccc35c2ea05ed29264986Vladimir Marko kOatFileSecondaryLookupLock, 857526d783ab68ed1dd53c763c75895cb432532b0fAndreas Gampe kTracingUniqueMethodsLock, 867526d783ab68ed1dd53c763c75895cb432532b0fAndreas Gampe kTracingStreamingLock, 87719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDefaultMutexLevel, 88719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMarkSweepLargeObjectLock, 89719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kPinTableLock, 90719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpObjectRegistryLock, 919e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu kModifyLdtLock, 929e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu kAllocatedThreadIdsLock, 9374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe kMonitorPoolLock, 942435a43f6c851c23922d8508fb17c6079248201cbowen_lai kMethodVerifiersLock, 95719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kClassLinkerClassesLock, 96719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kBreakpointLock, 97719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMonitorLock, 98440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier kMonitorListLock, 9968d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers kJniLoadLibraryLock, 100719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kThreadListLock, 101306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom kAllocTrackerLock, 102719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDeoptimizationLock, 103719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kProfilerLock, 1044e5b20863898006ec6c9d120cda167d38dda6e60Sebastien Hertz kJdwpShutdownLock, 105719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpEventListLock, 106719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpAttachLock, 107719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpStartLock, 108719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRuntimeShutdownLock, 10969dbec6d9d55eeb2867949c2791d01dc9aa916c8Jeff Hao kTraceLock, 110719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kHeapBitmapLock, 111719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMutatorLock, 1129ef78b59da51080882e47505896b420977fd79aeMathieu Chartier kInstrumentEntrypointsLock, 113719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kZygoteCreationLock, 114719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 115719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kLockLevelCount // Must come last. 116719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}; 117719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstd::ostream& operator<<(std::ostream& os, const LockLevel& rhs); 118719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 1192e250c826b3c405d675017efe79e5db3651c9ee6Brian Carlstromconst bool kDebugLocking = kIsDebugBuild; 12025fd14b87cced64a179dee885573113be5e11944Ian Rogers 1211afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Record Log contention information, dumpable via SIGQUIT. 1221afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#ifdef ART_USE_FUTEXES 12308f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Hao// To enable lock contention logging, set this to true. 12408f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Haoconst bool kLogLockContentions = false; 1251afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#else 1261afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Keep this false as lock contention logging is supported only with 1271afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// futex. 1281afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst bool kLogLockContentions = false; 1291afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#endif 130d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersconst size_t kContentionLogSize = 4; 1311afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kContentionLogDataSize = kLogLockContentions ? 1 : 0; 1321afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kAllMutexDataSize = kLogLockContentions ? 1 : 0; 1331afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 13400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Base class for all Mutex implementations 13500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass BaseMutex { 13600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 137bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers const char* GetName() const { 13800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return name_; 13900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 14000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 14100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsMutex() const { return false; } 14200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsReaderWriterMutex() const { return false; } 143eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li virtual bool IsMutatorMutex() const { return false; } 14400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 14556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const = 0; 14656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 14756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers static void DumpAll(std::ostream& os); 14856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 14900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers protected: 15000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers friend class ConditionVariable; 15100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 15281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers BaseMutex(const char* name, LockLevel level); 15356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual ~BaseMutex(); 15481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void RegisterAsLocked(Thread* self); 15581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void RegisterAsUnlocked(Thread* self); 15681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void CheckSafeToWait(Thread* self); 15700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 15856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers friend class ScopedContentionRecorder; 15956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 1601afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi void RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t nano_time_blocked); 16156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers void DumpContention(std::ostream& os) const; 16256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 16381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers const LockLevel level_; // Support for lock hierarchy. 164bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers const char* const name_; 1651afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 16656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers // A log entry that records contention but makes no guarantee that either tid will be held live. 16756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers struct ContentionLogEntry { 16856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers ContentionLogEntry() : blocked_tid(0), owner_tid(0) {} 16956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers uint64_t blocked_tid; 17056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers uint64_t owner_tid; 17156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers AtomicInteger count; 17256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers }; 1731afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi struct ContentionLogData { 1741afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi ContentionLogEntry contention_log[kContentionLogSize]; 1751afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // The next entry in the contention log to be updated. Value ranges from 0 to 1761afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // kContentionLogSize - 1. 1771afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi AtomicInteger cur_content_log_entry; 1781afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // Number of times the Mutex has been contended. 1791afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi AtomicInteger contention_count; 1801afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // Sum of time waited by all contenders in ns. 18137f3c968ecd04e77802fe17bb82dabc07de21ca1Ian Rogers Atomic<uint64_t> wait_time; 1821afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi void AddToWaitTime(uint64_t value); 1831afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi ContentionLogData() : wait_time(0) {} 1841afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi }; 1853e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers ContentionLogData contention_log_data_[kContentionLogDataSize]; 1861afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 1871afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi public: 1881afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi bool HasEverContended() const { 1891afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi if (kLogLockContentions) { 1903e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers return contention_log_data_->contention_count.LoadSequentiallyConsistent() > 0; 1911afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi } 1921afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi return false; 1931afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi } 19400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 19500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 19600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain 19700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// exclusive access to what it guards. A Mutex can be in one of two states: 19800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread, 19900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread. 20000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 20100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is: 20200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State | ExclusiveLock | ExclusiveUnlock 20300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ------------------------------------------- 20400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free | Exclusive | error 20500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block* | Free 20600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in 20700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// an error. Being non-reentrant simplifies Waiting on ConditionVariables. 20801ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const Mutex& mu); 20900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE Mutex : public BaseMutex { 21000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 21181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false); 21200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~Mutex(); 21300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 21400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsMutex() const { return true; } 21500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 21600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until mutex is free then acquire exclusive access. 21781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION(); 21881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void Lock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); } 21900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 22000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Returns true if acquires exclusive access, false otherwise. 22181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool ExclusiveTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true); 22281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool TryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true) { return ExclusiveTryLock(self); } 22300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 22400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release exclusive access. 22581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION(); 22681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void Unlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); } 22700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 22800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread the exclusive holder of the Mutex. 22981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsExclusiveHeld(const Thread* self) const; 2308daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 23100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert that the Mutex is exclusively held by the current thread. 23281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertExclusiveHeld(const Thread* self) { 233db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray if (kDebugLocking && (gAborting == 0)) { 23401ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(IsExclusiveHeld(self)) << *this; 23500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 23600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 23781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertHeld(const Thread* self) { AssertExclusiveHeld(self); } 23800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 23900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert that the Mutex is not held by the current thread. 24081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotHeldExclusive(const Thread* self) { 241db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray if (kDebugLocking && (gAborting == 0)) { 24201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(!IsExclusiveHeld(self)) << *this; 24300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 24400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 24581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotHeld(const Thread* self) { AssertNotHeldExclusive(self); } 24600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 247c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Id associated with exclusive owner. No memory ordering semantics if called from a thread other 248c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // than the owner. 24900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers uint64_t GetExclusiveOwnerTid() const; 25000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 25100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld. 25200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers unsigned int GetDepth() const { 25300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return recursion_count_; 25400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 25500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 25656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const; 25701ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 25800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 259c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES 260c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // 0 is unheld, 1 is held. 261c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers AtomicInteger state_; 262c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Exclusive owner. 263c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers volatile uint64_t exclusive_owner_; 264c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Number of waiting contenders. 265b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger num_contenders_; 266c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else 26700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_mutex_t mutex_; 268c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers volatile uint64_t exclusive_owner_; // Guarded by mutex_. 269c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif 27000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers const bool recursive_; // Can the lock be recursively held? 27100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers unsigned int recursion_count_; 272f1498437b0d6beb9f4f91980b98cbeb0b5c773ceElliott Hughes friend class ConditionVariable; 2738daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes DISALLOW_COPY_AND_ASSIGN(Mutex); 2748daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 2758daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 27600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex. 27700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader) 27800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a 27900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// condition variable. A ReaderWriterMutex can be in one of three states: 28000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread, 28100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread, 28200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Shared(n) - shared amongst n threads. 28300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 28400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is: 28500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 28600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State | ExclusiveLock | ExclusiveUnlock | SharedLock | SharedUnlock 28700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ---------------------------------------------------------------------------- 28800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free | Exclusive | error | SharedLock(1) | error 28900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block | Free | Block | error 29000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Shared(n) | Block | error | SharedLock(n+1)* | Shared(n-1) or Free 29100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * for large values of n the SharedLock may block. 29201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu); 29300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE ReaderWriterMutex : public BaseMutex { 2948daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes public: 29581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel); 29600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~ReaderWriterMutex(); 29700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 29800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsReaderWriterMutex() const { return true; } 29900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 30000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is free then acquire exclusive access. 30181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION(); 30281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void WriterLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); } 30300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 30400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release exclusive access. 30581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION(); 30681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void WriterUnlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); } 30700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 30800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success 30900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // or false if timeout is reached. 31066aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#if HAVE_TIMED_RWLOCK 311c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns) 31281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers EXCLUSIVE_TRYLOCK_FUNCTION(true); 31366aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#endif 31400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 31500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is shared or free then acquire a share on the access. 3161ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers void SharedLock(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE; 31781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ReaderLock(Thread* self) SHARED_LOCK_FUNCTION() { SharedLock(self); } 31800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 31900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Try to acquire share of ReaderWriterMutex. 32081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool SharedTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true); 32100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 32200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release a share of the access. 3231ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers void SharedUnlock(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE; 32481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ReaderUnlock(Thread* self) UNLOCK_FUNCTION() { SharedUnlock(self); } 32500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 32600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread the exclusive holder of the ReaderWriterMutex. 32781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsExclusiveHeld(const Thread* self) const; 32800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 32900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread has exclusive access to the ReaderWriterMutex. 33081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertExclusiveHeld(const Thread* self) { 331db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray if (kDebugLocking && (gAborting == 0)) { 33201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(IsExclusiveHeld(self)) << *this; 33300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 3348daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 33581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertWriterHeld(const Thread* self) { AssertExclusiveHeld(self); } 3368daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 33700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex. 33881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotExclusiveHeld(const Thread* self) { 339db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray if (kDebugLocking && (gAborting == 0)) { 340e3359f7ad7671c5816f17145ca3a01516512e8d6Ian Rogers CHECK(!IsExclusiveHeld(self)) << *this; 34100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 34200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 34381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotWriterHeld(const Thread* self) { AssertNotExclusiveHeld(self); } 34400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 34500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread a shared holder of the ReaderWriterMutex. 34681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsSharedHeld(const Thread* self) const; 34700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 34800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread has shared access to the ReaderWriterMutex. 34981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertSharedHeld(const Thread* self) { 350db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray if (kDebugLocking && (gAborting == 0)) { 3512cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier // TODO: we can only assert this well when self != null. 3522cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier CHECK(IsSharedHeld(self) || self == nullptr) << *this; 35300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 3548daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 35581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertReaderHeld(const Thread* self) { AssertSharedHeld(self); } 3568daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 35700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive 35800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // mode. 35981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotHeld(const Thread* self) { 360db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray if (kDebugLocking && (gAborting == 0)) { 36101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(!IsSharedHeld(self)) << *this; 36200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 36300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 36400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 365c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Id associated with exclusive owner. No memory ordering semantics if called from a thread other 366c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // than the owner. 36700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers uint64_t GetExclusiveOwnerTid() const; 36881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 36956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const; 37001ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 3718daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes private: 37251d212ef31945743abe8a469707aaa25bab95357Ian Rogers#if ART_USE_FUTEXES 373cf7f19135f0e273f7b0136315633c2abfc715343Ian Rogers // Out-of-inline path for handling contention for a SharedLock. 374cf7f19135f0e273f7b0136315633c2abfc715343Ian Rogers void HandleSharedLockContention(Thread* self, int32_t cur_state); 375cf7f19135f0e273f7b0136315633c2abfc715343Ian Rogers 37681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers // -1 implies held exclusive, +ve shared held by state_ many owners. 377c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers AtomicInteger state_; 378c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Exclusive owner. Modification guarded by this mutex. 37981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers volatile uint64_t exclusive_owner_; 380c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Number of contenders waiting for a reader share. 381c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers AtomicInteger num_pending_readers_; 382c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Number of contenders waiting to be the writer. 383b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger num_pending_writers_; 38481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#else 38500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_rwlock_t rwlock_; 386c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers volatile uint64_t exclusive_owner_; // Guarded by rwlock_. 38781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#endif 38800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex); 3898daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 3908daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 391eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// MutatorMutex is a special kind of ReaderWriterMutex created specifically for the 392eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// Locks::mutator_lock_ mutex. The behaviour is identical to the ReaderWriterMutex except that 393eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// thread state changes also play a part in lock ownership. The mutator_lock_ will not be truly 394eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// held by any mutator threads. However, a thread in the kRunnable state is considered to have 395eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// shared ownership of the mutator lock and therefore transitions in and out of the kRunnable 396eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// state have associated implications on lock ownership. Extra methods to handle the state 397eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// transitions have been added to the interface but are only accessible to the methods dealing 398eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// with state transitions. The thread state and flags attributes are used to ensure thread state 399eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// transitions are consistent with the permitted behaviour of the mutex. 400eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// 401eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// *) The most important consequence of this behaviour is that all threads must be in one of the 402eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// suspended states before exclusive ownership of the mutator mutex is sought. 403eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// 404eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Listd::ostream& operator<<(std::ostream& os, const MutatorMutex& mu); 405eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Liclass LOCKABLE MutatorMutex : public ReaderWriterMutex { 406eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li public: 407eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li explicit MutatorMutex(const char* name, LockLevel level = kDefaultMutexLevel) 408eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li : ReaderWriterMutex(name, level) {} 409eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li ~MutatorMutex() {} 410eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li 411eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li virtual bool IsMutatorMutex() const { return true; } 412eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li 413eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li private: 414eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li friend class Thread; 415eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li void TransitionFromRunnableToSuspended(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE; 416eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li void TransitionFromSuspendedToRunnable(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE; 417eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li 418eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li DISALLOW_COPY_AND_ASSIGN(MutatorMutex); 419eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li}; 420eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li 42100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually 42200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// (Signal) or all at once (Broadcast). 4235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesclass ConditionVariable { 4245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes public: 42523055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers explicit ConditionVariable(const char* name, Mutex& mutex); 4265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ~ConditionVariable(); 4275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 428c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Broadcast(Thread* self); 429c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Signal(Thread* self); 430c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their 431c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // pointer copy, thereby defeating annotalysis. 432c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS; 4337b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers bool TimedWait(Thread* self, int64_t ms, int32_t ns) NO_THREAD_SAFETY_ANALYSIS; 4341d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held 4351d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // when waiting. 4361d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // TODO: remove this. 4371d54e73444e017d3a65234e0f193846f3e27472bIan Rogers void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS; 4385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes private: 44023055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers const char* const name_; 441c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // The Mutex being used by waiters. It is an error to mix condition variables between different 442c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Mutexes. 443c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers Mutex& guard_; 444c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES 445c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // A counter that is modified by signals and broadcasts. This ensures that when a waiter gives up 446d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_ 447d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait 448d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // without guard_ held. 449b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger sequence_; 450c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Number of threads that have come into to wait, not the length of the waiters on the futex as 4515bd97c483c1de1eb97afe76123b1b9ab53095edfIan Rogers // waiters may have been requeued onto guard_. Guarded by guard_. 452c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers volatile int32_t num_waiters_; 453c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else 454c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers pthread_cond_t cond_; 455c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif 4565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DISALLOW_COPY_AND_ASSIGN(ConditionVariable); 4575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}; 4585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 45900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it 46000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// upon destruction. 46100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE MutexLock { 46200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 46381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit MutexLock(Thread* self, Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : self_(self), mu_(mu) { 46481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveLock(self_); 46581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 46681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 46700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~MutexLock() UNLOCK_FUNCTION() { 46881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveUnlock(self_); 46900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 47000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 47100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 47281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Thread* const self_; 47300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Mutex& mu_; 47400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(MutexLock); 47500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 47600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)". 477575e78c41ece0dec969d31f46be563d4eb7ae43bAndreas Gampe#define MutexLock(x) static_assert(0, "MutexLock declaration missing variable name") 47800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 47900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon 48000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction. 48100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE ReaderMutexLock { 48200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 48381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : 48481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers self_(self), mu_(mu) { 48581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.SharedLock(self_); 48681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 48781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 48800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~ReaderMutexLock() UNLOCK_FUNCTION() { 48981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.SharedUnlock(self_); 49000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 49100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 49200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 49381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Thread* const self_; 49400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ReaderWriterMutex& mu_; 49500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock); 49600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 49700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "ReaderMutexLock (lock);" instead of 49800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "ReaderMutexLock mu(lock)". 499575e78c41ece0dec969d31f46be563d4eb7ae43bAndreas Gampe#define ReaderMutexLock(x) static_assert(0, "ReaderMutexLock declaration missing variable name") 50000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 50100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon 50200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction. 50300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE WriterMutexLock { 50400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 50581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : 50681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers self_(self), mu_(mu) { 50781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveLock(self_); 50881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 50981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 51000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~WriterMutexLock() UNLOCK_FUNCTION() { 51181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveUnlock(self_); 51200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 51300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 51400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 51550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers Thread* const self_; 51600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ReaderWriterMutex& mu_; 51700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(WriterMutexLock); 51800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 51900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of 52000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "WriterMutexLock mu(lock)". 521575e78c41ece0dec969d31f46be563d4eb7ae43bAndreas Gampe#define WriterMutexLock(x) static_assert(0, "WriterMutexLock declaration missing variable name") 52200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 523719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Global mutexes corresponding to the levels above. 524719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersclass Locks { 525719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers public: 526719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static void Init(); 52791e56692c6bd9fa1d41951ee7dc311f19461f4beMathieu Chartier static void InitConditions() NO_THREAD_SAFETY_ANALYSIS; // Condition variables. 5289ef78b59da51080882e47505896b420977fd79aeMathieu Chartier // Guards allocation entrypoint instrumenting. 5294ad5cd3e7d519484559ef778d96fb3f0be8919faIan Rogers static Mutex* instrument_entrypoints_lock_; 5309ef78b59da51080882e47505896b420977fd79aeMathieu Chartier 531eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // A barrier is used to synchronize the GC/Debugger thread with mutator threads. When GC/Debugger 532eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // thread wants to suspend all mutator threads, it needs to wait for all mutator threads to pass 533eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // a barrier. Threads that are already suspended will get their barrier passed by the GC/Debugger 534eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // thread; threads in the runnable state will pass the barrier when they transit to the suspended 535eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // state. GC/Debugger thread will be woken up when all mutator threads are suspended. 536719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // 537719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Thread suspension: 538eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // mutator thread | GC/Debugger 539eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // .. running .. | .. running .. 540719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | Request thread suspension by: 541719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - acquiring thread_suspend_count_lock_ 542719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - incrementing Thread::suspend_count_ on 543719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | all mutator threads 544719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - releasing thread_suspend_count_lock_ 545eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // .. running .. | Block wait for all threads to pass a barrier 546719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Poll Thread::suspend_count_ and enter full | .. blocked .. 547719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // suspend code. | .. blocked .. 548eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // Change state to kSuspended (pass the barrier) | Wake up when all threads pass the barrier 549eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // x: Acquire thread_suspend_count_lock_ | .. running .. 550eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // while Thread::suspend_count_ > 0 | .. running .. 551eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // - wait on Thread::resume_cond_ | .. running .. 552eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // (releases thread_suspend_count_lock_) | .. running .. 553719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | Request thread resumption by: 554719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - acquiring thread_suspend_count_lock_ 555719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - decrementing Thread::suspend_count_ on 556719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | all mutator threads 557719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - notifying on Thread::resume_cond_ 558719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - re-acquire thread_suspend_count_lock_ | - releasing thread_suspend_count_lock_ 559719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Release thread_suspend_count_lock_ | .. running .. 560eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // Change to kRunnable | .. running .. 561eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // - this uses a CAS operation to ensure the | .. running .. 562eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // suspend request flag isn't raised as the | .. running .. 563eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // state is changed | .. running .. 564eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // - if the CAS operation fails then goto x | .. running .. 565719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | .. running .. 566eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li static MutatorMutex* mutator_lock_ ACQUIRED_AFTER(instrument_entrypoints_lock_); 567719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 568719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap. 569719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_); 570719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 571719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards shutdown of the runtime. 572719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_); 573719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 5749e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards background profiler global state. 5759e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* profiler_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_); 5769e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 5779e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards trace (ie traceview) requests. 5789e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* trace_lock_ ACQUIRED_AFTER(profiler_lock_); 5799e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 580306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom // Guards debugger recent allocation records. 581306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom static Mutex* alloc_tracker_lock_ ACQUIRED_AFTER(trace_lock_); 582306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom 583306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom // Guards updates to instrumentation to ensure mutual exclusion of 584306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom // events like deoptimization requests. 585306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom // TODO: improve name, perhaps instrumentation_update_lock_. 586306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom static Mutex* deoptimization_lock_ ACQUIRED_AFTER(alloc_tracker_lock_); 587306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom 588719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads 589719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // attaching and detaching. 590306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom static Mutex* thread_list_lock_ ACQUIRED_AFTER(deoptimization_lock_); 591719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 59291e56692c6bd9fa1d41951ee7dc311f19461f4beMathieu Chartier // Signaled when threads terminate. Used to determine when all non-daemons have terminated. 59391e56692c6bd9fa1d41951ee7dc311f19461f4beMathieu Chartier static ConditionVariable* thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_); 59491e56692c6bd9fa1d41951ee7dc311f19461f4beMathieu Chartier 59568d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers // Guards maintaining loading library data structures. 59668d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers static Mutex* jni_libraries_lock_ ACQUIRED_AFTER(thread_list_lock_); 59768d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers 598719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards breakpoints. 599ed2be1725fb79075892b1a9103487c9d9a95b350Sebastien Hertz static ReaderWriterMutex* breakpoint_lock_ ACQUIRED_AFTER(jni_libraries_lock_); 600719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 601719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards lists of classes within the class linker. 6029e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(breakpoint_lock_); 603719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 604719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code 605719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // doesn't try to hold a higher level Mutex. 60612d625f87bcd6c4059a205bb39007a255f57f382Mathieu Chartier #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(Locks::classlinker_classes_lock_) 607719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 60874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe static Mutex* allocated_monitor_ids_lock_ ACQUIRED_AFTER(classlinker_classes_lock_); 60974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe 6109e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guard the allocation/deallocation of thread ids. 61174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe static Mutex* allocated_thread_ids_lock_ ACQUIRED_AFTER(allocated_monitor_ids_lock_); 6129e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 6139e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards modification of the LDT on x86. 6149e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* modify_ldt_lock_ ACQUIRED_AFTER(allocated_thread_ids_lock_); 6159e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 616719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards intern table. 6179e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* intern_table_lock_ ACQUIRED_AFTER(modify_ldt_lock_); 618719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 619a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier // Guards reference processor. 620a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* reference_processor_lock_ ACQUIRED_AFTER(intern_table_lock_); 621a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier 622a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier // Guards cleared references queue. 623a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* reference_queue_cleared_references_lock_ ACQUIRED_AFTER(reference_processor_lock_); 624a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier 625a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier // Guards weak references queue. 626a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* reference_queue_weak_references_lock_ ACQUIRED_AFTER(reference_queue_cleared_references_lock_); 627a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier 628a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier // Guards finalizer references queue. 629a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* reference_queue_finalizer_references_lock_ ACQUIRED_AFTER(reference_queue_weak_references_lock_); 630a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier 631a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier // Guards phantom references queue. 632a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* reference_queue_phantom_references_lock_ ACQUIRED_AFTER(reference_queue_finalizer_references_lock_); 633a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier 634a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier // Guards soft references queue. 635a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* reference_queue_soft_references_lock_ ACQUIRED_AFTER(reference_queue_phantom_references_lock_); 636a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier 637719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Have an exclusive aborting thread. 638a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* abort_lock_ ACQUIRED_AFTER(reference_queue_soft_references_lock_); 639719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 640719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Allow mutual exclusion when manipulating Thread::suspend_count_. 641719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // TODO: Does the trade-off of a per-thread lock make sense? 642719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_); 643719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 644719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // One unexpected signal at a time lock. 645719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_); 646719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 6473eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi // Guards the maps in mem_map. 6483eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi static Mutex* mem_maps_lock_ ACQUIRED_AFTER(unexpected_signal_lock_); 6493eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi 650719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Have an exclusive logging thread. 651719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_); 652e2facc5b18cd756a8b5500fb3d90da69c9ee0fb7Igor Murashkin 653e2facc5b18cd756a8b5500fb3d90da69c9ee0fb7Igor Murashkin // Allow reader-writer mutual exclusion on the boxed table of lambda objects. 654e2facc5b18cd756a8b5500fb3d90da69c9ee0fb7Igor Murashkin // TODO: this should be a RW mutex lock, except that ConditionVariables don't work with it. 655e2facc5b18cd756a8b5500fb3d90da69c9ee0fb7Igor Murashkin static Mutex* lambda_table_lock_ ACQUIRED_AFTER(mutator_lock_); 656719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}; 657719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 6588daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 6598daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 660fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif // ART_RUNTIME_BASE_MUTEX_H_ 661