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 | #include "mutex.h" |
| 18 | |
| 19 | #include <errno.h> |
| 20 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 21 | #include "logging.h" |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 22 | #include "runtime.h" |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 23 | #include "thread.h" |
| 24 | #include "utils.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 26 | #define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_) |
| 27 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame^] | 30 | static inline void CheckSafeToLockOrUnlock(MutexRank rank, bool is_locking) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 31 | #ifndef NDEBUG |
| 32 | if (rank == -1) { |
| 33 | return; |
| 34 | } |
| 35 | Thread* self = Thread::Current(); |
| 36 | if (self != NULL) { |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame^] | 37 | self->CheckSafeToLockOrUnlock(rank, is_locking); |
| 38 | } |
| 39 | #endif |
| 40 | } |
| 41 | |
| 42 | static inline void CheckSafeToWait(MutexRank rank) { |
| 43 | #ifndef NDEBUG |
| 44 | Thread* self = Thread::Current(); |
| 45 | if (self != NULL) { |
| 46 | self->CheckSafeToWait(rank); |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 47 | } |
| 48 | #endif |
| 49 | } |
| 50 | |
| 51 | Mutex::Mutex(const char* name, MutexRank rank) : name_(name), rank_(rank) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 52 | // Like Java, we use recursive mutexes. |
| 53 | pthread_mutexattr_t attributes; |
| 54 | CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes)); |
| 55 | CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE)); |
| 56 | CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes)); |
| 57 | CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | Mutex::~Mutex() { |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 61 | int rc = pthread_mutex_destroy(&mutex_); |
| 62 | if (rc != 0) { |
| 63 | errno = rc; |
| 64 | bool shutting_down = Runtime::Current()->IsShuttingDown(); |
| 65 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_; |
| 66 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void Mutex::Lock() { |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame^] | 70 | CheckSafeToLockOrUnlock(rank_, true); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 71 | CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_)); |
Ian Rogers | 105245c | 2012-01-27 11:42:43 -0800 | [diff] [blame] | 72 | AssertHeld(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool Mutex::TryLock() { |
| 76 | int result = pthread_mutex_trylock(&mutex_); |
| 77 | if (result == EBUSY) { |
| 78 | return false; |
| 79 | } |
| 80 | if (result != 0) { |
| 81 | errno = result; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 82 | PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 83 | } |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame^] | 84 | CheckSafeToLockOrUnlock(rank_, true); |
Ian Rogers | 105245c | 2012-01-27 11:42:43 -0800 | [diff] [blame] | 85 | AssertHeld(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 86 | return true; |
| 87 | } |
| 88 | |
| 89 | void Mutex::Unlock() { |
Ian Rogers | 105245c | 2012-01-27 11:42:43 -0800 | [diff] [blame] | 90 | AssertHeld(); |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame^] | 91 | CheckSafeToLockOrUnlock(rank_, false); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 92 | CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | pid_t Mutex::GetOwner() { |
Elliott Hughes | 3147a23 | 2011-10-12 15:55:07 -0700 | [diff] [blame] | 96 | #if defined(__BIONIC__) |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 97 | return static_cast<pid_t>((mutex_.value >> 16) & 0xffff); |
Elliott Hughes | 3147a23 | 2011-10-12 15:55:07 -0700 | [diff] [blame] | 98 | #elif defined(__GLIBC__) |
| 99 | struct __attribute__((__may_alias__)) glibc_pthread_t { |
| 100 | int lock; |
| 101 | unsigned int count; |
| 102 | int owner; |
| 103 | // ...other stuff we don't care about. |
| 104 | }; |
| 105 | return reinterpret_cast<glibc_pthread_t*>(&mutex_)->owner; |
Elliott Hughes | cf04431 | 2012-01-23 18:48:51 -0800 | [diff] [blame] | 106 | #elif defined(__APPLE__) |
| 107 | // We don't know a way to implement this for Mac OS. |
| 108 | return 0; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 109 | #else |
| 110 | UNIMPLEMENTED(FATAL); |
| 111 | return 0; |
| 112 | #endif |
| 113 | } |
| 114 | |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 115 | uint32_t Mutex::GetDepth() { |
| 116 | bool held = (GetOwner() == GetTid()); |
| 117 | if (!held) { |
| 118 | return 0; |
| 119 | } |
| 120 | uint32_t depth; |
| 121 | #if defined(__BIONIC__) |
| 122 | depth = static_cast<uint32_t>((mutex_.value >> 2) & 0x7ff) + 1; |
| 123 | #elif defined(__GLIBC__) |
| 124 | struct __attribute__((__may_alias__)) glibc_pthread_t { |
| 125 | int lock; |
| 126 | unsigned int count; |
| 127 | int owner; |
| 128 | // ...other stuff we don't care about. |
| 129 | }; |
| 130 | depth = reinterpret_cast<glibc_pthread_t*>(&mutex_)->count; |
| 131 | #elif defined(__APPLE__) |
| 132 | // We don't know a way to implement this for Mac OS. |
| 133 | return 0; |
| 134 | #else |
| 135 | UNIMPLEMENTED(FATAL); |
| 136 | return 0; |
| 137 | #endif |
| 138 | CHECK_NE(0U, depth) << "owner=" << GetOwner() << " tid=" << GetTid(); |
| 139 | return depth; |
| 140 | } |
| 141 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 142 | pid_t Mutex::GetTid() { |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 143 | return ::art::GetTid(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 146 | ConditionVariable::ConditionVariable(const std::string& name) : name_(name) { |
| 147 | CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL)); |
| 148 | } |
| 149 | |
| 150 | ConditionVariable::~ConditionVariable() { |
| 151 | CHECK_MUTEX_CALL(pthread_cond_destroy, (&cond_)); |
| 152 | } |
| 153 | |
| 154 | void ConditionVariable::Broadcast() { |
| 155 | CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_)); |
| 156 | } |
| 157 | |
| 158 | void ConditionVariable::Signal() { |
| 159 | CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_)); |
| 160 | } |
| 161 | |
| 162 | void ConditionVariable::Wait(Mutex& mutex) { |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame^] | 163 | CheckSafeToWait(mutex.GetRank()); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 164 | CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, mutex.GetImpl())); |
| 165 | } |
| 166 | |
| 167 | void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) { |
| 168 | #ifdef HAVE_TIMEDWAIT_MONOTONIC |
| 169 | #define TIMEDWAIT pthread_cond_timedwait_monotonic |
| 170 | #else |
| 171 | #define TIMEDWAIT pthread_cond_timedwait |
| 172 | #endif |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame^] | 173 | CheckSafeToWait(mutex.GetRank()); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 174 | int rc = TIMEDWAIT(&cond_, mutex.GetImpl(), &ts); |
| 175 | if (rc != 0 && rc != ETIMEDOUT) { |
| 176 | errno = rc; |
| 177 | PLOG(FATAL) << "TimedWait failed for " << name_; |
| 178 | } |
| 179 | } |
| 180 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 181 | std::ostream& operator<<(std::ostream& os, const MutexRank& rhs) { |
| 182 | switch (rhs) { |
| 183 | case kHeapLock: os << "HeapLock"; break; |
| 184 | case kThreadListLock: os << "ThreadListLock"; break; |
| 185 | case kThreadSuspendCountLock: os << "ThreadSuspendCountLock"; break; |
| 186 | default: os << "MutexRank[" << static_cast<int>(rhs) << "]"; break; |
| 187 | } |
| 188 | return os; |
| 189 | } |
| 190 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 191 | } // namespace |