1 /*
2 *
3 * Copyright 2021-2023 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 kthe 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::timedWait()
103 **
104 ** Description: wait on the mCondVar or till timeout happens
105 **
106 ** Returns: none
107 **
108 *******************************************************************************/
timedWait(struct timespec * time)109 void NfcHalThreadCondVar::timedWait(struct timespec* time) {
110 pthread_cond_timedwait(&mCondVar, *this, time);
111 }
112
113 /*******************************************************************************
114 **
115 ** Function: NfcHalThreadCondVar::signal()
116 **
117 ** Description: signal the mCondVar
118 **
119 ** Returns: none
120 **
121 *******************************************************************************/
signal()122 void NfcHalThreadCondVar::signal() {
123 NfcHalAutoThreadMutex a(*this);
124 pthread_cond_signal(&mCondVar);
125 }
126
127 /*******************************************************************************
128 **
129 ** Function: NfcHalAutoThreadMutex::NfcHalAutoThreadMutex()
130 **
131 ** Description: class constructor, automatically lock the mutex
132 **
133 ** Returns: none
134 **
135 *******************************************************************************/
NfcHalAutoThreadMutex(NfcHalThreadMutex & m)136 NfcHalAutoThreadMutex::NfcHalAutoThreadMutex(NfcHalThreadMutex& m) : mm(m) {
137 mm.lock();
138 }
139
140 /*******************************************************************************
141 **
142 ** Function: NfcHalAutoThreadMutex::~NfcHalAutoThreadMutex()
143 **
144 ** Description: class destructor, automatically unlock the mutex
145 **
146 ** Returns: none
147 **
148 *******************************************************************************/
~NfcHalAutoThreadMutex()149 NfcHalAutoThreadMutex::~NfcHalAutoThreadMutex() { mm.unlock(); }
150