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 | |
| 26 | #include "logging.h" |
| 27 | #include "macros.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame^] | 31 | enum MutexRank { |
| 32 | kNoMutexRank = -1, |
| 33 | kHeapLock = 0, |
| 34 | kThreadListLock = 1, |
| 35 | kThreadSuspendCountLock = 2, |
| 36 | kMaxMutexRank = kThreadSuspendCountLock, |
| 37 | }; |
| 38 | std::ostream& operator<<(std::ostream& os, const MutexRank& rhs); |
| 39 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 40 | class Mutex { |
| 41 | public: |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame^] | 42 | explicit Mutex(const char* name, MutexRank rank = kNoMutexRank); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 43 | ~Mutex(); |
| 44 | |
| 45 | void Lock(); |
| 46 | |
| 47 | bool TryLock(); |
| 48 | |
| 49 | void Unlock(); |
| 50 | |
| 51 | const char* GetName() { |
| 52 | return name_.c_str(); |
| 53 | } |
| 54 | |
| 55 | pthread_mutex_t* GetImpl() { |
| 56 | return &mutex_; |
| 57 | } |
| 58 | |
| 59 | void AssertHeld() { |
Elliott Hughes | cf04431 | 2012-01-23 18:48:51 -0800 | [diff] [blame] | 60 | #if !defined(__APPLE__) |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 61 | DCHECK_EQ(GetOwner(), GetTid()); |
Elliott Hughes | cf04431 | 2012-01-23 18:48:51 -0800 | [diff] [blame] | 62 | #endif |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void AssertNotHeld() { |
Elliott Hughes | cf04431 | 2012-01-23 18:48:51 -0800 | [diff] [blame] | 66 | #if !defined(__APPLE__) |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 67 | DCHECK_NE(GetOwner(), GetTid()); |
Elliott Hughes | cf04431 | 2012-01-23 18:48:51 -0800 | [diff] [blame] | 68 | #endif |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 71 | pid_t GetOwner(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 72 | |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 73 | void AssertDepth(uint32_t depth) { |
| 74 | #if !defined(__APPLE__) |
| 75 | DCHECK_EQ(depth, GetDepth()); |
| 76 | #endif |
| 77 | } |
| 78 | |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 79 | private: |
| 80 | static pid_t GetTid(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 81 | |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 82 | uint32_t GetDepth(); |
| 83 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 84 | pthread_mutex_t mutex_; |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame^] | 85 | std::string name_; |
| 86 | MutexRank rank_; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(Mutex); |
| 89 | }; |
| 90 | |
| 91 | class MutexLock { |
| 92 | public: |
| 93 | explicit MutexLock(Mutex& mu) : mu_(mu) { |
| 94 | mu_.Lock(); |
| 95 | } |
| 96 | |
| 97 | ~MutexLock() { |
| 98 | mu_.Unlock(); |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | Mutex& mu_; |
| 103 | DISALLOW_COPY_AND_ASSIGN(MutexLock); |
| 104 | }; |
| 105 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 106 | class ConditionVariable { |
| 107 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 108 | explicit ConditionVariable(const std::string& name); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 109 | ~ConditionVariable(); |
| 110 | |
| 111 | void Broadcast(); |
| 112 | void Signal(); |
| 113 | void Wait(Mutex& mutex); |
| 114 | void TimedWait(Mutex& mutex, const timespec& ts); |
| 115 | |
| 116 | private: |
| 117 | pthread_cond_t cond_; |
| 118 | std::string name_; |
| 119 | DISALLOW_COPY_AND_ASSIGN(ConditionVariable); |
| 120 | }; |
| 121 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 122 | } // namespace art |
| 123 | |
| 124 | #endif // ART_SRC_MUTEX_H_ |