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