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