blob: 4760b2f2b312f44bc20e00b77b3da3a49a54b10f [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"
Elliott Hughes6b355752012-01-13 16:49:08 -080024#include "runtime.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070025
Elliott Hughes8d768a92011-09-14 16:35:25 -070026#define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_)
27
Elliott Hughes8daa0922011-09-11 13:46:25 -070028namespace art {
29
30Mutex::Mutex(const char* name) : name_(name) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080031 // Like Java, we use recursive mutexes.
32 pthread_mutexattr_t attributes;
33 CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes));
34 CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE));
35 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes));
36 CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes));
Elliott Hughes8daa0922011-09-11 13:46:25 -070037}
38
39Mutex::~Mutex() {
Elliott Hughes6b355752012-01-13 16:49:08 -080040 int rc = pthread_mutex_destroy(&mutex_);
41 if (rc != 0) {
42 errno = rc;
43 bool shutting_down = Runtime::Current()->IsShuttingDown();
44 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
45 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070046}
47
48void Mutex::Lock() {
Elliott Hughes8d768a92011-09-14 16:35:25 -070049 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
Ian Rogers105245c2012-01-27 11:42:43 -080050 AssertHeld();
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 }
Ian Rogers105245c2012-01-27 11:42:43 -080062 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -070063 return true;
64}
65
66void Mutex::Unlock() {
Ian Rogers105245c2012-01-27 11:42:43 -080067 AssertHeld();
Elliott Hughes8d768a92011-09-14 16:35:25 -070068 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
Elliott Hughes8daa0922011-09-11 13:46:25 -070069}
70
71pid_t Mutex::GetOwner() {
Elliott Hughes3147a232011-10-12 15:55:07 -070072#if defined(__BIONIC__)
Elliott Hughes8daa0922011-09-11 13:46:25 -070073 return static_cast<pid_t>((mutex_.value >> 16) & 0xffff);
Elliott Hughes3147a232011-10-12 15:55:07 -070074#elif defined(__GLIBC__)
75 struct __attribute__((__may_alias__)) glibc_pthread_t {
76 int lock;
77 unsigned int count;
78 int owner;
79 // ...other stuff we don't care about.
80 };
81 return reinterpret_cast<glibc_pthread_t*>(&mutex_)->owner;
Elliott Hughescf044312012-01-23 18:48:51 -080082#elif defined(__APPLE__)
83 // We don't know a way to implement this for Mac OS.
84 return 0;
Elliott Hughes8daa0922011-09-11 13:46:25 -070085#else
86 UNIMPLEMENTED(FATAL);
87 return 0;
88#endif
89}
90
Brian Carlstrom4514d3c2011-10-21 17:01:31 -070091void Mutex::ClearOwner() {
92#if defined(__BIONIC__)
93 mutex_.value = 0;
94#elif defined(__GLIBC__)
95 struct __attribute__((__may_alias__)) glibc_pthread_t {
96 int lock;
97 unsigned int count;
98 int owner;
99 // ...other stuff we don't care about.
100 };
101 reinterpret_cast<glibc_pthread_t*>(&mutex_)->owner = 0;
Elliott Hughescf044312012-01-23 18:48:51 -0800102#elif defined(__APPLE__)
103 // We don't know a way to implement this for Mac OS.
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700104#else
105 UNIMPLEMENTED(FATAL);
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700106#endif
107}
108
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800109uint32_t Mutex::GetDepth() {
110 bool held = (GetOwner() == GetTid());
111 if (!held) {
112 return 0;
113 }
114 uint32_t depth;
115#if defined(__BIONIC__)
116 depth = static_cast<uint32_t>((mutex_.value >> 2) & 0x7ff) + 1;
117#elif defined(__GLIBC__)
118 struct __attribute__((__may_alias__)) glibc_pthread_t {
119 int lock;
120 unsigned int count;
121 int owner;
122 // ...other stuff we don't care about.
123 };
124 depth = reinterpret_cast<glibc_pthread_t*>(&mutex_)->count;
125#elif defined(__APPLE__)
126 // We don't know a way to implement this for Mac OS.
127 return 0;
128#else
129 UNIMPLEMENTED(FATAL);
130 return 0;
131#endif
132 CHECK_NE(0U, depth) << "owner=" << GetOwner() << " tid=" << GetTid();
133 return depth;
134}
135
Elliott Hughes8daa0922011-09-11 13:46:25 -0700136pid_t Mutex::GetTid() {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800137 return ::art::GetTid();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700138}
139
Elliott Hughes5f791332011-09-15 17:45:30 -0700140ConditionVariable::ConditionVariable(const std::string& name) : name_(name) {
141 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL));
142}
143
144ConditionVariable::~ConditionVariable() {
145 CHECK_MUTEX_CALL(pthread_cond_destroy, (&cond_));
146}
147
148void ConditionVariable::Broadcast() {
149 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
150}
151
152void ConditionVariable::Signal() {
153 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
154}
155
156void ConditionVariable::Wait(Mutex& mutex) {
157 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, mutex.GetImpl()));
158}
159
160void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) {
161#ifdef HAVE_TIMEDWAIT_MONOTONIC
162#define TIMEDWAIT pthread_cond_timedwait_monotonic
163#else
164#define TIMEDWAIT pthread_cond_timedwait
165#endif
166 int rc = TIMEDWAIT(&cond_, mutex.GetImpl(), &ts);
167 if (rc != 0 && rc != ETIMEDOUT) {
168 errno = rc;
169 PLOG(FATAL) << "TimedWait failed for " << name_;
170 }
171}
172
Elliott Hughes8daa0922011-09-11 13:46:25 -0700173} // namespace