blob: 475c7dd3334c1170ced01771f8c25a7760b7d466 [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 Hughesffb465f2012-03-01 18:46:05 -080022
23#include <iosfwd>
Elliott Hughes8daa0922011-09-11 13:46:25 -070024#include <string>
25
26#include "logging.h"
27#include "macros.h"
28
29namespace art {
30
Elliott Hughesffb465f2012-03-01 18:46:05 -080031enum MutexRank {
32 kNoMutexRank = -1,
33 kHeapLock = 0,
34 kThreadListLock = 1,
35 kThreadSuspendCountLock = 2,
36 kMaxMutexRank = kThreadSuspendCountLock,
37};
38std::ostream& operator<<(std::ostream& os, const MutexRank& rhs);
39
Elliott Hughes8daa0922011-09-11 13:46:25 -070040class Mutex {
41 public:
Elliott Hughesffb465f2012-03-01 18:46:05 -080042 explicit Mutex(const char* name, MutexRank rank = kNoMutexRank);
Elliott Hughes8daa0922011-09-11 13:46:25 -070043 ~Mutex();
44
45 void Lock();
46
47 bool TryLock();
48
49 void Unlock();
50
51 const char* GetName() {
52 return name_.c_str();
53 }
54
55 pthread_mutex_t* GetImpl() {
56 return &mutex_;
57 }
58
Elliott Hughesa4060e52012-03-02 16:51:35 -080059 MutexRank GetRank() const {
60 return rank_;
61 }
62
Elliott Hughes8daa0922011-09-11 13:46:25 -070063 void AssertHeld() {
Elliott Hughescf044312012-01-23 18:48:51 -080064#if !defined(__APPLE__)
Elliott Hughes8daa0922011-09-11 13:46:25 -070065 DCHECK_EQ(GetOwner(), GetTid());
Elliott Hughescf044312012-01-23 18:48:51 -080066#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -070067 }
68
69 void AssertNotHeld() {
Elliott Hughescf044312012-01-23 18:48:51 -080070#if !defined(__APPLE__)
Elliott Hughes8daa0922011-09-11 13:46:25 -070071 DCHECK_NE(GetOwner(), GetTid());
Elliott Hughescf044312012-01-23 18:48:51 -080072#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -070073 }
74
Elliott Hughes8daa0922011-09-11 13:46:25 -070075 pid_t GetOwner();
Elliott Hughesaccd83d2011-10-17 14:25:58 -070076
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080077 void AssertDepth(uint32_t depth) {
78#if !defined(__APPLE__)
79 DCHECK_EQ(depth, GetDepth());
80#endif
81 }
82
Elliott Hughesaccd83d2011-10-17 14:25:58 -070083 private:
84 static pid_t GetTid();
Elliott Hughes8daa0922011-09-11 13:46:25 -070085
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080086 uint32_t GetDepth();
87
Elliott Hughes8daa0922011-09-11 13:46:25 -070088 pthread_mutex_t mutex_;
Elliott Hughesffb465f2012-03-01 18:46:05 -080089 std::string name_;
90 MutexRank rank_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070091
92 DISALLOW_COPY_AND_ASSIGN(Mutex);
93};
94
95class MutexLock {
96 public:
97 explicit MutexLock(Mutex& mu) : mu_(mu) {
98 mu_.Lock();
99 }
100
101 ~MutexLock() {
102 mu_.Unlock();
103 }
104
105 private:
106 Mutex& mu_;
107 DISALLOW_COPY_AND_ASSIGN(MutexLock);
108};
109
Elliott Hughes5f791332011-09-15 17:45:30 -0700110class ConditionVariable {
111 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700112 explicit ConditionVariable(const std::string& name);
Elliott Hughes5f791332011-09-15 17:45:30 -0700113 ~ConditionVariable();
114
115 void Broadcast();
116 void Signal();
117 void Wait(Mutex& mutex);
118 void TimedWait(Mutex& mutex, const timespec& ts);
119
120 private:
121 pthread_cond_t cond_;
122 std::string name_;
123 DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
124};
125
Elliott Hughes8daa0922011-09-11 13:46:25 -0700126} // namespace art
127
128#endif // ART_SRC_MUTEX_H_