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