blob: c066bec2dcd4cdff7234c7626e72ec6914f00825 [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>
20
Elliott Hughes8daa0922011-09-11 13:46:25 -070021#include "logging.h"
Elliott Hughes6b355752012-01-13 16:49:08 -080022#include "runtime.h"
Elliott Hughesffb465f2012-03-01 18:46:05 -080023#include "thread.h"
24#include "utils.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070025
Elliott Hughesb08e8a32012-04-02 10:51:41 -070026#if defined(__APPLE__)
27#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
28#endif
29
Elliott Hughes8d768a92011-09-14 16:35:25 -070030#define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_)
31
Elliott Hughesf8349362012-06-18 15:00:06 -070032extern int pthread_mutex_lock(pthread_mutex_t* mutex) EXCLUSIVE_LOCK_FUNCTION(mutex);
33extern int pthread_mutex_unlock(pthread_mutex_t* mutex) UNLOCK_FUNCTION(1);
34extern int pthread_mutex_trylock(pthread_mutex_t* mutex) EXCLUSIVE_TRYLOCK_FUNCTION(0, mutex);
35
Elliott Hughes8daa0922011-09-11 13:46:25 -070036namespace art {
37
Brian Carlstromf3a26412012-08-24 11:06:02 -070038// This works on Mac OS 10.6 but hasn't been tested on older releases.
Elliott Hughesf1498432012-03-28 19:34:27 -070039struct __attribute__((__may_alias__)) darwin_pthread_mutex_t {
Brian Carlstromf3a26412012-08-24 11:06:02 -070040 long padding0;
41 int padding1;
42 uint32_t padding2;
43 int16_t padding3;
44 int16_t padding4;
45 uint32_t padding5;
46 pthread_t darwin_pthread_mutex_owner;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047 // ...other stuff we don't care about.
48};
49
50struct __attribute__((__may_alias__)) darwin_pthread_rwlock_t {
Brian Carlstromf3a26412012-08-24 11:06:02 -070051 long padding0;
52 pthread_mutex_t padding1;
53 int padding2;
54 pthread_cond_t padding3;
55 pthread_cond_t padding4;
56 int padding5;
57 int padding6;
58 pthread_t darwin_pthread_rwlock_owner;
Elliott Hughesf1498432012-03-28 19:34:27 -070059 // ...other stuff we don't care about.
60};
61
62struct __attribute__((__may_alias__)) glibc_pthread_mutex_t {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063 int32_t padding0[2];
Elliott Hughesf1498432012-03-28 19:34:27 -070064 int owner;
65 // ...other stuff we don't care about.
66};
67
Ian Rogers00f7d0e2012-07-19 15:28:27 -070068struct __attribute__((__may_alias__)) glibc_pthread_rwlock_t {
69#ifdef __LP64__
70 int32_t padding0[6];
71#else
72 int32_t padding0[7];
73#endif
74 int writer;
75 // ...other stuff we don't care about.
76};
77
Ian Rogersb726dcb2012-09-05 08:57:23 -070078ReaderWriterMutex* Locks::mutator_lock_ = NULL;
79Mutex* Locks::thread_list_lock_ = NULL;
80Mutex* Locks::classlinker_classes_lock_ = NULL;
81ReaderWriterMutex* Locks::heap_bitmap_lock_ = NULL;
82Mutex* Locks::abort_lock_ = NULL;
83Mutex* Locks::logging_lock_ = NULL;
84Mutex* Locks::unexpected_signal_lock_ = NULL;
85Mutex* Locks::thread_suspend_count_lock_ = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070086
Ian Rogersb726dcb2012-09-05 08:57:23 -070087void Locks::Init() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 if (logging_lock_ != NULL) {
89 // Already initialized.
90 DCHECK(mutator_lock_ != NULL);
91 DCHECK(thread_list_lock_ != NULL);
92 DCHECK(classlinker_classes_lock_ != NULL);
93 DCHECK(heap_bitmap_lock_ != NULL);
94 DCHECK(abort_lock_ != NULL);
95 DCHECK(logging_lock_ != NULL);
96 DCHECK(unexpected_signal_lock_ != NULL);
97 DCHECK(thread_suspend_count_lock_ != NULL);
98 } else {
99 logging_lock_ = new Mutex("logging lock", kLoggingLock, true);
100 abort_lock_ = new Mutex("abort lock", kAbortLock, true);
101 DCHECK(mutator_lock_ == NULL);
102 mutator_lock_ = new ReaderWriterMutex("mutator lock", kMutatorLock);
103 DCHECK(thread_list_lock_ == NULL);
104 thread_list_lock_ = new Mutex("thread list lock", kThreadListLock);
105 DCHECK(classlinker_classes_lock_ == NULL);
106 classlinker_classes_lock_ = new Mutex("ClassLinker classes lock", kClassLinkerClassesLock);
107 DCHECK(heap_bitmap_lock_ == NULL);
108 heap_bitmap_lock_ = new ReaderWriterMutex("heap bitmap lock", kHeapBitmapLock);
109 DCHECK(unexpected_signal_lock_ == NULL);
110 unexpected_signal_lock_ = new Mutex("unexpected signal lock", kUnexpectedSignalLock, true);
111 DCHECK(thread_suspend_count_lock_ == NULL);
112 thread_suspend_count_lock_ = new Mutex("thread suspend count lock", kThreadSuspendCountLock);
Elliott Hughes67d92002012-03-26 15:08:51 -0700113 }
Elliott Hughesa4060e52012-03-02 16:51:35 -0800114}
115
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116BaseMutex::BaseMutex(const char* name, MutexLevel level) : level_(level), name_(name) {}
117
118static void CheckUnattachedThread(MutexLevel level) {
119 // The check below enumerates the cases where we expect not to be able to sanity check locks
120 // on a thread. TODO: tighten this check.
Ian Rogers25fd14b2012-09-05 10:56:38 -0700121 if (kDebugLocking) {
122 Runtime* runtime = Runtime::Current();
123 CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDown() ||
124 level == kDefaultMutexLevel || level == kThreadListLock ||
125 level == kLoggingLock || level == kAbortLock);
126 }
Elliott Hughesffb465f2012-03-01 18:46:05 -0800127}
128
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129void BaseMutex::RegisterAsLockedWithCurrentThread() {
130 Thread* self = Thread::Current();
131 if (self == NULL) {
132 CheckUnattachedThread(level_);
133 return;
134 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700135 if (kDebugLocking) {
136 // Check if a bad Mutex of this level or lower is held.
137 bool bad_mutexes_held = false;
138 for (int i = level_; i >= 0; --i) {
139 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<MutexLevel>(i));
140 if (UNLIKELY(held_mutex != NULL)) {
141 LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" (level " << i
142 << ") while locking \"" << name_ << "\" (level " << static_cast<int>(level_) << ")";
143 if (i > kAbortLock) {
144 // Only abort in the check below if this is more than abort level lock.
145 bad_mutexes_held = true;
146 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 }
148 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700149 CHECK(!bad_mutexes_held);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 // Don't record monitors as they are outside the scope of analysis. They may be inspected off of
152 // the monitor list.
153 if (level_ != kMonitorLock) {
154 self->SetHeldMutex(level_, this);
155 }
156}
157
158void BaseMutex::RegisterAsUnlockedWithCurrentThread() {
159 Thread* self = Thread::Current();
160 if (self == NULL) {
161 CheckUnattachedThread(level_);
162 return;
163 }
164 if (level_ != kMonitorLock) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700165 if (kDebugLocking) {
166 CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_;
167 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168 self->SetHeldMutex(level_, NULL);
169 }
170}
171
172void BaseMutex::CheckSafeToWait() {
173 Thread* self = Thread::Current();
174 if (self == NULL) {
175 CheckUnattachedThread(level_);
176 return;
177 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700178 if (kDebugLocking) {
179 CHECK(self->GetHeldMutex(level_) == this) << "Waiting on unacquired mutex: " << name_;
180 bool bad_mutexes_held = false;
181 for (int i = kMaxMutexLevel; i >= 0; --i) {
182 if (i != level_) {
183 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<MutexLevel>(i));
184 if (held_mutex != NULL) {
185 LOG(ERROR) << "Holding " << held_mutex->name_ << " (level " << i
186 << ") while performing wait on: "
187 << name_ << " (level " << static_cast<int>(level_) << ")";
188 bad_mutexes_held = true;
189 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700190 }
191 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700192 CHECK(!bad_mutexes_held);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700193 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700194}
195
196Mutex::Mutex(const char* name, MutexLevel level, bool recursive)
197 : BaseMutex(name, level), recursive_(recursive), recursion_count_(0) {
Brian Carlstromf3a26412012-08-24 11:06:02 -0700198#if defined(__BIONIC__) || defined(__APPLE__)
199 // Use recursive mutexes for bionic and Apple otherwise the
200 // non-recursive mutexes don't have TIDs to check lock ownership of.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800201 pthread_mutexattr_t attributes;
202 CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes));
203 CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE));
204 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes));
205 CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206#else
207 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, NULL));
208#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -0700209}
210
211Mutex::~Mutex() {
Elliott Hughese62934d2012-04-09 11:24:29 -0700212 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
213 // may still be using locks.
Elliott Hughes6b355752012-01-13 16:49:08 -0800214 int rc = pthread_mutex_destroy(&mutex_);
215 if (rc != 0) {
216 errno = rc;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800217 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Elliott Hughes6b355752012-01-13 16:49:08 -0800218 bool shutting_down = Runtime::Current()->IsShuttingDown();
219 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
220 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700221}
222
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223void Mutex::ExclusiveLock() {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700224 if (kDebugLocking && !recursive_) {
225 AssertNotHeld();
226 }
227 if (!recursive_ || !IsExclusiveHeld()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
229 RegisterAsLockedWithCurrentThread();
230 }
231 recursion_count_++;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700232 if (kDebugLocking) {
233 CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: "
234 << name_ << " " << recursion_count_;
235 AssertHeld();
236 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700237}
238
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700239bool Mutex::ExclusiveTryLock() {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700240 if (kDebugLocking && !recursive_) {
241 AssertNotHeld();
242 }
243 if (!recursive_ || !IsExclusiveHeld()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244 int result = pthread_mutex_trylock(&mutex_);
245 if (result == EBUSY) {
246 return false;
247 }
248 if (result != 0) {
249 errno = result;
250 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
251 }
252 RegisterAsLockedWithCurrentThread();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700253 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254 recursion_count_++;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700255 if (kDebugLocking) {
256 CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: "
257 << name_ << " " << recursion_count_;
258 AssertHeld();
259 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700260 return true;
261}
262
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700263void Mutex::ExclusiveUnlock() {
Ian Rogers105245c2012-01-27 11:42:43 -0800264 AssertHeld();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 recursion_count_--;
266 if (!recursive_ || recursion_count_ == 0) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700267 if (kDebugLocking) {
268 CHECK(recursion_count_ == 0 || recursive_) << "Unexpected recursion count on mutex: "
269 << name_ << " " << recursion_count_;
270 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271 RegisterAsUnlockedWithCurrentThread();
272 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
273 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700274}
275
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276bool Mutex::IsExclusiveHeld() const {
277 Thread* self = Thread::Current();
278 bool result;
279 if (self == NULL || level_ == kMonitorLock) { // Handle unattached threads and monitors.
280 result = (GetExclusiveOwnerTid() == static_cast<uint64_t>(GetTid()));
281 } else {
282 result = (self->GetHeldMutex(level_) == this);
283 // Sanity debug check that if we think it is locked, so does the pthread.
Ian Rogers25fd14b2012-09-05 10:56:38 -0700284 if (kDebugLocking) {
285 CHECK(result == (GetExclusiveOwnerTid() == static_cast<uint64_t>(GetTid())));
286 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 }
288 return result;
Elliott Hughesf1498432012-03-28 19:34:27 -0700289}
290
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291uint64_t Mutex::GetExclusiveOwnerTid() const {
Elliott Hughes3147a232011-10-12 15:55:07 -0700292#if defined(__BIONIC__)
Elliott Hughesf1498432012-03-28 19:34:27 -0700293 return static_cast<uint64_t>((mutex_.value >> 16) & 0xffff);
Elliott Hughes3147a232011-10-12 15:55:07 -0700294#elif defined(__GLIBC__)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295 return reinterpret_cast<const glibc_pthread_mutex_t*>(&mutex_)->owner;
Elliott Hughescf044312012-01-23 18:48:51 -0800296#elif defined(__APPLE__)
Brian Carlstromf3a26412012-08-24 11:06:02 -0700297 const darwin_pthread_mutex_t* dpmutex = reinterpret_cast<const darwin_pthread_mutex_t*>(&mutex_);
298 pthread_t owner = dpmutex->darwin_pthread_mutex_owner;
Brian Carlstrombd93c302012-08-27 16:58:28 -0700299 // 0 for unowned, -1 for PTHREAD_MTX_TID_SWITCHING
300 // TODO: should we make darwin_pthread_mutex_owner volatile and recheck until not -1?
Brian Carlstromf3a26412012-08-24 11:06:02 -0700301 if ((owner == (pthread_t)0) || (owner == (pthread_t)-1)) {
302 return 0;
303 }
304 uint64_t tid;
305 CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6
306 return tid;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700307#else
Elliott Hughesf1498432012-03-28 19:34:27 -0700308#error unsupported C library
Elliott Hughes8daa0922011-09-11 13:46:25 -0700309#endif
310}
311
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700312ReaderWriterMutex::ReaderWriterMutex(const char* name, MutexLevel level) : BaseMutex(name, level) {
313 CHECK_MUTEX_CALL(pthread_rwlock_init, (&rwlock_, NULL));
314}
315
316ReaderWriterMutex::~ReaderWriterMutex() {
317 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
318 // may still be using locks.
319 int rc = pthread_rwlock_destroy(&rwlock_);
320 if (rc != 0) {
321 errno = rc;
322 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
323 bool shutting_down = Runtime::Current()->IsShuttingDown();
324 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800325 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326}
327
328void ReaderWriterMutex::ExclusiveLock() {
329 AssertNotExclusiveHeld();
330 CHECK_MUTEX_CALL(pthread_rwlock_wrlock, (&rwlock_));
331 RegisterAsLockedWithCurrentThread();
332 AssertExclusiveHeld();
333}
334
335void ReaderWriterMutex::ExclusiveUnlock() {
336 AssertExclusiveHeld();
337 RegisterAsUnlockedWithCurrentThread();
338 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
339}
340
Ian Rogers66aee5c2012-08-15 17:17:47 -0700341#if HAVE_TIMED_RWLOCK
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342bool ReaderWriterMutex::ExclusiveLockWithTimeout(const timespec& abs_timeout) {
343 int result = pthread_rwlock_timedwrlock(&rwlock_, &abs_timeout);
344 if (result == ETIMEDOUT) {
345 return false;
346 }
347 if (result != 0) {
348 errno = result;
Ian Rogersa5acfd32012-08-15 11:50:10 -0700349 PLOG(FATAL) << "pthread_rwlock_timedwrlock failed for " << name_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350 }
351 RegisterAsLockedWithCurrentThread();
352 AssertSharedHeld();
353 return true;
354}
Ian Rogers66aee5c2012-08-15 17:17:47 -0700355#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700356
357void ReaderWriterMutex::SharedLock() {
358 CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_));
359 RegisterAsLockedWithCurrentThread();
360 AssertSharedHeld();
361}
362
363bool ReaderWriterMutex::SharedTryLock() {
364 int result = pthread_rwlock_tryrdlock(&rwlock_);
365 if (result == EBUSY) {
366 return false;
367 }
368 if (result != 0) {
369 errno = result;
370 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
371 }
372 RegisterAsLockedWithCurrentThread();
373 AssertSharedHeld();
374 return true;
375}
376
377void ReaderWriterMutex::SharedUnlock() {
378 AssertSharedHeld();
379 RegisterAsUnlockedWithCurrentThread();
380 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
381}
382
383bool ReaderWriterMutex::IsExclusiveHeld() const {
384 bool result = (GetExclusiveOwnerTid() == static_cast<uint64_t>(GetTid()));
Ian Rogers25fd14b2012-09-05 10:56:38 -0700385 if (kDebugLocking) {
386 // Sanity that if the pthread thinks we own the lock the Thread agrees.
387 Thread* self = Thread::Current();
388 CHECK((self == NULL) || !result || (self->GetHeldMutex(level_) == this));
389 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390 return result;
391}
392
393bool ReaderWriterMutex::IsSharedHeld() const {
394 Thread* self = Thread::Current();
395 bool result;
396 if (UNLIKELY(self == NULL)) { // Handle unattached threads.
397 result = IsExclusiveHeld(); // TODO: a better best effort here.
398 } else {
399 result = (self->GetHeldMutex(level_) == this);
400 }
401 return result;
402}
403
404uint64_t ReaderWriterMutex::GetExclusiveOwnerTid() const {
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800405#if defined(__BIONIC__)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700406 return rwlock_.writerThreadId;
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800407#elif defined(__GLIBC__)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408 return reinterpret_cast<const glibc_pthread_rwlock_t*>(&rwlock_)->writer;
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800409#elif defined(__APPLE__)
Brian Carlstromf3a26412012-08-24 11:06:02 -0700410 const darwin_pthread_rwlock_t* dprwlock = reinterpret_cast<const darwin_pthread_rwlock_t*>(&rwlock_);
411 pthread_t owner = dprwlock->darwin_pthread_rwlock_owner;
412 if (owner == (pthread_t)0) {
413 return 0;
414 }
415 uint64_t tid;
416 CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6
417 return tid;
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800418#else
Elliott Hughesf1498432012-03-28 19:34:27 -0700419#error unsupported C library
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800420#endif
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800421}
422
Elliott Hughes5f791332011-09-15 17:45:30 -0700423ConditionVariable::ConditionVariable(const std::string& name) : name_(name) {
424 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL));
425}
426
427ConditionVariable::~ConditionVariable() {
Elliott Hughese62934d2012-04-09 11:24:29 -0700428 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
429 // may still be using condition variables.
430 int rc = pthread_cond_destroy(&cond_);
431 if (rc != 0) {
432 errno = rc;
433 bool shutting_down = Runtime::Current()->IsShuttingDown();
434 PLOG(shutting_down ? WARNING : FATAL) << "pthread_cond_destroy failed for " << name_;
435 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700436}
437
438void ConditionVariable::Broadcast() {
439 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
440}
441
442void ConditionVariable::Signal() {
443 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
444}
445
446void ConditionVariable::Wait(Mutex& mutex) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700447 mutex.CheckSafeToWait();
448 unsigned int old_recursion_count = mutex.recursion_count_;
449 mutex.recursion_count_ = 0;
Elliott Hughesf1498432012-03-28 19:34:27 -0700450 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, &mutex.mutex_));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700451 mutex.recursion_count_ = old_recursion_count;
Elliott Hughes5f791332011-09-15 17:45:30 -0700452}
453
454void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) {
455#ifdef HAVE_TIMEDWAIT_MONOTONIC
456#define TIMEDWAIT pthread_cond_timedwait_monotonic
457#else
458#define TIMEDWAIT pthread_cond_timedwait
459#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700460 mutex.CheckSafeToWait();
461 unsigned int old_recursion_count = mutex.recursion_count_;
462 mutex.recursion_count_ = 0;
Elliott Hughesf1498432012-03-28 19:34:27 -0700463 int rc = TIMEDWAIT(&cond_, &mutex.mutex_, &ts);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700464 mutex.recursion_count_ = old_recursion_count;
Elliott Hughes5f791332011-09-15 17:45:30 -0700465 if (rc != 0 && rc != ETIMEDOUT) {
466 errno = rc;
467 PLOG(FATAL) << "TimedWait failed for " << name_;
468 }
469}
470
Elliott Hughese62934d2012-04-09 11:24:29 -0700471} // namespace art