blob: 27bb627f74b4ed5907640e6f73d7d6ae010843fa [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 Hughes5ea047b2011-09-13 14:38:18 -070021#include "heap.h" // for VERIFY_OBJECT_ENABLED
Elliott Hughes8daa0922011-09-11 13:46:25 -070022#include "logging.h"
23#include "utils.h"
24
Elliott Hughes8d768a92011-09-14 16:35:25 -070025#define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_)
26
Elliott Hughes8daa0922011-09-11 13:46:25 -070027namespace art {
28
29Mutex::Mutex(const char* name) : name_(name) {
30#ifndef NDEBUG
31 pthread_mutexattr_t debug_attributes;
Elliott Hughes8d768a92011-09-14 16:35:25 -070032 CHECK_MUTEX_CALL(pthread_mutexattr_init, (&debug_attributes));
Elliott Hughes8daa0922011-09-11 13:46:25 -070033#if VERIFY_OBJECT_ENABLED
Elliott Hughes8d768a92011-09-14 16:35:25 -070034 CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&debug_attributes, PTHREAD_MUTEX_RECURSIVE));
Elliott Hughes8daa0922011-09-11 13:46:25 -070035#else
Elliott Hughes8d768a92011-09-14 16:35:25 -070036 CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&debug_attributes, PTHREAD_MUTEX_ERRORCHECK));
Elliott Hughes8daa0922011-09-11 13:46:25 -070037#endif
Elliott Hughes8d768a92011-09-14 16:35:25 -070038 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &debug_attributes));
39 CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&debug_attributes));
Elliott Hughes8daa0922011-09-11 13:46:25 -070040#else
Elliott Hughes8d768a92011-09-14 16:35:25 -070041 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, NULL));
Elliott Hughes8daa0922011-09-11 13:46:25 -070042#endif
43}
44
45Mutex::~Mutex() {
Elliott Hughes8d768a92011-09-14 16:35:25 -070046 CHECK_MUTEX_CALL(pthread_mutex_destroy, (&mutex_));
Elliott Hughes8daa0922011-09-11 13:46:25 -070047}
48
49void Mutex::Lock() {
Elliott Hughes8d768a92011-09-14 16:35:25 -070050 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
Elliott Hughes8daa0922011-09-11 13:46:25 -070051}
52
53bool Mutex::TryLock() {
54 int result = pthread_mutex_trylock(&mutex_);
55 if (result == EBUSY) {
56 return false;
57 }
58 if (result != 0) {
59 errno = result;
Elliott Hughes8d768a92011-09-14 16:35:25 -070060 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070061 }
62 return true;
63}
64
65void Mutex::Unlock() {
Elliott Hughes8d768a92011-09-14 16:35:25 -070066 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
Elliott Hughes8daa0922011-09-11 13:46:25 -070067}
68
69pid_t Mutex::GetOwner() {
70#ifdef __BIONIC__
71 return static_cast<pid_t>((mutex_.value >> 16) & 0xffff);
72#else
73 UNIMPLEMENTED(FATAL);
74 return 0;
75#endif
76}
77
78pid_t Mutex::GetTid() {
79 return art::GetTid();
80}
81
Elliott Hughes5f791332011-09-15 17:45:30 -070082ConditionVariable::ConditionVariable(const std::string& name) : name_(name) {
83 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL));
84}
85
86ConditionVariable::~ConditionVariable() {
87 CHECK_MUTEX_CALL(pthread_cond_destroy, (&cond_));
88}
89
90void ConditionVariable::Broadcast() {
91 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
92}
93
94void ConditionVariable::Signal() {
95 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
96}
97
98void ConditionVariable::Wait(Mutex& mutex) {
99 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, mutex.GetImpl()));
100}
101
102void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) {
103#ifdef HAVE_TIMEDWAIT_MONOTONIC
104#define TIMEDWAIT pthread_cond_timedwait_monotonic
105#else
106#define TIMEDWAIT pthread_cond_timedwait
107#endif
108 int rc = TIMEDWAIT(&cond_, mutex.GetImpl(), &ts);
109 if (rc != 0 && rc != ETIMEDOUT) {
110 errno = rc;
111 PLOG(FATAL) << "TimedWait failed for " << name_;
112 }
113}
114
Elliott Hughes8daa0922011-09-11 13:46:25 -0700115} // namespace