| 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> |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 20 | #include <sys/time.h> |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 21 | |
| Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 22 | #include "base/logging.h" |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 23 | #include "cutils/atomic.h" |
| Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 24 | #include "runtime.h" |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change.h" |
| Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 26 | #include "thread.h" |
| 27 | #include "utils.h" |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 28 | |
| Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 29 | #define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_) |
| 30 | |
| Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 31 | extern int pthread_mutex_lock(pthread_mutex_t* mutex) EXCLUSIVE_LOCK_FUNCTION(mutex); |
| 32 | extern int pthread_mutex_unlock(pthread_mutex_t* mutex) UNLOCK_FUNCTION(1); |
| 33 | extern int pthread_mutex_trylock(pthread_mutex_t* mutex) EXCLUSIVE_TRYLOCK_FUNCTION(0, mutex); |
| 34 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 35 | #if ART_USE_FUTEXES |
| Ian Rogers | acc46d6 | 2012-09-27 21:39:40 -0700 | [diff] [blame] | 36 | #include "linux/futex.h" |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 37 | #include "sys/syscall.h" |
| 38 | #ifndef SYS_futex |
| 39 | #define SYS_futex __NR_futex |
| 40 | #endif |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 41 | int futex(volatile int *uaddr, int op, int val, const struct timespec *timeout, volatile int *uaddr2, int val3) { |
| 42 | return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 43 | } |
| 44 | #endif // ART_USE_FUTEXES |
| 45 | |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 46 | namespace art { |
| 47 | |
| Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 48 | // 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] | 49 | struct __attribute__((__may_alias__)) darwin_pthread_mutex_t { |
| Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 50 | long padding0; |
| 51 | int padding1; |
| 52 | uint32_t padding2; |
| 53 | int16_t padding3; |
| 54 | int16_t padding4; |
| 55 | uint32_t padding5; |
| 56 | pthread_t darwin_pthread_mutex_owner; |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 57 | // ...other stuff we don't care about. |
| 58 | }; |
| 59 | |
| 60 | struct __attribute__((__may_alias__)) darwin_pthread_rwlock_t { |
| Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 61 | long padding0; |
| 62 | pthread_mutex_t padding1; |
| 63 | int padding2; |
| 64 | pthread_cond_t padding3; |
| 65 | pthread_cond_t padding4; |
| 66 | int padding5; |
| 67 | int padding6; |
| 68 | pthread_t darwin_pthread_rwlock_owner; |
| Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 69 | // ...other stuff we don't care about. |
| 70 | }; |
| 71 | |
| 72 | struct __attribute__((__may_alias__)) glibc_pthread_mutex_t { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 73 | int32_t padding0[2]; |
| Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 74 | int owner; |
| 75 | // ...other stuff we don't care about. |
| 76 | }; |
| 77 | |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 78 | struct __attribute__((__may_alias__)) glibc_pthread_rwlock_t { |
| 79 | #ifdef __LP64__ |
| 80 | int32_t padding0[6]; |
| 81 | #else |
| 82 | int32_t padding0[7]; |
| 83 | #endif |
| 84 | int writer; |
| 85 | // ...other stuff we don't care about. |
| 86 | }; |
| 87 | |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 88 | static uint64_t SafeGetTid(const Thread* self) { |
| 89 | if (self != NULL) { |
| 90 | return static_cast<uint64_t>(self->GetTid()); |
| 91 | } else { |
| 92 | return static_cast<uint64_t>(GetTid()); |
| 93 | } |
| 94 | } |
| 95 | |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 96 | #if ART_USE_FUTEXES |
| 97 | static bool ComputeRelativeTimeSpec(timespec* result_ts, const timespec& lhs, const timespec& rhs) { |
| 98 | const long int one_sec = 1000 * 1000 * 1000; // one second in nanoseconds. |
| 99 | result_ts->tv_sec = lhs.tv_sec - rhs.tv_sec; |
| 100 | result_ts->tv_nsec = lhs.tv_nsec - rhs.tv_nsec; |
| 101 | if (result_ts->tv_nsec < 0) { |
| 102 | result_ts->tv_sec--; |
| 103 | result_ts->tv_nsec += one_sec; |
| 104 | } else if (result_ts->tv_nsec > one_sec) { |
| 105 | result_ts->tv_sec++; |
| 106 | result_ts->tv_nsec -= one_sec; |
| 107 | } |
| 108 | return result_ts->tv_sec < 0; |
| 109 | } |
| 110 | #endif |
| 111 | |
| Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 112 | #if CONTENTION_LOGGING |
| 113 | // A guard for all_mutexes_ that's not a mutex (Mutexes must CAS to acquire and busy wait). |
| 114 | static AtomicInteger all_mutexes_guard_; |
| 115 | // All created mutexes guarded by all_mutexes_guard_. |
| 116 | std::set<BaseMutex*>* all_mutexes_; |
| 117 | |
| 118 | class ScopedAllMutexesLock { |
| 119 | public: |
| 120 | ScopedAllMutexesLock(const BaseMutex* mutex) : mutex_(mutex) { |
| 121 | while (!all_mutexes_guard_.CompareAndSwap(0, reinterpret_cast<int32_t>(mutex))) { |
| 122 | NanoSleep(100); |
| 123 | } |
| 124 | } |
| 125 | ~ScopedAllMutexesLock() { |
| 126 | while (!all_mutexes_guard_.CompareAndSwap(reinterpret_cast<int32_t>(mutex_), 0)) { |
| 127 | NanoSleep(100); |
| 128 | } |
| 129 | } |
| 130 | private: |
| 131 | const BaseMutex* const mutex_; |
| 132 | }; |
| 133 | #endif |
| 134 | |
| 135 | BaseMutex::BaseMutex(const char* name, LockLevel level) : level_(level), name_(name) { |
| 136 | #if CONTENTION_LOGGING |
| 137 | ScopedAllMutexesLock mu(this); |
| 138 | if (all_mutexes_ == NULL) { |
| 139 | // We leak the global set of all mutexes to avoid ordering issues in global variable |
| 140 | // construction/destruction. |
| 141 | all_mutexes_ = new std::set<BaseMutex*>(); |
| 142 | } |
| 143 | all_mutexes_->insert(this); |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | BaseMutex::~BaseMutex() { |
| 148 | #if CONTENTION_LOGGING |
| 149 | ScopedAllMutexesLock mu(this); |
| 150 | all_mutexes_->erase(this); |
| 151 | #endif |
| 152 | } |
| 153 | |
| 154 | void BaseMutex::DumpAll(std::ostream& os) { |
| 155 | #if CONTENTION_LOGGING |
| 156 | os << "Mutex logging:\n"; |
| 157 | ScopedAllMutexesLock mu(reinterpret_cast<const BaseMutex*>(-1)); |
| 158 | typedef std::set<BaseMutex*>::const_iterator It; |
| 159 | for (It it = all_mutexes_->begin(); it != all_mutexes_->end(); ++it) { |
| 160 | BaseMutex* mutex = *it; |
| 161 | mutex->Dump(os); |
| 162 | os << "\n"; |
| 163 | } |
| 164 | #endif |
| 165 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 166 | |
| Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 167 | static void CheckUnattachedThread(LockLevel level) NO_THREAD_SAFETY_ANALYSIS { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 168 | // The check below enumerates the cases where we expect not to be able to sanity check locks |
| Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 169 | // on a thread. Lock checking is disabled to avoid deadlock when checking shutdown lock. |
| 170 | // TODO: tighten this check. |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 171 | if (kDebugLocking) { |
| 172 | Runtime* runtime = Runtime::Current(); |
| 173 | CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDown() || |
| Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 174 | level == kDefaultMutexLevel || level == kRuntimeShutdownLock || |
| 175 | level == kThreadListLock || level == kLoggingLock || level == kAbortLock); |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 176 | } |
| Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 179 | void BaseMutex::RegisterAsLocked(Thread* self) { |
| 180 | if (UNLIKELY(self == NULL)) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 181 | CheckUnattachedThread(level_); |
| 182 | return; |
| 183 | } |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 184 | if (kDebugLocking) { |
| 185 | // Check if a bad Mutex of this level or lower is held. |
| 186 | bool bad_mutexes_held = false; |
| 187 | for (int i = level_; i >= 0; --i) { |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 188 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i)); |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 189 | if (UNLIKELY(held_mutex != NULL)) { |
| 190 | LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" (level " << i |
| 191 | << ") while locking \"" << name_ << "\" (level " << static_cast<int>(level_) << ")"; |
| 192 | if (i > kAbortLock) { |
| 193 | // Only abort in the check below if this is more than abort level lock. |
| 194 | bad_mutexes_held = true; |
| 195 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 198 | CHECK(!bad_mutexes_held); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 199 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 200 | // Don't record monitors as they are outside the scope of analysis. They may be inspected off of |
| 201 | // the monitor list. |
| 202 | if (level_ != kMonitorLock) { |
| 203 | self->SetHeldMutex(level_, this); |
| 204 | } |
| 205 | } |
| 206 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 207 | void BaseMutex::RegisterAsUnlocked(Thread* self) { |
| 208 | if (UNLIKELY(self == NULL)) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 209 | CheckUnattachedThread(level_); |
| 210 | return; |
| 211 | } |
| 212 | if (level_ != kMonitorLock) { |
| Brian Carlstrom | 760c943 | 2012-11-29 16:46:27 -0800 | [diff] [blame] | 213 | if (kDebugLocking && !gAborting) { |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 214 | CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_; |
| 215 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 216 | self->SetHeldMutex(level_, NULL); |
| 217 | } |
| 218 | } |
| 219 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 220 | void BaseMutex::CheckSafeToWait(Thread* self) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 221 | if (self == NULL) { |
| 222 | CheckUnattachedThread(level_); |
| 223 | return; |
| 224 | } |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 225 | if (kDebugLocking) { |
| 226 | CHECK(self->GetHeldMutex(level_) == this) << "Waiting on unacquired mutex: " << name_; |
| 227 | bool bad_mutexes_held = false; |
| 228 | for (int i = kMaxMutexLevel; i >= 0; --i) { |
| 229 | if (i != level_) { |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 230 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i)); |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 231 | if (held_mutex != NULL) { |
| 232 | LOG(ERROR) << "Holding " << held_mutex->name_ << " (level " << i |
| 233 | << ") while performing wait on: " |
| 234 | << name_ << " (level " << static_cast<int>(level_) << ")"; |
| 235 | bad_mutexes_held = true; |
| 236 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 239 | CHECK(!bad_mutexes_held); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 240 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 243 | void BaseMutex::RecordContention(uint64_t blocked_tid, uint64_t owner_tid, uint64_t milli_time_blocked) { |
| 244 | #if CONTENTION_LOGGING |
| 245 | ++contention_count_; |
| 246 | wait_time_ += static_cast<uint32_t>(milli_time_blocked); // May overflow. |
| 247 | // This code is intentionally racy as it is only used for diagnostics. |
| 248 | uint32_t slot = cur_content_log_entry_; |
| 249 | if (contention_log_[slot].blocked_tid == blocked_tid && |
| 250 | contention_log_[slot].owner_tid == blocked_tid) { |
| 251 | ++contention_log_[slot].count; |
| 252 | } else { |
| 253 | uint32_t new_slot; |
| 254 | do { |
| 255 | slot = cur_content_log_entry_; |
| 256 | new_slot = (slot + 1) % kContentionLogSize; |
| 257 | } while(!cur_content_log_entry_.CompareAndSwap(slot, new_slot)); |
| 258 | contention_log_[new_slot].blocked_tid = blocked_tid; |
| 259 | contention_log_[new_slot].owner_tid = owner_tid; |
| 260 | contention_log_[new_slot].count = 1; |
| 261 | } |
| 262 | #endif |
| 263 | } |
| 264 | |
| 265 | class ScopedContentionRecorder { |
| 266 | public: |
| 267 | ScopedContentionRecorder(BaseMutex* mutex, uint64_t blocked_tid, uint64_t owner_tid) : |
| 268 | mutex_(mutex), blocked_tid_(blocked_tid), owner_tid_(owner_tid), |
| 269 | start_milli_time_(MilliTime()) { |
| 270 | } |
| 271 | |
| 272 | ~ScopedContentionRecorder() { |
| 273 | uint64_t end_milli_time = MilliTime(); |
| 274 | mutex_->RecordContention(blocked_tid_, owner_tid_, end_milli_time - start_milli_time_); |
| 275 | } |
| 276 | |
| 277 | private: |
| 278 | BaseMutex* const mutex_; |
| 279 | uint64_t blocked_tid_; |
| 280 | uint64_t owner_tid_; |
| 281 | const uint64_t start_milli_time_; |
| 282 | }; |
| 283 | |
| 284 | void BaseMutex::DumpContention(std::ostream& os) const { |
| 285 | #if CONTENTION_LOGGING |
| 286 | uint32_t wait_time = wait_time_; |
| 287 | uint32_t contention_count = contention_count_; |
| 288 | if (contention_count == 0) { |
| 289 | os << "never contended"; |
| 290 | } else { |
| 291 | os << "contended " << contention_count << " times, average wait of contender " << (wait_time / contention_count) << "ms"; |
| 292 | SafeMap<uint64_t, size_t> most_common_blocker; |
| 293 | SafeMap<uint64_t, size_t> most_common_blocked; |
| 294 | typedef SafeMap<uint64_t, size_t>::const_iterator It; |
| 295 | for (size_t i = 0; i < kContentionLogSize; ++i) { |
| 296 | uint64_t blocked_tid = contention_log_[i].blocked_tid; |
| 297 | uint64_t owner_tid = contention_log_[i].owner_tid; |
| 298 | uint32_t count = contention_log_[i].count; |
| 299 | if (count > 0) { |
| 300 | It it = most_common_blocked.find(blocked_tid); |
| 301 | if (it != most_common_blocked.end()) { |
| 302 | most_common_blocked.Overwrite(blocked_tid, it->second + count); |
| 303 | } else { |
| 304 | most_common_blocked.Put(blocked_tid, count); |
| 305 | } |
| 306 | it = most_common_blocker.find(owner_tid); |
| 307 | if (it != most_common_blocker.end()) { |
| 308 | most_common_blocker.Overwrite(owner_tid, it->second + count); |
| 309 | } else { |
| 310 | most_common_blocker.Put(owner_tid, count); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | uint64_t max_tid = 0; |
| 315 | size_t max_tid_count = 0; |
| 316 | for (It it = most_common_blocked.begin(); it != most_common_blocked.end(); ++it) { |
| 317 | if (it->second > max_tid_count) { |
| 318 | max_tid = it->first; |
| 319 | max_tid_count = it->second; |
| 320 | } |
| 321 | } |
| 322 | if (max_tid != 0) { |
| 323 | os << " sample shows most blocked tid=" << max_tid; |
| 324 | } |
| 325 | max_tid = 0; |
| 326 | max_tid_count = 0; |
| 327 | for (It it = most_common_blocker.begin(); it != most_common_blocker.end(); ++it) { |
| 328 | if (it->second > max_tid_count) { |
| 329 | max_tid = it->first; |
| 330 | max_tid_count = it->second; |
| 331 | } |
| 332 | } |
| 333 | if (max_tid != 0) { |
| 334 | os << " sample shows tid=" << max_tid << " owning during this time"; |
| 335 | } |
| 336 | } |
| 337 | #endif |
| 338 | } |
| 339 | |
| 340 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 341 | Mutex::Mutex(const char* name, LockLevel level, bool recursive) |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 342 | : BaseMutex(name, level), recursive_(recursive), recursion_count_(0) { |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 343 | #if ART_USE_FUTEXES |
| 344 | state_ = 0; |
| 345 | exclusive_owner_ = 0; |
| 346 | num_contenders_ = 0; |
| 347 | #elif defined(__BIONIC__) || defined(__APPLE__) |
| Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 348 | // Use recursive mutexes for bionic and Apple otherwise the |
| 349 | // non-recursive mutexes don't have TIDs to check lock ownership of. |
| Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 350 | pthread_mutexattr_t attributes; |
| 351 | CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes)); |
| 352 | CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE)); |
| 353 | CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes)); |
| 354 | CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes)); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 355 | #else |
| 356 | CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, NULL)); |
| 357 | #endif |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | Mutex::~Mutex() { |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 361 | #if ART_USE_FUTEXES |
| 362 | if (state_ != 0) { |
| 363 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
| 364 | Runtime* runtime = Runtime::Current(); |
| 365 | bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown(); |
| 366 | LOG(shutting_down ? WARNING : FATAL) << "destroying mutex with owner: " << exclusive_owner_; |
| 367 | } else { |
| 368 | CHECK_EQ(exclusive_owner_, 0U) << "unexpectedly found an owner on unlocked mutex " << name_; |
| 369 | CHECK_EQ(num_contenders_, 0) << "unexpectedly found a contender on mutex " << name_; |
| 370 | } |
| 371 | #else |
| Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 372 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 373 | // may still be using locks. |
| Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 374 | int rc = pthread_mutex_destroy(&mutex_); |
| 375 | if (rc != 0) { |
| 376 | errno = rc; |
| Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 377 | // TODO: should we just not log at all if shutting down? this could be the logging mutex! |
| Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 378 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
| Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 379 | Runtime* runtime = Runtime::Current(); |
| 380 | bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown(); |
| Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 381 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_; |
| 382 | } |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 383 | #endif |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 386 | void Mutex::ExclusiveLock(Thread* self) { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 387 | DCHECK(self == NULL || self == Thread::Current()); |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 388 | if (kDebugLocking && !recursive_) { |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 389 | AssertNotHeld(self); |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 390 | } |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 391 | if (!recursive_ || !IsExclusiveHeld(self)) { |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 392 | #if ART_USE_FUTEXES |
| 393 | bool done = false; |
| 394 | do { |
| 395 | int32_t cur_state = state_; |
| 396 | if (cur_state == 0) { |
| 397 | // Change state from 0 to 1. |
| 398 | done = android_atomic_cmpxchg(0, 1, &state_) == 0; |
| 399 | } else { |
| 400 | // Failed to acquire, hang up. |
| Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 401 | ScopedContentionRecorder scr(this, GetExclusiveOwnerTid(), SafeGetTid(self)); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 402 | android_atomic_inc(&num_contenders_); |
| 403 | if (futex(&state_, FUTEX_WAIT, 1, NULL, NULL, 0) != 0) { |
| 404 | if (errno != EAGAIN) { |
| 405 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 406 | } |
| 407 | } |
| 408 | android_atomic_dec(&num_contenders_); |
| 409 | } |
| 410 | } while(!done); |
| 411 | DCHECK_EQ(state_, 1); |
| 412 | exclusive_owner_ = SafeGetTid(self); |
| 413 | #else |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 414 | CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_)); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 415 | #endif |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 416 | RegisterAsLocked(self); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 417 | } |
| 418 | recursion_count_++; |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 419 | if (kDebugLocking) { |
| 420 | CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: " |
| 421 | << name_ << " " << recursion_count_; |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 422 | AssertHeld(self); |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 423 | } |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 426 | bool Mutex::ExclusiveTryLock(Thread* self) { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 427 | DCHECK(self == NULL || self == Thread::Current()); |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 428 | if (kDebugLocking && !recursive_) { |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 429 | AssertNotHeld(self); |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 430 | } |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 431 | if (!recursive_ || !IsExclusiveHeld(self)) { |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 432 | #if ART_USE_FUTEXES |
| 433 | bool done = false; |
| 434 | do { |
| 435 | int32_t cur_state = state_; |
| 436 | if (cur_state == 0) { |
| 437 | // Change state from 0 to 1. |
| 438 | done = android_atomic_cmpxchg(0, 1, &state_) == 0; |
| 439 | } else { |
| 440 | return false; |
| 441 | } |
| 442 | } while(!done); |
| 443 | DCHECK_EQ(state_, 1); |
| 444 | exclusive_owner_ = SafeGetTid(self); |
| 445 | #else |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 446 | int result = pthread_mutex_trylock(&mutex_); |
| 447 | if (result == EBUSY) { |
| 448 | return false; |
| 449 | } |
| 450 | if (result != 0) { |
| 451 | errno = result; |
| 452 | PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_; |
| 453 | } |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 454 | #endif |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 455 | RegisterAsLocked(self); |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 456 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 457 | recursion_count_++; |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 458 | if (kDebugLocking) { |
| 459 | CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: " |
| 460 | << name_ << " " << recursion_count_; |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 461 | AssertHeld(self); |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 462 | } |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 463 | return true; |
| 464 | } |
| 465 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 466 | void Mutex::ExclusiveUnlock(Thread* self) { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 467 | DCHECK(self == NULL || self == Thread::Current()); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 468 | AssertHeld(self); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 469 | recursion_count_--; |
| 470 | if (!recursive_ || recursion_count_ == 0) { |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 471 | if (kDebugLocking) { |
| 472 | CHECK(recursion_count_ == 0 || recursive_) << "Unexpected recursion count on mutex: " |
| 473 | << name_ << " " << recursion_count_; |
| 474 | } |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 475 | RegisterAsUnlocked(self); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 476 | #if ART_USE_FUTEXES |
| 477 | bool done = false; |
| 478 | do { |
| 479 | int32_t cur_state = state_; |
| 480 | if (cur_state == 1) { |
| 481 | // We're no longer the owner. |
| 482 | exclusive_owner_ = 0; |
| 483 | // Change state to 0. |
| 484 | done = android_atomic_cmpxchg(cur_state, 0, &state_) == 0; |
| 485 | if (done) { // Spurious fail? |
| 486 | // Wake a contender |
| 487 | if (num_contenders_ > 0) { |
| 488 | futex(&state_, FUTEX_WAKE, 1, NULL, NULL, 0); |
| 489 | } |
| 490 | } |
| 491 | } else { |
| 492 | LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_; |
| 493 | } |
| 494 | } while(!done); |
| 495 | #else |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 496 | CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_)); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 497 | #endif |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 498 | } |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 501 | bool Mutex::IsExclusiveHeld(const Thread* self) const { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 502 | DCHECK(self == NULL || self == Thread::Current()); |
| 503 | bool result = (GetExclusiveOwnerTid() == SafeGetTid(self)); |
| 504 | if (kDebugLocking) { |
| 505 | // Sanity debug check that if we think it is locked we have it in our held mutexes. |
| Brian Carlstrom | 9e419ca | 2012-11-27 11:31:49 -0800 | [diff] [blame] | 506 | if (result && self != NULL && level_ != kMonitorLock && !gAborting) { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 507 | CHECK_EQ(self->GetHeldMutex(level_), this); |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 508 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 509 | } |
| 510 | return result; |
| Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 513 | uint64_t Mutex::GetExclusiveOwnerTid() const { |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 514 | #if ART_USE_FUTEXES |
| 515 | return exclusive_owner_; |
| 516 | #elif defined(__BIONIC__) |
| Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 517 | return static_cast<uint64_t>((mutex_.value >> 16) & 0xffff); |
| Elliott Hughes | 3147a23 | 2011-10-12 15:55:07 -0700 | [diff] [blame] | 518 | #elif defined(__GLIBC__) |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 519 | return reinterpret_cast<const glibc_pthread_mutex_t*>(&mutex_)->owner; |
| Elliott Hughes | cf04431 | 2012-01-23 18:48:51 -0800 | [diff] [blame] | 520 | #elif defined(__APPLE__) |
| Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 521 | const darwin_pthread_mutex_t* dpmutex = reinterpret_cast<const darwin_pthread_mutex_t*>(&mutex_); |
| 522 | pthread_t owner = dpmutex->darwin_pthread_mutex_owner; |
| Brian Carlstrom | bd93c30 | 2012-08-27 16:58:28 -0700 | [diff] [blame] | 523 | // 0 for unowned, -1 for PTHREAD_MTX_TID_SWITCHING |
| 524 | // 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] | 525 | if ((owner == (pthread_t)0) || (owner == (pthread_t)-1)) { |
| 526 | return 0; |
| 527 | } |
| 528 | uint64_t tid; |
| 529 | CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6 |
| 530 | return tid; |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 531 | #else |
| Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 532 | #error unsupported C library |
| Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 533 | #endif |
| 534 | } |
| 535 | |
| Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 536 | void Mutex::Dump(std::ostream& os) const { |
| 537 | os << (recursive_ ? "recursive " : "non-recursive ") |
| 538 | << name_ |
| 539 | << " level=" << static_cast<int>(level_) |
| 540 | << " rec=" << recursion_count_ |
| 541 | << " owner=" << GetExclusiveOwnerTid() << " "; |
| 542 | DumpContention(os); |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | std::ostream& operator<<(std::ostream& os, const Mutex& mu) { |
| Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 546 | mu.Dump(os); |
| 547 | return os; |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 550 | ReaderWriterMutex::ReaderWriterMutex(const char* name, LockLevel level) : |
| 551 | BaseMutex(name, level) |
| 552 | #if ART_USE_FUTEXES |
| 553 | , state_(0), exclusive_owner_(0), num_pending_readers_(0), num_pending_writers_(0) |
| 554 | #endif |
| 555 | { |
| 556 | #if !ART_USE_FUTEXES |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 557 | CHECK_MUTEX_CALL(pthread_rwlock_init, (&rwlock_, NULL)); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 558 | #endif |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | ReaderWriterMutex::~ReaderWriterMutex() { |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 562 | #if ART_USE_FUTEXES |
| 563 | CHECK_EQ(state_, 0); |
| 564 | CHECK_EQ(exclusive_owner_, 0U); |
| 565 | CHECK_EQ(num_pending_readers_, 0); |
| 566 | CHECK_EQ(num_pending_writers_, 0); |
| 567 | #else |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 568 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 569 | // may still be using locks. |
| 570 | int rc = pthread_rwlock_destroy(&rwlock_); |
| 571 | if (rc != 0) { |
| 572 | errno = rc; |
| 573 | // TODO: should we just not log at all if shutting down? this could be the logging mutex! |
| Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 574 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
| Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 575 | Runtime* runtime = Runtime::Current(); |
| 576 | bool shutting_down = runtime == NULL || runtime->IsShuttingDown(); |
| 577 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_rwlock_destroy failed for " << name_; |
| Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 578 | } |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 579 | #endif |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 582 | void ReaderWriterMutex::ExclusiveLock(Thread* self) { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 583 | DCHECK(self == NULL || self == Thread::Current()); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 584 | AssertNotExclusiveHeld(self); |
| 585 | #if ART_USE_FUTEXES |
| 586 | bool done = false; |
| 587 | do { |
| 588 | int32_t cur_state = state_; |
| 589 | if (cur_state == 0) { |
| 590 | // Change state from 0 to -1. |
| 591 | done = android_atomic_cmpxchg(0, -1, &state_) == 0; |
| 592 | } else { |
| 593 | // Failed to acquire, hang up. |
| Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 594 | ScopedContentionRecorder scr(this, GetExclusiveOwnerTid(), SafeGetTid(self)); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 595 | android_atomic_inc(&num_pending_writers_); |
| 596 | if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) { |
| 597 | if (errno != EAGAIN) { |
| 598 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 599 | } |
| 600 | } |
| 601 | android_atomic_dec(&num_pending_writers_); |
| 602 | } |
| 603 | } while(!done); |
| Ian Rogers | ab47016 | 2012-09-29 23:06:53 -0700 | [diff] [blame] | 604 | DCHECK_EQ(state_, -1); |
| 605 | exclusive_owner_ = SafeGetTid(self); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 606 | #else |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 607 | CHECK_MUTEX_CALL(pthread_rwlock_wrlock, (&rwlock_)); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 608 | #endif |
| 609 | RegisterAsLocked(self); |
| 610 | AssertExclusiveHeld(self); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 613 | void ReaderWriterMutex::ExclusiveUnlock(Thread* self) { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 614 | DCHECK(self == NULL || self == Thread::Current()); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 615 | AssertExclusiveHeld(self); |
| 616 | RegisterAsUnlocked(self); |
| 617 | #if ART_USE_FUTEXES |
| 618 | bool done = false; |
| 619 | do { |
| 620 | int32_t cur_state = state_; |
| 621 | if (cur_state == -1) { |
| 622 | // We're no longer the owner. |
| 623 | exclusive_owner_ = 0; |
| 624 | // Change state from -1 to 0. |
| 625 | done = android_atomic_cmpxchg(-1, 0, &state_) == 0; |
| 626 | if (done) { // cmpxchg may fail due to noise? |
| 627 | // Wake any waiters. |
| 628 | if (num_pending_readers_ > 0 || num_pending_writers_ > 0) { |
| 629 | futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0); |
| 630 | } |
| 631 | } |
| 632 | } else { |
| 633 | LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_; |
| 634 | } |
| 635 | } while(!done); |
| 636 | #else |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 637 | CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_)); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 638 | #endif |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 641 | #if HAVE_TIMED_RWLOCK |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 642 | bool ReaderWriterMutex::ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns) { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 643 | DCHECK(self == NULL || self == Thread::Current()); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 644 | #if ART_USE_FUTEXES |
| 645 | bool done = false; |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 646 | timespec end_abs_ts; |
| Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 647 | InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &end_abs_ts); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 648 | do { |
| 649 | int32_t cur_state = state_; |
| 650 | if (cur_state == 0) { |
| 651 | // Change state from 0 to -1. |
| 652 | done = android_atomic_cmpxchg(0, -1, &state_) == 0; |
| 653 | } else { |
| 654 | // Failed to acquire, hang up. |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 655 | timespec now_abs_ts; |
| Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 656 | InitTimeSpec(true, CLOCK_REALTIME, 0, 0, &now_abs_ts); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 657 | timespec rel_ts; |
| 658 | if (ComputeRelativeTimeSpec(&rel_ts, end_abs_ts, now_abs_ts)) { |
| 659 | return false; // Timed out. |
| 660 | } |
| Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 661 | ScopedContentionRecorder scr(this, GetExclusiveOwnerTid(), SafeGetTid(self)); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 662 | android_atomic_inc(&num_pending_writers_); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 663 | if (futex(&state_, FUTEX_WAIT, cur_state, &rel_ts, NULL, 0) != 0) { |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 664 | if (errno == ETIMEDOUT) { |
| 665 | android_atomic_dec(&num_pending_writers_); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 666 | return false; // Timed out. |
| 667 | } else if (errno != EAGAIN && errno != EINTR) { |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 668 | PLOG(FATAL) << "timed futex wait failed for " << name_; |
| 669 | } |
| 670 | } |
| 671 | android_atomic_dec(&num_pending_writers_); |
| 672 | } |
| 673 | } while(!done); |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 674 | exclusive_owner_ = SafeGetTid(self); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 675 | #else |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 676 | timespec ts; |
| Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 677 | InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &ts); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 678 | int result = pthread_rwlock_timedwrlock(&rwlock_, &ts); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 679 | if (result == ETIMEDOUT) { |
| 680 | return false; |
| 681 | } |
| 682 | if (result != 0) { |
| 683 | errno = result; |
| Ian Rogers | a5acfd3 | 2012-08-15 11:50:10 -0700 | [diff] [blame] | 684 | PLOG(FATAL) << "pthread_rwlock_timedwrlock failed for " << name_; |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 685 | } |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 686 | #endif |
| 687 | RegisterAsLocked(self); |
| 688 | AssertSharedHeld(self); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 689 | return true; |
| 690 | } |
| Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 691 | #endif |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 692 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 693 | void ReaderWriterMutex::SharedLock(Thread* self) { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 694 | DCHECK(self == NULL || self == Thread::Current()); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 695 | #if ART_USE_FUTEXES |
| 696 | bool done = false; |
| 697 | do { |
| 698 | int32_t cur_state = state_; |
| 699 | if (cur_state >= 0) { |
| 700 | // Add as an extra reader. |
| 701 | done = android_atomic_cmpxchg(cur_state, cur_state + 1, &state_) == 0; |
| 702 | } else { |
| 703 | // Owner holds it exclusively, hang up. |
| Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 704 | ScopedContentionRecorder scr(this, GetExclusiveOwnerTid(), SafeGetTid(self)); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 705 | android_atomic_inc(&num_pending_readers_); |
| 706 | if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) { |
| 707 | if (errno != EAGAIN) { |
| 708 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 709 | } |
| 710 | } |
| 711 | android_atomic_dec(&num_pending_readers_); |
| 712 | } |
| 713 | } while(!done); |
| 714 | #else |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 715 | CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_)); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 716 | #endif |
| 717 | RegisterAsLocked(self); |
| 718 | AssertSharedHeld(self); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 721 | bool ReaderWriterMutex::SharedTryLock(Thread* self) { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 722 | DCHECK(self == NULL || self == Thread::Current()); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 723 | #if ART_USE_FUTEXES |
| 724 | bool done = false; |
| 725 | do { |
| 726 | int32_t cur_state = state_; |
| 727 | if (cur_state >= 0) { |
| 728 | // Add as an extra reader. |
| 729 | done = android_atomic_cmpxchg(cur_state, cur_state + 1, &state_) == 0; |
| 730 | } else { |
| 731 | // Owner holds it exclusively. |
| 732 | return false; |
| 733 | } |
| 734 | } while(!done); |
| 735 | #else |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 736 | int result = pthread_rwlock_tryrdlock(&rwlock_); |
| 737 | if (result == EBUSY) { |
| 738 | return false; |
| 739 | } |
| 740 | if (result != 0) { |
| 741 | errno = result; |
| 742 | PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_; |
| 743 | } |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 744 | #endif |
| 745 | RegisterAsLocked(self); |
| 746 | AssertSharedHeld(self); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 747 | return true; |
| 748 | } |
| 749 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 750 | void ReaderWriterMutex::SharedUnlock(Thread* self) { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 751 | DCHECK(self == NULL || self == Thread::Current()); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 752 | AssertSharedHeld(self); |
| 753 | RegisterAsUnlocked(self); |
| 754 | #if ART_USE_FUTEXES |
| 755 | bool done = false; |
| 756 | do { |
| 757 | int32_t cur_state = state_; |
| 758 | if (LIKELY(cur_state > 0)) { |
| 759 | // Reduce state by 1. |
| 760 | done = android_atomic_cmpxchg(cur_state, cur_state - 1, &state_) == 0; |
| 761 | if (done && (cur_state - 1) == 0) { // cmpxchg may fail due to noise? |
| 762 | if (num_pending_writers_ > 0 || num_pending_readers_ > 0) { |
| 763 | // Wake any exclusive waiters as there are now no readers. |
| 764 | futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0); |
| 765 | } |
| 766 | } |
| 767 | } else { |
| 768 | LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_; |
| 769 | } |
| 770 | } while(!done); |
| 771 | #else |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 772 | CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_)); |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 773 | #endif |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 776 | bool ReaderWriterMutex::IsExclusiveHeld(const Thread* self) const { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 777 | DCHECK(self == NULL || self == Thread::Current()); |
| 778 | bool result = (GetExclusiveOwnerTid() == SafeGetTid(self)); |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 779 | if (kDebugLocking) { |
| 780 | // Sanity that if the pthread thinks we own the lock the Thread agrees. |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 781 | if (self != NULL && result) { |
| 782 | CHECK_EQ(self->GetHeldMutex(level_), this); |
| 783 | } |
| Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 784 | } |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 785 | return result; |
| 786 | } |
| 787 | |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 788 | bool ReaderWriterMutex::IsSharedHeld(const Thread* self) const { |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 789 | DCHECK(self == NULL || self == Thread::Current()); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 790 | bool result; |
| 791 | if (UNLIKELY(self == NULL)) { // Handle unattached threads. |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 792 | result = IsExclusiveHeld(self); // TODO: a better best effort here. |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 793 | } else { |
| 794 | result = (self->GetHeldMutex(level_) == this); |
| 795 | } |
| 796 | return result; |
| 797 | } |
| 798 | |
| 799 | uint64_t ReaderWriterMutex::GetExclusiveOwnerTid() const { |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 800 | #if ART_USE_FUTEXES |
| Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 801 | int32_t state = state_; |
| 802 | if (state == 0) { |
| 803 | return 0; // No owner. |
| 804 | } else if (state > 0) { |
| 805 | return -1; // Shared. |
| 806 | } else { |
| 807 | return exclusive_owner_; |
| 808 | } |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 809 | #else |
| Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 810 | #if defined(__BIONIC__) |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 811 | return rwlock_.writerThreadId; |
| Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 812 | #elif defined(__GLIBC__) |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 813 | return reinterpret_cast<const glibc_pthread_rwlock_t*>(&rwlock_)->writer; |
| Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 814 | #elif defined(__APPLE__) |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 815 | const darwin_pthread_rwlock_t* |
| 816 | dprwlock = reinterpret_cast<const darwin_pthread_rwlock_t*>(&rwlock_); |
| Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 817 | pthread_t owner = dprwlock->darwin_pthread_rwlock_owner; |
| 818 | if (owner == (pthread_t)0) { |
| 819 | return 0; |
| 820 | } |
| 821 | uint64_t tid; |
| 822 | CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6 |
| 823 | return tid; |
| Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 824 | #else |
| Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 825 | #error unsupported C library |
| Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 826 | #endif |
| Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 827 | #endif |
| Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 828 | } |
| 829 | |
| Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 830 | void ReaderWriterMutex::Dump(std::ostream& os) const { |
| 831 | os << name_ |
| 832 | << " level=" << static_cast<int>(level_) |
| 833 | << " owner=" << GetExclusiveOwnerTid() << " "; |
| 834 | DumpContention(os); |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu) { |
| Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 838 | mu.Dump(os); |
| 839 | return os; |
| Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 842 | ConditionVariable::ConditionVariable(const std::string& name, Mutex& guard) |
| 843 | : name_(name), guard_(guard) { |
| 844 | #if ART_USE_FUTEXES |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 845 | sequence_ = 0; |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 846 | num_waiters_ = 0; |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 847 | #else |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 848 | CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL)); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 849 | #endif |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | ConditionVariable::~ConditionVariable() { |
| Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 853 | #if ART_USE_FUTEXES |
| 854 | if (num_waiters_!= 0) { |
| 855 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
| 856 | Runtime* runtime = Runtime::Current(); |
| 857 | bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown(); |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 858 | LOG(shutting_down ? WARNING : FATAL) << "ConditionVariable::~ConditionVariable for " << name_ |
| 859 | << " called with " << num_waiters_ << " waiters."; |
| Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 860 | } |
| 861 | #else |
| Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 862 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 863 | // may still be using condition variables. |
| 864 | int rc = pthread_cond_destroy(&cond_); |
| 865 | if (rc != 0) { |
| 866 | errno = rc; |
| Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 867 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
| Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 868 | Runtime* runtime = Runtime::Current(); |
| 869 | bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown(); |
| Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 870 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_cond_destroy failed for " << name_; |
| 871 | } |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 872 | #endif |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 873 | } |
| 874 | |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 875 | void ConditionVariable::Broadcast(Thread* self) { |
| 876 | DCHECK(self == NULL || self == Thread::Current()); |
| 877 | // TODO: enable below, there's a race in thread creation that causes false failures currently. |
| 878 | // guard_.AssertExclusiveHeld(self); |
| Mathieu Chartier | e46cd75 | 2012-10-31 16:56:18 -0700 | [diff] [blame] | 879 | DCHECK_EQ(guard_.GetExclusiveOwnerTid(), SafeGetTid(self)); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 880 | #if ART_USE_FUTEXES |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 881 | if (num_waiters_ > 0) { |
| 882 | android_atomic_inc(&sequence_); // Indicate the broadcast occurred. |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 883 | bool done = false; |
| 884 | do { |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 885 | int32_t cur_sequence = sequence_; |
| 886 | // Requeue waiters onto mutex. The waiter holds the contender count on the mutex high ensuring |
| 887 | // mutex unlocks will awaken the requeued waiter thread. |
| 888 | done = futex(&sequence_, FUTEX_CMP_REQUEUE, 0, |
| Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 889 | reinterpret_cast<const timespec*>(std::numeric_limits<int32_t>::max()), |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 890 | &guard_.state_, cur_sequence) != -1; |
| Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 891 | if (!done) { |
| 892 | if (errno != EAGAIN) { |
| 893 | PLOG(FATAL) << "futex cmp requeue failed for " << name_; |
| 894 | } |
| Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 895 | } |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 896 | } while (!done); |
| 897 | } |
| 898 | #else |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 899 | CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_)); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 900 | #endif |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 903 | void ConditionVariable::Signal(Thread* self) { |
| 904 | DCHECK(self == NULL || self == Thread::Current()); |
| 905 | guard_.AssertExclusiveHeld(self); |
| 906 | #if ART_USE_FUTEXES |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 907 | if (num_waiters_ > 0) { |
| 908 | android_atomic_inc(&sequence_); // Indicate a signal occurred. |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 909 | // Futex wake 1 waiter who will then come and in contend on mutex. It'd be nice to requeue them |
| 910 | // to avoid this, however, requeueing can only move all waiters. |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 911 | int num_woken = futex(&sequence_, FUTEX_WAKE, 1, NULL, NULL, 0); |
| 912 | // Check something was woken or else we changed sequence_ before they had chance to wait. |
| Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 913 | CHECK((num_woken == 0) || (num_woken == 1)); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 914 | } |
| 915 | #else |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 916 | CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_)); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 917 | #endif |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 920 | void ConditionVariable::Wait(Thread* self) { |
| 921 | DCHECK(self == NULL || self == Thread::Current()); |
| 922 | guard_.AssertExclusiveHeld(self); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 923 | unsigned int old_recursion_count = guard_.recursion_count_; |
| 924 | #if ART_USE_FUTEXES |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 925 | num_waiters_++; |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 926 | // Ensure the Mutex is contended so that requeued threads are awoken. |
| 927 | android_atomic_inc(&guard_.num_contenders_); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 928 | guard_.recursion_count_ = 1; |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 929 | int32_t cur_sequence = sequence_; |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 930 | guard_.ExclusiveUnlock(self); |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 931 | if (futex(&sequence_, FUTEX_WAIT, cur_sequence, NULL, NULL, 0) != 0) { |
| 932 | // Futex failed, check it is an expected error. |
| 933 | // EAGAIN == EWOULDBLK, so we let the caller try again. |
| 934 | // EINTR implies a signal was sent to this thread. |
| 935 | if ((errno != EINTR) && (errno != EAGAIN)) { |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 936 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 937 | } |
| 938 | } |
| 939 | guard_.ExclusiveLock(self); |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 940 | CHECK_GE(num_waiters_, 0); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 941 | num_waiters_--; |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 942 | // We awoke and so no longer require awakes from the guard_'s unlock. |
| 943 | CHECK_GE(guard_.num_contenders_, 0); |
| 944 | android_atomic_dec(&guard_.num_contenders_); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 945 | #else |
| 946 | guard_.recursion_count_ = 0; |
| 947 | CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, &guard_.mutex_)); |
| 948 | #endif |
| 949 | guard_.recursion_count_ = old_recursion_count; |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 952 | void ConditionVariable::TimedWait(Thread* self, int64_t ms, int32_t ns) { |
| 953 | DCHECK(self == NULL || self == Thread::Current()); |
| 954 | guard_.AssertExclusiveHeld(self); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 955 | unsigned int old_recursion_count = guard_.recursion_count_; |
| 956 | #if ART_USE_FUTEXES |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 957 | timespec rel_ts; |
| Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 958 | InitTimeSpec(false, CLOCK_REALTIME, ms, ns, &rel_ts); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 959 | num_waiters_++; |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 960 | // Ensure the Mutex is contended so that requeued threads are awoken. |
| 961 | android_atomic_inc(&guard_.num_contenders_); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 962 | guard_.recursion_count_ = 1; |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 963 | int32_t cur_sequence = sequence_; |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 964 | guard_.ExclusiveUnlock(self); |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 965 | if (futex(&sequence_, FUTEX_WAIT, cur_sequence, &rel_ts, NULL, 0) != 0) { |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 966 | if (errno == ETIMEDOUT) { |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 967 | // Timed out we're done. |
| Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 968 | } else if ((errno == EINTR) || (errno == EAGAIN)) { |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 969 | // A signal or ConditionVariable::Signal/Broadcast has come in. |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 970 | } else { |
| 971 | PLOG(FATAL) << "timed futex wait failed for " << name_; |
| 972 | } |
| 973 | } |
| 974 | guard_.ExclusiveLock(self); |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 975 | CHECK_GE(num_waiters_, 0); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 976 | num_waiters_--; |
| Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 977 | // We awoke and so no longer require awakes from the guard_'s unlock. |
| 978 | CHECK_GE(guard_.num_contenders_, 0); |
| 979 | android_atomic_dec(&guard_.num_contenders_); |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 980 | #else |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 981 | #ifdef HAVE_TIMEDWAIT_MONOTONIC |
| 982 | #define TIMEDWAIT pthread_cond_timedwait_monotonic |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 983 | int clock = CLOCK_MONOTONIC; |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 984 | #else |
| 985 | #define TIMEDWAIT pthread_cond_timedwait |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 986 | int clock = CLOCK_REALTIME; |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 987 | #endif |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 988 | guard_.recursion_count_ = 0; |
| 989 | timespec ts; |
| Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 990 | InitTimeSpec(true, clock, ms, ns, &ts); |
| 991 | int rc = TEMP_FAILURE_RETRY(TIMEDWAIT(&cond_, &guard_.mutex_, &ts)); |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 992 | if (rc != 0 && rc != ETIMEDOUT) { |
| 993 | errno = rc; |
| 994 | PLOG(FATAL) << "TimedWait failed for " << name_; |
| 995 | } |
| Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 996 | #endif |
| 997 | guard_.recursion_count_ = old_recursion_count; |
| Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 998 | } |
| 999 | |
| Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 1000 | } // namespace art |