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