mutex.h revision cd74c4b3a6893c876c6e03fd99a1264249653d80
18daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes/* 28daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Copyright (C) 2011 The Android Open Source Project 38daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * 48daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 58daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * you may not use this file except in compliance with the License. 68daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * You may obtain a copy of the License at 78daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * 88daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 98daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * 108daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Unless required by applicable law or agreed to in writing, software 118daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 128daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * See the License for the specific language governing permissions and 148daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * limitations under the License. 158daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes */ 168daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 178daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#ifndef ART_SRC_MUTEX_H_ 188daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#define ART_SRC_MUTEX_H_ 198daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 208daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include <pthread.h> 21cd74c4b3a6893c876c6e03fd99a1264249653d80Brian Carlstrom#include <stdint.h> 228daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include <string> 238daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 248daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "logging.h" 258daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "macros.h" 268daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 278daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesnamespace art { 288daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 298daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesclass Mutex { 308daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes public: 318daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes explicit Mutex(const char* name); 328daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes ~Mutex(); 338daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 348daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes void Lock(); 358daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 368daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes bool TryLock(); 378daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 388daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes void Unlock(); 398daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 408daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes const char* GetName() { 418daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes return name_.c_str(); 428daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 438daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 448daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes pthread_mutex_t* GetImpl() { 458daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes return &mutex_; 468daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 478daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 488daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes void AssertHeld() { 49cf044318c5638df0c400b731e94fab948ebd5ccbElliott Hughes#if !defined(__APPLE__) 508daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes DCHECK_EQ(GetOwner(), GetTid()); 51cf044318c5638df0c400b731e94fab948ebd5ccbElliott Hughes#endif 528daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 538daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 548daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes void AssertNotHeld() { 55cf044318c5638df0c400b731e94fab948ebd5ccbElliott Hughes#if !defined(__APPLE__) 568daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes DCHECK_NE(GetOwner(), GetTid()); 57cf044318c5638df0c400b731e94fab948ebd5ccbElliott Hughes#endif 588daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 598daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 608daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes pid_t GetOwner(); 61accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 62cd74c4b3a6893c876c6e03fd99a1264249653d80Brian Carlstrom void AssertDepth(uint32_t depth) { 63cd74c4b3a6893c876c6e03fd99a1264249653d80Brian Carlstrom#if !defined(__APPLE__) 64cd74c4b3a6893c876c6e03fd99a1264249653d80Brian Carlstrom DCHECK_EQ(depth, GetDepth()); 65cd74c4b3a6893c876c6e03fd99a1264249653d80Brian Carlstrom#endif 66cd74c4b3a6893c876c6e03fd99a1264249653d80Brian Carlstrom } 67cd74c4b3a6893c876c6e03fd99a1264249653d80Brian Carlstrom 68accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes private: 69accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes static pid_t GetTid(); 708daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 714514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom void ClearOwner(); 724514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom 73cd74c4b3a6893c876c6e03fd99a1264249653d80Brian Carlstrom uint32_t GetDepth(); 74cd74c4b3a6893c876c6e03fd99a1264249653d80Brian Carlstrom 758daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes std::string name_; 768daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 778daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes pthread_mutex_t mutex_; 788daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 794514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom friend class MonitorList; // for ClearOwner 808daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes DISALLOW_COPY_AND_ASSIGN(Mutex); 818daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 828daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 838daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesclass MutexLock { 848daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes public: 858daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes explicit MutexLock(Mutex& mu) : mu_(mu) { 868daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes mu_.Lock(); 878daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 888daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 898daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes ~MutexLock() { 908daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes mu_.Unlock(); 918daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes } 928daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 938daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes private: 948daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes Mutex& mu_; 958daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes DISALLOW_COPY_AND_ASSIGN(MutexLock); 968daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 978daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesclass ConditionVariable { 995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes public: 100a51a3dd5603daf3d368b7735067e1d9eb54c4c40Elliott Hughes explicit ConditionVariable(const std::string& name); 1015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ~ConditionVariable(); 1025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes void Broadcast(); 1045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes void Signal(); 1055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes void Wait(Mutex& mutex); 1065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes void TimedWait(Mutex& mutex, const timespec& ts); 1075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes private: 1095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes pthread_cond_t cond_; 1105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes std::string name_; 1115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DISALLOW_COPY_AND_ASSIGN(ConditionVariable); 1125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}; 1135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1148daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 1158daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 1168daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#endif // ART_SRC_MUTEX_H_ 117