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