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 | #define __STDC_FORMAT_MACROS 1 |
| 21 | #include <inttypes.h> |
| 22 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 23 | #include "mutex.h" |
| 24 | |
Hiroshi Yamauchi | b373308 | 2013-08-12 17:28:49 -0700 | [diff] [blame] | 25 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
| 26 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 27 | #include "cutils/atomic-inline.h" |
Hiroshi Yamauchi | b373308 | 2013-08-12 17:28:49 -0700 | [diff] [blame] | 28 | #include "cutils/trace.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 | |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 47 | #if defined(__APPLE__) |
| 48 | |
| 49 | // This works on Mac OS 10.6 but hasn't been tested on older releases. |
| 50 | struct __attribute__((__may_alias__)) darwin_pthread_mutex_t { |
| 51 | long padding0; // NOLINT(runtime/int) exact match to darwin type |
| 52 | int padding1; |
| 53 | uint32_t padding2; |
| 54 | int16_t padding3; |
| 55 | int16_t padding4; |
| 56 | uint32_t padding5; |
| 57 | pthread_t darwin_pthread_mutex_owner; |
| 58 | // ...other stuff we don't care about. |
| 59 | }; |
| 60 | |
| 61 | struct __attribute__((__may_alias__)) darwin_pthread_rwlock_t { |
| 62 | long padding0; // NOLINT(runtime/int) exact match to darwin type |
| 63 | pthread_mutex_t padding1; |
| 64 | int padding2; |
| 65 | pthread_cond_t padding3; |
| 66 | pthread_cond_t padding4; |
| 67 | int padding5; |
| 68 | int padding6; |
| 69 | pthread_t darwin_pthread_rwlock_owner; |
| 70 | // ...other stuff we don't care about. |
| 71 | }; |
| 72 | |
| 73 | #endif // __APPLE__ |
| 74 | |
| 75 | #if defined(__GLIBC__) |
| 76 | |
| 77 | struct __attribute__((__may_alias__)) glibc_pthread_mutex_t { |
| 78 | int32_t padding0[2]; |
| 79 | int owner; |
| 80 | // ...other stuff we don't care about. |
| 81 | }; |
| 82 | |
| 83 | struct __attribute__((__may_alias__)) glibc_pthread_rwlock_t { |
| 84 | #ifdef __LP64__ |
| 85 | int32_t padding0[6]; |
| 86 | #else |
| 87 | int32_t padding0[7]; |
| 88 | #endif |
| 89 | int writer; |
| 90 | // ...other stuff we don't care about. |
| 91 | }; |
| 92 | |
| 93 | #endif // __GLIBC__ |
| 94 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 95 | class ScopedContentionRecorder { |
| 96 | public: |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 97 | ScopedContentionRecorder(BaseMutex* mutex, uint64_t blocked_tid, uint64_t owner_tid) |
| 98 | : mutex_(kLogLockContentions ? mutex : NULL), |
| 99 | blocked_tid_(kLogLockContentions ? blocked_tid : 0), |
| 100 | owner_tid_(kLogLockContentions ? owner_tid : 0), |
| 101 | start_nano_time_(kLogLockContentions ? NanoTime() : 0) { |
Ian Rogers | 220228e | 2014-01-23 09:08:16 -0800 | [diff] [blame^] | 102 | std::string msg = StringPrintf("Lock contention on %s (owner tid: %" PRIu64 ")", |
Jeff Hao | 08f2e7b | 2013-09-09 16:44:02 -0700 | [diff] [blame] | 103 | mutex->GetName(), owner_tid); |
| 104 | ATRACE_BEGIN(msg.c_str()); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | ~ScopedContentionRecorder() { |
Jeff Hao | 08f2e7b | 2013-09-09 16:44:02 -0700 | [diff] [blame] | 108 | ATRACE_END(); |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 109 | if (kLogLockContentions) { |
| 110 | uint64_t end_nano_time = NanoTime(); |
| 111 | mutex_->RecordContention(blocked_tid_, owner_tid_, end_nano_time - start_nano_time_); |
| 112 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | private: |
| 116 | BaseMutex* const mutex_; |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 117 | const uint64_t blocked_tid_; |
| 118 | const uint64_t owner_tid_; |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 119 | const uint64_t start_nano_time_; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | static inline uint64_t SafeGetTid(const Thread* self) { |
| 123 | if (self != NULL) { |
| 124 | return static_cast<uint64_t>(self->GetTid()); |
| 125 | } else { |
| 126 | return static_cast<uint64_t>(GetTid()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static inline void CheckUnattachedThread(LockLevel level) NO_THREAD_SAFETY_ANALYSIS { |
| 131 | // The check below enumerates the cases where we expect not to be able to sanity check locks |
| 132 | // on a thread. Lock checking is disabled to avoid deadlock when checking shutdown lock. |
| 133 | // TODO: tighten this check. |
| 134 | if (kDebugLocking) { |
| 135 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 136 | CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDownLocked() || |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 137 | level == kDefaultMutexLevel || level == kRuntimeShutdownLock || |
| 138 | level == kThreadListLock || level == kLoggingLock || level == kAbortLock); |
| 139 | } |
| 140 | } |
| 141 | |
Ian Rogers | b6c31ea | 2013-02-04 18:11:33 -0800 | [diff] [blame] | 142 | inline void BaseMutex::RegisterAsLocked(Thread* self) { |
| 143 | if (UNLIKELY(self == NULL)) { |
| 144 | CheckUnattachedThread(level_); |
| 145 | return; |
| 146 | } |
| 147 | if (kDebugLocking) { |
| 148 | // Check if a bad Mutex of this level or lower is held. |
| 149 | bool bad_mutexes_held = false; |
| 150 | for (int i = level_; i >= 0; --i) { |
| 151 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i)); |
| 152 | if (UNLIKELY(held_mutex != NULL)) { |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 153 | LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" " |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 154 | << "(level " << LockLevel(i) << " - " << i |
| 155 | << ") while locking \"" << name_ << "\" " |
| 156 | << "(level " << level_ << " - " << static_cast<int>(level_) << ")"; |
Ian Rogers | b6c31ea | 2013-02-04 18:11:33 -0800 | [diff] [blame] | 157 | if (i > kAbortLock) { |
| 158 | // Only abort in the check below if this is more than abort level lock. |
| 159 | bad_mutexes_held = true; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | CHECK(!bad_mutexes_held); |
| 164 | } |
| 165 | // Don't record monitors as they are outside the scope of analysis. They may be inspected off of |
| 166 | // the monitor list. |
| 167 | if (level_ != kMonitorLock) { |
| 168 | self->SetHeldMutex(level_, this); |
| 169 | } |
| 170 | } |
| 171 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 172 | inline void BaseMutex::RegisterAsUnlocked(Thread* self) { |
| 173 | if (UNLIKELY(self == NULL)) { |
| 174 | CheckUnattachedThread(level_); |
| 175 | return; |
| 176 | } |
| 177 | if (level_ != kMonitorLock) { |
| 178 | if (kDebugLocking && !gAborting) { |
| 179 | CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_; |
| 180 | } |
| 181 | self->SetHeldMutex(level_, NULL); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | inline void ReaderWriterMutex::SharedLock(Thread* self) { |
| 186 | DCHECK(self == NULL || self == Thread::Current()); |
| 187 | #if ART_USE_FUTEXES |
| 188 | bool done = false; |
| 189 | do { |
| 190 | int32_t cur_state = state_; |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 191 | if (LIKELY(cur_state >= 0)) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 192 | // Add as an extra reader. |
| 193 | done = android_atomic_acquire_cas(cur_state, cur_state + 1, &state_) == 0; |
| 194 | } else { |
| 195 | // Owner holds it exclusively, hang up. |
| 196 | ScopedContentionRecorder scr(this, GetExclusiveOwnerTid(), SafeGetTid(self)); |
| 197 | android_atomic_inc(&num_pending_readers_); |
| 198 | if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) { |
| 199 | if (errno != EAGAIN) { |
| 200 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 201 | } |
| 202 | } |
| 203 | android_atomic_dec(&num_pending_readers_); |
| 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_rdlock, (&rwlock_)); |
| 208 | #endif |
| 209 | RegisterAsLocked(self); |
| 210 | AssertSharedHeld(self); |
| 211 | } |
| 212 | |
| 213 | inline void ReaderWriterMutex::SharedUnlock(Thread* self) { |
| 214 | DCHECK(self == NULL || self == Thread::Current()); |
| 215 | AssertSharedHeld(self); |
| 216 | RegisterAsUnlocked(self); |
| 217 | #if ART_USE_FUTEXES |
| 218 | bool done = false; |
| 219 | do { |
| 220 | int32_t cur_state = state_; |
| 221 | if (LIKELY(cur_state > 0)) { |
| 222 | // Reduce state by 1. |
| 223 | done = android_atomic_release_cas(cur_state, cur_state - 1, &state_) == 0; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 224 | if (done && (cur_state - 1) == 0) { // cas may fail due to noise? |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 225 | if (num_pending_writers_ > 0 || num_pending_readers_ > 0) { |
| 226 | // Wake any exclusive waiters as there are now no readers. |
| 227 | futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0); |
| 228 | } |
| 229 | } |
| 230 | } else { |
| 231 | LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_; |
| 232 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 233 | } while (!done); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 234 | #else |
| 235 | CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_)); |
| 236 | #endif |
| 237 | } |
| 238 | |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 239 | inline bool Mutex::IsExclusiveHeld(const Thread* self) const { |
| 240 | DCHECK(self == NULL || self == Thread::Current()); |
| 241 | bool result = (GetExclusiveOwnerTid() == SafeGetTid(self)); |
| 242 | if (kDebugLocking) { |
| 243 | // Sanity debug check that if we think it is locked we have it in our held mutexes. |
| 244 | if (result && self != NULL && level_ != kMonitorLock && !gAborting) { |
| 245 | CHECK_EQ(self->GetHeldMutex(level_), this); |
| 246 | } |
| 247 | } |
| 248 | return result; |
| 249 | } |
| 250 | |
| 251 | inline uint64_t Mutex::GetExclusiveOwnerTid() const { |
| 252 | #if ART_USE_FUTEXES |
| 253 | return exclusive_owner_; |
| 254 | #elif defined(__BIONIC__) |
| 255 | return static_cast<uint64_t>((mutex_.value >> 16) & 0xffff); |
| 256 | #elif defined(__GLIBC__) |
| 257 | return reinterpret_cast<const glibc_pthread_mutex_t*>(&mutex_)->owner; |
| 258 | #elif defined(__APPLE__) |
| 259 | const darwin_pthread_mutex_t* dpmutex = reinterpret_cast<const darwin_pthread_mutex_t*>(&mutex_); |
| 260 | pthread_t owner = dpmutex->darwin_pthread_mutex_owner; |
| 261 | // 0 for unowned, -1 for PTHREAD_MTX_TID_SWITCHING |
| 262 | // TODO: should we make darwin_pthread_mutex_owner volatile and recheck until not -1? |
| 263 | if ((owner == (pthread_t)0) || (owner == (pthread_t)-1)) { |
| 264 | return 0; |
| 265 | } |
| 266 | uint64_t tid; |
| 267 | CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6 |
| 268 | return tid; |
| 269 | #else |
| 270 | #error unsupported C library |
| 271 | #endif |
| 272 | } |
| 273 | |
| 274 | inline bool ReaderWriterMutex::IsExclusiveHeld(const Thread* self) const { |
| 275 | DCHECK(self == NULL || self == Thread::Current()); |
| 276 | bool result = (GetExclusiveOwnerTid() == SafeGetTid(self)); |
| 277 | if (kDebugLocking) { |
| 278 | // Sanity that if the pthread thinks we own the lock the Thread agrees. |
| 279 | if (self != NULL && result) { |
| 280 | CHECK_EQ(self->GetHeldMutex(level_), this); |
| 281 | } |
| 282 | } |
| 283 | return result; |
| 284 | } |
| 285 | |
| 286 | inline uint64_t ReaderWriterMutex::GetExclusiveOwnerTid() const { |
| 287 | #if ART_USE_FUTEXES |
| 288 | int32_t state = state_; |
| 289 | if (state == 0) { |
| 290 | return 0; // No owner. |
| 291 | } else if (state > 0) { |
| 292 | return -1; // Shared. |
| 293 | } else { |
| 294 | return exclusive_owner_; |
| 295 | } |
| 296 | #else |
| 297 | #if defined(__BIONIC__) |
| 298 | return rwlock_.writerThreadId; |
| 299 | #elif defined(__GLIBC__) |
| 300 | return reinterpret_cast<const glibc_pthread_rwlock_t*>(&rwlock_)->writer; |
| 301 | #elif defined(__APPLE__) |
| 302 | const darwin_pthread_rwlock_t* |
| 303 | dprwlock = reinterpret_cast<const darwin_pthread_rwlock_t*>(&rwlock_); |
| 304 | pthread_t owner = dprwlock->darwin_pthread_rwlock_owner; |
| 305 | if (owner == (pthread_t)0) { |
| 306 | return 0; |
| 307 | } |
| 308 | uint64_t tid; |
| 309 | CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6 |
| 310 | return tid; |
| 311 | #else |
| 312 | #error unsupported C library |
| 313 | #endif |
| 314 | #endif |
| 315 | } |
| 316 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 317 | } // namespace art |
| 318 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 319 | #endif // ART_RUNTIME_BASE_MUTEX_INL_H_ |