mutex.h revision 9e36931cc79ca665908db9575126881d1cfdea5a
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, 57719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kUnexpectedSignalLock, 58719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kThreadSuspendCountLock, 59719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kAbortLock, 60719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpSocketLock, 61719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRosAllocGlobalLock, 62719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRosAllocBracketLock, 63719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRosAllocBulkFreeLock, 64719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kAllocSpaceLock, 652d1ab0a7bf7639d1af0c453415f9625110c34f6dMathieu Chartier kReferenceProcessorLock, 66719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDexFileMethodInlinerLock, 67719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDexFileToMethodInlinerMapLock, 68719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMarkSweepMarkStackLock, 69719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kTransactionLogLock, 70719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kInternTableLock, 71719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMonitorPoolLock, 72719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDefaultMutexLevel, 73719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMarkSweepLargeObjectLock, 74719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kPinTableLock, 75719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kLoadLibraryLock, 76719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpObjectRegistryLock, 779e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu kModifyLdtLock, 789e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu kAllocatedThreadIdsLock, 79719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kClassLinkerClassesLock, 80719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kBreakpointLock, 81719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMonitorLock, 82440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier kMonitorListLock, 83719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kThreadListLock, 84719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kBreakpointInvokeLock, 85719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDeoptimizationLock, 86719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kTraceLock, 87719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kProfilerLock, 88719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpEventListLock, 89719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpAttachLock, 90719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpStartLock, 91719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRuntimeShutdownLock, 92719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kHeapBitmapLock, 93719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMutatorLock, 94719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kZygoteCreationLock, 95719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 96719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kLockLevelCount // Must come last. 97719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}; 98719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstd::ostream& operator<<(std::ostream& os, const LockLevel& rhs); 99719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 1002e250c826b3c405d675017efe79e5db3651c9ee6Brian Carlstromconst bool kDebugLocking = kIsDebugBuild; 10125fd14b87cced64a179dee885573113be5e11944Ian Rogers 1021afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Record Log contention information, dumpable via SIGQUIT. 1031afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#ifdef ART_USE_FUTEXES 10408f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Hao// To enable lock contention logging, set this to true. 10508f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Haoconst bool kLogLockContentions = false; 1061afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#else 1071afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Keep this false as lock contention logging is supported only with 1081afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// futex. 1091afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst bool kLogLockContentions = false; 1101afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#endif 111d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersconst size_t kContentionLogSize = 4; 1121afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kContentionLogDataSize = kLogLockContentions ? 1 : 0; 1131afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kAllMutexDataSize = kLogLockContentions ? 1 : 0; 1141afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 11500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Base class for all Mutex implementations 11600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass BaseMutex { 11700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 118bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers const char* GetName() const { 11900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return name_; 12000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 12100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 12200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsMutex() const { return false; } 12300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsReaderWriterMutex() const { return false; } 12400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 12556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const = 0; 12656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 12756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers static void DumpAll(std::ostream& os); 12856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 12900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers protected: 13000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers friend class ConditionVariable; 13100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 13281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers BaseMutex(const char* name, LockLevel level); 13356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual ~BaseMutex(); 13481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void RegisterAsLocked(Thread* self); 13581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void RegisterAsUnlocked(Thread* self); 13681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void CheckSafeToWait(Thread* self); 13700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 13856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers friend class ScopedContentionRecorder; 13956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 1401afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi void RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t nano_time_blocked); 14156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers void DumpContention(std::ostream& os) const; 14256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 14381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers const LockLevel level_; // Support for lock hierarchy. 144bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers const char* const name_; 1451afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 14656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers // A log entry that records contention but makes no guarantee that either tid will be held live. 14756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers struct ContentionLogEntry { 14856edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers ContentionLogEntry() : blocked_tid(0), owner_tid(0) {} 14956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers uint64_t blocked_tid; 15056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers uint64_t owner_tid; 15156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers AtomicInteger count; 15256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers }; 1531afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi struct ContentionLogData { 1541afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi ContentionLogEntry contention_log[kContentionLogSize]; 1551afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // The next entry in the contention log to be updated. Value ranges from 0 to 1561afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // kContentionLogSize - 1. 1571afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi AtomicInteger cur_content_log_entry; 1581afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // Number of times the Mutex has been contended. 1591afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi AtomicInteger contention_count; 1601afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // Sum of time waited by all contenders in ns. 1611afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi volatile uint64_t wait_time; 1621afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi void AddToWaitTime(uint64_t value); 1631afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi ContentionLogData() : wait_time(0) {} 1641afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi }; 1653e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers ContentionLogData contention_log_data_[kContentionLogDataSize]; 1661afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 1671afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi public: 1681afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi bool HasEverContended() const { 1691afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi if (kLogLockContentions) { 1703e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers return contention_log_data_->contention_count.LoadSequentiallyConsistent() > 0; 1711afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi } 1721afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi return false; 1731afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi } 17400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 17500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 17600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain 17700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// exclusive access to what it guards. A Mutex can be in one of two states: 17800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread, 17900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread. 18000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 18100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is: 18200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State | ExclusiveLock | ExclusiveUnlock 18300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ------------------------------------------- 18400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free | Exclusive | error 18500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block* | Free 18600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in 18700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// an error. Being non-reentrant simplifies Waiting on ConditionVariables. 18801ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const Mutex& mu); 18900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE Mutex : public BaseMutex { 19000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 19181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false); 19200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~Mutex(); 19300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 19400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsMutex() const { return true; } 19500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 19600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until mutex is free then acquire exclusive access. 19781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION(); 19881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void Lock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); } 19900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 20000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Returns true if acquires exclusive access, false otherwise. 20181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool ExclusiveTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true); 20281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool TryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true) { return ExclusiveTryLock(self); } 20300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 20400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release exclusive access. 20581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION(); 20681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void Unlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); } 20700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 20800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread the exclusive holder of the Mutex. 20981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsExclusiveHeld(const Thread* self) const; 2108daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 21100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert that the Mutex is exclusively held by the current thread. 21281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertExclusiveHeld(const Thread* self) { 2133c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum if (kDebugLocking && (gAborting == 0)) { 21401ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(IsExclusiveHeld(self)) << *this; 21500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 21600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 21781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertHeld(const Thread* self) { AssertExclusiveHeld(self); } 21800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 21900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert that the Mutex is not held by the current thread. 22081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotHeldExclusive(const Thread* self) { 2213c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum if (kDebugLocking && (gAborting == 0)) { 22201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(!IsExclusiveHeld(self)) << *this; 22300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 22400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 22581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotHeld(const Thread* self) { AssertNotHeldExclusive(self); } 22600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 22700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Id associated with exclusive owner. 22800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers uint64_t GetExclusiveOwnerTid() const; 22900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 23000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld. 23100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers unsigned int GetDepth() const { 23200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return recursion_count_; 23300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 23400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 23556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const; 23601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 23700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 238c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES 239c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // 0 is unheld, 1 is held. 240c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers volatile int32_t state_; 241c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Exclusive owner. 242c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers volatile uint64_t exclusive_owner_; 243c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Number of waiting contenders. 244b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger num_contenders_; 245c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else 24600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_mutex_t mutex_; 247c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif 24800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers const bool recursive_; // Can the lock be recursively held? 24900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers unsigned int recursion_count_; 250f1498437b0d6beb9f4f91980b98cbeb0b5c773ceElliott Hughes friend class ConditionVariable; 2518daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes DISALLOW_COPY_AND_ASSIGN(Mutex); 2528daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 2538daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 25400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex. 25500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader) 25600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a 25700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// condition variable. A ReaderWriterMutex can be in one of three states: 25800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread, 25900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread, 26000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Shared(n) - shared amongst n threads. 26100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 26200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is: 26300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 26400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State | ExclusiveLock | ExclusiveUnlock | SharedLock | SharedUnlock 26500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ---------------------------------------------------------------------------- 26600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free | Exclusive | error | SharedLock(1) | error 26700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block | Free | Block | error 26800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Shared(n) | Block | error | SharedLock(n+1)* | Shared(n-1) or Free 26900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * for large values of n the SharedLock may block. 27001ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu); 27100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE ReaderWriterMutex : public BaseMutex { 2728daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes public: 27381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel); 27400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~ReaderWriterMutex(); 27500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 27600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsReaderWriterMutex() const { return true; } 27700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 27800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is free then acquire exclusive access. 27981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION(); 28081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void WriterLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); } 28100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 28200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release exclusive access. 28381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION(); 28481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void WriterUnlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); } 28500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 28600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success 28700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // or false if timeout is reached. 28866aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#if HAVE_TIMED_RWLOCK 289c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns) 29081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers EXCLUSIVE_TRYLOCK_FUNCTION(true); 29166aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#endif 29200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 29300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is shared or free then acquire a share on the access. 2941ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers void SharedLock(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE; 29581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ReaderLock(Thread* self) SHARED_LOCK_FUNCTION() { SharedLock(self); } 29600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 29700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Try to acquire share of ReaderWriterMutex. 29881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool SharedTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true); 29900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 30000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release a share of the access. 3011ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers void SharedUnlock(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE; 30281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ReaderUnlock(Thread* self) UNLOCK_FUNCTION() { SharedUnlock(self); } 30300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 30400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread the exclusive holder of the ReaderWriterMutex. 30581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsExclusiveHeld(const Thread* self) const; 30600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 30700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread has exclusive access to the ReaderWriterMutex. 30881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertExclusiveHeld(const Thread* self) { 309702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz if (kDebugLocking && (gAborting == 0)) { 31001ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(IsExclusiveHeld(self)) << *this; 31100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 3128daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 31381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertWriterHeld(const Thread* self) { AssertExclusiveHeld(self); } 3148daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 31500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex. 31681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotExclusiveHeld(const Thread* self) { 317702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz if (kDebugLocking && (gAborting == 0)) { 318e3359f7ad7671c5816f17145ca3a01516512e8d6Ian Rogers CHECK(!IsExclusiveHeld(self)) << *this; 31900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 32000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 32181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotWriterHeld(const Thread* self) { AssertNotExclusiveHeld(self); } 32200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 32300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread a shared holder of the ReaderWriterMutex. 32481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsSharedHeld(const Thread* self) const; 32500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 32600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread has shared access to the ReaderWriterMutex. 32781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertSharedHeld(const Thread* self) { 328702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz if (kDebugLocking && (gAborting == 0)) { 32923055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers // TODO: we can only assert this well when self != NULL. 33023055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers CHECK(IsSharedHeld(self) || self == NULL) << *this; 33100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 3328daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 33381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertReaderHeld(const Thread* self) { AssertSharedHeld(self); } 3348daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 33500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive 33600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // mode. 33781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotHeld(const Thread* self) { 3383c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum if (kDebugLocking && (gAborting == 0)) { 33901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(!IsSharedHeld(self)) << *this; 34000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 34100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 34200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 34300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Id associated with exclusive owner. 34400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers uint64_t GetExclusiveOwnerTid() const; 34581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 34656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const; 34701ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 3488daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes private: 34981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#if ART_USE_FUTEXES 35081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers // -1 implies held exclusive, +ve shared held by state_ many owners. 35181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers volatile int32_t state_; 35281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers // Exclusive owner. 35381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers volatile uint64_t exclusive_owner_; 35481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers // Pending readers. 35581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers volatile int32_t num_pending_readers_; 35681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers // Pending writers. 357b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger num_pending_writers_; 35881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#else 35900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_rwlock_t rwlock_; 36081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#endif 36100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex); 3628daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 3638daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 36400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually 36500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// (Signal) or all at once (Broadcast). 3665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesclass ConditionVariable { 3675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes public: 36823055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers explicit ConditionVariable(const char* name, Mutex& mutex); 3695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ~ConditionVariable(); 3705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 371c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Broadcast(Thread* self); 372c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Signal(Thread* self); 373c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their 374c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // pointer copy, thereby defeating annotalysis. 375c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS; 376c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void TimedWait(Thread* self, int64_t ms, int32_t ns) NO_THREAD_SAFETY_ANALYSIS; 3771d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held 3781d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // when waiting. 3791d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // TODO: remove this. 3801d54e73444e017d3a65234e0f193846f3e27472bIan Rogers void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS; 3815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes private: 38323055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers const char* const name_; 384c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // The Mutex being used by waiters. It is an error to mix condition variables between different 385c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Mutexes. 386c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers Mutex& guard_; 387c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES 388c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // A counter that is modified by signals and broadcasts. This ensures that when a waiter gives up 389d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_ 390d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait 391d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // without guard_ held. 392b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger sequence_; 393c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Number of threads that have come into to wait, not the length of the waiters on the futex as 3945bd97c483c1de1eb97afe76123b1b9ab53095edfIan Rogers // waiters may have been requeued onto guard_. Guarded by guard_. 395c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers volatile int32_t num_waiters_; 396c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else 397c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers pthread_cond_t cond_; 398c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif 3995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DISALLOW_COPY_AND_ASSIGN(ConditionVariable); 4005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}; 4015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 40200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it 40300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// upon destruction. 40400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE MutexLock { 40500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 40681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit MutexLock(Thread* self, Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : self_(self), mu_(mu) { 40781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveLock(self_); 40881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 40981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 41000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~MutexLock() UNLOCK_FUNCTION() { 41181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveUnlock(self_); 41200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 41300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 41400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 41581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Thread* const self_; 41600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Mutex& mu_; 41700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(MutexLock); 41800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 41900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)". 42000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_declaration_missing_variable_name) 42100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 42200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon 42300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction. 42400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE ReaderMutexLock { 42500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 42681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : 42781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers self_(self), mu_(mu) { 42881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.SharedLock(self_); 42981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 43081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 43100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~ReaderMutexLock() UNLOCK_FUNCTION() { 43281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.SharedUnlock(self_); 43300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 43400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 43500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 43681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Thread* const self_; 43700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ReaderWriterMutex& mu_; 43800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock); 43900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 44000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "ReaderMutexLock (lock);" instead of 44100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "ReaderMutexLock mu(lock)". 44200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define ReaderMutexLock(x) COMPILE_ASSERT(0, reader_mutex_lock_declaration_missing_variable_name) 44300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 44400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon 44500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction. 44600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE WriterMutexLock { 44700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 44881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : 44981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers self_(self), mu_(mu) { 45081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveLock(self_); 45181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 45281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 45300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~WriterMutexLock() UNLOCK_FUNCTION() { 45481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveUnlock(self_); 45500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 45600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 45700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 45850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers Thread* const self_; 45900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ReaderWriterMutex& mu_; 46000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(WriterMutexLock); 46100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 46200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of 46300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "WriterMutexLock mu(lock)". 46400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define WriterMutexLock(x) COMPILE_ASSERT(0, writer_mutex_lock_declaration_missing_variable_name) 46500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 466719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Global mutexes corresponding to the levels above. 467719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersclass Locks { 468719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers public: 469719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static void Init(); 470719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 471719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // The mutator_lock_ is used to allow mutators to execute in a shared (reader) mode or to block 472719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // mutators by having an exclusive (writer) owner. In normal execution each mutator thread holds 473719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // a share on the mutator_lock_. The garbage collector may also execute with shared access but 474719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // at times requires exclusive access to the heap (not to be confused with the heap meta-data 475719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // guarded by the heap_lock_ below). When the garbage collector requires exclusive access it asks 476719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // the mutators to suspend themselves which also involves usage of the thread_suspend_count_lock_ 477719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // to cover weaknesses in using ReaderWriterMutexes with ConditionVariables. We use a condition 478719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // variable to wait upon in the suspension logic as releasing and then re-acquiring a share on 479719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // the mutator lock doesn't necessarily allow the exclusive user (e.g the garbage collector) 480719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // chance to acquire the lock. 481719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // 482719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Thread suspension: 483719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Shared users | Exclusive user 484719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // (holding mutator lock and in kRunnable state) | .. running .. 485719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | Request thread suspension by: 486719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - acquiring thread_suspend_count_lock_ 487719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - incrementing Thread::suspend_count_ on 488719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | all mutator threads 489719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - releasing thread_suspend_count_lock_ 490719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | Block trying to acquire exclusive mutator lock 491719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Poll Thread::suspend_count_ and enter full | .. blocked .. 492719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // suspend code. | .. blocked .. 493719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Change state to kSuspended | .. blocked .. 494719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // x: Release share on mutator_lock_ | Carry out exclusive access 495719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Acquire thread_suspend_count_lock_ | .. exclusive .. 496719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // while Thread::suspend_count_ > 0 | .. exclusive .. 497719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - wait on Thread::resume_cond_ | .. exclusive .. 498719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // (releases thread_suspend_count_lock_) | .. exclusive .. 499719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | Release mutator_lock_ 500719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | Request thread resumption by: 501719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - acquiring thread_suspend_count_lock_ 502719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - decrementing Thread::suspend_count_ on 503719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | all mutator threads 504719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - notifying on Thread::resume_cond_ 505719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - re-acquire thread_suspend_count_lock_ | - releasing thread_suspend_count_lock_ 506719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Release thread_suspend_count_lock_ | .. running .. 507719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Acquire share on mutator_lock_ | .. running .. 508719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - This could block but the thread still | .. running .. 509719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // has a state of kSuspended and so this | .. running .. 510719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // isn't an issue. | .. running .. 511719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Acquire thread_suspend_count_lock_ | .. running .. 512719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - we poll here as we're transitioning into | .. running .. 513719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // kRunnable and an individual thread suspend | .. running .. 514719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // request (e.g for debugging) won't try | .. running .. 515719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // to acquire the mutator lock (which would | .. running .. 516719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // block as we hold the mutator lock). This | .. running .. 517719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // poll ensures that if the suspender thought | .. running .. 518719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // we were suspended by incrementing our | .. running .. 519719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Thread::suspend_count_ and then reading | .. running .. 520719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // our state we go back to waiting on | .. running .. 521719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Thread::resume_cond_. | .. running .. 522719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // can_go_runnable = Thread::suspend_count_ == 0 | .. running .. 523719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Release thread_suspend_count_lock_ | .. running .. 524719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // if can_go_runnable | .. running .. 525719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Change state to kRunnable | .. running .. 526719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // else | .. running .. 527719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Goto x | .. running .. 528719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | .. running .. 529719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static ReaderWriterMutex* mutator_lock_; 530719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 531719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap. 532719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_); 533719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 534719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards shutdown of the runtime. 535719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_); 536719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 5379e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards background profiler global state. 5389e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* profiler_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_); 5399e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 5409e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards trace (ie traceview) requests. 5419e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* trace_lock_ ACQUIRED_AFTER(profiler_lock_); 5429e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 543719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads 544719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // attaching and detaching. 5459e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* thread_list_lock_ ACQUIRED_AFTER(trace_lock_); 546719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 547719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards breakpoints. 548719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* breakpoint_lock_ ACQUIRED_AFTER(thread_list_lock_); 549719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 550719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards lists of classes within the class linker. 5519e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(breakpoint_lock_); 552719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 553719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code 554719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // doesn't try to hold a higher level Mutex. 555719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(Locks::classlinker_classes_lock_) 556719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 5579e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guard the allocation/deallocation of thread ids. 5589e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* allocated_thread_ids_lock_ ACQUIRED_AFTER(classlinker_classes_lock_); 5599e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 5609e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards modification of the LDT on x86. 5619e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* modify_ldt_lock_ ACQUIRED_AFTER(allocated_thread_ids_lock_); 5629e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 563719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards intern table. 5649e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* intern_table_lock_ ACQUIRED_AFTER(modify_ldt_lock_); 565719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 566719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Have an exclusive aborting thread. 567719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* abort_lock_ ACQUIRED_AFTER(classlinker_classes_lock_); 568719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 569719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Allow mutual exclusion when manipulating Thread::suspend_count_. 570719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // TODO: Does the trade-off of a per-thread lock make sense? 571719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_); 572719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 573719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // One unexpected signal at a time lock. 574719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_); 575719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 576719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Have an exclusive logging thread. 577719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_); 578719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}; 579719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 5808daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 5818daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 582fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif // ART_RUNTIME_BASE_MUTEX_H_ 583