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