1 /* 2 * Copyright (C) 2012 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 /* 18 * Encapsulate a mutex for thread synchronization. 19 */ 20 21 #pragma once 22 #include <pthread.h> 23 24 class Mutex { 25 public: 26 /******************************************************************************* 27 ** 28 ** Function: Mutex 29 ** 30 ** Description: Initialize member variables. 31 ** 32 ** Returns: None. 33 ** 34 *******************************************************************************/ 35 Mutex(); 36 37 /******************************************************************************* 38 ** 39 ** Function: ~Mutex 40 ** 41 ** Description: Cleanup all resources. 42 ** 43 ** Returns: None. 44 ** 45 *******************************************************************************/ 46 ~Mutex(); 47 48 /******************************************************************************* 49 ** 50 ** Function: lock 51 ** 52 ** Description: Block the thread and try lock the mutex. 53 ** 54 ** Returns: None. 55 ** 56 *******************************************************************************/ 57 void lock(); 58 59 /******************************************************************************* 60 ** 61 ** Function: unlock 62 ** 63 ** Description: Unlock a mutex to unblock a thread. 64 ** 65 ** Returns: None. 66 ** 67 *******************************************************************************/ 68 void unlock(); 69 70 /******************************************************************************* 71 ** 72 ** Function: tryLock 73 ** 74 ** Description: Try to lock the mutex. 75 ** 76 ** Returns: True if the mutex is locked. 77 ** 78 *******************************************************************************/ 79 bool tryLock(); 80 81 /******************************************************************************* 82 ** 83 ** Function: nativeHandle 84 ** 85 ** Description: Get the handle of the mutex. 86 ** 87 ** Returns: Handle of the mutex. 88 ** 89 *******************************************************************************/ 90 pthread_mutex_t* nativeHandle(); 91 92 class Autolock { 93 public: Autolock(Mutex & mutex)94 inline Autolock(Mutex& mutex) : mLock(mutex) { mLock.lock(); } Autolock(Mutex * mutex)95 inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); } ~Autolock()96 inline ~Autolock() { mLock.unlock(); } 97 98 private: 99 Mutex& mLock; 100 }; 101 102 private: 103 pthread_mutex_t mMutex; 104 }; 105 106 typedef Mutex::Autolock AutoMutex; 107