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 condition variable for thread synchronization.
39 */
40
41 #include "CondVar.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: CondVar
53 **
54 ** Description: Initialize member variables.
55 **
56 ** Returns: None.
57 **
58 *******************************************************************************/
CondVar()59 CondVar::CondVar() {
60 pthread_condattr_t attr;
61 pthread_condattr_init(&attr);
62 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
63 memset(&mCondition, 0, sizeof(mCondition));
64 int const res = pthread_cond_init(&mCondition, &attr);
65 if (res) {
66 LOG(ERROR) << StringPrintf("CondVar::CondVar: fail init; error=0x%X", res);
67 }
68 }
69
70 /*******************************************************************************
71 **
72 ** Function: ~CondVar
73 **
74 ** Description: Cleanup all resources.
75 **
76 ** Returns: None.
77 **
78 *******************************************************************************/
~CondVar()79 CondVar::~CondVar() {
80 int const res = pthread_cond_destroy(&mCondition);
81 if (res) {
82 LOG(ERROR) << StringPrintf("CondVar::~CondVar: fail destroy; error=0x%X",
83 res);
84 }
85 }
86
87 /*******************************************************************************
88 **
89 ** Function: wait
90 **
91 ** Description: Block the caller and wait for a condition.
92 **
93 ** Returns: None.
94 **
95 *******************************************************************************/
wait(Mutex & mutex)96 void CondVar::wait(Mutex& mutex) {
97 int const res = pthread_cond_wait(&mCondition, mutex.nativeHandle());
98 if (res) {
99 LOG(ERROR) << StringPrintf("CondVar::wait: fail wait; error=0x%X", res);
100 }
101 }
102
103 /*******************************************************************************
104 **
105 ** Function: wait
106 **
107 ** Description: Block the caller and wait for a condition.
108 ** millisec: Timeout in milliseconds.
109 **
110 ** Returns: True if wait is successful; false if timeout occurs.
111 **
112 *******************************************************************************/
wait(Mutex & mutex,long millisec)113 bool CondVar::wait(Mutex& mutex, long millisec) {
114 bool retVal = false;
115 struct timespec absoluteTime;
116
117 if (clock_gettime(CLOCK_MONOTONIC, &absoluteTime) == -1) {
118 LOG(ERROR) << StringPrintf("CondVar::wait: fail get time; errno=0x%X",
119 errno);
120 } else {
121 absoluteTime.tv_sec += millisec / 1000;
122 long ns = absoluteTime.tv_nsec + ((millisec % 1000) * 1000000);
123 if (ns > 1000000000) {
124 absoluteTime.tv_sec++;
125 absoluteTime.tv_nsec = ns - 1000000000;
126 } else
127 absoluteTime.tv_nsec = ns;
128 }
129
130 int waitResult =
131 pthread_cond_timedwait(&mCondition, mutex.nativeHandle(), &absoluteTime);
132 if ((waitResult != 0) && (waitResult != ETIMEDOUT))
133 LOG(ERROR) << StringPrintf("CondVar::wait: fail timed wait; error=0x%X",
134 waitResult);
135 retVal = (waitResult == 0); // waited successfully
136 return retVal;
137 }
138
139 /*******************************************************************************
140 **
141 ** Function: notifyOne
142 **
143 ** Description: Unblock the waiting thread.
144 **
145 ** Returns: None.
146 **
147 *******************************************************************************/
notifyOne()148 void CondVar::notifyOne() {
149 int const res = pthread_cond_signal(&mCondition);
150 if (res) {
151 LOG(ERROR) << StringPrintf("CondVar::notifyOne: fail signal; error=0x%X",
152 res);
153 }
154 }
155