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