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