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