Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 21 | #include "heap.h" // for VERIFY_OBJECT_ENABLED |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 22 | #include "logging.h" |
| 23 | #include "utils.h" |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 24 | #include "runtime.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 26 | #define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_) |
| 27 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
| 30 | Mutex::Mutex(const char* name) : name_(name) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 31 | // 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 Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | Mutex::~Mutex() { |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 40 | 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 Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void Mutex::Lock() { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 49 | CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_)); |
Ian Rogers | 105245c | 2012-01-27 11:42:43 -0800 | [diff] [blame] | 50 | AssertHeld(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool 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 Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 60 | PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 61 | } |
Ian Rogers | 105245c | 2012-01-27 11:42:43 -0800 | [diff] [blame] | 62 | AssertHeld(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 63 | return true; |
| 64 | } |
| 65 | |
| 66 | void Mutex::Unlock() { |
Ian Rogers | 105245c | 2012-01-27 11:42:43 -0800 | [diff] [blame] | 67 | AssertHeld(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 68 | CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | pid_t Mutex::GetOwner() { |
Elliott Hughes | 3147a23 | 2011-10-12 15:55:07 -0700 | [diff] [blame] | 72 | #if defined(__BIONIC__) |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 73 | return static_cast<pid_t>((mutex_.value >> 16) & 0xffff); |
Elliott Hughes | 3147a23 | 2011-10-12 15:55:07 -0700 | [diff] [blame] | 74 | #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 Hughes | cf04431 | 2012-01-23 18:48:51 -0800 | [diff] [blame] | 82 | #elif defined(__APPLE__) |
| 83 | // We don't know a way to implement this for Mac OS. |
| 84 | return 0; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 85 | #else |
| 86 | UNIMPLEMENTED(FATAL); |
| 87 | return 0; |
| 88 | #endif |
| 89 | } |
| 90 | |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 91 | uint32_t Mutex::GetDepth() { |
| 92 | bool held = (GetOwner() == GetTid()); |
| 93 | if (!held) { |
| 94 | return 0; |
| 95 | } |
| 96 | uint32_t depth; |
| 97 | #if defined(__BIONIC__) |
| 98 | depth = static_cast<uint32_t>((mutex_.value >> 2) & 0x7ff) + 1; |
| 99 | #elif defined(__GLIBC__) |
| 100 | struct __attribute__((__may_alias__)) glibc_pthread_t { |
| 101 | int lock; |
| 102 | unsigned int count; |
| 103 | int owner; |
| 104 | // ...other stuff we don't care about. |
| 105 | }; |
| 106 | depth = reinterpret_cast<glibc_pthread_t*>(&mutex_)->count; |
| 107 | #elif defined(__APPLE__) |
| 108 | // We don't know a way to implement this for Mac OS. |
| 109 | return 0; |
| 110 | #else |
| 111 | UNIMPLEMENTED(FATAL); |
| 112 | return 0; |
| 113 | #endif |
| 114 | CHECK_NE(0U, depth) << "owner=" << GetOwner() << " tid=" << GetTid(); |
| 115 | return depth; |
| 116 | } |
| 117 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 118 | pid_t Mutex::GetTid() { |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 119 | return ::art::GetTid(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 122 | ConditionVariable::ConditionVariable(const std::string& name) : name_(name) { |
| 123 | CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL)); |
| 124 | } |
| 125 | |
| 126 | ConditionVariable::~ConditionVariable() { |
| 127 | CHECK_MUTEX_CALL(pthread_cond_destroy, (&cond_)); |
| 128 | } |
| 129 | |
| 130 | void ConditionVariable::Broadcast() { |
| 131 | CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_)); |
| 132 | } |
| 133 | |
| 134 | void ConditionVariable::Signal() { |
| 135 | CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_)); |
| 136 | } |
| 137 | |
| 138 | void ConditionVariable::Wait(Mutex& mutex) { |
| 139 | CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, mutex.GetImpl())); |
| 140 | } |
| 141 | |
| 142 | void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) { |
| 143 | #ifdef HAVE_TIMEDWAIT_MONOTONIC |
| 144 | #define TIMEDWAIT pthread_cond_timedwait_monotonic |
| 145 | #else |
| 146 | #define TIMEDWAIT pthread_cond_timedwait |
| 147 | #endif |
| 148 | int rc = TIMEDWAIT(&cond_, mutex.GetImpl(), &ts); |
| 149 | if (rc != 0 && rc != ETIMEDOUT) { |
| 150 | errno = rc; |
| 151 | PLOG(FATAL) << "TimedWait failed for " << name_; |
| 152 | } |
| 153 | } |
| 154 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 155 | } // namespace |