blob: 05e3a8327e83cb22bd1de48de195a89df147c739 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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
17#include "mutex.h"
18
19#include <errno.h>
Ian Rogersc604d732012-10-14 16:09:54 -070020#include <sys/time.h>
Elliott Hughes8daa0922011-09-11 13:46:25 -070021
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070022#include "atomic.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers693ff612013-02-01 10:56:12 -080024#include "mutex-inl.h"
Elliott Hughes6b355752012-01-13 16:49:08 -080025#include "runtime.h"
Ian Rogersc604d732012-10-14 16:09:54 -070026#include "scoped_thread_state_change.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070027#include "thread-inl.h"
Elliott Hughesffb465f2012-03-01 18:46:05 -080028#include "utils.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070029
30namespace art {
31
Ian Rogersc604d732012-10-14 16:09:54 -070032#if ART_USE_FUTEXES
33static bool ComputeRelativeTimeSpec(timespec* result_ts, const timespec& lhs, const timespec& rhs) {
Brian Carlstromfb6996f2013-07-18 18:21:14 -070034 const int32_t one_sec = 1000 * 1000 * 1000; // one second in nanoseconds.
Ian Rogersc604d732012-10-14 16:09:54 -070035 result_ts->tv_sec = lhs.tv_sec - rhs.tv_sec;
36 result_ts->tv_nsec = lhs.tv_nsec - rhs.tv_nsec;
37 if (result_ts->tv_nsec < 0) {
38 result_ts->tv_sec--;
39 result_ts->tv_nsec += one_sec;
40 } else if (result_ts->tv_nsec > one_sec) {
41 result_ts->tv_sec++;
42 result_ts->tv_nsec -= one_sec;
43 }
44 return result_ts->tv_sec < 0;
45}
46#endif
47
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070048struct AllMutexData {
49 // A guard for all_mutexes_ that's not a mutex (Mutexes must CAS to acquire and busy wait).
50 AtomicInteger all_mutexes_guard;
51 // All created mutexes guarded by all_mutexes_guard_.
52 std::set<BaseMutex*>* all_mutexes;
53 AllMutexData() : all_mutexes(NULL) {}
54};
Ian Rogersd9c4fc92013-10-01 19:45:43 -070055static struct AllMutexData gAllMutexData[kAllMutexDataSize];
Ian Rogers56edc432013-01-18 16:51:51 -080056
57class ScopedAllMutexesLock {
58 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070059 explicit ScopedAllMutexesLock(const BaseMutex* mutex) : mutex_(mutex) {
Ian Rogersb122a4b2013-11-19 18:00:50 -080060 while (!gAllMutexData->all_mutexes_guard.CompareAndSwap(0, reinterpret_cast<int32_t>(mutex))) {
Ian Rogers56edc432013-01-18 16:51:51 -080061 NanoSleep(100);
62 }
63 }
64 ~ScopedAllMutexesLock() {
Ian Rogersb122a4b2013-11-19 18:00:50 -080065 while (!gAllMutexData->all_mutexes_guard.CompareAndSwap(reinterpret_cast<int32_t>(mutex_), 0)) {
Ian Rogers56edc432013-01-18 16:51:51 -080066 NanoSleep(100);
67 }
68 }
69 private:
70 const BaseMutex* const mutex_;
71};
Ian Rogers56edc432013-01-18 16:51:51 -080072
73BaseMutex::BaseMutex(const char* name, LockLevel level) : level_(level), name_(name) {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070074 if (kLogLockContentions) {
75 ScopedAllMutexesLock mu(this);
Ian Rogersd9c4fc92013-10-01 19:45:43 -070076 std::set<BaseMutex*>** all_mutexes_ptr = &gAllMutexData->all_mutexes;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070077 if (*all_mutexes_ptr == NULL) {
78 // We leak the global set of all mutexes to avoid ordering issues in global variable
79 // construction/destruction.
80 *all_mutexes_ptr = new std::set<BaseMutex*>();
81 }
82 (*all_mutexes_ptr)->insert(this);
Ian Rogers56edc432013-01-18 16:51:51 -080083 }
Ian Rogers56edc432013-01-18 16:51:51 -080084}
85
86BaseMutex::~BaseMutex() {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070087 if (kLogLockContentions) {
88 ScopedAllMutexesLock mu(this);
Ian Rogersd9c4fc92013-10-01 19:45:43 -070089 gAllMutexData->all_mutexes->erase(this);
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070090 }
Ian Rogers56edc432013-01-18 16:51:51 -080091}
92
93void BaseMutex::DumpAll(std::ostream& os) {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070094 if (kLogLockContentions) {
95 os << "Mutex logging:\n";
96 ScopedAllMutexesLock mu(reinterpret_cast<const BaseMutex*>(-1));
Ian Rogersd9c4fc92013-10-01 19:45:43 -070097 std::set<BaseMutex*>* all_mutexes = gAllMutexData->all_mutexes;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070098 if (all_mutexes == NULL) {
99 // No mutexes have been created yet during at startup.
100 return;
101 }
102 typedef std::set<BaseMutex*>::const_iterator It;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700103 os << "(Contended)\n";
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700104 for (It it = all_mutexes->begin(); it != all_mutexes->end(); ++it) {
105 BaseMutex* mutex = *it;
106 if (mutex->HasEverContended()) {
107 mutex->Dump(os);
108 os << "\n";
109 }
110 }
111 os << "(Never contented)\n";
112 for (It it = all_mutexes->begin(); it != all_mutexes->end(); ++it) {
113 BaseMutex* mutex = *it;
114 if (!mutex->HasEverContended()) {
115 mutex->Dump(os);
116 os << "\n";
117 }
118 }
Ian Rogers56edc432013-01-18 16:51:51 -0800119 }
Ian Rogers56edc432013-01-18 16:51:51 -0800120}
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121
Ian Rogers81d425b2012-09-27 16:03:43 -0700122void BaseMutex::CheckSafeToWait(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700123 if (self == NULL) {
124 CheckUnattachedThread(level_);
125 return;
126 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700127 if (kDebugLocking) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700128 CHECK(self->GetHeldMutex(level_) == this || level_ == kMonitorLock)
129 << "Waiting on unacquired mutex: " << name_;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700130 bool bad_mutexes_held = false;
Elliott Hughes0f827162013-02-26 12:12:58 -0800131 for (int i = kLockLevelCount - 1; i >= 0; --i) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700132 if (i != level_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700133 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
Ian Rogers25fd14b2012-09-05 10:56:38 -0700134 if (held_mutex != NULL) {
Elliott Hughes0f827162013-02-26 12:12:58 -0800135 LOG(ERROR) << "Holding \"" << held_mutex->name_ << "\" "
136 << "(level " << LockLevel(i) << ") while performing wait on "
137 << "\"" << name_ << "\" (level " << level_ << ")";
Ian Rogers25fd14b2012-09-05 10:56:38 -0700138 bad_mutexes_held = true;
139 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140 }
141 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700142 CHECK(!bad_mutexes_held);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144}
145
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700146inline void BaseMutex::ContentionLogData::AddToWaitTime(uint64_t value) {
147 if (kLogLockContentions) {
148 // Atomically add value to wait_time.
149 uint64_t new_val, old_val;
150 volatile int64_t* addr = reinterpret_cast<volatile int64_t*>(&wait_time);
151 volatile const int64_t* caddr = const_cast<volatile const int64_t*>(addr);
152 do {
153 old_val = static_cast<uint64_t>(QuasiAtomic::Read64(caddr));
154 new_val = old_val + value;
155 } while (!QuasiAtomic::Cas64(static_cast<int64_t>(old_val), static_cast<int64_t>(new_val), addr));
156 }
157}
158
Brian Carlstrom0de79852013-07-25 22:29:58 -0700159void BaseMutex::RecordContention(uint64_t blocked_tid,
160 uint64_t owner_tid,
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700161 uint64_t nano_time_blocked) {
162 if (kLogLockContentions) {
163 ContentionLogData* data = contetion_log_data_;
164 ++(data->contention_count);
165 data->AddToWaitTime(nano_time_blocked);
166 ContentionLogEntry* log = data->contention_log;
167 // This code is intentionally racy as it is only used for diagnostics.
168 uint32_t slot = data->cur_content_log_entry;
169 if (log[slot].blocked_tid == blocked_tid &&
170 log[slot].owner_tid == blocked_tid) {
171 ++log[slot].count;
172 } else {
173 uint32_t new_slot;
174 do {
175 slot = data->cur_content_log_entry;
176 new_slot = (slot + 1) % kContentionLogSize;
Ian Rogersb122a4b2013-11-19 18:00:50 -0800177 } while (!data->cur_content_log_entry.CompareAndSwap(slot, new_slot));
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700178 log[new_slot].blocked_tid = blocked_tid;
179 log[new_slot].owner_tid = owner_tid;
180 log[new_slot].count = 1;
181 }
Ian Rogers56edc432013-01-18 16:51:51 -0800182 }
Ian Rogers56edc432013-01-18 16:51:51 -0800183}
184
Ian Rogers56edc432013-01-18 16:51:51 -0800185void BaseMutex::DumpContention(std::ostream& os) const {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700186 if (kLogLockContentions) {
187 const ContentionLogData* data = contetion_log_data_;
188 const ContentionLogEntry* log = data->contention_log;
189 uint64_t wait_time = data->wait_time;
190 uint32_t contention_count = data->contention_count;
191 if (contention_count == 0) {
192 os << "never contended";
193 } else {
194 os << "contended " << contention_count
195 << " times, average wait of contender " << PrettyDuration(wait_time / contention_count);
196 SafeMap<uint64_t, size_t> most_common_blocker;
197 SafeMap<uint64_t, size_t> most_common_blocked;
198 typedef SafeMap<uint64_t, size_t>::const_iterator It;
199 for (size_t i = 0; i < kContentionLogSize; ++i) {
200 uint64_t blocked_tid = log[i].blocked_tid;
201 uint64_t owner_tid = log[i].owner_tid;
202 uint32_t count = log[i].count;
203 if (count > 0) {
204 It it = most_common_blocked.find(blocked_tid);
205 if (it != most_common_blocked.end()) {
206 most_common_blocked.Overwrite(blocked_tid, it->second + count);
207 } else {
208 most_common_blocked.Put(blocked_tid, count);
209 }
210 it = most_common_blocker.find(owner_tid);
211 if (it != most_common_blocker.end()) {
212 most_common_blocker.Overwrite(owner_tid, it->second + count);
213 } else {
214 most_common_blocker.Put(owner_tid, count);
215 }
Ian Rogers56edc432013-01-18 16:51:51 -0800216 }
217 }
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700218 uint64_t max_tid = 0;
219 size_t max_tid_count = 0;
220 for (It it = most_common_blocked.begin(); it != most_common_blocked.end(); ++it) {
221 if (it->second > max_tid_count) {
222 max_tid = it->first;
223 max_tid_count = it->second;
224 }
Ian Rogers56edc432013-01-18 16:51:51 -0800225 }
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700226 if (max_tid != 0) {
227 os << " sample shows most blocked tid=" << max_tid;
Ian Rogers56edc432013-01-18 16:51:51 -0800228 }
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700229 max_tid = 0;
230 max_tid_count = 0;
231 for (It it = most_common_blocker.begin(); it != most_common_blocker.end(); ++it) {
232 if (it->second > max_tid_count) {
233 max_tid = it->first;
234 max_tid_count = it->second;
235 }
236 }
237 if (max_tid != 0) {
238 os << " sample shows tid=" << max_tid << " owning during this time";
239 }
Ian Rogers56edc432013-01-18 16:51:51 -0800240 }
241 }
Ian Rogers56edc432013-01-18 16:51:51 -0800242}
243
244
Ian Rogers81d425b2012-09-27 16:03:43 -0700245Mutex::Mutex(const char* name, LockLevel level, bool recursive)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 : BaseMutex(name, level), recursive_(recursive), recursion_count_(0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700247#if ART_USE_FUTEXES
248 state_ = 0;
249 exclusive_owner_ = 0;
250 num_contenders_ = 0;
251#elif defined(__BIONIC__) || defined(__APPLE__)
Brian Carlstromf3a26412012-08-24 11:06:02 -0700252 // Use recursive mutexes for bionic and Apple otherwise the
253 // non-recursive mutexes don't have TIDs to check lock ownership of.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800254 pthread_mutexattr_t attributes;
255 CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes));
256 CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE));
257 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes));
258 CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259#else
260 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, NULL));
261#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -0700262}
263
264Mutex::~Mutex() {
Ian Rogersc604d732012-10-14 16:09:54 -0700265#if ART_USE_FUTEXES
266 if (state_ != 0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700267 Runtime* runtime = Runtime::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700268 bool shutting_down = runtime == nullptr || runtime->IsShuttingDown(Thread::Current());
Ian Rogersc604d732012-10-14 16:09:54 -0700269 LOG(shutting_down ? WARNING : FATAL) << "destroying mutex with owner: " << exclusive_owner_;
270 } else {
271 CHECK_EQ(exclusive_owner_, 0U) << "unexpectedly found an owner on unlocked mutex " << name_;
272 CHECK_EQ(num_contenders_, 0) << "unexpectedly found a contender on mutex " << name_;
273 }
274#else
Elliott Hughese62934d2012-04-09 11:24:29 -0700275 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
276 // may still be using locks.
Elliott Hughes6b355752012-01-13 16:49:08 -0800277 int rc = pthread_mutex_destroy(&mutex_);
278 if (rc != 0) {
279 errno = rc;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800280 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Ian Rogers50b35e22012-10-04 10:09:15 -0700281 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
Ian Rogers120f1c72012-09-28 17:17:10 -0700282 Runtime* runtime = Runtime::Current();
Mathieu Chartier34e82932013-11-12 18:22:47 -0800283 bool shutting_down = (runtime == NULL) || runtime->IsShuttingDownLocked();
Elliott Hughes6b355752012-01-13 16:49:08 -0800284 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
285 }
Ian Rogersc604d732012-10-14 16:09:54 -0700286#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -0700287}
288
Ian Rogers81d425b2012-09-27 16:03:43 -0700289void Mutex::ExclusiveLock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700290 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers25fd14b2012-09-05 10:56:38 -0700291 if (kDebugLocking && !recursive_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700292 AssertNotHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700293 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700294 if (!recursive_ || !IsExclusiveHeld(self)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700295#if ART_USE_FUTEXES
296 bool done = false;
297 do {
298 int32_t cur_state = state_;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700299 if (LIKELY(cur_state == 0)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700300 // Change state from 0 to 1.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800301 done = __sync_bool_compare_and_swap(&state_, 0 /* cur_state */, 1 /* new state */);
Ian Rogersc604d732012-10-14 16:09:54 -0700302 } else {
303 // Failed to acquire, hang up.
Hiroshi Yamauchib3733082013-08-12 17:28:49 -0700304 ScopedContentionRecorder scr(this, SafeGetTid(self), GetExclusiveOwnerTid());
Ian Rogersb122a4b2013-11-19 18:00:50 -0800305 num_contenders_++;
Ian Rogersc604d732012-10-14 16:09:54 -0700306 if (futex(&state_, FUTEX_WAIT, 1, NULL, NULL, 0) != 0) {
Brian Carlstrom0de79852013-07-25 22:29:58 -0700307 // EAGAIN and EINTR both indicate a spurious failure, try again from the beginning.
308 // We don't use TEMP_FAILURE_RETRY so we can intentionally retry to acquire the lock.
309 if ((errno != EAGAIN) && (errno != EINTR)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700310 PLOG(FATAL) << "futex wait failed for " << name_;
311 }
312 }
Ian Rogersb122a4b2013-11-19 18:00:50 -0800313 num_contenders_--;
Ian Rogersc604d732012-10-14 16:09:54 -0700314 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700315 } while (!done);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800316 QuasiAtomic::MembarStoreLoad();
Ian Rogersc604d732012-10-14 16:09:54 -0700317 DCHECK_EQ(state_, 1);
318 exclusive_owner_ = SafeGetTid(self);
319#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
Ian Rogersc604d732012-10-14 16:09:54 -0700321#endif
Ian Rogers81d425b2012-09-27 16:03:43 -0700322 RegisterAsLocked(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700323 }
324 recursion_count_++;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700325 if (kDebugLocking) {
326 CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: "
327 << name_ << " " << recursion_count_;
Ian Rogers81d425b2012-09-27 16:03:43 -0700328 AssertHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700329 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700330}
331
Ian Rogers81d425b2012-09-27 16:03:43 -0700332bool Mutex::ExclusiveTryLock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700333 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers25fd14b2012-09-05 10:56:38 -0700334 if (kDebugLocking && !recursive_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700335 AssertNotHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700336 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700337 if (!recursive_ || !IsExclusiveHeld(self)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700338#if ART_USE_FUTEXES
339 bool done = false;
340 do {
341 int32_t cur_state = state_;
342 if (cur_state == 0) {
343 // Change state from 0 to 1.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800344 done = __sync_bool_compare_and_swap(&state_, 0 /* cur_state */, 1 /* new state */);
Ian Rogersc604d732012-10-14 16:09:54 -0700345 } else {
346 return false;
347 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700348 } while (!done);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800349 QuasiAtomic::MembarStoreLoad();
Ian Rogersc604d732012-10-14 16:09:54 -0700350 DCHECK_EQ(state_, 1);
351 exclusive_owner_ = SafeGetTid(self);
352#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700353 int result = pthread_mutex_trylock(&mutex_);
354 if (result == EBUSY) {
355 return false;
356 }
357 if (result != 0) {
358 errno = result;
359 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
360 }
Ian Rogersc604d732012-10-14 16:09:54 -0700361#endif
Ian Rogers81d425b2012-09-27 16:03:43 -0700362 RegisterAsLocked(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700363 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700364 recursion_count_++;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700365 if (kDebugLocking) {
366 CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: "
367 << name_ << " " << recursion_count_;
Ian Rogers81d425b2012-09-27 16:03:43 -0700368 AssertHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700369 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700370 return true;
371}
372
Ian Rogers81d425b2012-09-27 16:03:43 -0700373void Mutex::ExclusiveUnlock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700374 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700375 AssertHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700376 recursion_count_--;
377 if (!recursive_ || recursion_count_ == 0) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700378 if (kDebugLocking) {
379 CHECK(recursion_count_ == 0 || recursive_) << "Unexpected recursion count on mutex: "
380 << name_ << " " << recursion_count_;
381 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700382 RegisterAsUnlocked(self);
Ian Rogersc604d732012-10-14 16:09:54 -0700383#if ART_USE_FUTEXES
384 bool done = false;
385 do {
386 int32_t cur_state = state_;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700387 if (LIKELY(cur_state == 1)) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800388 QuasiAtomic::MembarStoreStore();
Ian Rogersc604d732012-10-14 16:09:54 -0700389 // We're no longer the owner.
390 exclusive_owner_ = 0;
391 // Change state to 0.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800392 done = __sync_bool_compare_and_swap(&state_, cur_state, 0 /* new state */);
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700393 if (LIKELY(done)) { // Spurious fail?
Ian Rogersc604d732012-10-14 16:09:54 -0700394 // Wake a contender
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700395 if (UNLIKELY(num_contenders_ > 0)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700396 futex(&state_, FUTEX_WAKE, 1, NULL, NULL, 0);
397 }
398 }
399 } else {
Ian Rogersc4ee12e2013-05-16 11:19:53 -0700400 // Logging acquires the logging lock, avoid infinite recursion in that case.
401 if (this != Locks::logging_lock_) {
402 LOG(FATAL) << "Unexpected state_ in unlock " << cur_state << " for " << name_;
403 } else {
404 LogMessageData data(__FILE__, __LINE__, INTERNAL_FATAL, -1);
405 LogMessage::LogLine(data, StringPrintf("Unexpected state_ %d in unlock for %s",
406 cur_state, name_).c_str());
407 _exit(1);
408 }
Ian Rogersc604d732012-10-14 16:09:54 -0700409 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700410 } while (!done);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800411 QuasiAtomic::MembarStoreLoad();
Ian Rogersc604d732012-10-14 16:09:54 -0700412#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700413 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
Ian Rogersc604d732012-10-14 16:09:54 -0700414#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700415 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700416}
417
Ian Rogers56edc432013-01-18 16:51:51 -0800418void Mutex::Dump(std::ostream& os) const {
419 os << (recursive_ ? "recursive " : "non-recursive ")
420 << name_
421 << " level=" << static_cast<int>(level_)
422 << " rec=" << recursion_count_
423 << " owner=" << GetExclusiveOwnerTid() << " ";
424 DumpContention(os);
Ian Rogers01ae5802012-09-28 16:14:01 -0700425}
426
427std::ostream& operator<<(std::ostream& os, const Mutex& mu) {
Ian Rogers56edc432013-01-18 16:51:51 -0800428 mu.Dump(os);
429 return os;
Ian Rogers01ae5802012-09-28 16:14:01 -0700430}
431
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700432ReaderWriterMutex::ReaderWriterMutex(const char* name, LockLevel level)
433 : BaseMutex(name, level)
Ian Rogers81d425b2012-09-27 16:03:43 -0700434#if ART_USE_FUTEXES
435 , state_(0), exclusive_owner_(0), num_pending_readers_(0), num_pending_writers_(0)
436#endif
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700437{ // NOLINT(whitespace/braces)
Ian Rogers81d425b2012-09-27 16:03:43 -0700438#if !ART_USE_FUTEXES
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700439 CHECK_MUTEX_CALL(pthread_rwlock_init, (&rwlock_, NULL));
Ian Rogers81d425b2012-09-27 16:03:43 -0700440#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700441}
442
443ReaderWriterMutex::~ReaderWriterMutex() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700444#if ART_USE_FUTEXES
445 CHECK_EQ(state_, 0);
446 CHECK_EQ(exclusive_owner_, 0U);
447 CHECK_EQ(num_pending_readers_, 0);
448 CHECK_EQ(num_pending_writers_, 0);
449#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700450 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
451 // may still be using locks.
452 int rc = pthread_rwlock_destroy(&rwlock_);
453 if (rc != 0) {
454 errno = rc;
455 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Ian Rogers50b35e22012-10-04 10:09:15 -0700456 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
Ian Rogers120f1c72012-09-28 17:17:10 -0700457 Runtime* runtime = Runtime::Current();
Mathieu Chartier34e82932013-11-12 18:22:47 -0800458 bool shutting_down = runtime == NULL || runtime->IsShuttingDownLocked();
Ian Rogers120f1c72012-09-28 17:17:10 -0700459 PLOG(shutting_down ? WARNING : FATAL) << "pthread_rwlock_destroy failed for " << name_;
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800460 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700461#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700462}
463
Ian Rogers81d425b2012-09-27 16:03:43 -0700464void ReaderWriterMutex::ExclusiveLock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700465 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700466 AssertNotExclusiveHeld(self);
467#if ART_USE_FUTEXES
468 bool done = false;
469 do {
470 int32_t cur_state = state_;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700471 if (LIKELY(cur_state == 0)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700472 // Change state from 0 to -1.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800473 done = __sync_bool_compare_and_swap(&state_, 0 /* cur_state*/, -1 /* new state */);
Ian Rogers81d425b2012-09-27 16:03:43 -0700474 } else {
475 // Failed to acquire, hang up.
Hiroshi Yamauchib3733082013-08-12 17:28:49 -0700476 ScopedContentionRecorder scr(this, SafeGetTid(self), GetExclusiveOwnerTid());
Ian Rogersb122a4b2013-11-19 18:00:50 -0800477 num_pending_writers_++;
Ian Rogers81d425b2012-09-27 16:03:43 -0700478 if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) {
Brian Carlstrom0de79852013-07-25 22:29:58 -0700479 // EAGAIN and EINTR both indicate a spurious failure, try again from the beginning.
480 // We don't use TEMP_FAILURE_RETRY so we can intentionally retry to acquire the lock.
481 if ((errno != EAGAIN) && (errno != EINTR)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700482 PLOG(FATAL) << "futex wait failed for " << name_;
483 }
484 }
Ian Rogersb122a4b2013-11-19 18:00:50 -0800485 num_pending_writers_--;
Ian Rogers81d425b2012-09-27 16:03:43 -0700486 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700487 } while (!done);
Ian Rogersab470162012-09-29 23:06:53 -0700488 DCHECK_EQ(state_, -1);
489 exclusive_owner_ = SafeGetTid(self);
Ian Rogers81d425b2012-09-27 16:03:43 -0700490#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700491 CHECK_MUTEX_CALL(pthread_rwlock_wrlock, (&rwlock_));
Ian Rogers81d425b2012-09-27 16:03:43 -0700492#endif
493 RegisterAsLocked(self);
494 AssertExclusiveHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700495}
496
Ian Rogers81d425b2012-09-27 16:03:43 -0700497void ReaderWriterMutex::ExclusiveUnlock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700498 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700499 AssertExclusiveHeld(self);
500 RegisterAsUnlocked(self);
501#if ART_USE_FUTEXES
502 bool done = false;
503 do {
504 int32_t cur_state = state_;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700505 if (LIKELY(cur_state == -1)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700506 // We're no longer the owner.
507 exclusive_owner_ = 0;
508 // Change state from -1 to 0.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800509 done = __sync_bool_compare_and_swap(&state_, -1 /* cur_state*/, 0 /* new state */);
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700510 if (LIKELY(done)) { // cmpxchg may fail due to noise?
Ian Rogers81d425b2012-09-27 16:03:43 -0700511 // Wake any waiters.
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700512 if (UNLIKELY(num_pending_readers_ > 0 || num_pending_writers_ > 0)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700513 futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0);
514 }
515 }
516 } else {
517 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
518 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700519 } while (!done);
Ian Rogers81d425b2012-09-27 16:03:43 -0700520#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700521 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
Ian Rogers81d425b2012-09-27 16:03:43 -0700522#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700523}
524
Ian Rogers66aee5c2012-08-15 17:17:47 -0700525#if HAVE_TIMED_RWLOCK
Ian Rogersc604d732012-10-14 16:09:54 -0700526bool ReaderWriterMutex::ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700527 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700528#if ART_USE_FUTEXES
529 bool done = false;
Ian Rogersc604d732012-10-14 16:09:54 -0700530 timespec end_abs_ts;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800531 InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &end_abs_ts);
Ian Rogers81d425b2012-09-27 16:03:43 -0700532 do {
533 int32_t cur_state = state_;
534 if (cur_state == 0) {
535 // Change state from 0 to -1.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800536 done = __sync_bool_compare_and_swap(&state_, 0 /* cur_state */, -1 /* new state */);
Ian Rogers81d425b2012-09-27 16:03:43 -0700537 } else {
538 // Failed to acquire, hang up.
Ian Rogersc604d732012-10-14 16:09:54 -0700539 timespec now_abs_ts;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800540 InitTimeSpec(true, CLOCK_REALTIME, 0, 0, &now_abs_ts);
Ian Rogersc604d732012-10-14 16:09:54 -0700541 timespec rel_ts;
542 if (ComputeRelativeTimeSpec(&rel_ts, end_abs_ts, now_abs_ts)) {
543 return false; // Timed out.
544 }
Hiroshi Yamauchib3733082013-08-12 17:28:49 -0700545 ScopedContentionRecorder scr(this, SafeGetTid(self), GetExclusiveOwnerTid());
Ian Rogersb122a4b2013-11-19 18:00:50 -0800546 num_pending_writers_++;
Ian Rogersc604d732012-10-14 16:09:54 -0700547 if (futex(&state_, FUTEX_WAIT, cur_state, &rel_ts, NULL, 0) != 0) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700548 if (errno == ETIMEDOUT) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800549 num_pending_writers_--;
Ian Rogersc604d732012-10-14 16:09:54 -0700550 return false; // Timed out.
Brian Carlstrom0de79852013-07-25 22:29:58 -0700551 } else if ((errno != EAGAIN) && (errno != EINTR)) {
552 // EAGAIN and EINTR both indicate a spurious failure,
553 // recompute the relative time out from now and try again.
554 // We don't use TEMP_FAILURE_RETRY so we can recompute rel_ts;
Ian Rogers81d425b2012-09-27 16:03:43 -0700555 PLOG(FATAL) << "timed futex wait failed for " << name_;
556 }
557 }
Ian Rogersb122a4b2013-11-19 18:00:50 -0800558 num_pending_writers_--;
Ian Rogers81d425b2012-09-27 16:03:43 -0700559 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700560 } while (!done);
Ian Rogers01ae5802012-09-28 16:14:01 -0700561 exclusive_owner_ = SafeGetTid(self);
Ian Rogers81d425b2012-09-27 16:03:43 -0700562#else
Ian Rogersc604d732012-10-14 16:09:54 -0700563 timespec ts;
Brian Carlstrombcc29262012-11-02 11:36:03 -0700564 InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &ts);
Ian Rogersc604d732012-10-14 16:09:54 -0700565 int result = pthread_rwlock_timedwrlock(&rwlock_, &ts);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700566 if (result == ETIMEDOUT) {
567 return false;
568 }
569 if (result != 0) {
570 errno = result;
Ian Rogersa5acfd32012-08-15 11:50:10 -0700571 PLOG(FATAL) << "pthread_rwlock_timedwrlock failed for " << name_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700572 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700573#endif
574 RegisterAsLocked(self);
575 AssertSharedHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700576 return true;
577}
Ian Rogers66aee5c2012-08-15 17:17:47 -0700578#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700579
Ian Rogers81d425b2012-09-27 16:03:43 -0700580bool ReaderWriterMutex::SharedTryLock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700581 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700582#if ART_USE_FUTEXES
583 bool done = false;
584 do {
585 int32_t cur_state = state_;
586 if (cur_state >= 0) {
587 // Add as an extra reader.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800588 done = __sync_bool_compare_and_swap(&state_, cur_state, cur_state + 1);
Ian Rogers81d425b2012-09-27 16:03:43 -0700589 } else {
590 // Owner holds it exclusively.
591 return false;
592 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700593 } while (!done);
Ian Rogers81d425b2012-09-27 16:03:43 -0700594#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700595 int result = pthread_rwlock_tryrdlock(&rwlock_);
596 if (result == EBUSY) {
597 return false;
598 }
599 if (result != 0) {
600 errno = result;
601 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
602 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700603#endif
604 RegisterAsLocked(self);
605 AssertSharedHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700606 return true;
607}
608
Ian Rogers81d425b2012-09-27 16:03:43 -0700609bool ReaderWriterMutex::IsSharedHeld(const Thread* self) const {
Ian Rogers01ae5802012-09-28 16:14:01 -0700610 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 bool result;
612 if (UNLIKELY(self == NULL)) { // Handle unattached threads.
Ian Rogers01ae5802012-09-28 16:14:01 -0700613 result = IsExclusiveHeld(self); // TODO: a better best effort here.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614 } else {
615 result = (self->GetHeldMutex(level_) == this);
616 }
617 return result;
618}
619
Ian Rogers56edc432013-01-18 16:51:51 -0800620void ReaderWriterMutex::Dump(std::ostream& os) const {
621 os << name_
622 << " level=" << static_cast<int>(level_)
623 << " owner=" << GetExclusiveOwnerTid() << " ";
624 DumpContention(os);
Ian Rogers01ae5802012-09-28 16:14:01 -0700625}
626
627std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu) {
Ian Rogers56edc432013-01-18 16:51:51 -0800628 mu.Dump(os);
629 return os;
Ian Rogers01ae5802012-09-28 16:14:01 -0700630}
631
Ian Rogers23055dc2013-04-18 16:29:16 -0700632ConditionVariable::ConditionVariable(const char* name, Mutex& guard)
Ian Rogersc604d732012-10-14 16:09:54 -0700633 : name_(name), guard_(guard) {
634#if ART_USE_FUTEXES
Ian Rogersd45f2012012-11-28 11:46:23 -0800635 sequence_ = 0;
Ian Rogersc604d732012-10-14 16:09:54 -0700636 num_waiters_ = 0;
Ian Rogersc604d732012-10-14 16:09:54 -0700637#else
Elliott Hughes5f791332011-09-15 17:45:30 -0700638 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL));
Ian Rogersc604d732012-10-14 16:09:54 -0700639#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700640}
641
642ConditionVariable::~ConditionVariable() {
Ian Rogers5bd97c42012-11-27 02:38:26 -0800643#if ART_USE_FUTEXES
644 if (num_waiters_!= 0) {
Ian Rogers5bd97c42012-11-27 02:38:26 -0800645 Runtime* runtime = Runtime::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700646 bool shutting_down = runtime == nullptr || runtime->IsShuttingDown(Thread::Current());
Ian Rogersd45f2012012-11-28 11:46:23 -0800647 LOG(shutting_down ? WARNING : FATAL) << "ConditionVariable::~ConditionVariable for " << name_
648 << " called with " << num_waiters_ << " waiters.";
Ian Rogers5bd97c42012-11-27 02:38:26 -0800649 }
650#else
Elliott Hughese62934d2012-04-09 11:24:29 -0700651 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
652 // may still be using condition variables.
653 int rc = pthread_cond_destroy(&cond_);
654 if (rc != 0) {
655 errno = rc;
Ian Rogers50b35e22012-10-04 10:09:15 -0700656 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
Ian Rogers120f1c72012-09-28 17:17:10 -0700657 Runtime* runtime = Runtime::Current();
Mathieu Chartier34e82932013-11-12 18:22:47 -0800658 bool shutting_down = (runtime == NULL) || runtime->IsShuttingDownLocked();
Elliott Hughese62934d2012-04-09 11:24:29 -0700659 PLOG(shutting_down ? WARNING : FATAL) << "pthread_cond_destroy failed for " << name_;
660 }
Ian Rogersc604d732012-10-14 16:09:54 -0700661#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700662}
663
Ian Rogersc604d732012-10-14 16:09:54 -0700664void ConditionVariable::Broadcast(Thread* self) {
665 DCHECK(self == NULL || self == Thread::Current());
666 // TODO: enable below, there's a race in thread creation that causes false failures currently.
667 // guard_.AssertExclusiveHeld(self);
Mathieu Chartiere46cd752012-10-31 16:56:18 -0700668 DCHECK_EQ(guard_.GetExclusiveOwnerTid(), SafeGetTid(self));
Ian Rogersc604d732012-10-14 16:09:54 -0700669#if ART_USE_FUTEXES
Ian Rogersd45f2012012-11-28 11:46:23 -0800670 if (num_waiters_ > 0) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800671 sequence_++; // Indicate the broadcast occurred.
Ian Rogersc604d732012-10-14 16:09:54 -0700672 bool done = false;
673 do {
Ian Rogersd45f2012012-11-28 11:46:23 -0800674 int32_t cur_sequence = sequence_;
675 // Requeue waiters onto mutex. The waiter holds the contender count on the mutex high ensuring
676 // mutex unlocks will awaken the requeued waiter thread.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800677 done = futex(sequence_.Address(), FUTEX_CMP_REQUEUE, 0,
Ian Rogers5bd97c42012-11-27 02:38:26 -0800678 reinterpret_cast<const timespec*>(std::numeric_limits<int32_t>::max()),
Ian Rogersd45f2012012-11-28 11:46:23 -0800679 &guard_.state_, cur_sequence) != -1;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800680 if (!done) {
681 if (errno != EAGAIN) {
682 PLOG(FATAL) << "futex cmp requeue failed for " << name_;
683 }
Ian Rogers5bd97c42012-11-27 02:38:26 -0800684 }
Ian Rogersc604d732012-10-14 16:09:54 -0700685 } while (!done);
686 }
687#else
Elliott Hughes5f791332011-09-15 17:45:30 -0700688 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
Ian Rogersc604d732012-10-14 16:09:54 -0700689#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700690}
691
Ian Rogersc604d732012-10-14 16:09:54 -0700692void ConditionVariable::Signal(Thread* self) {
693 DCHECK(self == NULL || self == Thread::Current());
694 guard_.AssertExclusiveHeld(self);
695#if ART_USE_FUTEXES
Ian Rogersd45f2012012-11-28 11:46:23 -0800696 if (num_waiters_ > 0) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800697 sequence_++; // Indicate a signal occurred.
Ian Rogersc604d732012-10-14 16:09:54 -0700698 // Futex wake 1 waiter who will then come and in contend on mutex. It'd be nice to requeue them
699 // to avoid this, however, requeueing can only move all waiters.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800700 int num_woken = futex(sequence_.Address(), FUTEX_WAKE, 1, NULL, NULL, 0);
Ian Rogersd45f2012012-11-28 11:46:23 -0800701 // Check something was woken or else we changed sequence_ before they had chance to wait.
Ian Rogers5bd97c42012-11-27 02:38:26 -0800702 CHECK((num_woken == 0) || (num_woken == 1));
Ian Rogersc604d732012-10-14 16:09:54 -0700703 }
704#else
Elliott Hughes5f791332011-09-15 17:45:30 -0700705 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
Ian Rogersc604d732012-10-14 16:09:54 -0700706#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700707}
708
Ian Rogersc604d732012-10-14 16:09:54 -0700709void ConditionVariable::Wait(Thread* self) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700710 guard_.CheckSafeToWait(self);
711 WaitHoldingLocks(self);
712}
713
714void ConditionVariable::WaitHoldingLocks(Thread* self) {
Ian Rogersc604d732012-10-14 16:09:54 -0700715 DCHECK(self == NULL || self == Thread::Current());
716 guard_.AssertExclusiveHeld(self);
Ian Rogersc604d732012-10-14 16:09:54 -0700717 unsigned int old_recursion_count = guard_.recursion_count_;
718#if ART_USE_FUTEXES
Ian Rogersc604d732012-10-14 16:09:54 -0700719 num_waiters_++;
Ian Rogersd45f2012012-11-28 11:46:23 -0800720 // Ensure the Mutex is contended so that requeued threads are awoken.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800721 guard_.num_contenders_++;
Ian Rogersc604d732012-10-14 16:09:54 -0700722 guard_.recursion_count_ = 1;
Ian Rogersd45f2012012-11-28 11:46:23 -0800723 int32_t cur_sequence = sequence_;
Ian Rogersc604d732012-10-14 16:09:54 -0700724 guard_.ExclusiveUnlock(self);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800725 if (futex(sequence_.Address(), FUTEX_WAIT, cur_sequence, NULL, NULL, 0) != 0) {
Ian Rogersd45f2012012-11-28 11:46:23 -0800726 // Futex failed, check it is an expected error.
727 // EAGAIN == EWOULDBLK, so we let the caller try again.
728 // EINTR implies a signal was sent to this thread.
729 if ((errno != EINTR) && (errno != EAGAIN)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700730 PLOG(FATAL) << "futex wait failed for " << name_;
731 }
732 }
733 guard_.ExclusiveLock(self);
Ian Rogersd45f2012012-11-28 11:46:23 -0800734 CHECK_GE(num_waiters_, 0);
Ian Rogersc604d732012-10-14 16:09:54 -0700735 num_waiters_--;
Ian Rogersd45f2012012-11-28 11:46:23 -0800736 // We awoke and so no longer require awakes from the guard_'s unlock.
737 CHECK_GE(guard_.num_contenders_, 0);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800738 guard_.num_contenders_--;
Ian Rogersc604d732012-10-14 16:09:54 -0700739#else
740 guard_.recursion_count_ = 0;
741 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, &guard_.mutex_));
742#endif
743 guard_.recursion_count_ = old_recursion_count;
Elliott Hughes5f791332011-09-15 17:45:30 -0700744}
745
Ian Rogersc604d732012-10-14 16:09:54 -0700746void ConditionVariable::TimedWait(Thread* self, int64_t ms, int32_t ns) {
747 DCHECK(self == NULL || self == Thread::Current());
748 guard_.AssertExclusiveHeld(self);
Ian Rogers1d54e732013-05-02 21:10:01 -0700749 guard_.CheckSafeToWait(self);
Ian Rogersc604d732012-10-14 16:09:54 -0700750 unsigned int old_recursion_count = guard_.recursion_count_;
751#if ART_USE_FUTEXES
Ian Rogersc604d732012-10-14 16:09:54 -0700752 timespec rel_ts;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800753 InitTimeSpec(false, CLOCK_REALTIME, ms, ns, &rel_ts);
Ian Rogersc604d732012-10-14 16:09:54 -0700754 num_waiters_++;
Ian Rogersd45f2012012-11-28 11:46:23 -0800755 // Ensure the Mutex is contended so that requeued threads are awoken.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800756 guard_.num_contenders_++;
Ian Rogersc604d732012-10-14 16:09:54 -0700757 guard_.recursion_count_ = 1;
Ian Rogersd45f2012012-11-28 11:46:23 -0800758 int32_t cur_sequence = sequence_;
Ian Rogersc604d732012-10-14 16:09:54 -0700759 guard_.ExclusiveUnlock(self);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800760 if (futex(sequence_.Address(), FUTEX_WAIT, cur_sequence, &rel_ts, NULL, 0) != 0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700761 if (errno == ETIMEDOUT) {
Ian Rogersd45f2012012-11-28 11:46:23 -0800762 // Timed out we're done.
Brian Carlstrom0de79852013-07-25 22:29:58 -0700763 } else if ((errno == EAGAIN) || (errno == EINTR)) {
Ian Rogersd45f2012012-11-28 11:46:23 -0800764 // A signal or ConditionVariable::Signal/Broadcast has come in.
Ian Rogersc604d732012-10-14 16:09:54 -0700765 } else {
766 PLOG(FATAL) << "timed futex wait failed for " << name_;
767 }
768 }
769 guard_.ExclusiveLock(self);
Ian Rogersd45f2012012-11-28 11:46:23 -0800770 CHECK_GE(num_waiters_, 0);
Ian Rogersc604d732012-10-14 16:09:54 -0700771 num_waiters_--;
Ian Rogersd45f2012012-11-28 11:46:23 -0800772 // We awoke and so no longer require awakes from the guard_'s unlock.
773 CHECK_GE(guard_.num_contenders_, 0);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800774 guard_.num_contenders_--;
Ian Rogersc604d732012-10-14 16:09:54 -0700775#else
Elliott Hughes5f791332011-09-15 17:45:30 -0700776#ifdef HAVE_TIMEDWAIT_MONOTONIC
777#define TIMEDWAIT pthread_cond_timedwait_monotonic
Ian Rogersc604d732012-10-14 16:09:54 -0700778 int clock = CLOCK_MONOTONIC;
Elliott Hughes5f791332011-09-15 17:45:30 -0700779#else
780#define TIMEDWAIT pthread_cond_timedwait
Ian Rogersc604d732012-10-14 16:09:54 -0700781 int clock = CLOCK_REALTIME;
Elliott Hughes5f791332011-09-15 17:45:30 -0700782#endif
Ian Rogersc604d732012-10-14 16:09:54 -0700783 guard_.recursion_count_ = 0;
784 timespec ts;
Brian Carlstrombcc29262012-11-02 11:36:03 -0700785 InitTimeSpec(true, clock, ms, ns, &ts);
786 int rc = TEMP_FAILURE_RETRY(TIMEDWAIT(&cond_, &guard_.mutex_, &ts));
Elliott Hughes5f791332011-09-15 17:45:30 -0700787 if (rc != 0 && rc != ETIMEDOUT) {
788 errno = rc;
789 PLOG(FATAL) << "TimedWait failed for " << name_;
790 }
Ian Rogersc604d732012-10-14 16:09:54 -0700791#endif
792 guard_.recursion_count_ = old_recursion_count;
Elliott Hughes5f791332011-09-15 17:45:30 -0700793}
794
Elliott Hughese62934d2012-04-09 11:24:29 -0700795} // namespace art