1 /* 2 * 3 * Copyright (C) 2021 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()29NfcHalThreadMutex::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()46NfcHalThreadMutex::~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()57void 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()68void 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()79NfcHalThreadCondVar::NfcHalThreadCondVar() { 80 pthread_condattr_t CondAttr; 81 82 pthread_condattr_init(&CondAttr); 83 pthread_cond_init(&mCondVar, &CondAttr); 84 85 pthread_condattr_destroy(&CondAttr); 86 } 87 88 /******************************************************************************* 89 ** 90 ** Function: NfcHalThreadCondVar::~NfcHalThreadCondVar() 91 ** 92 ** Description: class destructor 93 ** 94 ** Returns: none 95 ** 96 *******************************************************************************/ ~NfcHalThreadCondVar()97NfcHalThreadCondVar::~NfcHalThreadCondVar() { pthread_cond_destroy(&mCondVar); } 98 99 /******************************************************************************* 100 ** 101 ** Function: NfcHalThreadCondVar::wait() 102 ** 103 ** Description: wait on the mCondVar 104 ** 105 ** Returns: none 106 ** 107 *******************************************************************************/ wait()108void NfcHalThreadCondVar::wait() { 109 pthread_cond_wait(&mCondVar, *this); 110 pthread_mutex_unlock(*this); 111 } 112 113 /******************************************************************************* 114 ** 115 ** Function: NfcHalThreadCondVar::signal() 116 ** 117 ** Description: signal the mCondVar 118 ** 119 ** Returns: none 120 ** 121 *******************************************************************************/ signal()122void 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)136NfcHalAutoThreadMutex::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()149NfcHalAutoThreadMutex::~NfcHalAutoThreadMutex() { mm.unlock(); } 150