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