1 /*
2 *
3 * Copyright 2021-2022 NXP
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 #include "NxpNfcThreadMutex.h"
19
20 /*******************************************************************************
21 **
22 ** Function: NfcHalThreadMutex::NfcHalThreadMutex()
23 **
24 ** Description: class constructor
25 **
26 ** Returns: none
27 **
28 *******************************************************************************/
NfcHalThreadMutex()29 NfcHalThreadMutex::NfcHalThreadMutex() {
30 pthread_mutexattr_t mutexAttr;
31
32 pthread_mutexattr_init(&mutexAttr);
33 pthread_mutex_init(&mMutex, &mutexAttr);
34 pthread_mutexattr_destroy(&mutexAttr);
35 }
36
37 /*******************************************************************************
38 **
39 ** Function: NfcHalThreadMutex::~NfcHalThreadMutex()
40 **
41 ** Description: class destructor
42 **
43 ** Returns: none
44 **
45 *******************************************************************************/
~NfcHalThreadMutex()46 NfcHalThreadMutex::~NfcHalThreadMutex() { pthread_mutex_destroy(&mMutex); }
47
48 /*******************************************************************************
49 **
50 ** Function: NfcHalThreadMutex::lock()
51 **
52 ** Description: lock the mutex
53 **
54 ** Returns: none
55 **
56 *******************************************************************************/
lock()57 void NfcHalThreadMutex::lock() { pthread_mutex_lock(&mMutex); }
58
59 /*******************************************************************************
60 **
61 ** Function: NfcHalThreadMutex::unblock()
62 **
63 ** Description: unlock the mutex
64 **
65 ** Returns: none
66 **
67 *******************************************************************************/
unlock()68 void NfcHalThreadMutex::unlock() { pthread_mutex_unlock(&mMutex); }
69
70 /*******************************************************************************
71 **
72 ** Function: NfcHalThreadCondVar::NfcHalThreadCondVar()
73 **
74 ** Description: class constructor
75 **
76 ** Returns: none
77 **
78 *******************************************************************************/
NfcHalThreadCondVar()79 NfcHalThreadCondVar::NfcHalThreadCondVar() {
80 pthread_condattr_t CondAttr;
81
82 pthread_condattr_init(&CondAttr);
83 pthread_condattr_setclock(&CondAttr, CLOCK_MONOTONIC);
84 pthread_cond_init(&mCondVar, &CondAttr);
85
86 pthread_condattr_destroy(&CondAttr);
87 }
88
89 /*******************************************************************************
90 **
91 ** Function: NfcHalThreadCondVar::~NfcHalThreadCondVar()
92 **
93 ** Description: class destructor
94 **
95 ** Returns: none
96 **
97 *******************************************************************************/
~NfcHalThreadCondVar()98 NfcHalThreadCondVar::~NfcHalThreadCondVar() { pthread_cond_destroy(&mCondVar); }
99
100 /*******************************************************************************
101 **
102 ** Function: NfcHalThreadCondVar::wait()
103 **
104 ** Description: wait on the mCondVar
105 **
106 ** Returns: none
107 **
108 *******************************************************************************/
wait()109 void NfcHalThreadCondVar::wait() {
110 pthread_cond_wait(&mCondVar, *this);
111 pthread_mutex_unlock(*this);
112 }
113
114 /*******************************************************************************
115 **
116 ** Function: NfcHalThreadCondVar::timedWait()
117 **
118 ** Description: wait on the mCondVar or till timeout happens
119 **
120 ** Returns: none
121 **
122 *******************************************************************************/
timedWait(struct timespec * time)123 void NfcHalThreadCondVar::timedWait(struct timespec* time) {
124 pthread_cond_timedwait(&mCondVar, *this, time);
125 }
126
127 /*******************************************************************************
128 **
129 ** Function: NfcHalThreadCondVar::signal()
130 **
131 ** Description: signal the mCondVar
132 **
133 ** Returns: none
134 **
135 *******************************************************************************/
signal()136 void NfcHalThreadCondVar::signal() {
137 NfcHalAutoThreadMutex a(*this);
138 pthread_cond_signal(&mCondVar);
139 }
140
141 /*******************************************************************************
142 **
143 ** Function: NfcHalAutoThreadMutex::NfcHalAutoThreadMutex()
144 **
145 ** Description: class constructor, automatically lock the mutex
146 **
147 ** Returns: none
148 **
149 *******************************************************************************/
NfcHalAutoThreadMutex(NfcHalThreadMutex & m)150 NfcHalAutoThreadMutex::NfcHalAutoThreadMutex(NfcHalThreadMutex& m) : mm(m) {
151 mm.lock();
152 }
153
154 /*******************************************************************************
155 **
156 ** Function: NfcHalAutoThreadMutex::~NfcHalAutoThreadMutex()
157 **
158 ** Description: class destructor, automatically unlock the mutex
159 **
160 ** Returns: none
161 **
162 *******************************************************************************/
~NfcHalAutoThreadMutex()163 NfcHalAutoThreadMutex::~NfcHalAutoThreadMutex() { mm.unlock(); }
164