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