blob: d353873fa6e948d27bdabacd882c72a3b8dd100e [file] [log] [blame]
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -08001/*
2 * Copyright (C) 2016 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#define LOG_TAG "FMQ_EventFlags"
18
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -080019#include <linux/futex.h>
Dan Albert06c7a6f2017-10-11 12:44:12 -070020#include <string.h>
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -080021#include <sys/mman.h>
22#include <sys/syscall.h>
Steven Moreland26531e02017-04-13 18:20:19 -070023#include <unistd.h>
Dan Albert06c7a6f2017-10-11 12:44:12 -070024
Devin Mooreef38ed62021-04-07 10:10:36 -070025#include <limits>
Dan Albert06c7a6f2017-10-11 12:44:12 -070026#include <new>
27
28#include <fmq/EventFlag.h>
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -080029#include <utils/Log.h>
Hridya Valsarajuf542b5a2017-03-21 11:33:33 -070030#include <utils/SystemClock.h>
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -080031
32namespace android {
33namespace hardware {
34
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -080035status_t EventFlag::createEventFlag(std::atomic<uint32_t>* fwAddr,
36 EventFlag** flag) {
37 if (flag == nullptr) {
38 return BAD_VALUE;
39 }
40
41 status_t status = NO_MEMORY;
42 *flag = nullptr;
43
44 EventFlag* evFlag = new (std::nothrow) EventFlag(fwAddr, &status);
45 if (evFlag != nullptr) {
46 if (status == NO_ERROR) {
47 *flag = evFlag;
48 } else {
49 delete evFlag;
50 }
51 }
52
53 return status;
54}
55
56/*
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -080057 * Use this constructor if we already know where the futex word for
58 * the EventFlag group lives.
59 */
60EventFlag::EventFlag(std::atomic<uint32_t>* fwAddr, status_t* status) {
61 *status = NO_ERROR;
62 if (fwAddr == nullptr) {
63 *status = BAD_VALUE;
64 } else {
65 mEfWordPtr = fwAddr;
66 }
67}
68
69/*
70 * Set the specified bits of the futex word here and wake up any
71 * thread waiting on any of the bits.
72 */
73status_t EventFlag::wake(uint32_t bitmask) {
74 /*
75 * Return early if there are no set bits in bitmask.
76 */
77 if (bitmask == 0) {
78 return NO_ERROR;
79 }
80
81 status_t status = NO_ERROR;
82 uint32_t old = std::atomic_fetch_or(mEfWordPtr, bitmask);
83 /*
Hridya Valsarajuaf605d42017-01-19 12:12:29 -080084 * No need to call FUTEX_WAKE_BITSET if there were deferred wakes
85 * already available for all set bits from bitmask.
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -080086 */
Devin Mooreef38ed62021-04-07 10:10:36 -070087 constexpr size_t kIntMax = std::numeric_limits<int>::max();
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -080088 if ((~old & bitmask) != 0) {
Devin Mooreef38ed62021-04-07 10:10:36 -070089 int ret = syscall(__NR_futex, mEfWordPtr, FUTEX_WAKE_BITSET, kIntMax, NULL, NULL, bitmask);
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -080090 if (ret == -1) {
91 status = -errno;
92 ALOGE("Error in event flag wake attempt: %s\n", strerror(errno));
93 }
94 }
95 return status;
96}
97
98/*
99 * Wait for any of the bits in the bitmask to be set
100 * and return which bits caused the return.
101 */
Hridya Valsarajuf542b5a2017-03-21 11:33:33 -0700102status_t EventFlag::waitHelper(uint32_t bitmask, uint32_t* efState, int64_t timeoutNanoSeconds) {
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -0800103 /*
104 * Return early if there are no set bits in bitmask.
105 */
106 if (bitmask == 0 || efState == nullptr) {
107 return BAD_VALUE;
108 }
109
110 status_t status = NO_ERROR;
111 uint32_t old = std::atomic_fetch_and(mEfWordPtr, ~bitmask);
112 uint32_t setBits = old & bitmask;
113 /*
114 * If there was a deferred wake available, no need to call FUTEX_WAIT_BITSET.
115 */
116 if (setBits != 0) {
117 *efState = setBits;
118 return status;
119 }
120
121 uint32_t efWord = old & ~bitmask;
122 /*
123 * The syscall will put the thread to sleep only
124 * if the futex word still contains the expected
125 * value i.e. efWord. If the futex word contents have
Andreas Huber3c2dcb12017-01-11 12:40:22 -0800126 * changed, it fails with the error EAGAIN; If a timeout
127 * is specified and exceeded the syscall fails with ETIMEDOUT.
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -0800128 */
Hridya Valsaraju10f59dc2016-12-20 12:50:44 -0800129 int ret = 0;
130 if (timeoutNanoSeconds) {
131 struct timespec waitTimeAbsolute;
132 addNanosecondsToCurrentTime(timeoutNanoSeconds, &waitTimeAbsolute);
133
134 ret = syscall(__NR_futex, mEfWordPtr, FUTEX_WAIT_BITSET,
135 efWord, &waitTimeAbsolute, NULL, bitmask);
136 } else {
137 ret = syscall(__NR_futex, mEfWordPtr, FUTEX_WAIT_BITSET, efWord, NULL, NULL, bitmask);
138 }
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -0800139 if (ret == -1) {
140 status = -errno;
Andreas Huber3c2dcb12017-01-11 12:40:22 -0800141 if (status != -EAGAIN && status != -ETIMEDOUT) {
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -0800142 ALOGE("Event flag wait was unsuccessful: %s\n", strerror(errno));
143 }
144 *efState = 0;
145 } else {
Hridya Valsaraju92b79dc2016-12-19 14:57:44 -0800146 old = std::atomic_fetch_and(mEfWordPtr, ~bitmask);
147 *efState = old & bitmask;
Hridya Valsaraju77c8aba2017-03-03 07:57:37 -0800148
149 if (*efState == 0) {
150 /* Return -EINTR for a spurious wakeup */
151 status = -EINTR;
152 }
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -0800153 }
154 return status;
155}
156
Hridya Valsarajuf542b5a2017-03-21 11:33:33 -0700157/*
158 * Wait for any of the bits in the bitmask to be set
159 * and return which bits caused the return. If 'retry'
160 * is true, wait again on a spurious wake-up.
161 */
162status_t EventFlag::wait(uint32_t bitmask,
163 uint32_t* efState,
164 int64_t timeoutNanoSeconds,
165 bool retry) {
166 if (!retry) {
167 return waitHelper(bitmask, efState, timeoutNanoSeconds);
168 }
169
170 bool shouldTimeOut = timeoutNanoSeconds != 0;
171 int64_t prevTimeNs = shouldTimeOut ? android::elapsedRealtimeNano() : 0;
172 status_t status;
173 while (true) {
174 if (shouldTimeOut) {
175 int64_t currentTimeNs = android::elapsedRealtimeNano();
176 /*
177 * Decrement TimeOutNanos to account for the time taken to complete the last
178 * iteration of the while loop.
179 */
180 timeoutNanoSeconds -= currentTimeNs - prevTimeNs;
181 prevTimeNs = currentTimeNs;
182 if (timeoutNanoSeconds <= 0) {
183 status = -ETIMEDOUT;
184 *efState = 0;
185 break;
186 }
187 }
188
189 status = waitHelper(bitmask, efState, timeoutNanoSeconds);
190 if ((status != -EAGAIN) && (status != -EINTR)) {
191 break;
192 }
193 }
194 return status;
195}
196
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -0800197status_t EventFlag::unmapEventFlagWord(std::atomic<uint32_t>* efWordPtr,
198 bool* efWordNeedsUnmapping) {
199 status_t status = NO_ERROR;
200 if (*efWordNeedsUnmapping) {
201 int ret = munmap(efWordPtr, sizeof(std::atomic<uint32_t>));
202 if (ret != 0) {
203 status = -errno;
204 ALOGE("Error in deleting event flag group: %s\n", strerror(errno));
205 }
206 *efWordNeedsUnmapping = false;
207 }
208 return status;
209}
210
211status_t EventFlag::deleteEventFlag(EventFlag** evFlag) {
212 if (evFlag == nullptr || *evFlag == nullptr) {
213 return BAD_VALUE;
214 }
215
216 status_t status = unmapEventFlagWord((*evFlag)->mEfWordPtr,
217 &(*evFlag)->mEfWordNeedsUnmapping);
218 delete *evFlag;
219 *evFlag = nullptr;
220
221 return status;
222}
223
Hridya Valsaraju10f59dc2016-12-20 12:50:44 -0800224void EventFlag::addNanosecondsToCurrentTime(int64_t nanoSeconds, struct timespec* waitTime) {
225 static constexpr int64_t kNanosPerSecond = 1000000000;
226
227 clock_gettime(CLOCK_MONOTONIC, waitTime);
228 waitTime->tv_sec += nanoSeconds / kNanosPerSecond;
229 waitTime->tv_nsec += nanoSeconds % kNanosPerSecond;
230
231 if (waitTime->tv_nsec >= kNanosPerSecond) {
232 waitTime->tv_sec++;
233 waitTime->tv_nsec -= kNanosPerSecond;
234 }
235}
236
Hridya Valsaraju8b0d5a52016-12-16 10:29:03 -0800237EventFlag::~EventFlag() {
238 unmapEventFlagWord(mEfWordPtr, &mEfWordNeedsUnmapping);
239}
240
241} // namespace hardware
242} // namespace android