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 | b08e8a3 | 2012-04-02 10:51:41 -0700 | [diff] [blame] | 26 | #if defined(__APPLE__) |
| 27 | #include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED |
| 28 | #endif |
| 29 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 30 | #define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_) |
| 31 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 32 | extern int pthread_mutex_lock(pthread_mutex_t* mutex) EXCLUSIVE_LOCK_FUNCTION(mutex); |
| 33 | extern int pthread_mutex_unlock(pthread_mutex_t* mutex) UNLOCK_FUNCTION(1); |
| 34 | extern int pthread_mutex_trylock(pthread_mutex_t* mutex) EXCLUSIVE_TRYLOCK_FUNCTION(0, mutex); |
| 35 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 36 | namespace art { |
| 37 | |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 38 | // This works on Mac OS 10.7, but hasn't been tested on older releases. |
| 39 | struct __attribute__((__may_alias__)) darwin_pthread_mutex_t { |
| 40 | uint32_t padding0[2]; |
| 41 | uint32_t value; |
| 42 | uint32_t padding1[5]; |
| 43 | uint64_t owner_tid; |
| 44 | // ...other stuff we don't care about. |
| 45 | }; |
| 46 | |
| 47 | struct __attribute__((__may_alias__)) glibc_pthread_mutex_t { |
| 48 | int lock; |
| 49 | unsigned int count; |
| 50 | int owner; |
| 51 | // ...other stuff we don't care about. |
| 52 | }; |
| 53 | |
Elliott Hughes | 76e3694 | 2012-03-16 13:44:56 -0700 | [diff] [blame] | 54 | static inline void CheckSafeToLockOrUnlock(MutexRank rank, bool is_locking) { |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 55 | if (!kIsDebugBuild) { |
| 56 | return; |
| 57 | } |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 58 | if (rank == -1) { |
| 59 | return; |
| 60 | } |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 61 | Thread::Current()->CheckSafeToLockOrUnlock(rank, is_locking); |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Elliott Hughes | 76e3694 | 2012-03-16 13:44:56 -0700 | [diff] [blame] | 64 | static inline void CheckSafeToWait(MutexRank rank) { |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 65 | if (!kIsDebugBuild) { |
| 66 | return; |
| 67 | } |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 68 | Thread::Current()->CheckSafeToWait(rank); |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | Mutex::Mutex(const char* name, MutexRank rank) : name_(name), rank_(rank) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 72 | // Like Java, we use recursive mutexes. |
| 73 | pthread_mutexattr_t attributes; |
| 74 | CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes)); |
| 75 | CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE)); |
| 76 | CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes)); |
| 77 | CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | Mutex::~Mutex() { |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 81 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 82 | // may still be using locks. |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 83 | int rc = pthread_mutex_destroy(&mutex_); |
| 84 | if (rc != 0) { |
| 85 | errno = rc; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 86 | // TODO: should we just not log at all if shutting down? this could be the logging mutex! |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 87 | bool shutting_down = Runtime::Current()->IsShuttingDown(); |
| 88 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_; |
| 89 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void Mutex::Lock() { |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame] | 93 | CheckSafeToLockOrUnlock(rank_, true); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 94 | CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_)); |
Ian Rogers | 105245c | 2012-01-27 11:42:43 -0800 | [diff] [blame] | 95 | AssertHeld(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | bool Mutex::TryLock() { |
| 99 | int result = pthread_mutex_trylock(&mutex_); |
| 100 | if (result == EBUSY) { |
| 101 | return false; |
| 102 | } |
| 103 | if (result != 0) { |
| 104 | errno = result; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 105 | PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 106 | } |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame] | 107 | CheckSafeToLockOrUnlock(rank_, true); |
Ian Rogers | 105245c | 2012-01-27 11:42:43 -0800 | [diff] [blame] | 108 | AssertHeld(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
| 112 | void Mutex::Unlock() { |
Ian Rogers | 105245c | 2012-01-27 11:42:43 -0800 | [diff] [blame] | 113 | AssertHeld(); |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame] | 114 | CheckSafeToLockOrUnlock(rank_, false); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 115 | CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 118 | #if !defined(NDEBUG) |
Elliott Hughes | 5d6d5dc | 2012-03-29 11:59:27 -0700 | [diff] [blame] | 119 | #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1060 |
| 120 | // Mac OS 10.5 didn't have anything we could implement GetTid() with. One thing we could try would |
| 121 | // be using pthread_t instead of the actual tid; this would be acceptable in most places, and more |
| 122 | // portable. 10.5 is already obsolete, though, so doing so would probably be all pain for no gain. |
| 123 | void Mutex::AssertHeld() {} |
| 124 | void Mutex::AssertNotHeld() {} |
| 125 | #else |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 126 | void Mutex::AssertHeld() { |
| 127 | DCHECK_EQ(GetOwner(), static_cast<uint64_t>(GetTid())); |
| 128 | } |
| 129 | |
| 130 | void Mutex::AssertNotHeld() { |
| 131 | DCHECK_NE(GetOwner(), static_cast<uint64_t>(GetTid())); |
| 132 | } |
| 133 | #endif |
Elliott Hughes | 5d6d5dc | 2012-03-29 11:59:27 -0700 | [diff] [blame] | 134 | #endif |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 135 | |
| 136 | uint64_t Mutex::GetOwner() { |
Elliott Hughes | 3147a23 | 2011-10-12 15:55:07 -0700 | [diff] [blame] | 137 | #if defined(__BIONIC__) |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 138 | return static_cast<uint64_t>((mutex_.value >> 16) & 0xffff); |
Elliott Hughes | 3147a23 | 2011-10-12 15:55:07 -0700 | [diff] [blame] | 139 | #elif defined(__GLIBC__) |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 140 | return reinterpret_cast<glibc_pthread_mutex_t*>(&mutex_)->owner; |
Elliott Hughes | cf04431 | 2012-01-23 18:48:51 -0800 | [diff] [blame] | 141 | #elif defined(__APPLE__) |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 142 | return reinterpret_cast<darwin_pthread_mutex_t*>(&mutex_)->owner_tid; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 143 | #else |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 144 | #error unsupported C library |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 145 | #endif |
| 146 | } |
| 147 | |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 148 | uint32_t Mutex::GetDepth() { |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 149 | bool held = (GetOwner() == static_cast<uint64_t>(GetTid())); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 150 | if (!held) { |
| 151 | return 0; |
| 152 | } |
| 153 | uint32_t depth; |
| 154 | #if defined(__BIONIC__) |
| 155 | depth = static_cast<uint32_t>((mutex_.value >> 2) & 0x7ff) + 1; |
| 156 | #elif defined(__GLIBC__) |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 157 | depth = reinterpret_cast<glibc_pthread_mutex_t*>(&mutex_)->count; |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 158 | #elif defined(__APPLE__) |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 159 | darwin_pthread_mutex_t* darwin_mutex = reinterpret_cast<darwin_pthread_mutex_t*>(&mutex_); |
| 160 | depth = ((darwin_mutex->value >> 16) & 0xffff); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 161 | #else |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 162 | #error unsupported C library |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 163 | #endif |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 164 | CHECK_NE(depth, 0U) << "owner=" << GetOwner() << " tid=" << GetTid(); |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 165 | return depth; |
| 166 | } |
| 167 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 168 | ConditionVariable::ConditionVariable(const std::string& name) : name_(name) { |
| 169 | CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL)); |
| 170 | } |
| 171 | |
| 172 | ConditionVariable::~ConditionVariable() { |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 173 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 174 | // may still be using condition variables. |
| 175 | int rc = pthread_cond_destroy(&cond_); |
| 176 | if (rc != 0) { |
| 177 | errno = rc; |
| 178 | bool shutting_down = Runtime::Current()->IsShuttingDown(); |
| 179 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_cond_destroy failed for " << name_; |
| 180 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void ConditionVariable::Broadcast() { |
| 184 | CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_)); |
| 185 | } |
| 186 | |
| 187 | void ConditionVariable::Signal() { |
| 188 | CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_)); |
| 189 | } |
| 190 | |
| 191 | void ConditionVariable::Wait(Mutex& mutex) { |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 192 | CheckSafeToWait(mutex.rank_); |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 193 | uint unlock_depth = UnlockBeforeWait(mutex); |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 194 | CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, &mutex.mutex_)); |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 195 | RelockAfterWait(mutex, unlock_depth); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) { |
| 199 | #ifdef HAVE_TIMEDWAIT_MONOTONIC |
| 200 | #define TIMEDWAIT pthread_cond_timedwait_monotonic |
| 201 | #else |
| 202 | #define TIMEDWAIT pthread_cond_timedwait |
| 203 | #endif |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 204 | CheckSafeToWait(mutex.rank_); |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 205 | uint unlock_depth = UnlockBeforeWait(mutex); |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 206 | int rc = TIMEDWAIT(&cond_, &mutex.mutex_, &ts); |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 207 | RelockAfterWait(mutex, unlock_depth); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 208 | if (rc != 0 && rc != ETIMEDOUT) { |
| 209 | errno = rc; |
| 210 | PLOG(FATAL) << "TimedWait failed for " << name_; |
| 211 | } |
| 212 | } |
| 213 | |
Brian Carlstrom | 92c9a35 | 2012-06-21 18:21:59 -0700 | [diff] [blame] | 214 | // Unlock a mutex down to depth == 1 so pthread conditional waiting can be used. |
| 215 | // After waiting, use RelockAfterWait to restore the lock depth. |
| 216 | uint32_t ConditionVariable::UnlockBeforeWait(Mutex& mutex) { |
| 217 | uint32_t unlock_count = 0; |
| 218 | CHECK_GT(mutex.GetDepth(), 0U); |
| 219 | while (mutex.GetDepth() != 1) { |
| 220 | mutex.Unlock(); |
| 221 | unlock_count++; |
| 222 | } |
| 223 | return unlock_count; |
| 224 | } |
| 225 | |
| 226 | void ConditionVariable::RelockAfterWait(Mutex& mutex, uint32_t unlock_count) { |
| 227 | for (uint32_t i = 0; i < unlock_count; i++) { |
| 228 | mutex.Lock(); |
| 229 | } |
| 230 | } |
| 231 | |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 232 | } // namespace art |