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