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 | |
| 20 | #include "mutex.h" |
| 21 | |
| 22 | #include "cutils/atomic-inline.h" |
| 23 | #include "runtime.h" |
| 24 | #include "thread.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | #define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_) |
| 29 | |
| 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 |
| 36 | static inline int futex(volatile int *uaddr, int op, int val, const struct timespec *timeout, volatile int *uaddr2, int val3) { |
| 37 | return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3); |
| 38 | } |
| 39 | #endif // ART_USE_FUTEXES |
| 40 | |
| 41 | class ScopedContentionRecorder { |
| 42 | public: |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 43 | #if CONTENTION_LOGGING |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 44 | ScopedContentionRecorder(BaseMutex* mutex, uint64_t blocked_tid, uint64_t owner_tid) : |
| 45 | mutex_(mutex), blocked_tid_(blocked_tid), owner_tid_(owner_tid), |
| 46 | start_milli_time_(MilliTime()) { |
| 47 | } |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 48 | #else |
| 49 | ScopedContentionRecorder(BaseMutex*, uint64_t, uint64_t) {} |
| 50 | #endif |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 51 | |
| 52 | ~ScopedContentionRecorder() { |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 53 | #if CONTENTION_LOGGING |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 54 | uint64_t end_milli_time = MilliTime(); |
| 55 | mutex_->RecordContention(blocked_tid_, owner_tid_, end_milli_time - start_milli_time_); |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 56 | #endif |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | private: |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 60 | #if CONTENTION_LOGGING |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 61 | BaseMutex* const mutex_; |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 62 | const uint64_t blocked_tid_; |
| 63 | const uint64_t owner_tid_; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 64 | const uint64_t start_milli_time_; |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 65 | #endif |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static inline uint64_t SafeGetTid(const Thread* self) { |
| 69 | if (self != NULL) { |
| 70 | return static_cast<uint64_t>(self->GetTid()); |
| 71 | } else { |
| 72 | return static_cast<uint64_t>(GetTid()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static inline void CheckUnattachedThread(LockLevel level) NO_THREAD_SAFETY_ANALYSIS { |
| 77 | // The check below enumerates the cases where we expect not to be able to sanity check locks |
| 78 | // on a thread. Lock checking is disabled to avoid deadlock when checking shutdown lock. |
| 79 | // TODO: tighten this check. |
| 80 | if (kDebugLocking) { |
| 81 | Runtime* runtime = Runtime::Current(); |
| 82 | CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDown() || |
| 83 | level == kDefaultMutexLevel || level == kRuntimeShutdownLock || |
| 84 | level == kThreadListLock || level == kLoggingLock || level == kAbortLock); |
| 85 | } |
| 86 | } |
| 87 | |
Ian Rogers | b6c31ea | 2013-02-04 18:11:33 -0800 | [diff] [blame] | 88 | inline void BaseMutex::RegisterAsLocked(Thread* self) { |
| 89 | if (UNLIKELY(self == NULL)) { |
| 90 | CheckUnattachedThread(level_); |
| 91 | return; |
| 92 | } |
| 93 | if (kDebugLocking) { |
| 94 | // Check if a bad Mutex of this level or lower is held. |
| 95 | bool bad_mutexes_held = false; |
| 96 | for (int i = level_; i >= 0; --i) { |
| 97 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i)); |
| 98 | if (UNLIKELY(held_mutex != NULL)) { |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 99 | LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" " |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 100 | << "(level " << LockLevel(i) << " - " << i |
| 101 | << ") while locking \"" << name_ << "\" " |
| 102 | << "(level " << level_ << " - " << static_cast<int>(level_) << ")"; |
Ian Rogers | b6c31ea | 2013-02-04 18:11:33 -0800 | [diff] [blame] | 103 | if (i > kAbortLock) { |
| 104 | // Only abort in the check below if this is more than abort level lock. |
| 105 | bad_mutexes_held = true; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | CHECK(!bad_mutexes_held); |
| 110 | } |
| 111 | // Don't record monitors as they are outside the scope of analysis. They may be inspected off of |
| 112 | // the monitor list. |
| 113 | if (level_ != kMonitorLock) { |
| 114 | self->SetHeldMutex(level_, this); |
| 115 | } |
| 116 | } |
| 117 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 118 | inline void BaseMutex::RegisterAsUnlocked(Thread* self) { |
| 119 | if (UNLIKELY(self == NULL)) { |
| 120 | CheckUnattachedThread(level_); |
| 121 | return; |
| 122 | } |
| 123 | if (level_ != kMonitorLock) { |
| 124 | if (kDebugLocking && !gAborting) { |
| 125 | CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_; |
| 126 | } |
| 127 | self->SetHeldMutex(level_, NULL); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | inline void ReaderWriterMutex::SharedLock(Thread* self) { |
| 132 | DCHECK(self == NULL || self == Thread::Current()); |
| 133 | #if ART_USE_FUTEXES |
| 134 | bool done = false; |
| 135 | do { |
| 136 | int32_t cur_state = state_; |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 137 | if (LIKELY(cur_state >= 0)) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 138 | // Add as an extra reader. |
| 139 | done = android_atomic_acquire_cas(cur_state, cur_state + 1, &state_) == 0; |
| 140 | } else { |
| 141 | // Owner holds it exclusively, hang up. |
| 142 | ScopedContentionRecorder scr(this, GetExclusiveOwnerTid(), SafeGetTid(self)); |
| 143 | android_atomic_inc(&num_pending_readers_); |
| 144 | if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) { |
| 145 | if (errno != EAGAIN) { |
| 146 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 147 | } |
| 148 | } |
| 149 | android_atomic_dec(&num_pending_readers_); |
| 150 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 151 | } while (!done); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 152 | #else |
| 153 | CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_)); |
| 154 | #endif |
| 155 | RegisterAsLocked(self); |
| 156 | AssertSharedHeld(self); |
| 157 | } |
| 158 | |
| 159 | inline void ReaderWriterMutex::SharedUnlock(Thread* self) { |
| 160 | DCHECK(self == NULL || self == Thread::Current()); |
| 161 | AssertSharedHeld(self); |
| 162 | RegisterAsUnlocked(self); |
| 163 | #if ART_USE_FUTEXES |
| 164 | bool done = false; |
| 165 | do { |
| 166 | int32_t cur_state = state_; |
| 167 | if (LIKELY(cur_state > 0)) { |
| 168 | // Reduce state by 1. |
| 169 | done = android_atomic_release_cas(cur_state, cur_state - 1, &state_) == 0; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame^] | 170 | if (done && (cur_state - 1) == 0) { // cas may fail due to noise? |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 171 | if (num_pending_writers_ > 0 || num_pending_readers_ > 0) { |
| 172 | // Wake any exclusive waiters as there are now no readers. |
| 173 | futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0); |
| 174 | } |
| 175 | } |
| 176 | } else { |
| 177 | LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_; |
| 178 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 179 | } while (!done); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 180 | #else |
| 181 | CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_)); |
| 182 | #endif |
| 183 | } |
| 184 | |
| 185 | } // namespace art |
| 186 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 187 | #endif // ART_RUNTIME_BASE_MUTEX_INL_H_ |