1/******************************************************************************
2 *
3 *  Copyright (C) 2012 Broadcom Corporation
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 *  Encapsulate a mutex for thread synchronization.
22 *
23 ******************************************************************************/
24
25#define LOG_TAG "NfcNciHal"
26#include "Mutex.h"
27#include <errno.h>
28#include <string.h>
29#include "_OverrideLog.h"
30
31/*******************************************************************************
32**
33** Function:        Mutex
34**
35** Description:     Initialize member variables.
36**
37** Returns:         None.
38**
39*******************************************************************************/
40Mutex::Mutex() {
41  memset(&mMutex, 0, sizeof(mMutex));
42  int res = pthread_mutex_init(&mMutex, NULL);
43  if (res != 0) {
44    ALOGE("Mutex::Mutex: fail init; error=0x%X", res);
45  }
46}
47
48/*******************************************************************************
49**
50** Function:        ~Mutex
51**
52** Description:     Cleanup all resources.
53**
54** Returns:         None.
55**
56*******************************************************************************/
57Mutex::~Mutex() {
58  int res = pthread_mutex_destroy(&mMutex);
59  if (res != 0) {
60    ALOGE("Mutex::~Mutex: fail destroy; error=0x%X", res);
61  }
62}
63
64/*******************************************************************************
65**
66** Function:        lock
67**
68** Description:     Block the thread and try lock the mutex.
69**
70** Returns:         None.
71**
72*******************************************************************************/
73void Mutex::lock() {
74  int res = pthread_mutex_lock(&mMutex);
75  if (res != 0) {
76    ALOGE("Mutex::lock: fail lock; error=0x%X", res);
77  }
78}
79
80/*******************************************************************************
81**
82** Function:        unlock
83**
84** Description:     Unlock a mutex to unblock a thread.
85**
86** Returns:         None.
87**
88*******************************************************************************/
89void Mutex::unlock() {
90  int res = pthread_mutex_unlock(&mMutex);
91  if (res != 0) {
92    ALOGE("Mutex::unlock: fail unlock; error=0x%X", res);
93  }
94}
95
96/*******************************************************************************
97**
98** Function:        tryLock
99**
100** Description:     Try to lock the mutex.
101**
102** Returns:         True if the mutex is locked.
103**
104*******************************************************************************/
105bool Mutex::tryLock() {
106  int res = pthread_mutex_trylock(&mMutex);
107  if ((res != 0) && (res != EBUSY)) {
108    ALOGE("Mutex::tryLock: error=0x%X", res);
109  }
110  return res == 0;
111}
112
113/*******************************************************************************
114**
115** Function:        nativeHandle
116**
117** Description:     Get the handle of the mutex.
118**
119** Returns:         Handle of the mutex.
120**
121*******************************************************************************/
122pthread_mutex_t* Mutex::nativeHandle() { return &mMutex; }
123