blob: 93f6c3eaa963cd893518b2bcf2ad0d459bfbcfe8 [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 Carlstrom4514d3c2011-10-21 17:01:31 -070071 void ClearOwner();
72
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080073 uint32_t GetDepth();
74
Elliott Hughes8daa0922011-09-11 13:46:25 -070075 std::string name_;
76
77 pthread_mutex_t mutex_;
78
Brian Carlstrom4514d3c2011-10-21 17:01:31 -070079 friend class MonitorList; // for ClearOwner
Elliott Hughes8daa0922011-09-11 13:46:25 -070080 DISALLOW_COPY_AND_ASSIGN(Mutex);
81};
82
83class MutexLock {
84 public:
85 explicit MutexLock(Mutex& mu) : mu_(mu) {
86 mu_.Lock();
87 }
88
89 ~MutexLock() {
90 mu_.Unlock();
91 }
92
93 private:
94 Mutex& mu_;
95 DISALLOW_COPY_AND_ASSIGN(MutexLock);
96};
97
Elliott Hughes5f791332011-09-15 17:45:30 -070098class ConditionVariable {
99 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700100 explicit ConditionVariable(const std::string& name);
Elliott Hughes5f791332011-09-15 17:45:30 -0700101 ~ConditionVariable();
102
103 void Broadcast();
104 void Signal();
105 void Wait(Mutex& mutex);
106 void TimedWait(Mutex& mutex, const timespec& ts);
107
108 private:
109 pthread_cond_t cond_;
110 std::string name_;
111 DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
112};
113
Elliott Hughes8daa0922011-09-11 13:46:25 -0700114} // namespace art
115
116#endif // ART_SRC_MUTEX_H_