blob: f4658fb42025278f1e8774ece88bb124f89474ac [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#ifndef ART_SRC_MUTEX_H_
18#define ART_SRC_MUTEX_H_
19
20#include <pthread.h>
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080021#include <stdint.h>
Elliott Hughes8daa0922011-09-11 13:46:25 -070022#include <string>
23
24#include "logging.h"
25#include "macros.h"
26
27namespace art {
28
29class Mutex {
30 public:
31 explicit Mutex(const char* name);
32 ~Mutex();
33
34 void Lock();
35
36 bool TryLock();
37
38 void Unlock();
39
40 const char* GetName() {
41 return name_.c_str();
42 }
43
44 pthread_mutex_t* GetImpl() {
45 return &mutex_;
46 }
47
48 void AssertHeld() {
Elliott Hughescf044312012-01-23 18:48:51 -080049#if !defined(__APPLE__)
Elliott Hughes8daa0922011-09-11 13:46:25 -070050 DCHECK_EQ(GetOwner(), GetTid());
Elliott Hughescf044312012-01-23 18:48:51 -080051#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -070052 }
53
54 void AssertNotHeld() {
Elliott Hughescf044312012-01-23 18:48:51 -080055#if !defined(__APPLE__)
Elliott Hughes8daa0922011-09-11 13:46:25 -070056 DCHECK_NE(GetOwner(), GetTid());
Elliott Hughescf044312012-01-23 18:48:51 -080057#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -070058 }
59
Elliott Hughes8daa0922011-09-11 13:46:25 -070060 pid_t GetOwner();
Elliott Hughesaccd83d2011-10-17 14:25:58 -070061
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080062 void AssertDepth(uint32_t depth) {
63#if !defined(__APPLE__)
64 DCHECK_EQ(depth, GetDepth());
65#endif
66 }
67
Elliott Hughesaccd83d2011-10-17 14:25:58 -070068 private:
69 static pid_t GetTid();
Elliott Hughes8daa0922011-09-11 13:46:25 -070070
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080071 uint32_t GetDepth();
72
Elliott Hughes8daa0922011-09-11 13:46:25 -070073 std::string name_;
74
75 pthread_mutex_t mutex_;
76
77 DISALLOW_COPY_AND_ASSIGN(Mutex);
78};
79
80class MutexLock {
81 public:
82 explicit MutexLock(Mutex& mu) : mu_(mu) {
83 mu_.Lock();
84 }
85
86 ~MutexLock() {
87 mu_.Unlock();
88 }
89
90 private:
91 Mutex& mu_;
92 DISALLOW_COPY_AND_ASSIGN(MutexLock);
93};
94
Elliott Hughes5f791332011-09-15 17:45:30 -070095class ConditionVariable {
96 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -070097 explicit ConditionVariable(const std::string& name);
Elliott Hughes5f791332011-09-15 17:45:30 -070098 ~ConditionVariable();
99
100 void Broadcast();
101 void Signal();
102 void Wait(Mutex& mutex);
103 void TimedWait(Mutex& mutex, const timespec& ts);
104
105 private:
106 pthread_cond_t cond_;
107 std::string name_;
108 DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
109};
110
Elliott Hughes8daa0922011-09-11 13:46:25 -0700111} // namespace art
112
113#endif // ART_SRC_MUTEX_H_