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 | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 71 | uint32_t GetDepth(); |
| 72 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 73 | std::string name_; |
| 74 | |
| 75 | pthread_mutex_t mutex_; |
| 76 | |
| 77 | DISALLOW_COPY_AND_ASSIGN(Mutex); |
| 78 | }; |
| 79 | |
| 80 | class MutexLock { |
| 81 | public: |
| 82 | explicit MutexLock(Mutex& mu) : mu_(mu) { |
| 83 | mu_.Lock(); |
| 84 | } |
| 85 | |
| 86 | ~MutexLock() { |
| 87 | mu_.Unlock(); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | Mutex& mu_; |
| 92 | DISALLOW_COPY_AND_ASSIGN(MutexLock); |
| 93 | }; |
| 94 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 95 | class ConditionVariable { |
| 96 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 97 | explicit ConditionVariable(const std::string& name); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 98 | ~ConditionVariable(); |
| 99 | |
| 100 | void Broadcast(); |
| 101 | void Signal(); |
| 102 | void Wait(Mutex& mutex); |
| 103 | void TimedWait(Mutex& mutex, const timespec& ts); |
| 104 | |
| 105 | private: |
| 106 | pthread_cond_t cond_; |
| 107 | std::string name_; |
| 108 | DISALLOW_COPY_AND_ASSIGN(ConditionVariable); |
| 109 | }; |
| 110 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 111 | } // namespace art |
| 112 | |
| 113 | #endif // ART_SRC_MUTEX_H_ |