Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #ifndef ART_SRC_MUTEX_H_ |
| 18 | #define ART_SRC_MUTEX_H_ |
| 19 | |
| 20 | #include <pthread.h> |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 22 | |
| 23 | #include <iosfwd> |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 26 | #include "gtest/gtest.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 27 | #include "logging.h" |
| 28 | #include "macros.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 32 | enum MutexRank { |
| 33 | kNoMutexRank = -1, |
| 34 | kHeapLock = 0, |
| 35 | kThreadListLock = 1, |
| 36 | kThreadSuspendCountLock = 2, |
| 37 | kMaxMutexRank = kThreadSuspendCountLock, |
| 38 | }; |
| 39 | std::ostream& operator<<(std::ostream& os, const MutexRank& rhs); |
| 40 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 41 | class LOCKABLE Mutex { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 42 | public: |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 43 | explicit Mutex(const char* name, MutexRank rank = kNoMutexRank); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 44 | ~Mutex(); |
| 45 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 46 | void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 47 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 48 | bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 50 | void Unlock() UNLOCK_FUNCTION(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 51 | |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 52 | #if !defined(NDEBUG) |
| 53 | void AssertHeld(); |
| 54 | void AssertNotHeld(); |
| 55 | #else |
| 56 | void AssertHeld() {} |
| 57 | void AssertNotHeld() {} |
Elliott Hughes | cf04431 | 2012-01-23 18:48:51 -0800 | [diff] [blame] | 58 | #endif |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 59 | |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 60 | uint64_t GetOwner(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 61 | |
| 62 | private: |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 63 | uint32_t GetDepth(); |
| 64 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 65 | pthread_mutex_t mutex_; |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 66 | const std::string name_; |
| 67 | const MutexRank rank_; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 68 | |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 69 | friend class ConditionVariable; |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 70 | friend class MutexTester; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 71 | DISALLOW_COPY_AND_ASSIGN(Mutex); |
| 72 | }; |
| 73 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 74 | class SCOPED_LOCKABLE MutexLock { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 75 | public: |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 76 | explicit MutexLock(Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 77 | mu_.Lock(); |
| 78 | } |
| 79 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 80 | ~MutexLock() UNLOCK_FUNCTION() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 81 | mu_.Unlock(); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | Mutex& mu_; |
| 86 | DISALLOW_COPY_AND_ASSIGN(MutexLock); |
| 87 | }; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 88 | // Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)". |
| 89 | #define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_declaration_missing_variable_name) |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 90 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 91 | class ConditionVariable { |
| 92 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 93 | explicit ConditionVariable(const std::string& name); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 94 | ~ConditionVariable(); |
| 95 | |
| 96 | void Broadcast(); |
| 97 | void Signal(); |
| 98 | void Wait(Mutex& mutex); |
| 99 | void TimedWait(Mutex& mutex, const timespec& ts); |
| 100 | |
| 101 | private: |
| 102 | pthread_cond_t cond_; |
| 103 | std::string name_; |
| 104 | DISALLOW_COPY_AND_ASSIGN(ConditionVariable); |
| 105 | }; |
| 106 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 107 | } // namespace art |
| 108 | |
| 109 | #endif // ART_SRC_MUTEX_H_ |