blob: ee37388f3b88682eb22f0233579a9fd80b65dce6 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_MUTEX_H_
18#define ART_RUNTIME_BASE_MUTEX_H_
Elliott Hughes8daa0922011-09-11 13:46:25 -070019
20#include <pthread.h>
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080021#include <stdint.h>
Elliott Hughesffb465f2012-03-01 18:46:05 -080022
23#include <iosfwd>
Elliott Hughes8daa0922011-09-11 13:46:25 -070024#include <string>
25
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070026#include "atomic_integer.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080028#include "base/macros.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029#include "globals.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070030#include "locks.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070031
Ian Rogersab470162012-09-29 23:06:53 -070032#if defined(__APPLE__)
Ian Rogers81d425b2012-09-27 16:03:43 -070033#define ART_USE_FUTEXES 0
Ian Rogersab470162012-09-29 23:06:53 -070034#else
Ian Rogersa6f3aaf2013-01-16 15:17:58 -080035#define ART_USE_FUTEXES !defined(__mips__)
Ian Rogersab470162012-09-29 23:06:53 -070036#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -070037
Ian Rogers66aee5c2012-08-15 17:17:47 -070038// Currently Darwin doesn't support locks with timeouts.
39#if !defined(__APPLE__)
40#define HAVE_TIMED_RWLOCK 1
41#else
42#define HAVE_TIMED_RWLOCK 0
43#endif
44
Elliott Hughes8daa0922011-09-11 13:46:25 -070045namespace art {
46
Ian Rogers56edc432013-01-18 16:51:51 -080047class ScopedContentionRecorder;
Ian Rogers50b35e22012-10-04 10:09:15 -070048class Thread;
49
Brian Carlstrom2e250c82013-08-14 18:08:52 -070050const bool kDebugLocking = kIsDebugBuild;
Ian Rogers25fd14b2012-09-05 10:56:38 -070051
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070052// Record Log contention information, dumpable via SIGQUIT.
53#ifdef ART_USE_FUTEXES
Jeff Hao08f2e7b2013-09-09 16:44:02 -070054// To enable lock contention logging, set this to true.
55const bool kLogLockContentions = false;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070056#else
57// Keep this false as lock contention logging is supported only with
58// futex.
59const bool kLogLockContentions = false;
60#endif
61const size_t kContentionLogSize = 64;
62const size_t kContentionLogDataSize = kLogLockContentions ? 1 : 0;
63const size_t kAllMutexDataSize = kLogLockContentions ? 1 : 0;
64
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065// Base class for all Mutex implementations
66class BaseMutex {
67 public:
Ian Rogersbab74962013-04-19 10:04:10 -070068 const char* GetName() const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069 return name_;
70 }
71
72 virtual bool IsMutex() const { return false; }
73 virtual bool IsReaderWriterMutex() const { return false; }
74
Ian Rogers56edc432013-01-18 16:51:51 -080075 virtual void Dump(std::ostream& os) const = 0;
76
77 static void DumpAll(std::ostream& os);
78
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079 protected:
80 friend class ConditionVariable;
81
Ian Rogers81d425b2012-09-27 16:03:43 -070082 BaseMutex(const char* name, LockLevel level);
Ian Rogers56edc432013-01-18 16:51:51 -080083 virtual ~BaseMutex();
Ian Rogers81d425b2012-09-27 16:03:43 -070084 void RegisterAsLocked(Thread* self);
85 void RegisterAsUnlocked(Thread* self);
86 void CheckSafeToWait(Thread* self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087
Ian Rogers56edc432013-01-18 16:51:51 -080088 friend class ScopedContentionRecorder;
89
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070090 void RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t nano_time_blocked);
Ian Rogers56edc432013-01-18 16:51:51 -080091 void DumpContention(std::ostream& os) const;
92
Ian Rogers81d425b2012-09-27 16:03:43 -070093 const LockLevel level_; // Support for lock hierarchy.
Ian Rogersbab74962013-04-19 10:04:10 -070094 const char* const name_;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070095
Ian Rogers56edc432013-01-18 16:51:51 -080096 // A log entry that records contention but makes no guarantee that either tid will be held live.
97 struct ContentionLogEntry {
98 ContentionLogEntry() : blocked_tid(0), owner_tid(0) {}
99 uint64_t blocked_tid;
100 uint64_t owner_tid;
101 AtomicInteger count;
102 };
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700103 struct ContentionLogData {
104 ContentionLogEntry contention_log[kContentionLogSize];
105 // The next entry in the contention log to be updated. Value ranges from 0 to
106 // kContentionLogSize - 1.
107 AtomicInteger cur_content_log_entry;
108 // Number of times the Mutex has been contended.
109 AtomicInteger contention_count;
110 // Sum of time waited by all contenders in ns.
111 volatile uint64_t wait_time;
112 void AddToWaitTime(uint64_t value);
113 ContentionLogData() : wait_time(0) {}
114 };
115 ContentionLogData contetion_log_data_[kContentionLogDataSize];
116
117 public:
118 bool HasEverContended() const {
119 if (kLogLockContentions) {
120 return contetion_log_data_->contention_count > 0;
121 }
122 return false;
123 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124};
125
126// A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain
127// exclusive access to what it guards. A Mutex can be in one of two states:
128// - Free - not owned by any thread,
129// - Exclusive - owned by a single thread.
130//
131// The effect of locking and unlocking operations on the state is:
132// State | ExclusiveLock | ExclusiveUnlock
133// -------------------------------------------
134// Free | Exclusive | error
135// Exclusive | Block* | Free
136// * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in
137// an error. Being non-reentrant simplifies Waiting on ConditionVariables.
Ian Rogers01ae5802012-09-28 16:14:01 -0700138std::ostream& operator<<(std::ostream& os, const Mutex& mu);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139class LOCKABLE Mutex : public BaseMutex {
140 public:
Ian Rogers81d425b2012-09-27 16:03:43 -0700141 explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700142 ~Mutex();
143
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 virtual bool IsMutex() const { return true; }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700145
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 // Block until mutex is free then acquire exclusive access.
Ian Rogers81d425b2012-09-27 16:03:43 -0700147 void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION();
148 void Lock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700149
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150 // Returns true if acquires exclusive access, false otherwise.
Ian Rogers81d425b2012-09-27 16:03:43 -0700151 bool ExclusiveTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true);
152 bool TryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true) { return ExclusiveTryLock(self); }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700153
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700154 // Release exclusive access.
Ian Rogers81d425b2012-09-27 16:03:43 -0700155 void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION();
156 void Unlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700157
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700158 // Is the current thread the exclusive holder of the Mutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700159 bool IsExclusiveHeld(const Thread* self) const;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160
161 // Assert that the Mutex is exclusively held by the current thread.
Ian Rogers81d425b2012-09-27 16:03:43 -0700162 void AssertExclusiveHeld(const Thread* self) {
Anwar Ghuloum3c539ff2013-06-20 08:58:23 -0700163 if (kDebugLocking && (gAborting == 0)) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700164 CHECK(IsExclusiveHeld(self)) << *this;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165 }
166 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700167 void AssertHeld(const Thread* self) { AssertExclusiveHeld(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168
169 // Assert that the Mutex is not held by the current thread.
Ian Rogers81d425b2012-09-27 16:03:43 -0700170 void AssertNotHeldExclusive(const Thread* self) {
Anwar Ghuloum3c539ff2013-06-20 08:58:23 -0700171 if (kDebugLocking && (gAborting == 0)) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700172 CHECK(!IsExclusiveHeld(self)) << *this;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700173 }
174 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700175 void AssertNotHeld(const Thread* self) { AssertNotHeldExclusive(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700176
177 // Id associated with exclusive owner.
178 uint64_t GetExclusiveOwnerTid() const;
179
180 // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld.
181 unsigned int GetDepth() const {
182 return recursion_count_;
183 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700184
Ian Rogers56edc432013-01-18 16:51:51 -0800185 virtual void Dump(std::ostream& os) const;
Ian Rogers01ae5802012-09-28 16:14:01 -0700186
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700187 private:
Ian Rogersc604d732012-10-14 16:09:54 -0700188#if ART_USE_FUTEXES
189 // 0 is unheld, 1 is held.
190 volatile int32_t state_;
191 // Exclusive owner.
192 volatile uint64_t exclusive_owner_;
193 // Number of waiting contenders.
194 volatile int32_t num_contenders_;
195#else
Elliott Hughes8daa0922011-09-11 13:46:25 -0700196 pthread_mutex_t mutex_;
Ian Rogersc604d732012-10-14 16:09:54 -0700197#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700198 const bool recursive_; // Can the lock be recursively held?
199 unsigned int recursion_count_;
Elliott Hughesf1498432012-03-28 19:34:27 -0700200 friend class ConditionVariable;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700201 DISALLOW_COPY_AND_ASSIGN(Mutex);
202};
203
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700204// A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex.
205// Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader)
206// access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a
207// condition variable. A ReaderWriterMutex can be in one of three states:
208// - Free - not owned by any thread,
209// - Exclusive - owned by a single thread,
210// - Shared(n) - shared amongst n threads.
211//
212// The effect of locking and unlocking operations on the state is:
213//
214// State | ExclusiveLock | ExclusiveUnlock | SharedLock | SharedUnlock
215// ----------------------------------------------------------------------------
216// Free | Exclusive | error | SharedLock(1) | error
217// Exclusive | Block | Free | Block | error
218// Shared(n) | Block | error | SharedLock(n+1)* | Shared(n-1) or Free
219// * for large values of n the SharedLock may block.
Ian Rogers01ae5802012-09-28 16:14:01 -0700220std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221class LOCKABLE ReaderWriterMutex : public BaseMutex {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700222 public:
Ian Rogers81d425b2012-09-27 16:03:43 -0700223 explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700224 ~ReaderWriterMutex();
225
226 virtual bool IsReaderWriterMutex() const { return true; }
227
228 // Block until ReaderWriterMutex is free then acquire exclusive access.
Ian Rogers81d425b2012-09-27 16:03:43 -0700229 void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION();
230 void WriterLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231
232 // Release exclusive access.
Ian Rogers81d425b2012-09-27 16:03:43 -0700233 void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION();
234 void WriterUnlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700235
236 // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success
237 // or false if timeout is reached.
Ian Rogers66aee5c2012-08-15 17:17:47 -0700238#if HAVE_TIMED_RWLOCK
Ian Rogersc604d732012-10-14 16:09:54 -0700239 bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns)
Ian Rogers81d425b2012-09-27 16:03:43 -0700240 EXCLUSIVE_TRYLOCK_FUNCTION(true);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700241#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242
243 // Block until ReaderWriterMutex is shared or free then acquire a share on the access.
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800244 void SharedLock(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE;
Ian Rogers81d425b2012-09-27 16:03:43 -0700245 void ReaderLock(Thread* self) SHARED_LOCK_FUNCTION() { SharedLock(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246
247 // Try to acquire share of ReaderWriterMutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700248 bool SharedTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249
250 // Release a share of the access.
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800251 void SharedUnlock(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE;
Ian Rogers81d425b2012-09-27 16:03:43 -0700252 void ReaderUnlock(Thread* self) UNLOCK_FUNCTION() { SharedUnlock(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253
254 // Is the current thread the exclusive holder of the ReaderWriterMutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700255 bool IsExclusiveHeld(const Thread* self) const;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256
257 // Assert the current thread has exclusive access to the ReaderWriterMutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700258 void AssertExclusiveHeld(const Thread* self) {
Sebastien Hertz702a85b2013-08-05 16:37:51 +0200259 if (kDebugLocking && (gAborting == 0)) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700260 CHECK(IsExclusiveHeld(self)) << *this;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 }
262 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700263 void AssertWriterHeld(const Thread* self) { AssertExclusiveHeld(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264
265 // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700266 void AssertNotExclusiveHeld(const Thread* self) {
Sebastien Hertz702a85b2013-08-05 16:37:51 +0200267 if (kDebugLocking && (gAborting == 0)) {
Ian Rogerse3359f72013-06-11 15:14:11 -0700268 CHECK(!IsExclusiveHeld(self)) << *this;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 }
270 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700271 void AssertNotWriterHeld(const Thread* self) { AssertNotExclusiveHeld(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272
273 // Is the current thread a shared holder of the ReaderWriterMutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700274 bool IsSharedHeld(const Thread* self) const;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275
276 // Assert the current thread has shared access to the ReaderWriterMutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700277 void AssertSharedHeld(const Thread* self) {
Sebastien Hertz702a85b2013-08-05 16:37:51 +0200278 if (kDebugLocking && (gAborting == 0)) {
Ian Rogers23055dc2013-04-18 16:29:16 -0700279 // TODO: we can only assert this well when self != NULL.
280 CHECK(IsSharedHeld(self) || self == NULL) << *this;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 }
282 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700283 void AssertReaderHeld(const Thread* self) { AssertSharedHeld(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284
285 // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive
286 // mode.
Ian Rogers81d425b2012-09-27 16:03:43 -0700287 void AssertNotHeld(const Thread* self) {
Anwar Ghuloum3c539ff2013-06-20 08:58:23 -0700288 if (kDebugLocking && (gAborting == 0)) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700289 CHECK(!IsSharedHeld(self)) << *this;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700291 }
292
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 // Id associated with exclusive owner.
294 uint64_t GetExclusiveOwnerTid() const;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700295
Ian Rogers56edc432013-01-18 16:51:51 -0800296 virtual void Dump(std::ostream& os) const;
Ian Rogers01ae5802012-09-28 16:14:01 -0700297
Ian Rogers81d425b2012-09-27 16:03:43 -0700298 private:
299#if ART_USE_FUTEXES
300 // -1 implies held exclusive, +ve shared held by state_ many owners.
301 volatile int32_t state_;
302 // Exclusive owner.
303 volatile uint64_t exclusive_owner_;
304 // Pending readers.
305 volatile int32_t num_pending_readers_;
306 // Pending writers.
307 volatile int32_t num_pending_writers_;
308#else
309 pthread_rwlock_t rwlock_;
310#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311 DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex);
312};
313
314// ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually
315// (Signal) or all at once (Broadcast).
Elliott Hughes5f791332011-09-15 17:45:30 -0700316class ConditionVariable {
317 public:
Ian Rogers23055dc2013-04-18 16:29:16 -0700318 explicit ConditionVariable(const char* name, Mutex& mutex);
Elliott Hughes5f791332011-09-15 17:45:30 -0700319 ~ConditionVariable();
320
Ian Rogersc604d732012-10-14 16:09:54 -0700321 void Broadcast(Thread* self);
322 void Signal(Thread* self);
323 // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their
324 // pointer copy, thereby defeating annotalysis.
325 void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
326 void TimedWait(Thread* self, int64_t ms, int32_t ns) NO_THREAD_SAFETY_ANALYSIS;
Ian Rogers1d54e732013-05-02 21:10:01 -0700327 // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held
328 // when waiting.
329 // TODO: remove this.
330 void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
Elliott Hughes5f791332011-09-15 17:45:30 -0700331
332 private:
Ian Rogers23055dc2013-04-18 16:29:16 -0700333 const char* const name_;
Ian Rogersc604d732012-10-14 16:09:54 -0700334 // The Mutex being used by waiters. It is an error to mix condition variables between different
335 // Mutexes.
336 Mutex& guard_;
337#if ART_USE_FUTEXES
338 // A counter that is modified by signals and broadcasts. This ensures that when a waiter gives up
Ian Rogersd45f2012012-11-28 11:46:23 -0800339 // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_
340 // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait
341 // without guard_ held.
342 volatile int32_t sequence_;
Ian Rogersc604d732012-10-14 16:09:54 -0700343 // Number of threads that have come into to wait, not the length of the waiters on the futex as
Ian Rogers5bd97c42012-11-27 02:38:26 -0800344 // waiters may have been requeued onto guard_. Guarded by guard_.
Ian Rogersc604d732012-10-14 16:09:54 -0700345 volatile int32_t num_waiters_;
Ian Rogersc604d732012-10-14 16:09:54 -0700346#else
347 pthread_cond_t cond_;
348#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700349 DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
350};
351
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352// Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it
353// upon destruction.
354class SCOPED_LOCKABLE MutexLock {
355 public:
Ian Rogers81d425b2012-09-27 16:03:43 -0700356 explicit MutexLock(Thread* self, Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : self_(self), mu_(mu) {
357 mu_.ExclusiveLock(self_);
358 }
359
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700360 ~MutexLock() UNLOCK_FUNCTION() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700361 mu_.ExclusiveUnlock(self_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700362 }
363
364 private:
Ian Rogers81d425b2012-09-27 16:03:43 -0700365 Thread* const self_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700366 Mutex& mu_;
367 DISALLOW_COPY_AND_ASSIGN(MutexLock);
368};
369// Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)".
370#define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_declaration_missing_variable_name)
371
372// Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon
373// construction and releases it upon destruction.
374class SCOPED_LOCKABLE ReaderMutexLock {
375 public:
Ian Rogers81d425b2012-09-27 16:03:43 -0700376 explicit ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) :
377 self_(self), mu_(mu) {
378 mu_.SharedLock(self_);
379 }
380
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700381 ~ReaderMutexLock() UNLOCK_FUNCTION() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700382 mu_.SharedUnlock(self_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700383 }
384
385 private:
Ian Rogers81d425b2012-09-27 16:03:43 -0700386 Thread* const self_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700387 ReaderWriterMutex& mu_;
388 DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock);
389};
390// Catch bug where variable name is omitted. "ReaderMutexLock (lock);" instead of
391// "ReaderMutexLock mu(lock)".
392#define ReaderMutexLock(x) COMPILE_ASSERT(0, reader_mutex_lock_declaration_missing_variable_name)
393
394// Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon
395// construction and releases it upon destruction.
396class SCOPED_LOCKABLE WriterMutexLock {
397 public:
Ian Rogers81d425b2012-09-27 16:03:43 -0700398 explicit WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) :
399 self_(self), mu_(mu) {
400 mu_.ExclusiveLock(self_);
401 }
402
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700403 ~WriterMutexLock() UNLOCK_FUNCTION() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700404 mu_.ExclusiveUnlock(self_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700405 }
406
407 private:
Ian Rogers50b35e22012-10-04 10:09:15 -0700408 Thread* const self_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700409 ReaderWriterMutex& mu_;
410 DISALLOW_COPY_AND_ASSIGN(WriterMutexLock);
411};
412// Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of
413// "WriterMutexLock mu(lock)".
414#define WriterMutexLock(x) COMPILE_ASSERT(0, writer_mutex_lock_declaration_missing_variable_name)
415
Elliott Hughes8daa0922011-09-11 13:46:25 -0700416} // namespace art
417
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700418#endif // ART_RUNTIME_BASE_MUTEX_H_