mutex.h revision 8daa0929f08a3080ea64dbd4e997e72f411e6fc9
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>
218daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include <string>
228daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
238daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "logging.h"
248daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "macros.h"
258daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
268daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesnamespace art {
278daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
288daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesclass Mutex {
298daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes public:
308daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  explicit Mutex(const char* name);
318daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  ~Mutex();
328daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
338daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  void Lock();
348daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
358daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  bool TryLock();
368daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
378daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  void Unlock();
388daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
398daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  const char* GetName() {
408daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    return name_.c_str();
418daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
428daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
438daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  pthread_mutex_t* GetImpl() {
448daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    return &mutex_;
458daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
468daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
478daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  void AssertHeld() {
488daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#ifdef __BIONIC__
498daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    DCHECK_EQ(GetOwner(), GetTid());
508daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#endif
518daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
528daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
538daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  void AssertNotHeld() {
548daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#ifdef __BIONIC__
558daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    DCHECK_NE(GetOwner(), GetTid());
568daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#endif
578daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
588daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
598daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes private:
608daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  pid_t GetOwner();
618daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  pid_t GetTid();
628daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
638daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  std::string name_;
648daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
658daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  pthread_mutex_t mutex_;
668daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
678daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(Mutex);
688daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes};
698daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
708daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesclass MutexLock {
718daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes public:
728daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  explicit MutexLock(Mutex& mu) : mu_(mu) {
738daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    mu_.Lock();
748daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
758daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
768daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  ~MutexLock() {
778daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes    mu_.Unlock();
788daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  }
798daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
808daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes private:
818daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  Mutex& mu_;
828daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(MutexLock);
838daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes};
848daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
858daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}  // namespace art
868daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
878daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#endif  // ART_SRC_MUTEX_H_
88