blob: 963ac99e9b09aabaaf8e3912b2f64fe1ccb91c62 [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 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
Elliott Hughesffb465f2012-03-01 18:46:05 -080030static inline void CheckRank(MutexRank rank, bool is_locking) {
31#ifndef NDEBUG
32 if (rank == -1) {
33 return;
34 }
35 Thread* self = Thread::Current();
36 if (self != NULL) {
37 self->CheckRank(rank, is_locking);
38 }
39#endif
40}
41
42Mutex::Mutex(const char* name, MutexRank rank) : name_(name), rank_(rank) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080043 // Like Java, we use recursive mutexes.
44 pthread_mutexattr_t attributes;
45 CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes));
46 CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE));
47 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes));
48 CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes));
Elliott Hughes8daa0922011-09-11 13:46:25 -070049}
50
51Mutex::~Mutex() {
Elliott Hughes6b355752012-01-13 16:49:08 -080052 int rc = pthread_mutex_destroy(&mutex_);
53 if (rc != 0) {
54 errno = rc;
55 bool shutting_down = Runtime::Current()->IsShuttingDown();
56 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
57 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070058}
59
60void Mutex::Lock() {
Elliott Hughes8d768a92011-09-14 16:35:25 -070061 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
Elliott Hughesffb465f2012-03-01 18:46:05 -080062 CheckRank(rank_, true);
Ian Rogers105245c2012-01-27 11:42:43 -080063 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -070064}
65
66bool Mutex::TryLock() {
67 int result = pthread_mutex_trylock(&mutex_);
68 if (result == EBUSY) {
69 return false;
70 }
71 if (result != 0) {
72 errno = result;
Elliott Hughes8d768a92011-09-14 16:35:25 -070073 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070074 }
Elliott Hughesffb465f2012-03-01 18:46:05 -080075 CheckRank(rank_, true);
Ian Rogers105245c2012-01-27 11:42:43 -080076 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -070077 return true;
78}
79
80void Mutex::Unlock() {
Ian Rogers105245c2012-01-27 11:42:43 -080081 AssertHeld();
Elliott Hughes8d768a92011-09-14 16:35:25 -070082 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
Elliott Hughesffb465f2012-03-01 18:46:05 -080083 CheckRank(rank_, false);
Elliott Hughes8daa0922011-09-11 13:46:25 -070084}
85
86pid_t Mutex::GetOwner() {
Elliott Hughes3147a232011-10-12 15:55:07 -070087#if defined(__BIONIC__)
Elliott Hughes8daa0922011-09-11 13:46:25 -070088 return static_cast<pid_t>((mutex_.value >> 16) & 0xffff);
Elliott Hughes3147a232011-10-12 15:55:07 -070089#elif defined(__GLIBC__)
90 struct __attribute__((__may_alias__)) glibc_pthread_t {
91 int lock;
92 unsigned int count;
93 int owner;
94 // ...other stuff we don't care about.
95 };
96 return reinterpret_cast<glibc_pthread_t*>(&mutex_)->owner;
Elliott Hughescf044312012-01-23 18:48:51 -080097#elif defined(__APPLE__)
98 // We don't know a way to implement this for Mac OS.
99 return 0;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700100#else
101 UNIMPLEMENTED(FATAL);
102 return 0;
103#endif
104}
105
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800106uint32_t Mutex::GetDepth() {
107 bool held = (GetOwner() == GetTid());
108 if (!held) {
109 return 0;
110 }
111 uint32_t depth;
112#if defined(__BIONIC__)
113 depth = static_cast<uint32_t>((mutex_.value >> 2) & 0x7ff) + 1;
114#elif defined(__GLIBC__)
115 struct __attribute__((__may_alias__)) glibc_pthread_t {
116 int lock;
117 unsigned int count;
118 int owner;
119 // ...other stuff we don't care about.
120 };
121 depth = reinterpret_cast<glibc_pthread_t*>(&mutex_)->count;
122#elif defined(__APPLE__)
123 // We don't know a way to implement this for Mac OS.
124 return 0;
125#else
126 UNIMPLEMENTED(FATAL);
127 return 0;
128#endif
129 CHECK_NE(0U, depth) << "owner=" << GetOwner() << " tid=" << GetTid();
130 return depth;
131}
132
Elliott Hughes8daa0922011-09-11 13:46:25 -0700133pid_t Mutex::GetTid() {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800134 return ::art::GetTid();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700135}
136
Elliott Hughes5f791332011-09-15 17:45:30 -0700137ConditionVariable::ConditionVariable(const std::string& name) : name_(name) {
138 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL));
139}
140
141ConditionVariable::~ConditionVariable() {
142 CHECK_MUTEX_CALL(pthread_cond_destroy, (&cond_));
143}
144
145void ConditionVariable::Broadcast() {
146 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
147}
148
149void ConditionVariable::Signal() {
150 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
151}
152
153void ConditionVariable::Wait(Mutex& mutex) {
154 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, mutex.GetImpl()));
155}
156
157void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) {
158#ifdef HAVE_TIMEDWAIT_MONOTONIC
159#define TIMEDWAIT pthread_cond_timedwait_monotonic
160#else
161#define TIMEDWAIT pthread_cond_timedwait
162#endif
163 int rc = TIMEDWAIT(&cond_, mutex.GetImpl(), &ts);
164 if (rc != 0 && rc != ETIMEDOUT) {
165 errno = rc;
166 PLOG(FATAL) << "TimedWait failed for " << name_;
167 }
168}
169
Elliott Hughesffb465f2012-03-01 18:46:05 -0800170std::ostream& operator<<(std::ostream& os, const MutexRank& rhs) {
171 switch (rhs) {
172 case kHeapLock: os << "HeapLock"; break;
173 case kThreadListLock: os << "ThreadListLock"; break;
174 case kThreadSuspendCountLock: os << "ThreadSuspendCountLock"; break;
175 default: os << "MutexRank[" << static_cast<int>(rhs) << "]"; break;
176 }
177 return os;
178}
179
Elliott Hughes8daa0922011-09-11 13:46:25 -0700180} // namespace