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