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