Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BASE_MUTEX_INL_H_ |
| 18 | #define ART_RUNTIME_BASE_MUTEX_INL_H_ |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 19 | |
Ian Rogers | 220228e | 2014-01-23 09:08:16 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
| 21 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 22 | #include "mutex.h" |
| 23 | |
Hiroshi Yamauchi | b373308 | 2013-08-12 17:28:49 -0700 | [diff] [blame] | 24 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
| 25 | |
Hiroshi Yamauchi | b373308 | 2013-08-12 17:28:49 -0700 | [diff] [blame] | 26 | #include "cutils/trace.h" |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 27 | |
| 28 | #include "base/stringprintf.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 29 | #include "runtime.h" |
| 30 | #include "thread.h" |
| 31 | |
| 32 | namespace art { |
| 33 | |
| 34 | #define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_) |
| 35 | |
| 36 | #if ART_USE_FUTEXES |
| 37 | #include "linux/futex.h" |
| 38 | #include "sys/syscall.h" |
| 39 | #ifndef SYS_futex |
| 40 | #define SYS_futex __NR_futex |
| 41 | #endif |
| 42 | static inline int futex(volatile int *uaddr, int op, int val, const struct timespec *timeout, volatile int *uaddr2, int val3) { |
| 43 | return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3); |
| 44 | } |
| 45 | #endif // ART_USE_FUTEXES |
| 46 | |
| 47 | class ScopedContentionRecorder { |
| 48 | public: |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 49 | ScopedContentionRecorder(BaseMutex* mutex, uint64_t blocked_tid, uint64_t owner_tid) |
| 50 | : mutex_(kLogLockContentions ? mutex : NULL), |
| 51 | blocked_tid_(kLogLockContentions ? blocked_tid : 0), |
| 52 | owner_tid_(kLogLockContentions ? owner_tid : 0), |
| 53 | start_nano_time_(kLogLockContentions ? NanoTime() : 0) { |
Ian Rogers | 220228e | 2014-01-23 09:08:16 -0800 | [diff] [blame] | 54 | std::string msg = StringPrintf("Lock contention on %s (owner tid: %" PRIu64 ")", |
Jeff Hao | 08f2e7b | 2013-09-09 16:44:02 -0700 | [diff] [blame] | 55 | mutex->GetName(), owner_tid); |
| 56 | ATRACE_BEGIN(msg.c_str()); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | ~ScopedContentionRecorder() { |
Jeff Hao | 08f2e7b | 2013-09-09 16:44:02 -0700 | [diff] [blame] | 60 | ATRACE_END(); |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 61 | if (kLogLockContentions) { |
| 62 | uint64_t end_nano_time = NanoTime(); |
| 63 | mutex_->RecordContention(blocked_tid_, owner_tid_, end_nano_time - start_nano_time_); |
| 64 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | private: |
| 68 | BaseMutex* const mutex_; |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 69 | const uint64_t blocked_tid_; |
| 70 | const uint64_t owner_tid_; |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 71 | const uint64_t start_nano_time_; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | static inline uint64_t SafeGetTid(const Thread* self) { |
| 75 | if (self != NULL) { |
| 76 | return static_cast<uint64_t>(self->GetTid()); |
| 77 | } else { |
| 78 | return static_cast<uint64_t>(GetTid()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static inline void CheckUnattachedThread(LockLevel level) NO_THREAD_SAFETY_ANALYSIS { |
| 83 | // The check below enumerates the cases where we expect not to be able to sanity check locks |
| 84 | // on a thread. Lock checking is disabled to avoid deadlock when checking shutdown lock. |
| 85 | // TODO: tighten this check. |
| 86 | if (kDebugLocking) { |
| 87 | Runtime* runtime = Runtime::Current(); |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 88 | CHECK(runtime == nullptr || !runtime->IsStarted() || runtime->IsShuttingDownLocked() || |
| 89 | // Used during thread creation to avoid races with runtime shutdown. Thread::Current not |
| 90 | // yet established. |
| 91 | level == kRuntimeShutdownLock || |
| 92 | // Thread Ids are allocated/released before threads are established. |
| 93 | level == kAllocatedThreadIdsLock || |
| 94 | // Thread LDT's are initialized without Thread::Current established. |
| 95 | level == kModifyLdtLock || |
| 96 | // Threads are unregistered while holding the thread list lock, during this process they |
| 97 | // no longer exist and so we expect an unlock with no self. |
| 98 | level == kThreadListLock || |
| 99 | // Ignore logging which may or may not have set up thread data structures. |
| 100 | level == kLoggingLock || |
| 101 | // Avoid recursive death. |
Ian Rogers | 06abcdf | 2014-05-23 11:39:11 -0700 | [diff] [blame] | 102 | level == kAbortLock) << level; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Ian Rogers | b6c31ea | 2013-02-04 18:11:33 -0800 | [diff] [blame] | 106 | inline void BaseMutex::RegisterAsLocked(Thread* self) { |
| 107 | if (UNLIKELY(self == NULL)) { |
| 108 | CheckUnattachedThread(level_); |
| 109 | return; |
| 110 | } |
| 111 | if (kDebugLocking) { |
| 112 | // Check if a bad Mutex of this level or lower is held. |
| 113 | bool bad_mutexes_held = false; |
| 114 | for (int i = level_; i >= 0; --i) { |
| 115 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i)); |
| 116 | if (UNLIKELY(held_mutex != NULL)) { |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 117 | LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" " |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 118 | << "(level " << LockLevel(i) << " - " << i |
| 119 | << ") while locking \"" << name_ << "\" " |
| 120 | << "(level " << level_ << " - " << static_cast<int>(level_) << ")"; |
Ian Rogers | b6c31ea | 2013-02-04 18:11:33 -0800 | [diff] [blame] | 121 | if (i > kAbortLock) { |
| 122 | // Only abort in the check below if this is more than abort level lock. |
| 123 | bad_mutexes_held = true; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | CHECK(!bad_mutexes_held); |
| 128 | } |
| 129 | // Don't record monitors as they are outside the scope of analysis. They may be inspected off of |
| 130 | // the monitor list. |
| 131 | if (level_ != kMonitorLock) { |
| 132 | self->SetHeldMutex(level_, this); |
| 133 | } |
| 134 | } |
| 135 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 136 | inline void BaseMutex::RegisterAsUnlocked(Thread* self) { |
| 137 | if (UNLIKELY(self == NULL)) { |
| 138 | CheckUnattachedThread(level_); |
| 139 | return; |
| 140 | } |
| 141 | if (level_ != kMonitorLock) { |
| 142 | if (kDebugLocking && !gAborting) { |
| 143 | CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_; |
| 144 | } |
| 145 | self->SetHeldMutex(level_, NULL); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | inline void ReaderWriterMutex::SharedLock(Thread* self) { |
| 150 | DCHECK(self == NULL || self == Thread::Current()); |
| 151 | #if ART_USE_FUTEXES |
| 152 | bool done = false; |
| 153 | do { |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame^] | 154 | int32_t cur_state = state_.LoadRelaxed(); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 155 | if (LIKELY(cur_state >= 0)) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 156 | // Add as an extra reader. |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame^] | 157 | done = state_.CompareExchangeWeakAcquire(cur_state, cur_state + 1); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 158 | } else { |
| 159 | // Owner holds it exclusively, hang up. |
| 160 | ScopedContentionRecorder scr(this, GetExclusiveOwnerTid(), SafeGetTid(self)); |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame^] | 161 | ++num_pending_readers_; |
| 162 | if (futex(state_.Address(), FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 163 | if (errno != EAGAIN) { |
| 164 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 165 | } |
| 166 | } |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame^] | 167 | --num_pending_readers_; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 168 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 169 | } while (!done); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 170 | #else |
| 171 | CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_)); |
| 172 | #endif |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 173 | DCHECK(exclusive_owner_ == 0U || exclusive_owner_ == -1U); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 174 | RegisterAsLocked(self); |
| 175 | AssertSharedHeld(self); |
| 176 | } |
| 177 | |
| 178 | inline void ReaderWriterMutex::SharedUnlock(Thread* self) { |
| 179 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 180 | DCHECK(exclusive_owner_ == 0U || exclusive_owner_ == -1U); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 181 | AssertSharedHeld(self); |
| 182 | RegisterAsUnlocked(self); |
| 183 | #if ART_USE_FUTEXES |
| 184 | bool done = false; |
| 185 | do { |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame^] | 186 | int32_t cur_state = state_.LoadRelaxed(); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 187 | if (LIKELY(cur_state > 0)) { |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame^] | 188 | // Reduce state by 1 and impose lock release load/store ordering. |
| 189 | // Note, the relaxed loads below musn't reorder before the CompareExchange. |
| 190 | // TODO: the ordering here is non-trivial as state is split across 3 fields, fix by placing |
| 191 | // a status bit into the state on contention. |
| 192 | done = state_.CompareExchangeWeakSequentiallyConsistent(cur_state, cur_state - 1); |
| 193 | if (done && (cur_state - 1) == 0) { // Weak CAS may fail spuriously. |
| 194 | if (num_pending_writers_.LoadRelaxed() > 0 || |
| 195 | num_pending_readers_.LoadRelaxed() > 0) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 196 | // Wake any exclusive waiters as there are now no readers. |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame^] | 197 | futex(state_.Address(), FUTEX_WAKE, -1, NULL, NULL, 0); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } else { |
| 201 | LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_; |
| 202 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 203 | } while (!done); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 204 | #else |
| 205 | CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_)); |
| 206 | #endif |
| 207 | } |
| 208 | |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 209 | inline bool Mutex::IsExclusiveHeld(const Thread* self) const { |
| 210 | DCHECK(self == NULL || self == Thread::Current()); |
| 211 | bool result = (GetExclusiveOwnerTid() == SafeGetTid(self)); |
| 212 | if (kDebugLocking) { |
| 213 | // Sanity debug check that if we think it is locked we have it in our held mutexes. |
| 214 | if (result && self != NULL && level_ != kMonitorLock && !gAborting) { |
| 215 | CHECK_EQ(self->GetHeldMutex(level_), this); |
| 216 | } |
| 217 | } |
| 218 | return result; |
| 219 | } |
| 220 | |
| 221 | inline uint64_t Mutex::GetExclusiveOwnerTid() const { |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 222 | return exclusive_owner_; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | inline bool ReaderWriterMutex::IsExclusiveHeld(const Thread* self) const { |
| 226 | DCHECK(self == NULL || self == Thread::Current()); |
| 227 | bool result = (GetExclusiveOwnerTid() == SafeGetTid(self)); |
| 228 | if (kDebugLocking) { |
| 229 | // Sanity that if the pthread thinks we own the lock the Thread agrees. |
| 230 | if (self != NULL && result) { |
| 231 | CHECK_EQ(self->GetHeldMutex(level_), this); |
| 232 | } |
| 233 | } |
| 234 | return result; |
| 235 | } |
| 236 | |
| 237 | inline uint64_t ReaderWriterMutex::GetExclusiveOwnerTid() const { |
| 238 | #if ART_USE_FUTEXES |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame^] | 239 | int32_t state = state_.LoadRelaxed(); |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 240 | if (state == 0) { |
| 241 | return 0; // No owner. |
| 242 | } else if (state > 0) { |
| 243 | return -1; // Shared. |
| 244 | } else { |
| 245 | return exclusive_owner_; |
| 246 | } |
| 247 | #else |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 248 | return exclusive_owner_; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 249 | #endif |
| 250 | } |
| 251 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 252 | } // namespace art |
| 253 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 254 | #endif // ART_RUNTIME_BASE_MUTEX_INL_H_ |