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