mutex.h revision fb3de3d72832177e4a8d1f322ed11cbe58e45c9f
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> 220882af2e3ca253184b6ab56a8966a2f37407144eHans Boehm#include <unistd.h> // for pid_t 23ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes 24ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes#include <iosfwd> 258daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include <string> 268daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 2757943810cfc789da890d73621741729da5feaaf8Andreas Gampe#include <android-base/logging.h> 2857943810cfc789da890d73621741729da5feaaf8Andreas Gampe 29ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers#include "atomic.h" 3039b378ca3b4b6dc6da1651b84ee4289cd9bff0f8Andreas Gampe#include "base/aborting.h" 31761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/macros.h" 3200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "globals.h" 3381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 34ab47016374d340011680e9cd970ee7d6225d6704Ian Rogers#if defined(__APPLE__) 3581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#define ART_USE_FUTEXES 0 36ab47016374d340011680e9cd970ee7d6225d6704Ian Rogers#else 37c01417898a6e4f8a5663d6c0556982488c133cdfChris Dearman#define ART_USE_FUTEXES 1 38ab47016374d340011680e9cd970ee7d6225d6704Ian Rogers#endif 398daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 4066aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers// Currently Darwin doesn't support locks with timeouts. 4166aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#if !defined(__APPLE__) 4266aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#define HAVE_TIMED_RWLOCK 1 4366aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#else 4466aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#define HAVE_TIMED_RWLOCK 0 4566aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#endif 4666aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers 478daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesnamespace art { 488daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 4990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SHARED_LOCKABLE ReaderWriterMutex; 5090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SHARED_LOCKABLE MutatorMutex; 5156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogersclass ScopedContentionRecorder; 5250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogersclass Thread; 5350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers 54719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// LockLevel is used to impose a lock hierarchy [1] where acquisition of a Mutex at a higher or 55719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// equal level to a lock a thread holds is invalid. The lock hierarchy achieves a cycle free 56719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// partial ordering and thereby cause deadlock situations to fail checks. 57719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// 58719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// [1] http://www.drdobbs.com/parallel/use-lock-hierarchies-to-avoid-deadlock/204801163 59719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersenum LockLevel { 60719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kLoggingLock = 0, 61fb3de3d72832177e4a8d1f322ed11cbe58e45c9fDavid Srbecky kNativeDebugInterfaceLock, 627de77dd4f2d3cbb0615ee001589eb99ae82c3dccRaghu Gandham kSwapMutexesLock, 63719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kUnexpectedSignalLock, 64719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kThreadSuspendCountLock, 65719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kAbortLock, 66d0a160d722e696b1a3ffb91dcfde61ee11bfb3e0Tao Wu kJdwpAdbStateLock, 67719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpSocketLock, 682cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchi kRegionSpaceRegionLock, 69f169e27100acbec024f699de9ef842a27c0b2036Mathieu Chartier kMarkSweepMarkStackLock, 702994605707219f67af8340ddd9379ce9bd86d74cMathieu Chartier kRosAllocGlobalLock, 712994605707219f67af8340ddd9379ce9bd86d74cMathieu Chartier kRosAllocBracketLock, 722994605707219f67af8340ddd9379ce9bd86d74cMathieu Chartier kRosAllocBulkFreeLock, 73f169e27100acbec024f699de9ef842a27c0b2036Mathieu Chartier kTaggingLockLevel, 74bc4d218ce2ceef0c4f308d4ff42f7ec1ec43c40eAndreas Gampe kTransactionLogLock, 75c8089540ccf0f1c43d8db3828f21d489b28a4013Andreas Gampe kJniFunctionTableLock, 76673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier kJniWeakGlobalsLock, 7705a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe kJniGlobalsLock, 78a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier kReferenceQueueSoftReferencesLock, 79a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier kReferenceQueuePhantomReferencesLock, 80a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier kReferenceQueueFinalizerReferencesLock, 81a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier kReferenceQueueWeakReferencesLock, 82a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier kReferenceQueueClearedReferencesLock, 83a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier kReferenceProcessorLock, 845cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky kJitDebugInterfaceLock, 85719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kAllocSpaceLock, 862cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchi kBumpPointerSpaceBlockLock, 87c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier kArenaPoolLock, 88719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kInternTableLock, 893f5838d7d0b9fc63db0ccc35c2ea05ed29264986Vladimir Marko kOatFileSecondaryLookupLock, 90a206c745dbb64b14f05c87891d425475c2f6d63aRichard Uhler kHostDlOpenHandlesLock, 91ca3c8c33501bf199d6fd0a5db30a27d8e010cb23David Brazdil kVerifierDepsLock, 92f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier kOatFileManagerLock, 937526d783ab68ed1dd53c763c75895cb432532b0fAndreas Gampe kTracingUniqueMethodsLock, 947526d783ab68ed1dd53c763c75895cb432532b0fAndreas Gampe kTracingStreamingLock, 95b8aa1e4c10dcdc7fef96634f87e259dfee83a1cfMathieu Chartier kDeoptimizedMethodsLock, 96b8aa1e4c10dcdc7fef96634f87e259dfee83a1cfMathieu Chartier kClassLoaderClassesLock, 97719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDefaultMutexLevel, 986c60d8420e51fda65ad247ae04b5a823c88c26b6Mathieu Chartier kDexLock, 99719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMarkSweepLargeObjectLock, 100719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpObjectRegistryLock, 1019e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu kModifyLdtLock, 1029e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu kAllocatedThreadIdsLock, 10374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe kMonitorPoolLock, 104b8aa1e4c10dcdc7fef96634f87e259dfee83a1cfMathieu Chartier kClassLinkerClassesLock, // TODO rename. 105a79efdb69350fa66e1beabed4499ef4d0e809785Mathieu Chartier kDexToDexCompilerLock, 10665975776f807d55c83af6cca1e447f8daa794413Mathieu Chartier kJitCodeCacheLock, 107063fc772b5b8aed7d769cd7cccb6ddc7619326eeMingyao Yang kCHALock, 108495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin kSubtypeCheckLock, 109719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kBreakpointLock, 110719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMonitorLock, 111440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier kMonitorListLock, 11268d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers kJniLoadLibraryLock, 113719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kThreadListLock, 114306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom kAllocTrackerLock, 115719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDeoptimizationLock, 116719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kProfilerLock, 1174e5b20863898006ec6c9d120cda167d38dda6e60Sebastien Hertz kJdwpShutdownLock, 118719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpEventListLock, 119719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpAttachLock, 120719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpStartLock, 121719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRuntimeShutdownLock, 12269dbec6d9d55eeb2867949c2791d01dc9aa916c8Jeff Hao kTraceLock, 123719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kHeapBitmapLock, 124719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMutatorLock, 12588fd720b6799184c8ad61e766a6d37af33ed30efAlex Light kUserCodeSuspensionLock, 1269ef78b59da51080882e47505896b420977fd79aeMathieu Chartier kInstrumentEntrypointsLock, 127719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kZygoteCreationLock, 128719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 129b284f8d775ac32d8109744d94b99da451570beefAlex Light // The highest valid lock level. Use this if there is code that should only be called with no 130b284f8d775ac32d8109744d94b99da451570beefAlex Light // other locks held. Since this is the highest lock level we also allow it to be held even if the 131b284f8d775ac32d8109744d94b99da451570beefAlex Light // runtime or current thread is not fully set-up yet (for example during thread attach). Note that 132b284f8d775ac32d8109744d94b99da451570beefAlex Light // this lock also has special behavior around the mutator_lock_. Since the mutator_lock_ is not 133b284f8d775ac32d8109744d94b99da451570beefAlex Light // really a 'real' lock we allow this to be locked when the mutator_lock_ is held exclusive. 134b284f8d775ac32d8109744d94b99da451570beefAlex Light // Furthermore, the mutator_lock_ may not be acquired in any form when a lock of this level is 135b284f8d775ac32d8109744d94b99da451570beefAlex Light // held. Since the mutator_lock_ being held strong means that all other threads are suspended this 136b284f8d775ac32d8109744d94b99da451570beefAlex Light // will prevent deadlocks while still allowing this lock level to function as a "highest" level. 137b284f8d775ac32d8109744d94b99da451570beefAlex Light kTopLockLevel, 138b284f8d775ac32d8109744d94b99da451570beefAlex Light 139719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kLockLevelCount // Must come last. 140719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}; 141719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstd::ostream& operator<<(std::ostream& os, const LockLevel& rhs); 142719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 1432e250c826b3c405d675017efe79e5db3651c9ee6Brian Carlstromconst bool kDebugLocking = kIsDebugBuild; 14425fd14b87cced64a179dee885573113be5e11944Ian Rogers 1451afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Record Log contention information, dumpable via SIGQUIT. 1461afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#ifdef ART_USE_FUTEXES 14708f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Hao// To enable lock contention logging, set this to true. 14808f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Haoconst bool kLogLockContentions = false; 1491afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#else 1501afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Keep this false as lock contention logging is supported only with 1511afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// futex. 1521afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst bool kLogLockContentions = false; 1531afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#endif 154d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersconst size_t kContentionLogSize = 4; 1551afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kContentionLogDataSize = kLogLockContentions ? 1 : 0; 1561afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kAllMutexDataSize = kLogLockContentions ? 1 : 0; 1571afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 15800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Base class for all Mutex implementations 15900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass BaseMutex { 16000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 161bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers const char* GetName() const { 16200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return name_; 16300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 16400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 16500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsMutex() const { return false; } 16600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsReaderWriterMutex() const { return false; } 167eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li virtual bool IsMutatorMutex() const { return false; } 16800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 16956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const = 0; 17056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 17156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers static void DumpAll(std::ostream& os); 17256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 173a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi bool ShouldRespondToEmptyCheckpointRequest() const { 174a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi return should_respond_to_empty_checkpoint_request_; 175a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi } 176a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi 177a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi void SetShouldRespondToEmptyCheckpointRequest(bool value) { 178a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi should_respond_to_empty_checkpoint_request_ = value; 179a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi } 180a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi 181a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi virtual void WakeupToRespondToEmptyCheckpoint() = 0; 182a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi 18300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers protected: 18400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers friend class ConditionVariable; 18500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 18681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers BaseMutex(const char* name, LockLevel level); 18756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual ~BaseMutex(); 18881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void RegisterAsLocked(Thread* self); 18981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void RegisterAsUnlocked(Thread* self); 19081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void CheckSafeToWait(Thread* self); 19100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 19256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers friend class ScopedContentionRecorder; 19356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 1941afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi void RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t nano_time_blocked); 19556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers void DumpContention(std::ostream& os) const; 19656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 19781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers const LockLevel level_; // Support for lock hierarchy. 198bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers const char* const name_; 199a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi bool should_respond_to_empty_checkpoint_request_; 2001afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 20156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers // A log entry that records contention but makes no guarantee that either tid will be held live. 20256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers struct ContentionLogEntry { 20356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers ContentionLogEntry() : blocked_tid(0), owner_tid(0) {} 20456edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers uint64_t blocked_tid; 20556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers uint64_t owner_tid; 20656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers AtomicInteger count; 20756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers }; 2081afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi struct ContentionLogData { 2091afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi ContentionLogEntry contention_log[kContentionLogSize]; 2101afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // The next entry in the contention log to be updated. Value ranges from 0 to 2111afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // kContentionLogSize - 1. 2121afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi AtomicInteger cur_content_log_entry; 2131afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // Number of times the Mutex has been contended. 2141afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi AtomicInteger contention_count; 2151afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // Sum of time waited by all contenders in ns. 21637f3c968ecd04e77802fe17bb82dabc07de21ca1Ian Rogers Atomic<uint64_t> wait_time; 2171afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi void AddToWaitTime(uint64_t value); 2181afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi ContentionLogData() : wait_time(0) {} 2191afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi }; 2203e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers ContentionLogData contention_log_data_[kContentionLogDataSize]; 2211afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 2221afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi public: 2231afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi bool HasEverContended() const { 2241afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi if (kLogLockContentions) { 2253e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers return contention_log_data_->contention_count.LoadSequentiallyConsistent() > 0; 2261afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi } 2271afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi return false; 2281afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi } 22900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 23000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 23100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain 23200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// exclusive access to what it guards. A Mutex can be in one of two states: 23300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread, 23400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread. 23500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 23600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is: 23700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State | ExclusiveLock | ExclusiveUnlock 23800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ------------------------------------------- 23900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free | Exclusive | error 24000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block* | Free 24100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in 24200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// an error. Being non-reentrant simplifies Waiting on ConditionVariables. 24301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const Mutex& mu); 24400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE Mutex : public BaseMutex { 24500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 24681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false); 24700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~Mutex(); 24800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 24900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsMutex() const { return true; } 25000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 25100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until mutex is free then acquire exclusive access. 25290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void ExclusiveLock(Thread* self) ACQUIRE(); 25390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void Lock(Thread* self) ACQUIRE() { ExclusiveLock(self); } 25400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 25500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Returns true if acquires exclusive access, false otherwise. 25690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier bool ExclusiveTryLock(Thread* self) TRY_ACQUIRE(true); 25790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier bool TryLock(Thread* self) TRY_ACQUIRE(true) { return ExclusiveTryLock(self); } 25800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 25900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release exclusive access. 26090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void ExclusiveUnlock(Thread* self) RELEASE(); 26190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void Unlock(Thread* self) RELEASE() { ExclusiveUnlock(self); } 26200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 26300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread the exclusive holder of the Mutex. 264b486a98aadc95d80548953410cf23edba62259faAndreas Gampe ALWAYS_INLINE bool IsExclusiveHeld(const Thread* self) const; 2658daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 26600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert that the Mutex is exclusively held by the current thread. 267b486a98aadc95d80548953410cf23edba62259faAndreas Gampe ALWAYS_INLINE void AssertExclusiveHeld(const Thread* self) const ASSERT_CAPABILITY(this); 268b486a98aadc95d80548953410cf23edba62259faAndreas Gampe ALWAYS_INLINE void AssertHeld(const Thread* self) const ASSERT_CAPABILITY(this); 26900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 27000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert that the Mutex is not held by the current thread. 27190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void AssertNotHeldExclusive(const Thread* self) ASSERT_CAPABILITY(!*this) { 272db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray if (kDebugLocking && (gAborting == 0)) { 27301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(!IsExclusiveHeld(self)) << *this; 27400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 27500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 27690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void AssertNotHeld(const Thread* self) ASSERT_CAPABILITY(!*this) { 27790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier AssertNotHeldExclusive(self); 27890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier } 27900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 280c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Id associated with exclusive owner. No memory ordering semantics if called from a thread other 281c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // than the owner. 2820882af2e3ca253184b6ab56a8966a2f37407144eHans Boehm pid_t GetExclusiveOwnerTid() const; 28300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 28400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld. 28500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers unsigned int GetDepth() const { 28600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return recursion_count_; 28700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 28800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 28956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const; 29001ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 29190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier // For negative capabilities in clang annotations. 29290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier const Mutex& operator!() const { return *this; } 29390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier 294a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi void WakeupToRespondToEmptyCheckpoint() OVERRIDE; 295a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi 29600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 297c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES 298c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // 0 is unheld, 1 is held. 299c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers AtomicInteger state_; 300c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Exclusive owner. 3010882af2e3ca253184b6ab56a8966a2f37407144eHans Boehm Atomic<pid_t> exclusive_owner_; 302c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Number of waiting contenders. 303b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger num_contenders_; 304c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else 30500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_mutex_t mutex_; 3060882af2e3ca253184b6ab56a8966a2f37407144eHans Boehm Atomic<pid_t> exclusive_owner_; // Guarded by mutex_. Asynchronous reads are OK. 307c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif 30800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers const bool recursive_; // Can the lock be recursively held? 30900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers unsigned int recursion_count_; 310f1498437b0d6beb9f4f91980b98cbeb0b5c773ceElliott Hughes friend class ConditionVariable; 3118daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes DISALLOW_COPY_AND_ASSIGN(Mutex); 3128daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 3138daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 31400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex. 31500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader) 31600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a 31700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// condition variable. A ReaderWriterMutex can be in one of three states: 31800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread, 31900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread, 32000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Shared(n) - shared amongst n threads. 32100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 32200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is: 32300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 32400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State | ExclusiveLock | ExclusiveUnlock | SharedLock | SharedUnlock 32500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ---------------------------------------------------------------------------- 32600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free | Exclusive | error | SharedLock(1) | error 32700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block | Free | Block | error 32800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Shared(n) | Block | error | SharedLock(n+1)* | Shared(n-1) or Free 32900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * for large values of n the SharedLock may block. 33001ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu); 33190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SHARED_LOCKABLE ReaderWriterMutex : public BaseMutex { 3328daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes public: 33381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel); 33400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~ReaderWriterMutex(); 33500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 33600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsReaderWriterMutex() const { return true; } 33700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 33800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is free then acquire exclusive access. 33990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void ExclusiveLock(Thread* self) ACQUIRE(); 34090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void WriterLock(Thread* self) ACQUIRE() { ExclusiveLock(self); } 34100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 34200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release exclusive access. 34390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void ExclusiveUnlock(Thread* self) RELEASE(); 34490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void WriterUnlock(Thread* self) RELEASE() { ExclusiveUnlock(self); } 34500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 34600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success 34700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // or false if timeout is reached. 34866aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#if HAVE_TIMED_RWLOCK 349c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns) 35081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers EXCLUSIVE_TRYLOCK_FUNCTION(true); 35166aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#endif 35200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 35300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is shared or free then acquire a share on the access. 35490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void SharedLock(Thread* self) ACQUIRE_SHARED() ALWAYS_INLINE; 35590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void ReaderLock(Thread* self) ACQUIRE_SHARED() { SharedLock(self); } 35600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 35700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Try to acquire share of ReaderWriterMutex. 35890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier bool SharedTryLock(Thread* self) SHARED_TRYLOCK_FUNCTION(true); 35900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 36000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release a share of the access. 36190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void SharedUnlock(Thread* self) RELEASE_SHARED() ALWAYS_INLINE; 36290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier void ReaderUnlock(Thread* self) RELEASE_SHARED() { SharedUnlock(self); } 36300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 36400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread the exclusive holder of the ReaderWriterMutex. 365b486a98aadc95d80548953410cf23edba62259faAndreas Gampe ALWAYS_INLINE bool IsExclusiveHeld(const Thread* self) const; 36600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 36700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread has exclusive access to the ReaderWriterMutex. 368b486a98aadc95d80548953410cf23edba62259faAndreas Gampe ALWAYS_INLINE void AssertExclusiveHeld(const Thread* self) const ASSERT_CAPABILITY(this); 369b486a98aadc95d80548953410cf23edba62259faAndreas Gampe ALWAYS_INLINE void AssertWriterHeld(const Thread* self) const ASSERT_CAPABILITY(this); 3708daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 37100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex. 37290ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier void AssertNotExclusiveHeld(const Thread* self) ASSERT_CAPABILITY(!this) { 373db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray if (kDebugLocking && (gAborting == 0)) { 374e3359f7ad7671c5816f17145ca3a01516512e8d6Ian Rogers CHECK(!IsExclusiveHeld(self)) << *this; 37500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 37600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 37790ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier void AssertNotWriterHeld(const Thread* self) ASSERT_CAPABILITY(!this) { 37890ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier AssertNotExclusiveHeld(self); 37990ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier } 38000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 38100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread a shared holder of the ReaderWriterMutex. 38281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsSharedHeld(const Thread* self) const; 38300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 38400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread has shared access to the ReaderWriterMutex. 3853768adec2f819cb506577618fdde158ba659ddd4Mathieu Chartier ALWAYS_INLINE void AssertSharedHeld(const Thread* self) ASSERT_SHARED_CAPABILITY(this) { 386db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray if (kDebugLocking && (gAborting == 0)) { 3872cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier // TODO: we can only assert this well when self != null. 3882cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier CHECK(IsSharedHeld(self) || self == nullptr) << *this; 38900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 3908daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 3913768adec2f819cb506577618fdde158ba659ddd4Mathieu Chartier ALWAYS_INLINE void AssertReaderHeld(const Thread* self) ASSERT_SHARED_CAPABILITY(this) { 39290ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier AssertSharedHeld(self); 39390ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier } 3948daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 39500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive 39600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // mode. 3973768adec2f819cb506577618fdde158ba659ddd4Mathieu Chartier ALWAYS_INLINE void AssertNotHeld(const Thread* self) ASSERT_SHARED_CAPABILITY(!this) { 398db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray if (kDebugLocking && (gAborting == 0)) { 39901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(!IsSharedHeld(self)) << *this; 40000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 40100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 40200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 403c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Id associated with exclusive owner. No memory ordering semantics if called from a thread other 4040882af2e3ca253184b6ab56a8966a2f37407144eHans Boehm // than the owner. Returns 0 if the lock is not held. Returns either 0 or -1 if it is held by 4050882af2e3ca253184b6ab56a8966a2f37407144eHans Boehm // one or more readers. 4060882af2e3ca253184b6ab56a8966a2f37407144eHans Boehm pid_t GetExclusiveOwnerTid() const; 40781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 40856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const; 40901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 41090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier // For negative capabilities in clang annotations. 41190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier const ReaderWriterMutex& operator!() const { return *this; } 41290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier 413a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi void WakeupToRespondToEmptyCheckpoint() OVERRIDE; 414a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi 4158daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes private: 41651d212ef31945743abe8a469707aaa25bab95357Ian Rogers#if ART_USE_FUTEXES 417cf7f19135f0e273f7b0136315633c2abfc715343Ian Rogers // Out-of-inline path for handling contention for a SharedLock. 418cf7f19135f0e273f7b0136315633c2abfc715343Ian Rogers void HandleSharedLockContention(Thread* self, int32_t cur_state); 419cf7f19135f0e273f7b0136315633c2abfc715343Ian Rogers 42081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers // -1 implies held exclusive, +ve shared held by state_ many owners. 421c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers AtomicInteger state_; 422c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Exclusive owner. Modification guarded by this mutex. 4230882af2e3ca253184b6ab56a8966a2f37407144eHans Boehm Atomic<pid_t> exclusive_owner_; 424c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Number of contenders waiting for a reader share. 425c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers AtomicInteger num_pending_readers_; 426c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Number of contenders waiting to be the writer. 427b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger num_pending_writers_; 42881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#else 42900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_rwlock_t rwlock_; 4300882af2e3ca253184b6ab56a8966a2f37407144eHans Boehm Atomic<pid_t> exclusive_owner_; // Writes guarded by rwlock_. Asynchronous reads are OK. 43181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#endif 43200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex); 4338daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 4348daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 435eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// MutatorMutex is a special kind of ReaderWriterMutex created specifically for the 436eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// Locks::mutator_lock_ mutex. The behaviour is identical to the ReaderWriterMutex except that 437eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// thread state changes also play a part in lock ownership. The mutator_lock_ will not be truly 438eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// held by any mutator threads. However, a thread in the kRunnable state is considered to have 439eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// shared ownership of the mutator lock and therefore transitions in and out of the kRunnable 440eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// state have associated implications on lock ownership. Extra methods to handle the state 441eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// transitions have been added to the interface but are only accessible to the methods dealing 442eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// with state transitions. The thread state and flags attributes are used to ensure thread state 443eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// transitions are consistent with the permitted behaviour of the mutex. 444eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// 445eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// *) The most important consequence of this behaviour is that all threads must be in one of the 446eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// suspended states before exclusive ownership of the mutator mutex is sought. 447eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li// 448eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Listd::ostream& operator<<(std::ostream& os, const MutatorMutex& mu); 44990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SHARED_LOCKABLE MutatorMutex : public ReaderWriterMutex { 450eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li public: 451eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li explicit MutatorMutex(const char* name, LockLevel level = kDefaultMutexLevel) 452eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li : ReaderWriterMutex(name, level) {} 453eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li ~MutatorMutex() {} 454eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li 455eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li virtual bool IsMutatorMutex() const { return true; } 456eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li 45790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier // For negative capabilities in clang annotations. 45890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier const MutatorMutex& operator!() const { return *this; } 45990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier 460eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li private: 461eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li friend class Thread; 462eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li void TransitionFromRunnableToSuspended(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE; 463eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li void TransitionFromSuspendedToRunnable(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE; 464eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li 465eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li DISALLOW_COPY_AND_ASSIGN(MutatorMutex); 466eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li}; 467eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li 46800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually 46900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// (Signal) or all at once (Broadcast). 4705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesclass ConditionVariable { 4715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes public: 4723887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain ConditionVariable(const char* name, Mutex& mutex); 4735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ~ConditionVariable(); 4745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 475c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Broadcast(Thread* self); 476c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Signal(Thread* self); 477c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their 478c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // pointer copy, thereby defeating annotalysis. 479c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS; 4807b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers bool TimedWait(Thread* self, int64_t ms, int32_t ns) NO_THREAD_SAFETY_ANALYSIS; 4811d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held 4821d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // when waiting. 4831d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // TODO: remove this. 4841d54e73444e017d3a65234e0f193846f3e27472bIan Rogers void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS; 4855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes private: 48723055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers const char* const name_; 488c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // The Mutex being used by waiters. It is an error to mix condition variables between different 489c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Mutexes. 490c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers Mutex& guard_; 491c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES 492c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // A counter that is modified by signals and broadcasts. This ensures that when a waiter gives up 493d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_ 494d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait 495d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // without guard_ held. 496b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger sequence_; 497c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Number of threads that have come into to wait, not the length of the waiters on the futex as 4985bd97c483c1de1eb97afe76123b1b9ab53095edfIan Rogers // waiters may have been requeued onto guard_. Guarded by guard_. 499c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers volatile int32_t num_waiters_; 500c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else 501c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers pthread_cond_t cond_; 502c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif 5035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DISALLOW_COPY_AND_ASSIGN(ConditionVariable); 5045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}; 5055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 50600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it 50700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// upon destruction. 50890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SCOPED_CAPABILITY MutexLock { 50900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 5103887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain MutexLock(Thread* self, Mutex& mu) ACQUIRE(mu) : self_(self), mu_(mu) { 51181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveLock(self_); 51281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 51381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 5144e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier ~MutexLock() RELEASE() { 51581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveUnlock(self_); 51600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 51700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 51800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 51981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Thread* const self_; 52000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Mutex& mu_; 52100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(MutexLock); 52200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 52300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)". 524575e78c41ece0dec969d31f46be563d4eb7ae43bAndreas Gampe#define MutexLock(x) static_assert(0, "MutexLock declaration missing variable name") 52500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 52600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon 52700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction. 52890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SCOPED_CAPABILITY ReaderMutexLock { 52900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 530b486a98aadc95d80548953410cf23edba62259faAndreas Gampe ALWAYS_INLINE ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) ACQUIRE(mu); 53181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 532b486a98aadc95d80548953410cf23edba62259faAndreas Gampe ALWAYS_INLINE ~ReaderMutexLock() RELEASE(); 53300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 53400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 53581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Thread* const self_; 53600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ReaderWriterMutex& mu_; 53700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock); 53800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 53900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 54000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon 54100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction. 54290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierclass SCOPED_CAPABILITY WriterMutexLock { 54300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 5443887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : 54581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers self_(self), mu_(mu) { 54681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveLock(self_); 54781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 54881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 54900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~WriterMutexLock() UNLOCK_FUNCTION() { 55081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveUnlock(self_); 55100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 55200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 55300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 55450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers Thread* const self_; 55500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ReaderWriterMutex& mu_; 55600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(WriterMutexLock); 55700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 55800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of 55900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "WriterMutexLock mu(lock)". 560575e78c41ece0dec969d31f46be563d4eb7ae43bAndreas Gampe#define WriterMutexLock(x) static_assert(0, "WriterMutexLock declaration missing variable name") 56100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 5624e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier// For StartNoThreadSuspension and EndNoThreadSuspension. 5634e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartierclass CAPABILITY("role") Role { 5644e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier public: 5654e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier void Acquire() ACQUIRE() {} 5664e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier void Release() RELEASE() {} 5674e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier const Role& operator!() const { return *this; } 5684e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier}; 5694e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier 5704e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartierclass Uninterruptible : public Role { 5714e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier}; 5724e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier 573719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Global mutexes corresponding to the levels above. 574719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersclass Locks { 575719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers public: 576719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static void Init(); 57791e56692c6bd9fa1d41951ee7dc311f19461f4beMathieu Chartier static void InitConditions() NO_THREAD_SAFETY_ANALYSIS; // Condition variables. 578f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr 579f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr // Destroying various lock types can emit errors that vary depending upon 580f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr // whether the client (art::Runtime) is currently active. Allow the client 581f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr // to set a callback that is used to check when it is acceptable to call 582f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr // Abort. The default behavior is that the client *is not* able to call 583f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr // Abort if no callback is established. 584f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr using ClientCallback = bool(); 585f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr static void SetClientCallback(ClientCallback* is_safe_to_call_abort_cb) NO_THREAD_SAFETY_ANALYSIS; 586f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr // Checks for whether it is safe to call Abort() without using locks. 587f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr static bool IsSafeToCallAbortRacy() NO_THREAD_SAFETY_ANALYSIS; 588f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr 5898a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi // Add a mutex to expected_mutexes_on_weak_ref_access_. 5908a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi static void AddToExpectedMutexesOnWeakRefAccess(BaseMutex* mutex, bool need_lock = true); 5918a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi // Remove a mutex from expected_mutexes_on_weak_ref_access_. 5928a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi static void RemoveFromExpectedMutexesOnWeakRefAccess(BaseMutex* mutex, bool need_lock = true); 5938a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi // Check if the given mutex is in expected_mutexes_on_weak_ref_access_. 5948a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi static bool IsExpectedOnWeakRefAccess(BaseMutex* mutex); 595f42eb2c7801dbb45a6ba20a372d5ba4712ebefbaDavid Sehr 5969ef78b59da51080882e47505896b420977fd79aeMathieu Chartier // Guards allocation entrypoint instrumenting. 5974ad5cd3e7d519484559ef778d96fb3f0be8919faIan Rogers static Mutex* instrument_entrypoints_lock_; 5989ef78b59da51080882e47505896b420977fd79aeMathieu Chartier 59988fd720b6799184c8ad61e766a6d37af33ed30efAlex Light // Guards code that deals with user-code suspension. This mutex must be held when suspending or 60088fd720b6799184c8ad61e766a6d37af33ed30efAlex Light // resuming threads with SuspendReason::kForUserCode. It may be held by a suspended thread, but 60188fd720b6799184c8ad61e766a6d37af33ed30efAlex Light // only if the suspension is not due to SuspendReason::kForUserCode. 60288fd720b6799184c8ad61e766a6d37af33ed30efAlex Light static Mutex* user_code_suspension_lock_ ACQUIRED_AFTER(instrument_entrypoints_lock_); 60388fd720b6799184c8ad61e766a6d37af33ed30efAlex Light 604eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // A barrier is used to synchronize the GC/Debugger thread with mutator threads. When GC/Debugger 605eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // thread wants to suspend all mutator threads, it needs to wait for all mutator threads to pass 606eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // a barrier. Threads that are already suspended will get their barrier passed by the GC/Debugger 607eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // thread; threads in the runnable state will pass the barrier when they transit to the suspended 608eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // state. GC/Debugger thread will be woken up when all mutator threads are suspended. 609719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // 610719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Thread suspension: 611eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // mutator thread | GC/Debugger 612eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // .. running .. | .. running .. 613719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | Request thread suspension by: 614719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - acquiring thread_suspend_count_lock_ 615719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - incrementing Thread::suspend_count_ on 616719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | all mutator threads 617719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - releasing thread_suspend_count_lock_ 618eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // .. running .. | Block wait for all threads to pass a barrier 619719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Poll Thread::suspend_count_ and enter full | .. blocked .. 620719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // suspend code. | .. blocked .. 621eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // Change state to kSuspended (pass the barrier) | Wake up when all threads pass the barrier 622eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // x: Acquire thread_suspend_count_lock_ | .. running .. 623eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // while Thread::suspend_count_ > 0 | .. running .. 624eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // - wait on Thread::resume_cond_ | .. running .. 625eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // (releases thread_suspend_count_lock_) | .. running .. 626719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | Request thread resumption by: 627719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - acquiring thread_suspend_count_lock_ 628719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - decrementing Thread::suspend_count_ on 629719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | all mutator threads 630719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - notifying on Thread::resume_cond_ 631719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - re-acquire thread_suspend_count_lock_ | - releasing thread_suspend_count_lock_ 632719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Release thread_suspend_count_lock_ | .. running .. 633eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // Change to kRunnable | .. running .. 634eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // - this uses a CAS operation to ensure the | .. running .. 635eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // suspend request flag isn't raised as the | .. running .. 636eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // state is changed | .. running .. 637eac4424b3420c280f97ff2f815b5dedd8dac9801Yu Li // - if the CAS operation fails then goto x | .. running .. 638719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | .. running .. 63988fd720b6799184c8ad61e766a6d37af33ed30efAlex Light static MutatorMutex* mutator_lock_ ACQUIRED_AFTER(user_code_suspension_lock_); 640719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 641719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap. 642719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_); 643719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 644719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards shutdown of the runtime. 645719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_); 646719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 6479e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards background profiler global state. 648b139b6d3833cd6a90f345204202ec57ff277c088Hiroshi Yamauchi static Mutex* profiler_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_); 6499e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 6509e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards trace (ie traceview) requests. 6519e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* trace_lock_ ACQUIRED_AFTER(profiler_lock_); 6529e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 653306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom // Guards debugger recent allocation records. 654306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom static Mutex* alloc_tracker_lock_ ACQUIRED_AFTER(trace_lock_); 655306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom 656306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom // Guards updates to instrumentation to ensure mutual exclusion of 657306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom // events like deoptimization requests. 658306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom // TODO: improve name, perhaps instrumentation_update_lock_. 659306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom static Mutex* deoptimization_lock_ ACQUIRED_AFTER(alloc_tracker_lock_); 660306db81aba41eb244a4e8299cf58ac18ae9999c7Brian Carlstrom 661063fc772b5b8aed7d769cd7cccb6ddc7619326eeMingyao Yang // Guards Class Hierarchy Analysis (CHA). 662063fc772b5b8aed7d769cd7cccb6ddc7619326eeMingyao Yang static Mutex* cha_lock_ ACQUIRED_AFTER(deoptimization_lock_); 663063fc772b5b8aed7d769cd7cccb6ddc7619326eeMingyao Yang 664495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin // Guard the update of the SubtypeCheck data stores in each Class::status_ field. 665495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin // This lock is used in SubtypeCheck methods which are the interface for 666495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin // any SubtypeCheck-mutating methods. 667495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin // In Class::IsSubClass, the lock is not required since it does not update the SubtypeCheck data. 668495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin static Mutex* subtype_check_lock_ ACQUIRED_AFTER(cha_lock_); 669495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin 670719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads 671719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // attaching and detaching. 672495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin static Mutex* thread_list_lock_ ACQUIRED_AFTER(subtype_check_lock_); 673719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 67491e56692c6bd9fa1d41951ee7dc311f19461f4beMathieu Chartier // Signaled when threads terminate. Used to determine when all non-daemons have terminated. 67591e56692c6bd9fa1d41951ee7dc311f19461f4beMathieu Chartier static ConditionVariable* thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_); 67691e56692c6bd9fa1d41951ee7dc311f19461f4beMathieu Chartier 67768d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers // Guards maintaining loading library data structures. 67868d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers static Mutex* jni_libraries_lock_ ACQUIRED_AFTER(thread_list_lock_); 67968d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers 680719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards breakpoints. 681ed2be1725fb79075892b1a9103487c9d9a95b350Sebastien Hertz static ReaderWriterMutex* breakpoint_lock_ ACQUIRED_AFTER(jni_libraries_lock_); 682719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 683719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards lists of classes within the class linker. 6849e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(breakpoint_lock_); 685719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 686719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code 687719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // doesn't try to hold a higher level Mutex. 6880fa1786bdcc333873ed65a1f77a4669d5701ac5eAlex Light #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(art::Locks::classlinker_classes_lock_) 689719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 69074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe static Mutex* allocated_monitor_ids_lock_ ACQUIRED_AFTER(classlinker_classes_lock_); 69174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe 6929e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guard the allocation/deallocation of thread ids. 69374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe static Mutex* allocated_thread_ids_lock_ ACQUIRED_AFTER(allocated_monitor_ids_lock_); 6949e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 6959e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards modification of the LDT on x86. 6969e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* modify_ldt_lock_ ACQUIRED_AFTER(allocated_thread_ids_lock_); 6979e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 698cc1b5357f83f0b787d51fbfde3fe870c8a2fa050Andreas Gampe static ReaderWriterMutex* dex_lock_ ACQUIRED_AFTER(modify_ldt_lock_); 699cc1b5357f83f0b787d51fbfde3fe870c8a2fa050Andreas Gampe 700f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier // Guards opened oat files in OatFileManager. 701cc1b5357f83f0b787d51fbfde3fe870c8a2fa050Andreas Gampe static ReaderWriterMutex* oat_file_manager_lock_ ACQUIRED_AFTER(dex_lock_); 702f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier 703340dafabc8e88378e395cda9027cf17726910e91Nicolas Geoffray // Guards extra string entries for VerifierDeps. 704340dafabc8e88378e395cda9027cf17726910e91Nicolas Geoffray static ReaderWriterMutex* verifier_deps_lock_ ACQUIRED_AFTER(oat_file_manager_lock_); 705ca3c8c33501bf199d6fd0a5db30a27d8e010cb23David Brazdil 706a206c745dbb64b14f05c87891d425475c2f6d63aRichard Uhler // Guards dlopen_handles_ in DlOpenOatFile. 707ca3c8c33501bf199d6fd0a5db30a27d8e010cb23David Brazdil static Mutex* host_dlopen_handles_lock_ ACQUIRED_AFTER(verifier_deps_lock_); 708e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier 709719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards intern table. 710a206c745dbb64b14f05c87891d425475c2f6d63aRichard Uhler static Mutex* intern_table_lock_ ACQUIRED_AFTER(host_dlopen_handles_lock_); 711719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 712a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier // Guards reference processor. 713a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* reference_processor_lock_ ACQUIRED_AFTER(intern_table_lock_); 714a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier 715a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier // Guards cleared references queue. 716a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* reference_queue_cleared_references_lock_ ACQUIRED_AFTER(reference_processor_lock_); 717a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier 718a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier // Guards weak references queue. 719a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* reference_queue_weak_references_lock_ ACQUIRED_AFTER(reference_queue_cleared_references_lock_); 720a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier 721a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier // Guards finalizer references queue. 722a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* reference_queue_finalizer_references_lock_ ACQUIRED_AFTER(reference_queue_weak_references_lock_); 723a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier 724a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier // Guards phantom references queue. 725a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* reference_queue_phantom_references_lock_ ACQUIRED_AFTER(reference_queue_finalizer_references_lock_); 726a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier 727a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier // Guards soft references queue. 728a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier static Mutex* reference_queue_soft_references_lock_ ACQUIRED_AFTER(reference_queue_phantom_references_lock_); 729a5a53efea976af505f4f849b5925d5e14c4f8e5cMathieu Chartier 73005a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe // Guard accesses to the JNI Global Reference table. 73105a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe static ReaderWriterMutex* jni_globals_lock_ ACQUIRED_AFTER(reference_queue_soft_references_lock_); 73205a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe 73305a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe // Guard accesses to the JNI Weak Global Reference table. 73405a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe static Mutex* jni_weak_globals_lock_ ACQUIRED_AFTER(jni_globals_lock_); 73505a364c8d8271ceeca307d04736f53e92d03de9dAndreas Gampe 736c8089540ccf0f1c43d8db3828f21d489b28a4013Andreas Gampe // Guard accesses to the JNI function table override. 737c8089540ccf0f1c43d8db3828f21d489b28a4013Andreas Gampe static Mutex* jni_function_table_lock_ ACQUIRED_AFTER(jni_weak_globals_lock_); 738c8089540ccf0f1c43d8db3828f21d489b28a4013Andreas Gampe 739719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Have an exclusive aborting thread. 740c8089540ccf0f1c43d8db3828f21d489b28a4013Andreas Gampe static Mutex* abort_lock_ ACQUIRED_AFTER(jni_function_table_lock_); 741719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 742719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Allow mutual exclusion when manipulating Thread::suspend_count_. 743719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // TODO: Does the trade-off of a per-thread lock make sense? 744719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_); 745719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 746719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // One unexpected signal at a time lock. 747719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_); 748719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 749fb3de3d72832177e4a8d1f322ed11cbe58e45c9fDavid Srbecky // Guards the magic global variables used by native tools (e.g. libunwind). 750fb3de3d72832177e4a8d1f322ed11cbe58e45c9fDavid Srbecky static Mutex* native_debug_interface_lock_ ACQUIRED_AFTER(unexpected_signal_lock_); 751fb3de3d72832177e4a8d1f322ed11cbe58e45c9fDavid Srbecky 752719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Have an exclusive logging thread. 753fb3de3d72832177e4a8d1f322ed11cbe58e45c9fDavid Srbecky static Mutex* logging_lock_ ACQUIRED_AFTER(native_debug_interface_lock_); 754a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi 755a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi // List of mutexes that we expect a thread may hold when accessing weak refs. This is used to 756a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi // avoid a deadlock in the empty checkpoint while weak ref access is disabled (b/34964016). If we 757a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi // encounter an unexpected mutex on accessing weak refs, 758a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi // Thread::CheckEmptyCheckpointFromWeakRefAccess will detect it. 759a222404a5832ab16786931576d52825d08eed3caHiroshi Yamauchi static std::vector<BaseMutex*> expected_mutexes_on_weak_ref_access_; 7608a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi static Atomic<const BaseMutex*> expected_mutexes_on_weak_ref_access_guard_; 7618a433246d0f26f1f8519925ee6fc5cee8fbd0788Hiroshi Yamauchi class ScopedExpectedMutexesOnWeakRefAccessLock; 762719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}; 763719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 7644e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartierclass Roles { 7654e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier public: 76690ef3db4bd1d4865f5f9cb95c8e7d9afb46994f9Mathieu Chartier // Uninterruptible means that the thread may not become suspended. 7674e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier static Uninterruptible uninterruptible_; 7684e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier}; 7694e2cb098017bf073335ebb02b1bc0a36828cd720Mathieu Chartier 7708daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 7718daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 772fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif // ART_RUNTIME_BASE_MUTEX_H_ 773