blob: 949d221ff883260032a2d8ee268f9665f52b9c95 [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>
21#include <string>
22
23#include "logging.h"
24#include "macros.h"
25
26namespace art {
27
28class Mutex {
29 public:
30 explicit Mutex(const char* name);
31 ~Mutex();
32
33 void Lock();
34
35 bool TryLock();
36
37 void Unlock();
38
39 const char* GetName() {
40 return name_.c_str();
41 }
42
43 pthread_mutex_t* GetImpl() {
44 return &mutex_;
45 }
46
47 void AssertHeld() {
Elliott Hughescf044312012-01-23 18:48:51 -080048#if !defined(__APPLE__)
Elliott Hughes8daa0922011-09-11 13:46:25 -070049 DCHECK_EQ(GetOwner(), GetTid());
Elliott Hughescf044312012-01-23 18:48:51 -080050#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -070051 }
52
53 void AssertNotHeld() {
Elliott Hughescf044312012-01-23 18:48:51 -080054#if !defined(__APPLE__)
Elliott Hughes8daa0922011-09-11 13:46:25 -070055 DCHECK_NE(GetOwner(), GetTid());
Elliott Hughescf044312012-01-23 18:48:51 -080056#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -070057 }
58
Elliott Hughes8daa0922011-09-11 13:46:25 -070059 pid_t GetOwner();
Elliott Hughesaccd83d2011-10-17 14:25:58 -070060
61 private:
62 static pid_t GetTid();
Elliott Hughes8daa0922011-09-11 13:46:25 -070063
Brian Carlstrom4514d3c2011-10-21 17:01:31 -070064 void ClearOwner();
65
Elliott Hughes8daa0922011-09-11 13:46:25 -070066 std::string name_;
67
68 pthread_mutex_t mutex_;
69
Brian Carlstrom4514d3c2011-10-21 17:01:31 -070070 friend class MonitorList; // for ClearOwner
Elliott Hughes8daa0922011-09-11 13:46:25 -070071 DISALLOW_COPY_AND_ASSIGN(Mutex);
72};
73
74class MutexLock {
75 public:
76 explicit MutexLock(Mutex& mu) : mu_(mu) {
77 mu_.Lock();
78 }
79
80 ~MutexLock() {
81 mu_.Unlock();
82 }
83
84 private:
85 Mutex& mu_;
86 DISALLOW_COPY_AND_ASSIGN(MutexLock);
87};
88
Elliott Hughes5f791332011-09-15 17:45:30 -070089class ConditionVariable {
90 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -070091 explicit ConditionVariable(const std::string& name);
Elliott Hughes5f791332011-09-15 17:45:30 -070092 ~ConditionVariable();
93
94 void Broadcast();
95 void Signal();
96 void Wait(Mutex& mutex);
97 void TimedWait(Mutex& mutex, const timespec& ts);
98
99 private:
100 pthread_cond_t cond_;
101 std::string name_;
102 DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
103};
104
Elliott Hughes8daa0922011-09-11 13:46:25 -0700105} // namespace art
106
107#endif // ART_SRC_MUTEX_H_