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 #include "Mutex.h" 22 #include "NfcJniUtil.h" 23 24 #include <cutils/log.h> 25 #include <errno.h> 26 #include <string.h> 27 28 /******************************************************************************* 29 ** 30 ** Function: Mutex 31 ** 32 ** Description: Initialize member variables. 33 ** 34 ** Returns: None. 35 ** 36 *******************************************************************************/ 37 Mutex::Mutex () 38 { 39 memset (&mMutex, 0, sizeof(mMutex)); 40 int res = pthread_mutex_init (&mMutex, NULL); 41 if (res != 0) 42 { 43 ALOGE ("Mutex::Mutex: fail init; error=0x%X", res); 44 } 45 } 46 47 48 /******************************************************************************* 49 ** 50 ** Function: ~Mutex 51 ** 52 ** Description: Cleanup all resources. 53 ** 54 ** Returns: None. 55 ** 56 *******************************************************************************/ 57 Mutex::~Mutex () 58 { 59 int res = pthread_mutex_destroy (&mMutex); 60 if (res != 0) 61 { 62 ALOGE ("Mutex::~Mutex: fail destroy; error=0x%X", res); 63 } 64 } 65 66 67 /******************************************************************************* 68 ** 69 ** Function: lock 70 ** 71 ** Description: Block the thread and try lock the mutex. 72 ** 73 ** Returns: None. 74 ** 75 *******************************************************************************/ 76 void Mutex::lock () 77 { 78 int res = pthread_mutex_lock (&mMutex); 79 if (res != 0) 80 { 81 ALOGE ("Mutex::lock: fail lock; error=0x%X", res); 82 } 83 } 84 85 86 /******************************************************************************* 87 ** 88 ** Function: unlock 89 ** 90 ** Description: Unlock a mutex to unblock a thread. 91 ** 92 ** Returns: None. 93 ** 94 *******************************************************************************/ 95 void Mutex::unlock () 96 { 97 int res = pthread_mutex_unlock (&mMutex); 98 if (res != 0) 99 { 100 ALOGE ("Mutex::unlock: fail unlock; error=0x%X", res); 101 } 102 } 103 104 105 /******************************************************************************* 106 ** 107 ** Function: tryLock 108 ** 109 ** Description: Try to lock the mutex. 110 ** 111 ** Returns: True if the mutex is locked. 112 ** 113 *******************************************************************************/ 114 bool Mutex::tryLock () 115 { 116 int res = pthread_mutex_trylock (&mMutex); 117 if ((res != 0) && (res != EBUSY)) 118 { 119 ALOGE ("Mutex::tryLock: error=0x%X", res); 120 } 121 return res == 0; 122 } 123 124 125 /******************************************************************************* 126 ** 127 ** Function: nativeHandle 128 ** 129 ** Description: Get the handle of the mutex. 130 ** 131 ** Returns: Handle of the mutex. 132 ** 133 *******************************************************************************/ 134 pthread_mutex_t* Mutex::nativeHandle () 135 { 136 return &mMutex; 137 } 138 139 140