blob: 02d127f5d06d29453f866098c23df04621114d72 [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
Elliott Hughes3efb8412012-03-16 16:09:38 -070026#include "gtest/gtest.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070027#include "logging.h"
28#include "macros.h"
29
30namespace art {
31
Elliott Hughesffb465f2012-03-01 18:46:05 -080032enum MutexRank {
33 kNoMutexRank = -1,
34 kHeapLock = 0,
35 kThreadListLock = 1,
36 kThreadSuspendCountLock = 2,
37 kMaxMutexRank = kThreadSuspendCountLock,
38};
39std::ostream& operator<<(std::ostream& os, const MutexRank& rhs);
40
Elliott Hughes8daa0922011-09-11 13:46:25 -070041class Mutex {
42 public:
Elliott Hughesffb465f2012-03-01 18:46:05 -080043 explicit Mutex(const char* name, MutexRank rank = kNoMutexRank);
Elliott Hughes8daa0922011-09-11 13:46:25 -070044 ~Mutex();
45
46 void Lock();
47
48 bool TryLock();
49
50 void Unlock();
51
52 const char* GetName() {
53 return name_.c_str();
54 }
55
56 pthread_mutex_t* GetImpl() {
57 return &mutex_;
58 }
59
Elliott Hughesa4060e52012-03-02 16:51:35 -080060 MutexRank GetRank() const {
61 return rank_;
62 }
63
Elliott Hughes8daa0922011-09-11 13:46:25 -070064 void AssertHeld() {
Elliott Hughescf044312012-01-23 18:48:51 -080065#if !defined(__APPLE__)
Elliott Hughes8daa0922011-09-11 13:46:25 -070066 DCHECK_EQ(GetOwner(), GetTid());
Elliott Hughescf044312012-01-23 18:48:51 -080067#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -070068 }
69
70 void AssertNotHeld() {
Elliott Hughescf044312012-01-23 18:48:51 -080071#if !defined(__APPLE__)
Elliott Hughes8daa0922011-09-11 13:46:25 -070072 DCHECK_NE(GetOwner(), GetTid());
Elliott Hughescf044312012-01-23 18:48:51 -080073#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -070074 }
75
Elliott Hughes8daa0922011-09-11 13:46:25 -070076 pid_t GetOwner();
Elliott Hughesaccd83d2011-10-17 14:25:58 -070077
78 private:
79 static pid_t GetTid();
Elliott Hughes8daa0922011-09-11 13:46:25 -070080
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080081 uint32_t GetDepth();
82
Elliott Hughes8daa0922011-09-11 13:46:25 -070083 pthread_mutex_t mutex_;
Elliott Hughesffb465f2012-03-01 18:46:05 -080084 std::string name_;
85 MutexRank rank_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070086
Elliott Hughes3efb8412012-03-16 16:09:38 -070087 friend class MutexTester;
Elliott Hughes8daa0922011-09-11 13:46:25 -070088 DISALLOW_COPY_AND_ASSIGN(Mutex);
89};
90
91class MutexLock {
92 public:
93 explicit MutexLock(Mutex& mu) : mu_(mu) {
94 mu_.Lock();
95 }
96
97 ~MutexLock() {
98 mu_.Unlock();
99 }
100
101 private:
102 Mutex& mu_;
103 DISALLOW_COPY_AND_ASSIGN(MutexLock);
104};
105
Elliott Hughes5f791332011-09-15 17:45:30 -0700106class ConditionVariable {
107 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700108 explicit ConditionVariable(const std::string& name);
Elliott Hughes5f791332011-09-15 17:45:30 -0700109 ~ConditionVariable();
110
111 void Broadcast();
112 void Signal();
113 void Wait(Mutex& mutex);
114 void TimedWait(Mutex& mutex, const timespec& ts);
115
116 private:
117 pthread_cond_t cond_;
118 std::string name_;
119 DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
120};
121
Elliott Hughes8daa0922011-09-11 13:46:25 -0700122} // namespace art
123
124#endif // ART_SRC_MUTEX_H_