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 *
19 * The original Work has been changed by NXP.
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License");
22 * you may not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS,
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
32 *
33 * Copyright 2024 NXP
34 *
35 ******************************************************************************/
36
37 /*
38 * Encapsulate a mutex for thread synchronization.
39 */
40
41 #include "Mutex.h"
42
43 #include <android-base/logging.h>
44 #include <android-base/stringprintf.h>
45 #include <errno.h>
46 #include <string.h>
47
48 using android::base::StringPrintf;
49
50 /*******************************************************************************
51 **
52 ** Function: Mutex
53 **
54 ** Description: Initialize member variables.
55 **
56 ** Returns: None.
57 **
58 *******************************************************************************/
Mutex()59 Mutex::Mutex() {
60 memset(&mMutex, 0, sizeof(mMutex));
61 int res = pthread_mutex_init(&mMutex, NULL);
62 if (res != 0) {
63 LOG(ERROR) << StringPrintf("Mutex::Mutex: fail init; error=0x%X", res);
64 }
65 }
66
67 /*******************************************************************************
68 **
69 ** Function: ~Mutex
70 **
71 ** Description: Cleanup all resources.
72 **
73 ** Returns: None.
74 **
75 *******************************************************************************/
~Mutex()76 Mutex::~Mutex() {
77 int res = pthread_mutex_destroy(&mMutex);
78 if (res != 0) {
79 LOG(ERROR) << StringPrintf("Mutex::~Mutex: fail destroy; error=0x%X", res);
80 }
81 }
82
83 /*******************************************************************************
84 **
85 ** Function: lock
86 **
87 ** Description: Block the thread and try lock the mutex.
88 **
89 ** Returns: None.
90 **
91 *******************************************************************************/
lock()92 void Mutex::lock() {
93 int res = pthread_mutex_lock(&mMutex);
94 if (res != 0) {
95 LOG(ERROR) << StringPrintf("Mutex::lock: fail lock; error=0x%X", res);
96 }
97 }
98
99 /*******************************************************************************
100 **
101 ** Function: unlock
102 **
103 ** Description: Unlock a mutex to unblock a thread.
104 **
105 ** Returns: None.
106 **
107 *******************************************************************************/
unlock()108 void Mutex::unlock() {
109 int res = pthread_mutex_unlock(&mMutex);
110 if (res != 0) {
111 LOG(ERROR) << StringPrintf("Mutex::unlock: fail unlock; error=0x%X", res);
112 }
113 }
114
115 /*******************************************************************************
116 **
117 ** Function: tryLock
118 **
119 ** Description: Try to lock the mutex.
120 **
121 ** Returns: True if the mutex is locked.
122 **
123 *******************************************************************************/
tryLock()124 bool Mutex::tryLock() {
125 int res = pthread_mutex_trylock(&mMutex);
126 if ((res != 0) && (res != EBUSY)) {
127 LOG(ERROR) << StringPrintf("Mutex::tryLock: error=0x%X", res);
128 }
129 return res == 0;
130 }
131
132 /*******************************************************************************
133 **
134 ** Function: nativeHandle
135 **
136 ** Description: Get the handle of the mutex.
137 **
138 ** Returns: Handle of the mutex.
139 **
140 *******************************************************************************/
nativeHandle()141 pthread_mutex_t* Mutex::nativeHandle() { return &mMutex; }
142