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 | |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 38 | // This works on Mac OS 10.6 but hasn't been tested on older releases. |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 39 | struct __attribute__((__may_alias__)) darwin_pthread_mutex_t { |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 40 | long padding0; |
| 41 | int padding1; |
| 42 | uint32_t padding2; |
| 43 | int16_t padding3; |
| 44 | int16_t padding4; |
| 45 | uint32_t padding5; |
| 46 | pthread_t darwin_pthread_mutex_owner; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 47 | // ...other stuff we don't care about. |
| 48 | }; |
| 49 | |
| 50 | struct __attribute__((__may_alias__)) darwin_pthread_rwlock_t { |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 51 | long padding0; |
| 52 | pthread_mutex_t padding1; |
| 53 | int padding2; |
| 54 | pthread_cond_t padding3; |
| 55 | pthread_cond_t padding4; |
| 56 | int padding5; |
| 57 | int padding6; |
| 58 | pthread_t darwin_pthread_rwlock_owner; |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 59 | // ...other stuff we don't care about. |
| 60 | }; |
| 61 | |
| 62 | struct __attribute__((__may_alias__)) glibc_pthread_mutex_t { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 63 | int32_t padding0[2]; |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 64 | int owner; |
| 65 | // ...other stuff we don't care about. |
| 66 | }; |
| 67 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 68 | struct __attribute__((__may_alias__)) glibc_pthread_rwlock_t { |
| 69 | #ifdef __LP64__ |
| 70 | int32_t padding0[6]; |
| 71 | #else |
| 72 | int32_t padding0[7]; |
| 73 | #endif |
| 74 | int writer; |
| 75 | // ...other stuff we don't care about. |
| 76 | }; |
| 77 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 78 | ReaderWriterMutex* Locks::mutator_lock_ = NULL; |
| 79 | Mutex* Locks::thread_list_lock_ = NULL; |
| 80 | Mutex* Locks::classlinker_classes_lock_ = NULL; |
| 81 | ReaderWriterMutex* Locks::heap_bitmap_lock_ = NULL; |
| 82 | Mutex* Locks::abort_lock_ = NULL; |
| 83 | Mutex* Locks::logging_lock_ = NULL; |
| 84 | Mutex* Locks::unexpected_signal_lock_ = NULL; |
| 85 | Mutex* Locks::thread_suspend_count_lock_ = NULL; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 86 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 87 | void Locks::Init() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 88 | if (logging_lock_ != NULL) { |
| 89 | // Already initialized. |
| 90 | DCHECK(mutator_lock_ != NULL); |
| 91 | DCHECK(thread_list_lock_ != NULL); |
| 92 | DCHECK(classlinker_classes_lock_ != NULL); |
| 93 | DCHECK(heap_bitmap_lock_ != NULL); |
| 94 | DCHECK(abort_lock_ != NULL); |
| 95 | DCHECK(logging_lock_ != NULL); |
| 96 | DCHECK(unexpected_signal_lock_ != NULL); |
| 97 | DCHECK(thread_suspend_count_lock_ != NULL); |
| 98 | } else { |
| 99 | logging_lock_ = new Mutex("logging lock", kLoggingLock, true); |
| 100 | abort_lock_ = new Mutex("abort lock", kAbortLock, true); |
| 101 | DCHECK(mutator_lock_ == NULL); |
| 102 | mutator_lock_ = new ReaderWriterMutex("mutator lock", kMutatorLock); |
| 103 | DCHECK(thread_list_lock_ == NULL); |
| 104 | thread_list_lock_ = new Mutex("thread list lock", kThreadListLock); |
| 105 | DCHECK(classlinker_classes_lock_ == NULL); |
| 106 | classlinker_classes_lock_ = new Mutex("ClassLinker classes lock", kClassLinkerClassesLock); |
| 107 | DCHECK(heap_bitmap_lock_ == NULL); |
| 108 | heap_bitmap_lock_ = new ReaderWriterMutex("heap bitmap lock", kHeapBitmapLock); |
| 109 | DCHECK(unexpected_signal_lock_ == NULL); |
| 110 | unexpected_signal_lock_ = new Mutex("unexpected signal lock", kUnexpectedSignalLock, true); |
| 111 | DCHECK(thread_suspend_count_lock_ == NULL); |
| 112 | thread_suspend_count_lock_ = new Mutex("thread suspend count lock", kThreadSuspendCountLock); |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 113 | } |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 116 | BaseMutex::BaseMutex(const char* name, MutexLevel level) : level_(level), name_(name) {} |
| 117 | |
| 118 | static void CheckUnattachedThread(MutexLevel level) { |
| 119 | // The check below enumerates the cases where we expect not to be able to sanity check locks |
| 120 | // on a thread. TODO: tighten this check. |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 121 | if (kDebugLocking) { |
| 122 | Runtime* runtime = Runtime::Current(); |
| 123 | CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDown() || |
| 124 | level == kDefaultMutexLevel || level == kThreadListLock || |
| 125 | level == kLoggingLock || level == kAbortLock); |
| 126 | } |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 129 | void BaseMutex::RegisterAsLockedWithCurrentThread() { |
| 130 | Thread* self = Thread::Current(); |
| 131 | if (self == NULL) { |
| 132 | CheckUnattachedThread(level_); |
| 133 | return; |
| 134 | } |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 135 | if (kDebugLocking) { |
| 136 | // Check if a bad Mutex of this level or lower is held. |
| 137 | bool bad_mutexes_held = false; |
| 138 | for (int i = level_; i >= 0; --i) { |
| 139 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<MutexLevel>(i)); |
| 140 | if (UNLIKELY(held_mutex != NULL)) { |
| 141 | LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" (level " << i |
| 142 | << ") while locking \"" << name_ << "\" (level " << static_cast<int>(level_) << ")"; |
| 143 | if (i > kAbortLock) { |
| 144 | // Only abort in the check below if this is more than abort level lock. |
| 145 | bad_mutexes_held = true; |
| 146 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 149 | CHECK(!bad_mutexes_held); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 150 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 151 | // Don't record monitors as they are outside the scope of analysis. They may be inspected off of |
| 152 | // the monitor list. |
| 153 | if (level_ != kMonitorLock) { |
| 154 | self->SetHeldMutex(level_, this); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void BaseMutex::RegisterAsUnlockedWithCurrentThread() { |
| 159 | Thread* self = Thread::Current(); |
| 160 | if (self == NULL) { |
| 161 | CheckUnattachedThread(level_); |
| 162 | return; |
| 163 | } |
| 164 | if (level_ != kMonitorLock) { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 165 | if (kDebugLocking) { |
| 166 | CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_; |
| 167 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 168 | self->SetHeldMutex(level_, NULL); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void BaseMutex::CheckSafeToWait() { |
| 173 | Thread* self = Thread::Current(); |
| 174 | if (self == NULL) { |
| 175 | CheckUnattachedThread(level_); |
| 176 | return; |
| 177 | } |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 178 | if (kDebugLocking) { |
| 179 | CHECK(self->GetHeldMutex(level_) == this) << "Waiting on unacquired mutex: " << name_; |
| 180 | bool bad_mutexes_held = false; |
| 181 | for (int i = kMaxMutexLevel; i >= 0; --i) { |
| 182 | if (i != level_) { |
| 183 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<MutexLevel>(i)); |
| 184 | if (held_mutex != NULL) { |
| 185 | LOG(ERROR) << "Holding " << held_mutex->name_ << " (level " << i |
| 186 | << ") while performing wait on: " |
| 187 | << name_ << " (level " << static_cast<int>(level_) << ")"; |
| 188 | bad_mutexes_held = true; |
| 189 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 192 | CHECK(!bad_mutexes_held); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 193 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | Mutex::Mutex(const char* name, MutexLevel level, bool recursive) |
| 197 | : BaseMutex(name, level), recursive_(recursive), recursion_count_(0) { |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 198 | #if defined(__BIONIC__) || defined(__APPLE__) |
| 199 | // Use recursive mutexes for bionic and Apple otherwise the |
| 200 | // non-recursive mutexes don't have TIDs to check lock ownership of. |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 201 | pthread_mutexattr_t attributes; |
| 202 | CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes)); |
| 203 | CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE)); |
| 204 | CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes)); |
| 205 | CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 206 | #else |
| 207 | CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, NULL)); |
| 208 | #endif |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | Mutex::~Mutex() { |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 212 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 213 | // may still be using locks. |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 214 | int rc = pthread_mutex_destroy(&mutex_); |
| 215 | if (rc != 0) { |
| 216 | errno = rc; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 217 | // 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] | 218 | bool shutting_down = Runtime::Current()->IsShuttingDown(); |
| 219 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_; |
| 220 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 223 | void Mutex::ExclusiveLock() { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 224 | if (kDebugLocking && !recursive_) { |
| 225 | AssertNotHeld(); |
| 226 | } |
| 227 | if (!recursive_ || !IsExclusiveHeld()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 228 | CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_)); |
| 229 | RegisterAsLockedWithCurrentThread(); |
| 230 | } |
| 231 | recursion_count_++; |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 232 | if (kDebugLocking) { |
| 233 | CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: " |
| 234 | << name_ << " " << recursion_count_; |
| 235 | AssertHeld(); |
| 236 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 239 | bool Mutex::ExclusiveTryLock() { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 240 | if (kDebugLocking && !recursive_) { |
| 241 | AssertNotHeld(); |
| 242 | } |
| 243 | if (!recursive_ || !IsExclusiveHeld()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 244 | int result = pthread_mutex_trylock(&mutex_); |
| 245 | if (result == EBUSY) { |
| 246 | return false; |
| 247 | } |
| 248 | if (result != 0) { |
| 249 | errno = result; |
| 250 | PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_; |
| 251 | } |
| 252 | RegisterAsLockedWithCurrentThread(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 253 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 254 | recursion_count_++; |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 255 | if (kDebugLocking) { |
| 256 | CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: " |
| 257 | << name_ << " " << recursion_count_; |
| 258 | AssertHeld(); |
| 259 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 260 | return true; |
| 261 | } |
| 262 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 263 | void Mutex::ExclusiveUnlock() { |
Ian Rogers | 105245c | 2012-01-27 11:42:43 -0800 | [diff] [blame] | 264 | AssertHeld(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 265 | recursion_count_--; |
| 266 | if (!recursive_ || recursion_count_ == 0) { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 267 | if (kDebugLocking) { |
| 268 | CHECK(recursion_count_ == 0 || recursive_) << "Unexpected recursion count on mutex: " |
| 269 | << name_ << " " << recursion_count_; |
| 270 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 271 | RegisterAsUnlockedWithCurrentThread(); |
| 272 | CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_)); |
| 273 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 276 | bool Mutex::IsExclusiveHeld() const { |
| 277 | Thread* self = Thread::Current(); |
| 278 | bool result; |
| 279 | if (self == NULL || level_ == kMonitorLock) { // Handle unattached threads and monitors. |
| 280 | result = (GetExclusiveOwnerTid() == static_cast<uint64_t>(GetTid())); |
| 281 | } else { |
| 282 | result = (self->GetHeldMutex(level_) == this); |
| 283 | // Sanity debug check that if we think it is locked, so does the pthread. |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 284 | if (kDebugLocking) { |
| 285 | CHECK(result == (GetExclusiveOwnerTid() == static_cast<uint64_t>(GetTid()))); |
| 286 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 287 | } |
| 288 | return result; |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 291 | uint64_t Mutex::GetExclusiveOwnerTid() const { |
Elliott Hughes | 3147a23 | 2011-10-12 15:55:07 -0700 | [diff] [blame] | 292 | #if defined(__BIONIC__) |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 293 | return static_cast<uint64_t>((mutex_.value >> 16) & 0xffff); |
Elliott Hughes | 3147a23 | 2011-10-12 15:55:07 -0700 | [diff] [blame] | 294 | #elif defined(__GLIBC__) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 295 | return reinterpret_cast<const glibc_pthread_mutex_t*>(&mutex_)->owner; |
Elliott Hughes | cf04431 | 2012-01-23 18:48:51 -0800 | [diff] [blame] | 296 | #elif defined(__APPLE__) |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 297 | const darwin_pthread_mutex_t* dpmutex = reinterpret_cast<const darwin_pthread_mutex_t*>(&mutex_); |
| 298 | pthread_t owner = dpmutex->darwin_pthread_mutex_owner; |
Brian Carlstrom | bd93c30 | 2012-08-27 16:58:28 -0700 | [diff] [blame] | 299 | // 0 for unowned, -1 for PTHREAD_MTX_TID_SWITCHING |
| 300 | // TODO: should we make darwin_pthread_mutex_owner volatile and recheck until not -1? |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 301 | if ((owner == (pthread_t)0) || (owner == (pthread_t)-1)) { |
| 302 | return 0; |
| 303 | } |
| 304 | uint64_t tid; |
| 305 | CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6 |
| 306 | return tid; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 307 | #else |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 308 | #error unsupported C library |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 309 | #endif |
| 310 | } |
| 311 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 312 | ReaderWriterMutex::ReaderWriterMutex(const char* name, MutexLevel level) : BaseMutex(name, level) { |
| 313 | CHECK_MUTEX_CALL(pthread_rwlock_init, (&rwlock_, NULL)); |
| 314 | } |
| 315 | |
| 316 | ReaderWriterMutex::~ReaderWriterMutex() { |
| 317 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 318 | // may still be using locks. |
| 319 | int rc = pthread_rwlock_destroy(&rwlock_); |
| 320 | if (rc != 0) { |
| 321 | errno = rc; |
| 322 | // TODO: should we just not log at all if shutting down? this could be the logging mutex! |
| 323 | bool shutting_down = Runtime::Current()->IsShuttingDown(); |
| 324 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_; |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 325 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | void ReaderWriterMutex::ExclusiveLock() { |
| 329 | AssertNotExclusiveHeld(); |
| 330 | CHECK_MUTEX_CALL(pthread_rwlock_wrlock, (&rwlock_)); |
| 331 | RegisterAsLockedWithCurrentThread(); |
| 332 | AssertExclusiveHeld(); |
| 333 | } |
| 334 | |
| 335 | void ReaderWriterMutex::ExclusiveUnlock() { |
| 336 | AssertExclusiveHeld(); |
| 337 | RegisterAsUnlockedWithCurrentThread(); |
| 338 | CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_)); |
| 339 | } |
| 340 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 341 | #if HAVE_TIMED_RWLOCK |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 342 | bool ReaderWriterMutex::ExclusiveLockWithTimeout(const timespec& abs_timeout) { |
| 343 | int result = pthread_rwlock_timedwrlock(&rwlock_, &abs_timeout); |
| 344 | if (result == ETIMEDOUT) { |
| 345 | return false; |
| 346 | } |
| 347 | if (result != 0) { |
| 348 | errno = result; |
Ian Rogers | a5acfd3 | 2012-08-15 11:50:10 -0700 | [diff] [blame] | 349 | PLOG(FATAL) << "pthread_rwlock_timedwrlock failed for " << name_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 350 | } |
| 351 | RegisterAsLockedWithCurrentThread(); |
| 352 | AssertSharedHeld(); |
| 353 | return true; |
| 354 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 355 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 356 | |
| 357 | void ReaderWriterMutex::SharedLock() { |
| 358 | CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_)); |
| 359 | RegisterAsLockedWithCurrentThread(); |
| 360 | AssertSharedHeld(); |
| 361 | } |
| 362 | |
| 363 | bool ReaderWriterMutex::SharedTryLock() { |
| 364 | int result = pthread_rwlock_tryrdlock(&rwlock_); |
| 365 | if (result == EBUSY) { |
| 366 | return false; |
| 367 | } |
| 368 | if (result != 0) { |
| 369 | errno = result; |
| 370 | PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_; |
| 371 | } |
| 372 | RegisterAsLockedWithCurrentThread(); |
| 373 | AssertSharedHeld(); |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | void ReaderWriterMutex::SharedUnlock() { |
| 378 | AssertSharedHeld(); |
| 379 | RegisterAsUnlockedWithCurrentThread(); |
| 380 | CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_)); |
| 381 | } |
| 382 | |
| 383 | bool ReaderWriterMutex::IsExclusiveHeld() const { |
| 384 | bool result = (GetExclusiveOwnerTid() == static_cast<uint64_t>(GetTid())); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame^] | 385 | if (kDebugLocking) { |
| 386 | // Sanity that if the pthread thinks we own the lock the Thread agrees. |
| 387 | Thread* self = Thread::Current(); |
| 388 | CHECK((self == NULL) || !result || (self->GetHeldMutex(level_) == this)); |
| 389 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 390 | return result; |
| 391 | } |
| 392 | |
| 393 | bool ReaderWriterMutex::IsSharedHeld() const { |
| 394 | Thread* self = Thread::Current(); |
| 395 | bool result; |
| 396 | if (UNLIKELY(self == NULL)) { // Handle unattached threads. |
| 397 | result = IsExclusiveHeld(); // TODO: a better best effort here. |
| 398 | } else { |
| 399 | result = (self->GetHeldMutex(level_) == this); |
| 400 | } |
| 401 | return result; |
| 402 | } |
| 403 | |
| 404 | uint64_t ReaderWriterMutex::GetExclusiveOwnerTid() const { |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 405 | #if defined(__BIONIC__) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 406 | return rwlock_.writerThreadId; |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 407 | #elif defined(__GLIBC__) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 408 | return reinterpret_cast<const glibc_pthread_rwlock_t*>(&rwlock_)->writer; |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 409 | #elif defined(__APPLE__) |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 410 | const darwin_pthread_rwlock_t* dprwlock = reinterpret_cast<const darwin_pthread_rwlock_t*>(&rwlock_); |
| 411 | pthread_t owner = dprwlock->darwin_pthread_rwlock_owner; |
| 412 | if (owner == (pthread_t)0) { |
| 413 | return 0; |
| 414 | } |
| 415 | uint64_t tid; |
| 416 | CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6 |
| 417 | return tid; |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 418 | #else |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 419 | #error unsupported C library |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 420 | #endif |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 423 | ConditionVariable::ConditionVariable(const std::string& name) : name_(name) { |
| 424 | CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL)); |
| 425 | } |
| 426 | |
| 427 | ConditionVariable::~ConditionVariable() { |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 428 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 429 | // may still be using condition variables. |
| 430 | int rc = pthread_cond_destroy(&cond_); |
| 431 | if (rc != 0) { |
| 432 | errno = rc; |
| 433 | bool shutting_down = Runtime::Current()->IsShuttingDown(); |
| 434 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_cond_destroy failed for " << name_; |
| 435 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | void ConditionVariable::Broadcast() { |
| 439 | CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_)); |
| 440 | } |
| 441 | |
| 442 | void ConditionVariable::Signal() { |
| 443 | CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_)); |
| 444 | } |
| 445 | |
| 446 | void ConditionVariable::Wait(Mutex& mutex) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 447 | mutex.CheckSafeToWait(); |
| 448 | unsigned int old_recursion_count = mutex.recursion_count_; |
| 449 | mutex.recursion_count_ = 0; |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 450 | CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, &mutex.mutex_)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 451 | mutex.recursion_count_ = old_recursion_count; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) { |
| 455 | #ifdef HAVE_TIMEDWAIT_MONOTONIC |
| 456 | #define TIMEDWAIT pthread_cond_timedwait_monotonic |
| 457 | #else |
| 458 | #define TIMEDWAIT pthread_cond_timedwait |
| 459 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 460 | mutex.CheckSafeToWait(); |
| 461 | unsigned int old_recursion_count = mutex.recursion_count_; |
| 462 | mutex.recursion_count_ = 0; |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 463 | int rc = TIMEDWAIT(&cond_, &mutex.mutex_, &ts); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 464 | mutex.recursion_count_ = old_recursion_count; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 465 | if (rc != 0 && rc != ETIMEDOUT) { |
| 466 | errno = rc; |
| 467 | PLOG(FATAL) << "TimedWait failed for " << name_; |
| 468 | } |
| 469 | } |
| 470 | |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 471 | } // namespace art |