blob: 67a6a8d11c7f3501872aeaf7c6962b51c951da1a [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
19#include <fmq/EventFlag.h>
20#include <linux/futex.h>
21#include <sys/mman.h>
22#include <sys/syscall.h>
23#include <utils/Log.h>
24#include <new>
25
26namespace android {
27namespace hardware {
28
29status_t EventFlag::createEventFlag(int fd, off_t offset, EventFlag** flag) {
30 if (flag == nullptr) {
31 return BAD_VALUE;
32 }
33
34 status_t status = NO_MEMORY;
35 *flag = nullptr;
36
37 EventFlag* evFlag = new (std::nothrow) EventFlag(fd, offset, &status);
38 if (evFlag != nullptr) {
39 if (status == NO_ERROR) {
40 *flag = evFlag;
41 } else {
42 delete evFlag;
43 }
44 }
45
46 return status;
47}
48
49status_t EventFlag::createEventFlag(std::atomic<uint32_t>* fwAddr,
50 EventFlag** flag) {
51 if (flag == nullptr) {
52 return BAD_VALUE;
53 }
54
55 status_t status = NO_MEMORY;
56 *flag = nullptr;
57
58 EventFlag* evFlag = new (std::nothrow) EventFlag(fwAddr, &status);
59 if (evFlag != nullptr) {
60 if (status == NO_ERROR) {
61 *flag = evFlag;
62 } else {
63 delete evFlag;
64 }
65 }
66
67 return status;
68}
69
70/*
71 * mmap memory for the futex word
72 */
73EventFlag::EventFlag(int fd, off_t offset, status_t* status) {
74 mEfWordPtr = static_cast<std::atomic<uint32_t>*>(mmap(NULL,
75 sizeof(std::atomic<uint32_t>),
76 PROT_READ | PROT_WRITE,
77 MAP_SHARED, fd, offset));
78 mEfWordNeedsUnmapping = true;
79 if (mEfWordPtr != MAP_FAILED) {
80 *status = NO_ERROR;
81 } else {
82 *status = -errno;
83 ALOGE("Attempt to mmap event flag word failed: %s\n", strerror(errno));
84 }
85}
86
87/*
88 * Use this constructor if we already know where the futex word for
89 * the EventFlag group lives.
90 */
91EventFlag::EventFlag(std::atomic<uint32_t>* fwAddr, status_t* status) {
92 *status = NO_ERROR;
93 if (fwAddr == nullptr) {
94 *status = BAD_VALUE;
95 } else {
96 mEfWordPtr = fwAddr;
97 }
98}
99
100/*
101 * Set the specified bits of the futex word here and wake up any
102 * thread waiting on any of the bits.
103 */
104status_t EventFlag::wake(uint32_t bitmask) {
105 /*
106 * Return early if there are no set bits in bitmask.
107 */
108 if (bitmask == 0) {
109 return NO_ERROR;
110 }
111
112 status_t status = NO_ERROR;
113 uint32_t old = std::atomic_fetch_or(mEfWordPtr, bitmask);
114 /*
115 * No need to call FUTEX_WAKE_BITSET if there is a deferred wake
116 * already available for any bit in the bitmask.
117 */
118 if ((~old & bitmask) != 0) {
119 int ret = syscall(__NR_futex, mEfWordPtr, FUTEX_WAKE_BITSET,
120 INT_MAX, NULL, NULL, bitmask);
121 if (ret == -1) {
122 status = -errno;
123 ALOGE("Error in event flag wake attempt: %s\n", strerror(errno));
124 }
125 }
126 return status;
127}
128
129/*
130 * Wait for any of the bits in the bitmask to be set
131 * and return which bits caused the return.
132 */
133status_t EventFlag::wait(uint32_t bitmask, uint32_t* efState, const struct timespec* timeout) {
134 /*
135 * Return early if there are no set bits in bitmask.
136 */
137 if (bitmask == 0 || efState == nullptr) {
138 return BAD_VALUE;
139 }
140
141 status_t status = NO_ERROR;
142 uint32_t old = std::atomic_fetch_and(mEfWordPtr, ~bitmask);
143 uint32_t setBits = old & bitmask;
144 /*
145 * If there was a deferred wake available, no need to call FUTEX_WAIT_BITSET.
146 */
147 if (setBits != 0) {
148 *efState = setBits;
149 return status;
150 }
151
152 uint32_t efWord = old & ~bitmask;
153 /*
154 * The syscall will put the thread to sleep only
155 * if the futex word still contains the expected
156 * value i.e. efWord. If the futex word contents have
157 * changed, it fails with the error EAGAIN.
158 */
159 int ret = syscall(__NR_futex, mEfWordPtr, FUTEX_WAIT_BITSET, efWord, timeout, NULL, bitmask);
160 if (ret == -1) {
161 status = -errno;
162 if (status != -EAGAIN) {
163 ALOGE("Event flag wait was unsuccessful: %s\n", strerror(errno));
164 }
165 *efState = 0;
166 } else {
167 *efState = efWord & bitmask;
168 }
169 return status;
170}
171
172status_t EventFlag::unmapEventFlagWord(std::atomic<uint32_t>* efWordPtr,
173 bool* efWordNeedsUnmapping) {
174 status_t status = NO_ERROR;
175 if (*efWordNeedsUnmapping) {
176 int ret = munmap(efWordPtr, sizeof(std::atomic<uint32_t>));
177 if (ret != 0) {
178 status = -errno;
179 ALOGE("Error in deleting event flag group: %s\n", strerror(errno));
180 }
181 *efWordNeedsUnmapping = false;
182 }
183 return status;
184}
185
186status_t EventFlag::deleteEventFlag(EventFlag** evFlag) {
187 if (evFlag == nullptr || *evFlag == nullptr) {
188 return BAD_VALUE;
189 }
190
191 status_t status = unmapEventFlagWord((*evFlag)->mEfWordPtr,
192 &(*evFlag)->mEfWordNeedsUnmapping);
193 delete *evFlag;
194 *evFlag = nullptr;
195
196 return status;
197}
198
199EventFlag::~EventFlag() {
200 unmapEventFlagWord(mEfWordPtr, &mEfWordNeedsUnmapping);
201}
202
203} // namespace hardware
204} // namespace android