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