mutex.h revision 3eed93dd5be03e5539827bebf0f414251a12e15e
18daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes/* 28daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Copyright (C) 2011 The Android Open Source Project 38daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * 48daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 58daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * you may not use this file except in compliance with the License. 68daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * You may obtain a copy of the License at 78daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * 88daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 98daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * 108daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Unless required by applicable law or agreed to in writing, software 118daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 128daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * See the License for the specific language governing permissions and 148daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * limitations under the License. 158daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes */ 168daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_BASE_MUTEX_H_ 18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_BASE_MUTEX_H_ 198daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 208daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include <pthread.h> 21cd74c4b3a6893c876c6e03fd99a1264249653d80Brian Carlstrom#include <stdint.h> 22ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes 23ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes#include <iosfwd> 248daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include <string> 258daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 26ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers#include "atomic.h" 2707ed66b5ae659c452cbe1ab20c3dbf1d6f546461Elliott Hughes#include "base/logging.h" 28761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/macros.h" 2900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "globals.h" 3081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 31ab47016374d340011680e9cd970ee7d6225d6704Ian Rogers#if defined(__APPLE__) 3281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#define ART_USE_FUTEXES 0 33ab47016374d340011680e9cd970ee7d6225d6704Ian Rogers#else 34c01417898a6e4f8a5663d6c0556982488c133cdfChris Dearman#define ART_USE_FUTEXES 1 35ab47016374d340011680e9cd970ee7d6225d6704Ian Rogers#endif 368daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 3766aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers// Currently Darwin doesn't support locks with timeouts. 3866aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#if !defined(__APPLE__) 3966aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#define HAVE_TIMED_RWLOCK 1 4066aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#else 4166aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#define HAVE_TIMED_RWLOCK 0 4266aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#endif 4366aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers 448daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesnamespace art { 458daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 46719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersclass LOCKABLE ReaderWriterMutex; 4756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogersclass ScopedContentionRecorder; 4850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogersclass Thread; 4950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers 50719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// LockLevel is used to impose a lock hierarchy [1] where acquisition of a Mutex at a higher or 51719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// equal level to a lock a thread holds is invalid. The lock hierarchy achieves a cycle free 52719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// partial ordering and thereby cause deadlock situations to fail checks. 53719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// 54719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// [1] http://www.drdobbs.com/parallel/use-lock-hierarchies-to-avoid-deadlock/204801163 55719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersenum LockLevel { 56719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kLoggingLock = 0, 573eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi kMemMapsLock, 58719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kUnexpectedSignalLock, 59719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kThreadSuspendCountLock, 60719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kAbortLock, 61719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpSocketLock, 62719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRosAllocGlobalLock, 63719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRosAllocBracketLock, 64719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRosAllocBulkFreeLock, 65719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kAllocSpaceLock, 662d1ab0a7bf7639d1af0c453415f9625110c34f6dMathieu Chartier kReferenceProcessorLock, 67719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDexFileMethodInlinerLock, 68719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDexFileToMethodInlinerMapLock, 69719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMarkSweepMarkStackLock, 70719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kTransactionLogLock, 71719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kInternTableLock, 72719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMonitorPoolLock, 73719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDefaultMutexLevel, 74719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMarkSweepLargeObjectLock, 75719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kPinTableLock, 76719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kLoadLibraryLock, 77719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpObjectRegistryLock, 789e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu kModifyLdtLock, 799e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu kAllocatedThreadIdsLock, 80719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kClassLinkerClassesLock, 81719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kBreakpointLock, 82719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMonitorLock, 83440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier kMonitorListLock, 84719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kThreadListLock, 85719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kBreakpointInvokeLock, 86719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDeoptimizationLock, 87719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kTraceLock, 88719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kProfilerLock, 89719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpEventListLock, 90719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpAttachLock, 91719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpStartLock, 92719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRuntimeShutdownLock, 93719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kHeapBitmapLock, 94719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMutatorLock, 95719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kZygoteCreationLock, 96719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 97719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kLockLevelCount // Must come last. 98719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}; 99719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstd::ostream& operator<<(std::ostream& os, const LockLevel& rhs); 100719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 1012e250c826b3c405d675017efe79e5db3651c9ee6Brian Carlstromconst bool kDebugLocking = kIsDebugBuild; 10225fd14b87cced64a179dee885573113be5e11944Ian Rogers 1031afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Record Log contention information, dumpable via SIGQUIT. 1041afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#ifdef ART_USE_FUTEXES 10508f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Hao// To enable lock contention logging, set this to true. 10608f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Haoconst bool kLogLockContentions = false; 1071afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#else 1081afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Keep this false as lock contention logging is supported only with 1091afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// futex. 1101afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst bool kLogLockContentions = false; 1111afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#endif 112d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersconst size_t kContentionLogSize = 4; 1131afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kContentionLogDataSize = kLogLockContentions ? 1 : 0; 1141afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kAllMutexDataSize = kLogLockContentions ? 1 : 0; 1151afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 11600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Base class for all Mutex implementations 11700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass BaseMutex { 11800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 119bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers const char* GetName() const { 12000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return name_; 12100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 12200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 12300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsMutex() const { return false; } 12400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsReaderWriterMutex() const { return false; } 12500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 12656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const = 0; 12756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 12856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers static void DumpAll(std::ostream& os); 12956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 13000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers protected: 13100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers friend class ConditionVariable; 13200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 13381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers BaseMutex(const char* name, LockLevel level); 13456edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual ~BaseMutex(); 13581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void RegisterAsLocked(Thread* self); 13681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void RegisterAsUnlocked(Thread* self); 13781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void CheckSafeToWait(Thread* self); 13800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 13956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers friend class ScopedContentionRecorder; 14056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 1411afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi void RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t nano_time_blocked); 14256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers void DumpContention(std::ostream& os) const; 14356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 14481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers const LockLevel level_; // Support for lock hierarchy. 145bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers const char* const name_; 1461afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 14756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers // A log entry that records contention but makes no guarantee that either tid will be held live. 14856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers struct ContentionLogEntry { 14956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers ContentionLogEntry() : blocked_tid(0), owner_tid(0) {} 15056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers uint64_t blocked_tid; 15156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers uint64_t owner_tid; 15256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers AtomicInteger count; 15356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers }; 1541afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi struct ContentionLogData { 1551afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi ContentionLogEntry contention_log[kContentionLogSize]; 1561afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // The next entry in the contention log to be updated. Value ranges from 0 to 1571afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // kContentionLogSize - 1. 1581afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi AtomicInteger cur_content_log_entry; 1591afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // Number of times the Mutex has been contended. 1601afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi AtomicInteger contention_count; 1611afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // Sum of time waited by all contenders in ns. 1621afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi volatile uint64_t wait_time; 1631afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi void AddToWaitTime(uint64_t value); 1641afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi ContentionLogData() : wait_time(0) {} 1651afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi }; 1663e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers ContentionLogData contention_log_data_[kContentionLogDataSize]; 1671afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 1681afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi public: 1691afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi bool HasEverContended() const { 1701afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi if (kLogLockContentions) { 1713e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers return contention_log_data_->contention_count.LoadSequentiallyConsistent() > 0; 1721afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi } 1731afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi return false; 1741afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi } 17500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 17600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 17700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain 17800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// exclusive access to what it guards. A Mutex can be in one of two states: 17900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread, 18000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread. 18100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 18200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is: 18300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State | ExclusiveLock | ExclusiveUnlock 18400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ------------------------------------------- 18500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free | Exclusive | error 18600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block* | Free 18700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in 18800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// an error. Being non-reentrant simplifies Waiting on ConditionVariables. 18901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const Mutex& mu); 19000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE Mutex : public BaseMutex { 19100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 19281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false); 19300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~Mutex(); 19400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 19500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsMutex() const { return true; } 19600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 19700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until mutex is free then acquire exclusive access. 19881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION(); 19981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void Lock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); } 20000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 20100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Returns true if acquires exclusive access, false otherwise. 20281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool ExclusiveTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true); 20381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool TryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true) { return ExclusiveTryLock(self); } 20400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 20500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release exclusive access. 20681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION(); 20781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void Unlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); } 20800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 20900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread the exclusive holder of the Mutex. 21081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsExclusiveHeld(const Thread* self) const; 2118daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 21200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert that the Mutex is exclusively held by the current thread. 21381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertExclusiveHeld(const Thread* self) { 2143c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum if (kDebugLocking && (gAborting == 0)) { 21501ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(IsExclusiveHeld(self)) << *this; 21600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 21700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 21881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertHeld(const Thread* self) { AssertExclusiveHeld(self); } 21900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 22000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert that the Mutex is not held by the current thread. 22181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotHeldExclusive(const Thread* self) { 2223c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum if (kDebugLocking && (gAborting == 0)) { 22301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(!IsExclusiveHeld(self)) << *this; 22400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 22500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 22681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotHeld(const Thread* self) { AssertNotHeldExclusive(self); } 22700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 22800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Id associated with exclusive owner. 22900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers uint64_t GetExclusiveOwnerTid() const; 23000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 23100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld. 23200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers unsigned int GetDepth() const { 23300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return recursion_count_; 23400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 23500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 23656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const; 23701ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 23800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 239c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES 240c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // 0 is unheld, 1 is held. 241c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers volatile int32_t state_; 242c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Exclusive owner. 243c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers volatile uint64_t exclusive_owner_; 244c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Number of waiting contenders. 245b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger num_contenders_; 246c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else 24700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_mutex_t mutex_; 248c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif 24900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers const bool recursive_; // Can the lock be recursively held? 25000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers unsigned int recursion_count_; 251f1498437b0d6beb9f4f91980b98cbeb0b5c773ceElliott Hughes friend class ConditionVariable; 2528daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes DISALLOW_COPY_AND_ASSIGN(Mutex); 2538daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 2548daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 25500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex. 25600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader) 25700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a 25800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// condition variable. A ReaderWriterMutex can be in one of three states: 25900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread, 26000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread, 26100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Shared(n) - shared amongst n threads. 26200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 26300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is: 26400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 26500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State | ExclusiveLock | ExclusiveUnlock | SharedLock | SharedUnlock 26600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ---------------------------------------------------------------------------- 26700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free | Exclusive | error | SharedLock(1) | error 26800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block | Free | Block | error 26900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Shared(n) | Block | error | SharedLock(n+1)* | Shared(n-1) or Free 27000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * for large values of n the SharedLock may block. 27101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu); 27200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE ReaderWriterMutex : public BaseMutex { 2738daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes public: 27481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel); 27500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~ReaderWriterMutex(); 27600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 27700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsReaderWriterMutex() const { return true; } 27800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 27900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is free then acquire exclusive access. 28081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION(); 28181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void WriterLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); } 28200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 28300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release exclusive access. 28481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION(); 28581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void WriterUnlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); } 28600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 28700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success 28800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // or false if timeout is reached. 28966aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#if HAVE_TIMED_RWLOCK 290c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns) 29181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers EXCLUSIVE_TRYLOCK_FUNCTION(true); 29266aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#endif 29300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 29400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is shared or free then acquire a share on the access. 2951ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers void SharedLock(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE; 29681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ReaderLock(Thread* self) SHARED_LOCK_FUNCTION() { SharedLock(self); } 29700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 29800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Try to acquire share of ReaderWriterMutex. 29981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool SharedTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true); 30000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 30100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release a share of the access. 3021ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers void SharedUnlock(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE; 30381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ReaderUnlock(Thread* self) UNLOCK_FUNCTION() { SharedUnlock(self); } 30400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 30500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread the exclusive holder of the ReaderWriterMutex. 30681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsExclusiveHeld(const Thread* self) const; 30700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 30800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread has exclusive access to the ReaderWriterMutex. 30981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertExclusiveHeld(const Thread* self) { 310702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz if (kDebugLocking && (gAborting == 0)) { 31101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(IsExclusiveHeld(self)) << *this; 31200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 3138daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 31481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertWriterHeld(const Thread* self) { AssertExclusiveHeld(self); } 3158daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 31600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex. 31781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotExclusiveHeld(const Thread* self) { 318702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz if (kDebugLocking && (gAborting == 0)) { 319e3359f7ad7671c5816f17145ca3a01516512e8d6Ian Rogers CHECK(!IsExclusiveHeld(self)) << *this; 32000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 32100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 32281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotWriterHeld(const Thread* self) { AssertNotExclusiveHeld(self); } 32300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 32400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread a shared holder of the ReaderWriterMutex. 32581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsSharedHeld(const Thread* self) const; 32600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 32700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread has shared access to the ReaderWriterMutex. 32881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertSharedHeld(const Thread* self) { 329702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz if (kDebugLocking && (gAborting == 0)) { 33023055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers // TODO: we can only assert this well when self != NULL. 33123055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers CHECK(IsSharedHeld(self) || self == NULL) << *this; 33200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 3338daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 33481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertReaderHeld(const Thread* self) { AssertSharedHeld(self); } 3358daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 33600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive 33700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // mode. 33881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotHeld(const Thread* self) { 3393c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum if (kDebugLocking && (gAborting == 0)) { 34001ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(!IsSharedHeld(self)) << *this; 34100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 34200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 34300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 34400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Id associated with exclusive owner. 34500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers uint64_t GetExclusiveOwnerTid() const; 34681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 34756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const; 34801ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 3498daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes private: 35081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#if ART_USE_FUTEXES 35181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers // -1 implies held exclusive, +ve shared held by state_ many owners. 35281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers volatile int32_t state_; 35381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers // Exclusive owner. 35481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers volatile uint64_t exclusive_owner_; 35581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers // Pending readers. 35681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers volatile int32_t num_pending_readers_; 35781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers // Pending writers. 358b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger num_pending_writers_; 35981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#else 36000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_rwlock_t rwlock_; 36181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#endif 36200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex); 3638daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 3648daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 36500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually 36600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// (Signal) or all at once (Broadcast). 3675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesclass ConditionVariable { 3685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes public: 36923055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers explicit ConditionVariable(const char* name, Mutex& mutex); 3705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ~ConditionVariable(); 3715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 372c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Broadcast(Thread* self); 373c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Signal(Thread* self); 374c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their 375c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // pointer copy, thereby defeating annotalysis. 376c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS; 377c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void TimedWait(Thread* self, int64_t ms, int32_t ns) NO_THREAD_SAFETY_ANALYSIS; 3781d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held 3791d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // when waiting. 3801d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // TODO: remove this. 3811d54e73444e017d3a65234e0f193846f3e27472bIan Rogers void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS; 3825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes private: 38423055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers const char* const name_; 385c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // The Mutex being used by waiters. It is an error to mix condition variables between different 386c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Mutexes. 387c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers Mutex& guard_; 388c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES 389c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // A counter that is modified by signals and broadcasts. This ensures that when a waiter gives up 390d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_ 391d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait 392d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // without guard_ held. 393b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger sequence_; 394c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Number of threads that have come into to wait, not the length of the waiters on the futex as 3955bd97c483c1de1eb97afe76123b1b9ab53095edfIan Rogers // waiters may have been requeued onto guard_. Guarded by guard_. 396c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers volatile int32_t num_waiters_; 397c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else 398c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers pthread_cond_t cond_; 399c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif 4005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DISALLOW_COPY_AND_ASSIGN(ConditionVariable); 4015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}; 4025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 40300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it 40400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// upon destruction. 40500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE MutexLock { 40600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 40781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit MutexLock(Thread* self, Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : self_(self), mu_(mu) { 40881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveLock(self_); 40981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 41081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 41100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~MutexLock() UNLOCK_FUNCTION() { 41281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveUnlock(self_); 41300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 41400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 41500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 41681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Thread* const self_; 41700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Mutex& mu_; 41800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(MutexLock); 41900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 42000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)". 42100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_declaration_missing_variable_name) 42200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 42300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon 42400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction. 42500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE ReaderMutexLock { 42600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 42781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : 42881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers self_(self), mu_(mu) { 42981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.SharedLock(self_); 43081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 43181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 43200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~ReaderMutexLock() UNLOCK_FUNCTION() { 43381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.SharedUnlock(self_); 43400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 43500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 43600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 43781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Thread* const self_; 43800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ReaderWriterMutex& mu_; 43900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock); 44000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 44100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "ReaderMutexLock (lock);" instead of 44200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "ReaderMutexLock mu(lock)". 44300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define ReaderMutexLock(x) COMPILE_ASSERT(0, reader_mutex_lock_declaration_missing_variable_name) 44400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 44500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon 44600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction. 44700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE WriterMutexLock { 44800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 44981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : 45081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers self_(self), mu_(mu) { 45181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveLock(self_); 45281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 45381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 45400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~WriterMutexLock() UNLOCK_FUNCTION() { 45581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveUnlock(self_); 45600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 45700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 45800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 45950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers Thread* const self_; 46000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ReaderWriterMutex& mu_; 46100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(WriterMutexLock); 46200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 46300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of 46400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "WriterMutexLock mu(lock)". 46500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define WriterMutexLock(x) COMPILE_ASSERT(0, writer_mutex_lock_declaration_missing_variable_name) 46600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 467719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Global mutexes corresponding to the levels above. 468719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersclass Locks { 469719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers public: 470719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static void Init(); 471719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 472719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // The mutator_lock_ is used to allow mutators to execute in a shared (reader) mode or to block 473719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // mutators by having an exclusive (writer) owner. In normal execution each mutator thread holds 474719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // a share on the mutator_lock_. The garbage collector may also execute with shared access but 475719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // at times requires exclusive access to the heap (not to be confused with the heap meta-data 476719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // guarded by the heap_lock_ below). When the garbage collector requires exclusive access it asks 477719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // the mutators to suspend themselves which also involves usage of the thread_suspend_count_lock_ 478719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // to cover weaknesses in using ReaderWriterMutexes with ConditionVariables. We use a condition 479719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // variable to wait upon in the suspension logic as releasing and then re-acquiring a share on 480719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // the mutator lock doesn't necessarily allow the exclusive user (e.g the garbage collector) 481719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // chance to acquire the lock. 482719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // 483719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Thread suspension: 484719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Shared users | Exclusive user 485719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // (holding mutator lock and in kRunnable state) | .. running .. 486719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | Request thread suspension by: 487719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - acquiring thread_suspend_count_lock_ 488719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - incrementing Thread::suspend_count_ on 489719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | all mutator threads 490719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - releasing thread_suspend_count_lock_ 491719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | Block trying to acquire exclusive mutator lock 492719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Poll Thread::suspend_count_ and enter full | .. blocked .. 493719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // suspend code. | .. blocked .. 494719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Change state to kSuspended | .. blocked .. 495719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // x: Release share on mutator_lock_ | Carry out exclusive access 496719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Acquire thread_suspend_count_lock_ | .. exclusive .. 497719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // while Thread::suspend_count_ > 0 | .. exclusive .. 498719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - wait on Thread::resume_cond_ | .. exclusive .. 499719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // (releases thread_suspend_count_lock_) | .. exclusive .. 500719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | Release mutator_lock_ 501719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | Request thread resumption by: 502719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - acquiring thread_suspend_count_lock_ 503719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - decrementing Thread::suspend_count_ on 504719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | all mutator threads 505719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - notifying on Thread::resume_cond_ 506719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - re-acquire thread_suspend_count_lock_ | - releasing thread_suspend_count_lock_ 507719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Release thread_suspend_count_lock_ | .. running .. 508719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Acquire share on mutator_lock_ | .. running .. 509719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - This could block but the thread still | .. running .. 510719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // has a state of kSuspended and so this | .. running .. 511719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // isn't an issue. | .. running .. 512719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Acquire thread_suspend_count_lock_ | .. running .. 513719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - we poll here as we're transitioning into | .. running .. 514719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // kRunnable and an individual thread suspend | .. running .. 515719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // request (e.g for debugging) won't try | .. running .. 516719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // to acquire the mutator lock (which would | .. running .. 517719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // block as we hold the mutator lock). This | .. running .. 518719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // poll ensures that if the suspender thought | .. running .. 519719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // we were suspended by incrementing our | .. running .. 520719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Thread::suspend_count_ and then reading | .. running .. 521719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // our state we go back to waiting on | .. running .. 522719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Thread::resume_cond_. | .. running .. 523719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // can_go_runnable = Thread::suspend_count_ == 0 | .. running .. 524719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Release thread_suspend_count_lock_ | .. running .. 525719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // if can_go_runnable | .. running .. 526719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Change state to kRunnable | .. running .. 527719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // else | .. running .. 528719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Goto x | .. running .. 529719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | .. running .. 530719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static ReaderWriterMutex* mutator_lock_; 531719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 532719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap. 533719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_); 534719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 535719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards shutdown of the runtime. 536719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_); 537719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 5389e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards background profiler global state. 5399e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* profiler_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_); 5409e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 5419e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards trace (ie traceview) requests. 5429e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* trace_lock_ ACQUIRED_AFTER(profiler_lock_); 5439e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 544719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads 545719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // attaching and detaching. 5469e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* thread_list_lock_ ACQUIRED_AFTER(trace_lock_); 547719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 548719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards breakpoints. 549719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* breakpoint_lock_ ACQUIRED_AFTER(thread_list_lock_); 550719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 551719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards lists of classes within the class linker. 5529e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(breakpoint_lock_); 553719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 554719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code 555719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // doesn't try to hold a higher level Mutex. 556719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(Locks::classlinker_classes_lock_) 557719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 5589e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guard the allocation/deallocation of thread ids. 5599e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* allocated_thread_ids_lock_ ACQUIRED_AFTER(classlinker_classes_lock_); 5609e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 5619e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards modification of the LDT on x86. 5629e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* modify_ldt_lock_ ACQUIRED_AFTER(allocated_thread_ids_lock_); 5639e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 564719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards intern table. 5659e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* intern_table_lock_ ACQUIRED_AFTER(modify_ldt_lock_); 566719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 567719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Have an exclusive aborting thread. 568719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* abort_lock_ ACQUIRED_AFTER(classlinker_classes_lock_); 569719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 570719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Allow mutual exclusion when manipulating Thread::suspend_count_. 571719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // TODO: Does the trade-off of a per-thread lock make sense? 572719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_); 573719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 574719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // One unexpected signal at a time lock. 575719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_); 576719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 5773eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi // Guards the maps in mem_map. 5783eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi static Mutex* mem_maps_lock_ ACQUIRED_AFTER(unexpected_signal_lock_); 5793eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi 580719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Have an exclusive logging thread. 581719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_); 582719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}; 583719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 5848daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 5858daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 586fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif // ART_RUNTIME_BASE_MUTEX_H_ 587