mutex.h revision ed2be1725fb79075892b1a9103487c9d9a95b350
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, 587de77dd4f2d3cbb0615ee001589eb99ae82c3dccRaghu Gandham kSwapMutexesLock, 59719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kUnexpectedSignalLock, 60719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kThreadSuspendCountLock, 61719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kAbortLock, 62719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpSocketLock, 63719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRosAllocGlobalLock, 64719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRosAllocBracketLock, 65719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRosAllocBulkFreeLock, 66719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kAllocSpaceLock, 672d1ab0a7bf7639d1af0c453415f9625110c34f6dMathieu Chartier kReferenceProcessorLock, 68719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDexFileMethodInlinerLock, 69719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDexFileToMethodInlinerMapLock, 70719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMarkSweepMarkStackLock, 71719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kTransactionLogLock, 72719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kInternTableLock, 733f5838d7d0b9fc63db0ccc35c2ea05ed29264986Vladimir Marko kOatFileSecondaryLookupLock, 74719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDefaultMutexLevel, 75719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMarkSweepLargeObjectLock, 76719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kPinTableLock, 77719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpObjectRegistryLock, 789e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu kModifyLdtLock, 799e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu kAllocatedThreadIdsLock, 8074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe kMonitorPoolLock, 81719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kClassLinkerClassesLock, 82719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kBreakpointLock, 83719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMonitorLock, 84440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier kMonitorListLock, 8568d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers kJniLoadLibraryLock, 86719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kThreadListLock, 87719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kBreakpointInvokeLock, 88719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kDeoptimizationLock, 89719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kTraceLock, 90719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kProfilerLock, 91719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpEventListLock, 92719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpAttachLock, 93719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kJdwpStartLock, 94719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kRuntimeShutdownLock, 95719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kHeapBitmapLock, 96719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kMutatorLock, 97f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers kThreadListSuspendThreadLock, 98719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kZygoteCreationLock, 99719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 100719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers kLockLevelCount // Must come last. 101719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}; 102719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstd::ostream& operator<<(std::ostream& os, const LockLevel& rhs); 103719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 1042e250c826b3c405d675017efe79e5db3651c9ee6Brian Carlstromconst bool kDebugLocking = kIsDebugBuild; 10525fd14b87cced64a179dee885573113be5e11944Ian Rogers 1061afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Record Log contention information, dumpable via SIGQUIT. 1071afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#ifdef ART_USE_FUTEXES 10808f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Hao// To enable lock contention logging, set this to true. 10908f2e7b59fab9df108d3d91e6eeb4bbccbb325d1Jeff Haoconst bool kLogLockContentions = false; 1101afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#else 1111afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// Keep this false as lock contention logging is supported only with 1121afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi// futex. 1131afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst bool kLogLockContentions = false; 1141afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi#endif 115d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersconst size_t kContentionLogSize = 4; 1161afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kContentionLogDataSize = kLogLockContentions ? 1 : 0; 1171afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchiconst size_t kAllMutexDataSize = kLogLockContentions ? 1 : 0; 1181afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 11900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Base class for all Mutex implementations 12000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass BaseMutex { 12100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 122bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers const char* GetName() const { 12300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return name_; 12400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 12500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 12600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsMutex() const { return false; } 12700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsReaderWriterMutex() const { return false; } 12800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 12956edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const = 0; 13056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 13156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers static void DumpAll(std::ostream& os); 13256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 13300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers protected: 13400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers friend class ConditionVariable; 13500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 13681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers BaseMutex(const char* name, LockLevel level); 13756edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual ~BaseMutex(); 13881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void RegisterAsLocked(Thread* self); 13981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void RegisterAsUnlocked(Thread* self); 14081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void CheckSafeToWait(Thread* self); 14100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 14256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers friend class ScopedContentionRecorder; 14356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 1441afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi void RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t nano_time_blocked); 14556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers void DumpContention(std::ostream& os) const; 14656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers 14781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers const LockLevel level_; // Support for lock hierarchy. 148bab74963db2484ea5f10a82ea26e8a99722bfefeIan Rogers const char* const name_; 1491afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 15056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers // A log entry that records contention but makes no guarantee that either tid will be held live. 15156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers struct ContentionLogEntry { 15256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers ContentionLogEntry() : blocked_tid(0), owner_tid(0) {} 15356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers uint64_t blocked_tid; 15456edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers uint64_t owner_tid; 15556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers AtomicInteger count; 15656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers }; 1571afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi struct ContentionLogData { 1581afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi ContentionLogEntry contention_log[kContentionLogSize]; 1591afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // The next entry in the contention log to be updated. Value ranges from 0 to 1601afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // kContentionLogSize - 1. 1611afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi AtomicInteger cur_content_log_entry; 1621afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // Number of times the Mutex has been contended. 1631afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi AtomicInteger contention_count; 1641afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi // Sum of time waited by all contenders in ns. 16537f3c968ecd04e77802fe17bb82dabc07de21ca1Ian Rogers Atomic<uint64_t> wait_time; 1661afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi void AddToWaitTime(uint64_t value); 1671afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi ContentionLogData() : wait_time(0) {} 1681afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi }; 1693e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers ContentionLogData contention_log_data_[kContentionLogDataSize]; 1701afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi 1711afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi public: 1721afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi bool HasEverContended() const { 1731afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi if (kLogLockContentions) { 1743e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers return contention_log_data_->contention_count.LoadSequentiallyConsistent() > 0; 1751afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi } 1761afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi return false; 1771afde13b36cc1d67528104c2b1395495f669cd3fHiroshi Yamauchi } 17800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 17900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 18000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain 18100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// exclusive access to what it guards. A Mutex can be in one of two states: 18200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread, 18300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread. 18400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 18500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is: 18600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State | ExclusiveLock | ExclusiveUnlock 18700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ------------------------------------------- 18800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free | Exclusive | error 18900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block* | Free 19000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in 19100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// an error. Being non-reentrant simplifies Waiting on ConditionVariables. 19201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const Mutex& mu); 19300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE Mutex : public BaseMutex { 19400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 19581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false); 19600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~Mutex(); 19700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 19800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsMutex() const { return true; } 19900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 20000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until mutex is free then acquire exclusive access. 20181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION(); 20281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void Lock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); } 20300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 20400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Returns true if acquires exclusive access, false otherwise. 20581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool ExclusiveTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true); 20681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool TryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true) { return ExclusiveTryLock(self); } 20700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 20800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release exclusive access. 20981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION(); 21081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void Unlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); } 21100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 21200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread the exclusive holder of the Mutex. 21381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsExclusiveHeld(const Thread* self) const; 2148daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 21500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert that the Mutex is exclusively held by the current thread. 21681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertExclusiveHeld(const Thread* self) { 2173c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum if (kDebugLocking && (gAborting == 0)) { 21801ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(IsExclusiveHeld(self)) << *this; 21900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 22000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 22181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertHeld(const Thread* self) { AssertExclusiveHeld(self); } 22200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 22300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert that the Mutex is not held by the current thread. 22481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotHeldExclusive(const Thread* self) { 2253c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum if (kDebugLocking && (gAborting == 0)) { 22601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(!IsExclusiveHeld(self)) << *this; 22700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 22800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 22981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotHeld(const Thread* self) { AssertNotHeldExclusive(self); } 23000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 231c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Id associated with exclusive owner. No memory ordering semantics if called from a thread other 232c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // than the owner. 23300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers uint64_t GetExclusiveOwnerTid() const; 23400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 23500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld. 23600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers unsigned int GetDepth() const { 23700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return recursion_count_; 23800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 23900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 24056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const; 24101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 24200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 243c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES 244c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // 0 is unheld, 1 is held. 245c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers AtomicInteger state_; 246c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Exclusive owner. 247c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers volatile uint64_t exclusive_owner_; 248c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Number of waiting contenders. 249b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger num_contenders_; 250c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else 25100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_mutex_t mutex_; 252c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers volatile uint64_t exclusive_owner_; // Guarded by mutex_. 253c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif 25400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers const bool recursive_; // Can the lock be recursively held? 25500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers unsigned int recursion_count_; 256f1498437b0d6beb9f4f91980b98cbeb0b5c773ceElliott Hughes friend class ConditionVariable; 2578daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes DISALLOW_COPY_AND_ASSIGN(Mutex); 2588daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 2598daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 26000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex. 26100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader) 26200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a 26300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// condition variable. A ReaderWriterMutex can be in one of three states: 26400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Free - not owned by any thread, 26500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Exclusive - owned by a single thread, 26600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// - Shared(n) - shared amongst n threads. 26700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 26800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// The effect of locking and unlocking operations on the state is: 26900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// 27000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// State | ExclusiveLock | ExclusiveUnlock | SharedLock | SharedUnlock 27100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ---------------------------------------------------------------------------- 27200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Free | Exclusive | error | SharedLock(1) | error 27300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Exclusive | Block | Free | Block | error 27400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Shared(n) | Block | error | SharedLock(n+1)* | Shared(n-1) or Free 27500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// * for large values of n the SharedLock may block. 27601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstd::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu); 27700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass LOCKABLE ReaderWriterMutex : public BaseMutex { 2788daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes public: 27981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel); 28000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~ReaderWriterMutex(); 28100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 28200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers virtual bool IsReaderWriterMutex() const { return true; } 28300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 28400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is free then acquire exclusive access. 28581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION(); 28681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void WriterLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); } 28700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 28800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release exclusive access. 28981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION(); 29081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void WriterUnlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); } 29100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 29200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success 29300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // or false if timeout is reached. 29466aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#if HAVE_TIMED_RWLOCK 295c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns) 29681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers EXCLUSIVE_TRYLOCK_FUNCTION(true); 29766aee5cd571cf4739d2735769304202ea5051fb8Ian Rogers#endif 29800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 29900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Block until ReaderWriterMutex is shared or free then acquire a share on the access. 3001ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers void SharedLock(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE; 30181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ReaderLock(Thread* self) SHARED_LOCK_FUNCTION() { SharedLock(self); } 30200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 30300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Try to acquire share of ReaderWriterMutex. 30481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool SharedTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true); 30500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 30600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release a share of the access. 3071ffa32f0be7becec4907b26ead353e4b17e1219cIan Rogers void SharedUnlock(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE; 30881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void ReaderUnlock(Thread* self) UNLOCK_FUNCTION() { SharedUnlock(self); } 30900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 31000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread the exclusive holder of the ReaderWriterMutex. 31181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsExclusiveHeld(const Thread* self) const; 31200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 31300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread has exclusive access to the ReaderWriterMutex. 31481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertExclusiveHeld(const Thread* self) { 315702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz if (kDebugLocking && (gAborting == 0)) { 31601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(IsExclusiveHeld(self)) << *this; 31700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 3188daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 31981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertWriterHeld(const Thread* self) { AssertExclusiveHeld(self); } 3208daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 32100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex. 32281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotExclusiveHeld(const Thread* self) { 323702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz if (kDebugLocking && (gAborting == 0)) { 324e3359f7ad7671c5816f17145ca3a01516512e8d6Ian Rogers CHECK(!IsExclusiveHeld(self)) << *this; 32500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 32600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 32781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotWriterHeld(const Thread* self) { AssertNotExclusiveHeld(self); } 32800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 32900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Is the current thread a shared holder of the ReaderWriterMutex. 33081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool IsSharedHeld(const Thread* self) const; 33100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 33200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread has shared access to the ReaderWriterMutex. 33381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertSharedHeld(const Thread* self) { 334702a85b33c57da99e83698129a289687cdb0be1fSebastien Hertz if (kDebugLocking && (gAborting == 0)) { 33523055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers // TODO: we can only assert this well when self != NULL. 33623055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers CHECK(IsSharedHeld(self) || self == NULL) << *this; 33700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 3388daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 33981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertReaderHeld(const Thread* self) { AssertSharedHeld(self); } 3408daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 34100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive 34200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // mode. 34381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers void AssertNotHeld(const Thread* self) { 3443c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum if (kDebugLocking && (gAborting == 0)) { 34501ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers CHECK(!IsSharedHeld(self)) << *this; 34600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 34700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 34800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 349c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Id associated with exclusive owner. No memory ordering semantics if called from a thread other 350c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // than the owner. 35100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers uint64_t GetExclusiveOwnerTid() const; 35281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 35356edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers virtual void Dump(std::ostream& os) const; 35401ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 3558daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes private: 35681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#if ART_USE_FUTEXES 35781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers // -1 implies held exclusive, +ve shared held by state_ many owners. 358c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers AtomicInteger state_; 359c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Exclusive owner. Modification guarded by this mutex. 36081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers volatile uint64_t exclusive_owner_; 361c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Number of contenders waiting for a reader share. 362c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers AtomicInteger num_pending_readers_; 363c7190697f8665e706f6ebb4ae36fa63c46a32cd5Ian Rogers // Number of contenders waiting to be the writer. 364b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger num_pending_writers_; 36581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#else 36600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_rwlock_t rwlock_; 367c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers volatile uint64_t exclusive_owner_; // Guarded by rwlock_. 36881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#endif 36900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex); 3708daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 3718daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 37200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually 37300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// (Signal) or all at once (Broadcast). 3745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesclass ConditionVariable { 3755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes public: 37623055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers explicit ConditionVariable(const char* name, Mutex& mutex); 3775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ~ConditionVariable(); 3785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 379c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Broadcast(Thread* self); 380c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Signal(Thread* self); 381c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their 382c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // pointer copy, thereby defeating annotalysis. 383c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS; 384c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers void TimedWait(Thread* self, int64_t ms, int32_t ns) NO_THREAD_SAFETY_ANALYSIS; 3851d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held 3861d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // when waiting. 3871d54e73444e017d3a65234e0f193846f3e27472bIan Rogers // TODO: remove this. 3881d54e73444e017d3a65234e0f193846f3e27472bIan Rogers void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS; 3895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes private: 39123055dc5d7a90c4a12e259fd0ed7cd4d04d89182Ian Rogers const char* const name_; 392c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // The Mutex being used by waiters. It is an error to mix condition variables between different 393c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Mutexes. 394c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers Mutex& guard_; 395c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#if ART_USE_FUTEXES 396c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // A counter that is modified by signals and broadcasts. This ensures that when a waiter gives up 397d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_ 398d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait 399d45f201e9bd43490e30a35710865789b8d70e249Ian Rogers // without guard_ held. 400b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers AtomicInteger sequence_; 401c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers // Number of threads that have come into to wait, not the length of the waiters on the futex as 4025bd97c483c1de1eb97afe76123b1b9ab53095edfIan Rogers // waiters may have been requeued onto guard_. Guarded by guard_. 403c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers volatile int32_t num_waiters_; 404c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#else 405c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers pthread_cond_t cond_; 406c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers#endif 4075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DISALLOW_COPY_AND_ASSIGN(ConditionVariable); 4085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}; 4095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 41000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it 41100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// upon destruction. 41200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE MutexLock { 41300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 41481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit MutexLock(Thread* self, Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : self_(self), mu_(mu) { 41581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveLock(self_); 41681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 41781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 41800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~MutexLock() UNLOCK_FUNCTION() { 41981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveUnlock(self_); 42000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 42100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 42200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 42381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Thread* const self_; 42400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Mutex& mu_; 42500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(MutexLock); 42600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 42700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)". 42800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_declaration_missing_variable_name) 42900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 43000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon 43100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction. 43200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE ReaderMutexLock { 43300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 43481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : 43581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers self_(self), mu_(mu) { 43681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.SharedLock(self_); 43781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 43881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 43900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~ReaderMutexLock() UNLOCK_FUNCTION() { 44081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.SharedUnlock(self_); 44100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 44200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 44300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 44481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Thread* const self_; 44500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ReaderWriterMutex& mu_; 44600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock); 44700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 44800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "ReaderMutexLock (lock);" instead of 44900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "ReaderMutexLock mu(lock)". 45000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define ReaderMutexLock(x) COMPILE_ASSERT(0, reader_mutex_lock_declaration_missing_variable_name) 45100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 45200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon 45300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// construction and releases it upon destruction. 45400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersclass SCOPED_LOCKABLE WriterMutexLock { 45500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers public: 45681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers explicit WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : 45781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers self_(self), mu_(mu) { 45881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveLock(self_); 45981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 46081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 46100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ~WriterMutexLock() UNLOCK_FUNCTION() { 46281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers mu_.ExclusiveUnlock(self_); 46300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 46400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 46500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers private: 46650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers Thread* const self_; 46700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ReaderWriterMutex& mu_; 46800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DISALLOW_COPY_AND_ASSIGN(WriterMutexLock); 46900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers}; 47000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of 47100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// "WriterMutexLock mu(lock)". 47200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#define WriterMutexLock(x) COMPILE_ASSERT(0, writer_mutex_lock_declaration_missing_variable_name) 47300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 474719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Global mutexes corresponding to the levels above. 475719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersclass Locks { 476719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers public: 477719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static void Init(); 478719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 479f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers // There's a potential race for two threads to try to suspend each other and for both of them 480f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers // to succeed and get blocked becoming runnable. This lock ensures that only one thread is 481f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers // requesting suspension of another at any time. As the the thread list suspend thread logic 482f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers // transitions to runnable, if the current thread were tried to be suspended then this thread 483f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers // would block holding this lock until it could safely request thread suspension of the other 484f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers // thread without that thread having a suspension request against this thread. This avoids a 485f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers // potential deadlock cycle. 486f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers static Mutex* thread_list_suspend_thread_lock_; 487f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers 488719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // The mutator_lock_ is used to allow mutators to execute in a shared (reader) mode or to block 489719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // mutators by having an exclusive (writer) owner. In normal execution each mutator thread holds 490719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // a share on the mutator_lock_. The garbage collector may also execute with shared access but 491719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // at times requires exclusive access to the heap (not to be confused with the heap meta-data 492719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // guarded by the heap_lock_ below). When the garbage collector requires exclusive access it asks 493719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // the mutators to suspend themselves which also involves usage of the thread_suspend_count_lock_ 494719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // to cover weaknesses in using ReaderWriterMutexes with ConditionVariables. We use a condition 495719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // variable to wait upon in the suspension logic as releasing and then re-acquiring a share on 496719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // the mutator lock doesn't necessarily allow the exclusive user (e.g the garbage collector) 497719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // chance to acquire the lock. 498719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // 499719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Thread suspension: 500719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Shared users | Exclusive user 501719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // (holding mutator lock and in kRunnable state) | .. running .. 502719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | Request thread suspension by: 503719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - acquiring thread_suspend_count_lock_ 504719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - incrementing Thread::suspend_count_ on 505719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | all mutator threads 506719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | - releasing thread_suspend_count_lock_ 507719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | Block trying to acquire exclusive mutator lock 508719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Poll Thread::suspend_count_ and enter full | .. blocked .. 509719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // suspend code. | .. blocked .. 510719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Change state to kSuspended | .. blocked .. 511719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // x: Release share on mutator_lock_ | Carry out exclusive access 512719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Acquire thread_suspend_count_lock_ | .. exclusive .. 513719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // while Thread::suspend_count_ > 0 | .. exclusive .. 514719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - wait on Thread::resume_cond_ | .. exclusive .. 515719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // (releases thread_suspend_count_lock_) | .. exclusive .. 516719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | Release mutator_lock_ 517719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | Request thread resumption by: 518719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - acquiring thread_suspend_count_lock_ 519719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - decrementing Thread::suspend_count_ on 520719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | all mutator threads 521719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. waiting .. | - notifying on Thread::resume_cond_ 522719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - re-acquire thread_suspend_count_lock_ | - releasing thread_suspend_count_lock_ 523719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Release thread_suspend_count_lock_ | .. running .. 524719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Acquire share on mutator_lock_ | .. running .. 525719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - This could block but the thread still | .. running .. 526719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // has a state of kSuspended and so this | .. running .. 527719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // isn't an issue. | .. running .. 528719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Acquire thread_suspend_count_lock_ | .. running .. 529719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // - we poll here as we're transitioning into | .. running .. 530719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // kRunnable and an individual thread suspend | .. running .. 531719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // request (e.g for debugging) won't try | .. running .. 532719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // to acquire the mutator lock (which would | .. running .. 533719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // block as we hold the mutator lock). This | .. running .. 534719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // poll ensures that if the suspender thought | .. running .. 535719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // we were suspended by incrementing our | .. running .. 536719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Thread::suspend_count_ and then reading | .. running .. 537719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // our state we go back to waiting on | .. running .. 538719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Thread::resume_cond_. | .. running .. 539719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // can_go_runnable = Thread::suspend_count_ == 0 | .. running .. 540719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Release thread_suspend_count_lock_ | .. running .. 541719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // if can_go_runnable | .. running .. 542719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Change state to kRunnable | .. running .. 543719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // else | .. running .. 544719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Goto x | .. running .. 545719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // .. running .. | .. running .. 546f3d874c60ee3ada19ce26a5c4e532312b6f3a9e9Ian Rogers static ReaderWriterMutex* mutator_lock_ ACQUIRED_AFTER(thread_list_suspend_thread_lock_); 547719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 548719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap. 549719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_); 550719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 551719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards shutdown of the runtime. 552719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_); 553719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 5549e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards background profiler global state. 5559e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* profiler_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_); 5569e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 5579e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards trace (ie traceview) requests. 5589e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* trace_lock_ ACQUIRED_AFTER(profiler_lock_); 5599e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 560719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads 561719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // attaching and detaching. 5629e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* thread_list_lock_ ACQUIRED_AFTER(trace_lock_); 563719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 56468d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers // Guards maintaining loading library data structures. 56568d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers static Mutex* jni_libraries_lock_ ACQUIRED_AFTER(thread_list_lock_); 56668d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers 567719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards breakpoints. 568ed2be1725fb79075892b1a9103487c9d9a95b350Sebastien Hertz static ReaderWriterMutex* breakpoint_lock_ ACQUIRED_AFTER(jni_libraries_lock_); 569719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 570719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards lists of classes within the class linker. 5719e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(breakpoint_lock_); 572719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 573719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code 574719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // doesn't try to hold a higher level Mutex. 575719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(Locks::classlinker_classes_lock_) 576719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 57774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe static Mutex* allocated_monitor_ids_lock_ ACQUIRED_AFTER(classlinker_classes_lock_); 57874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe 5799e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guard the allocation/deallocation of thread ids. 58074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe static Mutex* allocated_thread_ids_lock_ ACQUIRED_AFTER(allocated_monitor_ids_lock_); 5819e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 5829e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu // Guards modification of the LDT on x86. 5839e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* modify_ldt_lock_ ACQUIRED_AFTER(allocated_thread_ids_lock_); 5849e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu 585719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Guards intern table. 5869e36931cc79ca665908db9575126881d1cfdea5aChao-ying Fu static Mutex* intern_table_lock_ ACQUIRED_AFTER(modify_ldt_lock_); 587719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 588719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Have an exclusive aborting thread. 589719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* abort_lock_ ACQUIRED_AFTER(classlinker_classes_lock_); 590719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 591719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Allow mutual exclusion when manipulating Thread::suspend_count_. 592719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // TODO: Does the trade-off of a per-thread lock make sense? 593719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_); 594719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 595719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // One unexpected signal at a time lock. 596719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_); 597719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 5983eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi // Guards the maps in mem_map. 5993eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi static Mutex* mem_maps_lock_ ACQUIRED_AFTER(unexpected_signal_lock_); 6003eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi 601719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers // Have an exclusive logging thread. 602719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_); 603719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}; 604719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 6058daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 6068daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 607fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif // ART_RUNTIME_BASE_MUTEX_H_ 608