blob: 0b13e312d3d615ae5b7115bdf4a63b29176b0b32 [file] [log] [blame]
Ian Rogers693ff612013-02-01 10:56:12 -08001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_MUTEX_INL_H_
18#define ART_RUNTIME_BASE_MUTEX_INL_H_
Ian Rogers693ff612013-02-01 10:56:12 -080019
Ian Rogers220228e2014-01-23 09:08:16 -080020#include <inttypes.h>
21
Ian Rogers693ff612013-02-01 10:56:12 -080022#include "mutex.h"
23
Ian Rogers576ca0c2014-06-06 15:58:22 -070024#include "base/stringprintf.h"
Ian Rogerscf7f1912014-10-22 22:06:39 -070025#include "base/value_object.h"
Ian Rogers693ff612013-02-01 10:56:12 -080026#include "runtime.h"
27#include "thread.h"
28
Ian Rogers693ff612013-02-01 10:56:12 -080029#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 Hsieh729c1cf2014-11-06 10:49:16 -080035#define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_)
36#endif // ART_USE_FUTEXES
37
38namespace art {
39
40#if ART_USE_FUTEXES
Ian Rogers693ff612013-02-01 10:56:12 -080041static inline int futex(volatile int *uaddr, int op, int val, const struct timespec *timeout, volatile int *uaddr2, int val3) {
42 return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3);
43}
44#endif // ART_USE_FUTEXES
45
Ian Rogers693ff612013-02-01 10:56:12 -080046static inline uint64_t SafeGetTid(const Thread* self) {
47 if (self != NULL) {
48 return static_cast<uint64_t>(self->GetTid());
49 } else {
50 return static_cast<uint64_t>(GetTid());
51 }
52}
53
54static inline void CheckUnattachedThread(LockLevel level) NO_THREAD_SAFETY_ANALYSIS {
55 // The check below enumerates the cases where we expect not to be able to sanity check locks
56 // on a thread. Lock checking is disabled to avoid deadlock when checking shutdown lock.
57 // TODO: tighten this check.
58 if (kDebugLocking) {
59 Runtime* runtime = Runtime::Current();
Chao-ying Fu9e369312014-05-21 11:20:52 -070060 CHECK(runtime == nullptr || !runtime->IsStarted() || runtime->IsShuttingDownLocked() ||
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 ||
73 // Avoid recursive death.
Ian Rogers06abcdf2014-05-23 11:39:11 -070074 level == kAbortLock) << level;
Ian Rogers693ff612013-02-01 10:56:12 -080075 }
76}
77
Ian Rogersb6c31ea2013-02-04 18:11:33 -080078inline void BaseMutex::RegisterAsLocked(Thread* self) {
79 if (UNLIKELY(self == NULL)) {
80 CheckUnattachedThread(level_);
81 return;
82 }
83 if (kDebugLocking) {
84 // Check if a bad Mutex of this level or lower is held.
85 bool bad_mutexes_held = false;
86 for (int i = level_; i >= 0; --i) {
87 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
88 if (UNLIKELY(held_mutex != NULL)) {
Elliott Hughes0f827162013-02-26 12:12:58 -080089 LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" "
Ian Rogers62d6c772013-02-27 08:32:07 -080090 << "(level " << LockLevel(i) << " - " << i
91 << ") while locking \"" << name_ << "\" "
92 << "(level " << level_ << " - " << static_cast<int>(level_) << ")";
Ian Rogersb6c31ea2013-02-04 18:11:33 -080093 if (i > kAbortLock) {
94 // Only abort in the check below if this is more than abort level lock.
95 bad_mutexes_held = true;
96 }
97 }
98 }
99 CHECK(!bad_mutexes_held);
100 }
101 // Don't record monitors as they are outside the scope of analysis. They may be inspected off of
102 // the monitor list.
103 if (level_ != kMonitorLock) {
104 self->SetHeldMutex(level_, this);
105 }
106}
107
Ian Rogers693ff612013-02-01 10:56:12 -0800108inline void BaseMutex::RegisterAsUnlocked(Thread* self) {
109 if (UNLIKELY(self == NULL)) {
110 CheckUnattachedThread(level_);
111 return;
112 }
113 if (level_ != kMonitorLock) {
114 if (kDebugLocking && !gAborting) {
115 CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_;
116 }
117 self->SetHeldMutex(level_, NULL);
118 }
119}
120
121inline void ReaderWriterMutex::SharedLock(Thread* self) {
122 DCHECK(self == NULL || self == Thread::Current());
123#if ART_USE_FUTEXES
124 bool done = false;
125 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700126 int32_t cur_state = state_.LoadRelaxed();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800127 if (LIKELY(cur_state >= 0)) {
Ian Rogers693ff612013-02-01 10:56:12 -0800128 // Add as an extra reader.
Ian Rogersc7190692014-07-08 23:50:26 -0700129 done = state_.CompareExchangeWeakAcquire(cur_state, cur_state + 1);
Ian Rogers693ff612013-02-01 10:56:12 -0800130 } else {
Ian Rogerscf7f1912014-10-22 22:06:39 -0700131 HandleSharedLockContention(self, cur_state);
Ian Rogers693ff612013-02-01 10:56:12 -0800132 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700133 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800134#else
135 CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_));
136#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700137 DCHECK(exclusive_owner_ == 0U || exclusive_owner_ == -1U);
Ian Rogers693ff612013-02-01 10:56:12 -0800138 RegisterAsLocked(self);
139 AssertSharedHeld(self);
140}
141
142inline void ReaderWriterMutex::SharedUnlock(Thread* self) {
143 DCHECK(self == NULL || self == Thread::Current());
Ian Rogersc5f17732014-06-05 20:48:42 -0700144 DCHECK(exclusive_owner_ == 0U || exclusive_owner_ == -1U);
Ian Rogers693ff612013-02-01 10:56:12 -0800145 AssertSharedHeld(self);
146 RegisterAsUnlocked(self);
147#if ART_USE_FUTEXES
148 bool done = false;
149 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700150 int32_t cur_state = state_.LoadRelaxed();
Ian Rogers693ff612013-02-01 10:56:12 -0800151 if (LIKELY(cur_state > 0)) {
Ian Rogersc7190692014-07-08 23:50:26 -0700152 // Reduce state by 1 and impose lock release load/store ordering.
153 // Note, the relaxed loads below musn't reorder before the CompareExchange.
154 // TODO: the ordering here is non-trivial as state is split across 3 fields, fix by placing
155 // a status bit into the state on contention.
156 done = state_.CompareExchangeWeakSequentiallyConsistent(cur_state, cur_state - 1);
157 if (done && (cur_state - 1) == 0) { // Weak CAS may fail spuriously.
158 if (num_pending_writers_.LoadRelaxed() > 0 ||
159 num_pending_readers_.LoadRelaxed() > 0) {
Ian Rogers693ff612013-02-01 10:56:12 -0800160 // Wake any exclusive waiters as there are now no readers.
Ian Rogersc7190692014-07-08 23:50:26 -0700161 futex(state_.Address(), FUTEX_WAKE, -1, NULL, NULL, 0);
Ian Rogers693ff612013-02-01 10:56:12 -0800162 }
163 }
164 } else {
165 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
166 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700167 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800168#else
169 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
170#endif
171}
172
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700173inline bool Mutex::IsExclusiveHeld(const Thread* self) const {
174 DCHECK(self == NULL || self == Thread::Current());
175 bool result = (GetExclusiveOwnerTid() == SafeGetTid(self));
176 if (kDebugLocking) {
177 // Sanity debug check that if we think it is locked we have it in our held mutexes.
178 if (result && self != NULL && level_ != kMonitorLock && !gAborting) {
179 CHECK_EQ(self->GetHeldMutex(level_), this);
180 }
181 }
182 return result;
183}
184
185inline uint64_t Mutex::GetExclusiveOwnerTid() const {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700186 return exclusive_owner_;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700187}
188
189inline bool ReaderWriterMutex::IsExclusiveHeld(const Thread* self) const {
190 DCHECK(self == NULL || self == Thread::Current());
191 bool result = (GetExclusiveOwnerTid() == SafeGetTid(self));
192 if (kDebugLocking) {
193 // Sanity that if the pthread thinks we own the lock the Thread agrees.
194 if (self != NULL && result) {
195 CHECK_EQ(self->GetHeldMutex(level_), this);
196 }
197 }
198 return result;
199}
200
201inline uint64_t ReaderWriterMutex::GetExclusiveOwnerTid() const {
202#if ART_USE_FUTEXES
Ian Rogersc7190692014-07-08 23:50:26 -0700203 int32_t state = state_.LoadRelaxed();
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700204 if (state == 0) {
205 return 0; // No owner.
206 } else if (state > 0) {
207 return -1; // Shared.
208 } else {
209 return exclusive_owner_;
210 }
211#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700212 return exclusive_owner_;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700213#endif
214}
215
Ian Rogers693ff612013-02-01 10:56:12 -0800216} // namespace art
217
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700218#endif // ART_RUNTIME_BASE_MUTEX_INL_H_