blob: 48c85859d6b564d5c938ec728e3bbde57ec99a97 [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
Elliott Hughes76b61672012-12-12 17:47:30 -080017#ifndef ART_SRC_BASE_MUTEX_H_
18#define ART_SRC_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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/macros.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070028#include "globals.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070029#include "locks.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070030
Ian Rogersab470162012-09-29 23:06:53 -070031#if defined(__APPLE__)
Ian Rogers81d425b2012-09-27 16:03:43 -070032#define ART_USE_FUTEXES 0
Ian Rogersab470162012-09-29 23:06:53 -070033#else
Ian Rogersa6f3aaf2013-01-16 15:17:58 -080034#define ART_USE_FUTEXES !defined(__mips__)
Ian Rogersab470162012-09-29 23:06:53 -070035#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -070036
Ian Rogers66aee5c2012-08-15 17:17:47 -070037// Currently Darwin doesn't support locks with timeouts.
38#if !defined(__APPLE__)
39#define HAVE_TIMED_RWLOCK 1
40#else
41#define HAVE_TIMED_RWLOCK 0
42#endif
43
Ian Rogers56edc432013-01-18 16:51:51 -080044// Record Log contention information, dumpable via SIGQUIT.
45#define CONTENTION_LOGGING (0 && ART_USE_FUTEXES)
46const size_t kContentionLogSize = 64;
47#if CONTENTION_LOGGING
48#include "atomic_integer.h"
49#endif
50
Elliott Hughes8daa0922011-09-11 13:46:25 -070051namespace art {
52
Ian Rogers56edc432013-01-18 16:51:51 -080053class ScopedContentionRecorder;
Ian Rogers50b35e22012-10-04 10:09:15 -070054class Thread;
55
Ian Rogers25fd14b2012-09-05 10:56:38 -070056const bool kDebugLocking = kIsDebugBuild;
57
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058// Base class for all Mutex implementations
59class BaseMutex {
60 public:
61 const std::string& GetName() const {
62 return name_;
63 }
64
65 virtual bool IsMutex() const { return false; }
66 virtual bool IsReaderWriterMutex() const { return false; }
67
Ian Rogers56edc432013-01-18 16:51:51 -080068 virtual void Dump(std::ostream& os) const = 0;
69
70 static void DumpAll(std::ostream& os);
71
Ian Rogers00f7d0e2012-07-19 15:28:27 -070072 protected:
73 friend class ConditionVariable;
74
Ian Rogers81d425b2012-09-27 16:03:43 -070075 BaseMutex(const char* name, LockLevel level);
Ian Rogers56edc432013-01-18 16:51:51 -080076 virtual ~BaseMutex();
Ian Rogers81d425b2012-09-27 16:03:43 -070077 void RegisterAsLocked(Thread* self);
78 void RegisterAsUnlocked(Thread* self);
79 void CheckSafeToWait(Thread* self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070080
Ian Rogers56edc432013-01-18 16:51:51 -080081 friend class ScopedContentionRecorder;
82
83 void RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t milli_time_blocked);
84 void DumpContention(std::ostream& os) const;
85
Ian Rogers81d425b2012-09-27 16:03:43 -070086 const LockLevel level_; // Support for lock hierarchy.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 const std::string name_;
Ian Rogers56edc432013-01-18 16:51:51 -080088#if CONTENTION_LOGGING
89 // A log entry that records contention but makes no guarantee that either tid will be held live.
90 struct ContentionLogEntry {
91 ContentionLogEntry() : blocked_tid(0), owner_tid(0) {}
92 uint64_t blocked_tid;
93 uint64_t owner_tid;
94 AtomicInteger count;
95 };
96 ContentionLogEntry contention_log_[kContentionLogSize];
97 // The next entry in the contention log to be updated. Value ranges from 0 to
98 // kContentionLogSize - 1.
99 AtomicInteger cur_content_log_entry_;
100 // Number of times the Mutex has been contended.
101 AtomicInteger contention_count_;
102 // Sum of time waited by all contenders in ms.
103 AtomicInteger wait_time_;
104#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105};
106
107// A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain
108// exclusive access to what it guards. A Mutex can be in one of two states:
109// - Free - not owned by any thread,
110// - Exclusive - owned by a single thread.
111//
112// The effect of locking and unlocking operations on the state is:
113// State | ExclusiveLock | ExclusiveUnlock
114// -------------------------------------------
115// Free | Exclusive | error
116// Exclusive | Block* | Free
117// * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in
118// an error. Being non-reentrant simplifies Waiting on ConditionVariables.
Ian Rogers01ae5802012-09-28 16:14:01 -0700119std::ostream& operator<<(std::ostream& os, const Mutex& mu);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120class LOCKABLE Mutex : public BaseMutex {
121 public:
Ian Rogers81d425b2012-09-27 16:03:43 -0700122 explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700123 ~Mutex();
124
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 virtual bool IsMutex() const { return true; }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700126
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 // Block until mutex is free then acquire exclusive access.
Ian Rogers81d425b2012-09-27 16:03:43 -0700128 void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION();
129 void Lock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700130
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131 // Returns true if acquires exclusive access, false otherwise.
Ian Rogers81d425b2012-09-27 16:03:43 -0700132 bool ExclusiveTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true);
133 bool TryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true) { return ExclusiveTryLock(self); }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700134
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 // Release exclusive access.
Ian Rogers81d425b2012-09-27 16:03:43 -0700136 void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION();
137 void Unlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700138
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 // Is the current thread the exclusive holder of the Mutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700140 bool IsExclusiveHeld(const Thread* self) const;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141
142 // Assert that the Mutex is exclusively held by the current thread.
Ian Rogers81d425b2012-09-27 16:03:43 -0700143 void AssertExclusiveHeld(const Thread* self) {
Brian Carlstrom81b88712012-11-05 19:21:30 -0800144 if (kDebugLocking && !gAborting) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700145 CHECK(IsExclusiveHeld(self)) << *this;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 }
147 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700148 void AssertHeld(const Thread* self) { AssertExclusiveHeld(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149
150 // Assert that the Mutex is not held by the current thread.
Ian Rogers81d425b2012-09-27 16:03:43 -0700151 void AssertNotHeldExclusive(const Thread* self) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700152 if (kDebugLocking) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700153 CHECK(!IsExclusiveHeld(self)) << *this;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700154 }
155 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700156 void AssertNotHeld(const Thread* self) { AssertNotHeldExclusive(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157
158 // Id associated with exclusive owner.
159 uint64_t GetExclusiveOwnerTid() const;
160
161 // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld.
162 unsigned int GetDepth() const {
163 return recursion_count_;
164 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700165
Ian Rogers56edc432013-01-18 16:51:51 -0800166 virtual void Dump(std::ostream& os) const;
Ian Rogers01ae5802012-09-28 16:14:01 -0700167
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700168 private:
Ian Rogersc604d732012-10-14 16:09:54 -0700169#if ART_USE_FUTEXES
170 // 0 is unheld, 1 is held.
171 volatile int32_t state_;
172 // Exclusive owner.
173 volatile uint64_t exclusive_owner_;
174 // Number of waiting contenders.
175 volatile int32_t num_contenders_;
176#else
Elliott Hughes8daa0922011-09-11 13:46:25 -0700177 pthread_mutex_t mutex_;
Ian Rogersc604d732012-10-14 16:09:54 -0700178#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700179 const bool recursive_; // Can the lock be recursively held?
180 unsigned int recursion_count_;
Elliott Hughesf1498432012-03-28 19:34:27 -0700181 friend class ConditionVariable;
Elliott Hughes3efb8412012-03-16 16:09:38 -0700182 friend class MutexTester;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700183 DISALLOW_COPY_AND_ASSIGN(Mutex);
184};
185
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700186// A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex.
187// Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader)
188// access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a
189// condition variable. A ReaderWriterMutex can be in one of three states:
190// - Free - not owned by any thread,
191// - Exclusive - owned by a single thread,
192// - Shared(n) - shared amongst n threads.
193//
194// The effect of locking and unlocking operations on the state is:
195//
196// State | ExclusiveLock | ExclusiveUnlock | SharedLock | SharedUnlock
197// ----------------------------------------------------------------------------
198// Free | Exclusive | error | SharedLock(1) | error
199// Exclusive | Block | Free | Block | error
200// Shared(n) | Block | error | SharedLock(n+1)* | Shared(n-1) or Free
201// * for large values of n the SharedLock may block.
Ian Rogers01ae5802012-09-28 16:14:01 -0700202std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203class LOCKABLE ReaderWriterMutex : public BaseMutex {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700204 public:
Ian Rogers81d425b2012-09-27 16:03:43 -0700205 explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 ~ReaderWriterMutex();
207
208 virtual bool IsReaderWriterMutex() const { return true; }
209
210 // Block until ReaderWriterMutex is free then acquire exclusive access.
Ian Rogers81d425b2012-09-27 16:03:43 -0700211 void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION();
212 void WriterLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700213
214 // Release exclusive access.
Ian Rogers81d425b2012-09-27 16:03:43 -0700215 void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION();
216 void WriterUnlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700217
218 // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success
219 // or false if timeout is reached.
Ian Rogers66aee5c2012-08-15 17:17:47 -0700220#if HAVE_TIMED_RWLOCK
Ian Rogersc604d732012-10-14 16:09:54 -0700221 bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns)
Ian Rogers81d425b2012-09-27 16:03:43 -0700222 EXCLUSIVE_TRYLOCK_FUNCTION(true);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700223#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700224
225 // Block until ReaderWriterMutex is shared or free then acquire a share on the access.
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800226 void SharedLock(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE;
Ian Rogers81d425b2012-09-27 16:03:43 -0700227 void ReaderLock(Thread* self) SHARED_LOCK_FUNCTION() { SharedLock(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228
229 // Try to acquire share of ReaderWriterMutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700230 bool SharedTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231
232 // Release a share of the access.
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800233 void SharedUnlock(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE;
Ian Rogers81d425b2012-09-27 16:03:43 -0700234 void ReaderUnlock(Thread* self) UNLOCK_FUNCTION() { SharedUnlock(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700235
236 // Is the current thread the exclusive holder of the ReaderWriterMutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700237 bool IsExclusiveHeld(const Thread* self) const;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700238
239 // Assert the current thread has exclusive access to the ReaderWriterMutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700240 void AssertExclusiveHeld(const Thread* self) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700241 if (kDebugLocking) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700242 CHECK(IsExclusiveHeld(self)) << *this;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 }
244 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700245 void AssertWriterHeld(const Thread* self) { AssertExclusiveHeld(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246
247 // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700248 void AssertNotExclusiveHeld(const Thread* self) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700249 if (kDebugLocking) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700250 CHECK(!IsExclusiveHeld(self));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 }
252 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700253 void AssertNotWriterHeld(const Thread* self) { AssertNotExclusiveHeld(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254
255 // Is the current thread a shared holder of the ReaderWriterMutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700256 bool IsSharedHeld(const Thread* self) const;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257
258 // Assert the current thread has shared access to the ReaderWriterMutex.
Ian Rogers81d425b2012-09-27 16:03:43 -0700259 void AssertSharedHeld(const Thread* self) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700260 if (kDebugLocking) {
Ian Rogers23055dc2013-04-18 16:29:16 -0700261 // TODO: we can only assert this well when self != NULL.
262 CHECK(IsSharedHeld(self) || self == NULL) << *this;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700263 }
264 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700265 void AssertReaderHeld(const Thread* self) { AssertSharedHeld(self); }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266
267 // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive
268 // mode.
Ian Rogers81d425b2012-09-27 16:03:43 -0700269 void AssertNotHeld(const Thread* self) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700270 if (kDebugLocking) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700271 CHECK(!IsSharedHeld(self)) << *this;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700273 }
274
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 // Id associated with exclusive owner.
276 uint64_t GetExclusiveOwnerTid() const;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700277
Ian Rogers56edc432013-01-18 16:51:51 -0800278 virtual void Dump(std::ostream& os) const;
Ian Rogers01ae5802012-09-28 16:14:01 -0700279
Ian Rogers81d425b2012-09-27 16:03:43 -0700280 private:
281#if ART_USE_FUTEXES
282 // -1 implies held exclusive, +ve shared held by state_ many owners.
283 volatile int32_t state_;
284 // Exclusive owner.
285 volatile uint64_t exclusive_owner_;
286 // Pending readers.
287 volatile int32_t num_pending_readers_;
288 // Pending writers.
289 volatile int32_t num_pending_writers_;
290#else
291 pthread_rwlock_t rwlock_;
292#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 friend class MutexTester;
294 DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex);
295};
296
297// ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually
298// (Signal) or all at once (Broadcast).
Elliott Hughes5f791332011-09-15 17:45:30 -0700299class ConditionVariable {
300 public:
Ian Rogers23055dc2013-04-18 16:29:16 -0700301 explicit ConditionVariable(const char* name, Mutex& mutex);
Elliott Hughes5f791332011-09-15 17:45:30 -0700302 ~ConditionVariable();
303
Ian Rogersc604d732012-10-14 16:09:54 -0700304 void Broadcast(Thread* self);
305 void Signal(Thread* self);
306 // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their
307 // pointer copy, thereby defeating annotalysis.
308 void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
309 void TimedWait(Thread* self, int64_t ms, int32_t ns) NO_THREAD_SAFETY_ANALYSIS;
Elliott Hughes5f791332011-09-15 17:45:30 -0700310
311 private:
Ian Rogers23055dc2013-04-18 16:29:16 -0700312 const char* const name_;
Ian Rogersc604d732012-10-14 16:09:54 -0700313 // The Mutex being used by waiters. It is an error to mix condition variables between different
314 // Mutexes.
315 Mutex& guard_;
316#if ART_USE_FUTEXES
317 // A counter that is modified by signals and broadcasts. This ensures that when a waiter gives up
Ian Rogersd45f2012012-11-28 11:46:23 -0800318 // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_
319 // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait
320 // without guard_ held.
321 volatile int32_t sequence_;
Ian Rogersc604d732012-10-14 16:09:54 -0700322 // 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 -0800323 // waiters may have been requeued onto guard_. Guarded by guard_.
Ian Rogersc604d732012-10-14 16:09:54 -0700324 volatile int32_t num_waiters_;
Ian Rogersc604d732012-10-14 16:09:54 -0700325#else
326 pthread_cond_t cond_;
327#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700328 DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
329};
330
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331// Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it
332// upon destruction.
333class SCOPED_LOCKABLE MutexLock {
334 public:
Ian Rogers81d425b2012-09-27 16:03:43 -0700335 explicit MutexLock(Thread* self, Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : self_(self), mu_(mu) {
336 mu_.ExclusiveLock(self_);
337 }
338
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339 ~MutexLock() UNLOCK_FUNCTION() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700340 mu_.ExclusiveUnlock(self_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341 }
342
343 private:
Ian Rogers81d425b2012-09-27 16:03:43 -0700344 Thread* const self_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 Mutex& mu_;
346 DISALLOW_COPY_AND_ASSIGN(MutexLock);
347};
348// Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)".
349#define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_declaration_missing_variable_name)
350
351// Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon
352// construction and releases it upon destruction.
353class SCOPED_LOCKABLE ReaderMutexLock {
354 public:
Ian Rogers81d425b2012-09-27 16:03:43 -0700355 explicit ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) :
356 self_(self), mu_(mu) {
357 mu_.SharedLock(self_);
358 }
359
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700360 ~ReaderMutexLock() UNLOCK_FUNCTION() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700361 mu_.SharedUnlock(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 ReaderWriterMutex& mu_;
367 DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock);
368};
369// Catch bug where variable name is omitted. "ReaderMutexLock (lock);" instead of
370// "ReaderMutexLock mu(lock)".
371#define ReaderMutexLock(x) COMPILE_ASSERT(0, reader_mutex_lock_declaration_missing_variable_name)
372
373// Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon
374// construction and releases it upon destruction.
375class SCOPED_LOCKABLE WriterMutexLock {
376 public:
Ian Rogers81d425b2012-09-27 16:03:43 -0700377 explicit WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) :
378 self_(self), mu_(mu) {
379 mu_.ExclusiveLock(self_);
380 }
381
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 ~WriterMutexLock() UNLOCK_FUNCTION() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700383 mu_.ExclusiveUnlock(self_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700384 }
385
386 private:
Ian Rogers50b35e22012-10-04 10:09:15 -0700387 Thread* const self_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700388 ReaderWriterMutex& mu_;
389 DISALLOW_COPY_AND_ASSIGN(WriterMutexLock);
390};
391// Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of
392// "WriterMutexLock mu(lock)".
393#define WriterMutexLock(x) COMPILE_ASSERT(0, writer_mutex_lock_declaration_missing_variable_name)
394
Elliott Hughes8daa0922011-09-11 13:46:25 -0700395} // namespace art
396
Elliott Hughes76b61672012-12-12 17:47:30 -0800397#endif // ART_SRC_BASE_MUTEX_H_