blob: 0466d8bbf04ecd6f378594951c1d2775542e7f02 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 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#include "rsLocklessFifo.h"
Jason Sams2353ae32010-10-14 17:48:46 -070018#include "utils/Timers.h"
19#include "utils/StopWatch.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070020
21using namespace android;
Jason Sams12b14ae2010-03-18 11:39:44 -070022using namespace android::renderscript;
Jason Sams326e0dd2009-05-22 14:03:28 -070023
Alex Sakhartchouk98cc3552011-08-19 09:43:18 -070024LocklessCommandFifo::LocklessCommandFifo() : mBuffer(0), mInitialized(false) {
Jason Sams2382aba2011-09-13 15:41:01 -070025 mTimeoutCallback = NULL;
26 mTimeoutCallbackData = NULL;
27 mTimeoutWait = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070028}
29
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080030LocklessCommandFifo::~LocklessCommandFifo() {
Alex Sakhartchouk98cc3552011-08-19 09:43:18 -070031 if (!mInShutdown && mInitialized) {
Jason Sams8c0ee652009-08-25 14:49:07 -070032 shutdown();
33 }
Alex Sakhartchouk6b0c0042011-07-13 17:32:05 -070034 if (mBuffer) {
35 free(mBuffer);
36 }
Jason Sams8c0ee652009-08-25 14:49:07 -070037}
38
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080039void LocklessCommandFifo::shutdown() {
Jason Sams8c0ee652009-08-25 14:49:07 -070040 mInShutdown = true;
41 mSignalToWorker.set();
Jason Sams326e0dd2009-05-22 14:03:28 -070042}
43
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080044bool LocklessCommandFifo::init(uint32_t sizeInBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -070045 // Add room for a buffer reset command
46 mBuffer = static_cast<uint8_t *>(malloc(sizeInBytes + 4));
47 if (!mBuffer) {
Steve Blockaf12ac62012-01-06 19:20:56 +000048 ALOGE("LocklessFifo allocation failure");
Jason Sams326e0dd2009-05-22 14:03:28 -070049 return false;
50 }
51
Jason Sams732f1c02009-06-18 16:58:42 -070052 if (!mSignalToControl.init() || !mSignalToWorker.init()) {
Steve Blockaf12ac62012-01-06 19:20:56 +000053 ALOGE("Signal setup failed");
Jason Sams326e0dd2009-05-22 14:03:28 -070054 free(mBuffer);
55 return false;
56 }
57
Jason Sams8c0ee652009-08-25 14:49:07 -070058 mInShutdown = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070059 mSize = sizeInBytes;
60 mPut = mBuffer;
61 mGet = mBuffer;
62 mEnd = mBuffer + (sizeInBytes) - 1;
Jason Sams613cad12009-11-12 15:10:25 -080063 //dumpState("init");
Alex Sakhartchouk98cc3552011-08-19 09:43:18 -070064 mInitialized = true;
Jason Sams326e0dd2009-05-22 14:03:28 -070065 return true;
66}
67
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080068uint32_t LocklessCommandFifo::getFreeSpace() const {
Jason Sams326e0dd2009-05-22 14:03:28 -070069 int32_t freeSpace = 0;
70 //dumpState("getFreeSpace");
71
72 if (mPut >= mGet) {
73 freeSpace = mEnd - mPut;
74 } else {
75 freeSpace = mGet - mPut;
76 }
77
78 if (freeSpace < 0) {
79 freeSpace = 0;
80 }
Jason Sams326e0dd2009-05-22 14:03:28 -070081 return freeSpace;
82}
83
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080084bool LocklessCommandFifo::isEmpty() const {
Jason Samseef823f2011-01-24 17:33:21 -080085 uint32_t p = android_atomic_acquire_load((int32_t *)&mPut);
86 return ((uint8_t *)p) == mGet;
Jason Sams326e0dd2009-05-22 14:03:28 -070087}
88
89
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080090void * LocklessCommandFifo::reserve(uint32_t sizeInBytes) {
Jason Sams565ac362009-06-03 16:04:54 -070091 // Add space for command header and loop token;
92 sizeInBytes += 8;
Jason Sams326e0dd2009-05-22 14:03:28 -070093
94 //dumpState("reserve");
95 if (getFreeSpace() < sizeInBytes) {
96 makeSpace(sizeInBytes);
97 }
98
99 return mPut + 4;
100}
101
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800102void LocklessCommandFifo::commit(uint32_t command, uint32_t sizeInBytes) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700103 if (mInShutdown) {
104 return;
105 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700106 //dumpState("commit 1");
107 reinterpret_cast<uint16_t *>(mPut)[0] = command;
108 reinterpret_cast<uint16_t *>(mPut)[1] = sizeInBytes;
Jason Sams9bc9a3c2011-01-27 18:07:06 -0800109
110 int32_t s = ((sizeInBytes + 3) & ~3) + 4;
111 android_atomic_add(s, (int32_t *)&mPut);
Jason Sams326e0dd2009-05-22 14:03:28 -0700112 //dumpState("commit 2");
Jason Sams732f1c02009-06-18 16:58:42 -0700113 mSignalToWorker.set();
Jason Sams326e0dd2009-05-22 14:03:28 -0700114}
115
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800116void LocklessCommandFifo::commitSync(uint32_t command, uint32_t sizeInBytes) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700117 if (mInShutdown) {
118 return;
119 }
Jason Sams2353ae32010-10-14 17:48:46 -0700120
121 //char buf[1024];
122 //sprintf(buf, "RenderScript LocklessCommandFifo::commitSync %p %i %i", this, command, sizeInBytes);
123 //StopWatch compileTimer(buf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700124 commit(command, sizeInBytes);
125 flush();
126}
127
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128void LocklessCommandFifo::flush() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700129 //dumpState("flush 1");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800130 while (mPut != mGet) {
Jason Sams2382aba2011-09-13 15:41:01 -0700131 while (!mSignalToControl.wait(mTimeoutWait)) {
132 if (mTimeoutCallback) {
133 mTimeoutCallback(mTimeoutCallbackData);
134 }
135 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700136 }
137 //dumpState("flush 2");
138}
139
Jason Sams2382aba2011-09-13 15:41:01 -0700140void LocklessCommandFifo::setTimoutCallback(void (*cbk)(void *), void *data, uint64_t timeout) {
141 mTimeoutCallback = cbk;
142 mTimeoutCallbackData = data;
143 mTimeoutWait = timeout;
144}
145
Jason Samse0aab4a2011-08-12 15:05:15 -0700146bool LocklessCommandFifo::wait(uint64_t timeout) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800147 while (isEmpty() && !mInShutdown) {
Jason Sams12b14ae2010-03-18 11:39:44 -0700148 mSignalToControl.set();
Jason Samse0aab4a2011-08-12 15:05:15 -0700149 return mSignalToWorker.wait(timeout);
Jason Sams12b14ae2010-03-18 11:39:44 -0700150 }
Jason Samse0aab4a2011-08-12 15:05:15 -0700151 return true;
Jason Sams12b14ae2010-03-18 11:39:44 -0700152}
153
Jason Samse0aab4a2011-08-12 15:05:15 -0700154const void * LocklessCommandFifo::get(uint32_t *command, uint32_t *bytesData, uint64_t timeout) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800155 while (1) {
Jason Sams732f1c02009-06-18 16:58:42 -0700156 //dumpState("get");
Jason Samse0aab4a2011-08-12 15:05:15 -0700157 wait(timeout);
158
159 if (isEmpty() || mInShutdown) {
Jason Samse514b452009-09-25 14:51:22 -0700160 *command = 0;
161 *bytesData = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700162 return NULL;
Jason Samse514b452009-09-25 14:51:22 -0700163 }
164
Jason Sams326e0dd2009-05-22 14:03:28 -0700165 *command = reinterpret_cast<const uint16_t *>(mGet)[0];
166 *bytesData = reinterpret_cast<const uint16_t *>(mGet)[1];
Jason Sams326e0dd2009-05-22 14:03:28 -0700167 if (*command) {
168 // non-zero command is valid
169 return mGet+4;
170 }
Jason Sams8c0ee652009-08-25 14:49:07 -0700171
Jason Sams326e0dd2009-05-22 14:03:28 -0700172 // zero command means reset to beginning.
173 mGet = mBuffer;
174 }
175}
176
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800177void LocklessCommandFifo::next() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700178 uint32_t bytes = reinterpret_cast<const uint16_t *>(mGet)[1];
Jason Samseef823f2011-01-24 17:33:21 -0800179
180 android_atomic_add(((bytes + 3) & ~3) + 4, (int32_t *)&mGet);
181 //mGet += ((bytes + 3) & ~3) + 4;
Jason Sams732f1c02009-06-18 16:58:42 -0700182 if (isEmpty()) {
183 mSignalToControl.set();
184 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700185 //dumpState("next");
186}
187
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800188bool LocklessCommandFifo::makeSpaceNonBlocking(uint32_t bytes) {
Jason Sams8c46b102010-08-18 12:38:03 -0700189 //dumpState("make space non-blocking");
190 if ((mPut+bytes) > mEnd) {
191 // Need to loop regardless of where get is.
Bryan Mawhinneyb17f2012010-12-05 17:21:07 +0000192 if ((mGet > mPut) || (mBuffer+4 >= mGet)) {
Jason Sams8c46b102010-08-18 12:38:03 -0700193 return false;
194 }
195
196 // Toss in a reset then the normal wait for space will do the rest.
197 reinterpret_cast<uint16_t *>(mPut)[0] = 0;
198 reinterpret_cast<uint16_t *>(mPut)[1] = 0;
199 mPut = mBuffer;
200 mSignalToWorker.set();
201 }
202
203 // it will fit here so we just need to wait for space.
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800204 if (getFreeSpace() < bytes) {
Jason Sams8c46b102010-08-18 12:38:03 -0700205 return false;
206 }
207
208 return true;
209}
210
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800211void LocklessCommandFifo::makeSpace(uint32_t bytes) {
Jason Sams565ac362009-06-03 16:04:54 -0700212 //dumpState("make space");
Jason Sams326e0dd2009-05-22 14:03:28 -0700213 if ((mPut+bytes) > mEnd) {
214 // Need to loop regardless of where get is.
Bryan Mawhinneyb17f2012010-12-05 17:21:07 +0000215 while ((mGet > mPut) || (mBuffer+4 >= mGet)) {
Jason Samsada7f272009-09-24 14:55:38 -0700216 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700217 }
218
219 // Toss in a reset then the normal wait for space will do the rest.
220 reinterpret_cast<uint16_t *>(mPut)[0] = 0;
221 reinterpret_cast<uint16_t *>(mPut)[1] = 0;
Jason Sams565ac362009-06-03 16:04:54 -0700222 mPut = mBuffer;
Jason Sams8c46b102010-08-18 12:38:03 -0700223 mSignalToWorker.set();
Jason Sams326e0dd2009-05-22 14:03:28 -0700224 }
225
226 // it will fit here so we just need to wait for space.
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800227 while (getFreeSpace() < bytes) {
Jason Samsada7f272009-09-24 14:55:38 -0700228 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700229 }
Jason Sams8c0ee652009-08-25 14:49:07 -0700230
Jason Sams326e0dd2009-05-22 14:03:28 -0700231}
232
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800233void LocklessCommandFifo::dumpState(const char *s) const {
Steve Block65982012011-10-20 11:56:00 +0100234 ALOGV("%s %p put %p, get %p, buf %p, end %p", s, this, mPut, mGet, mBuffer, mEnd);
Jason Sams326e0dd2009-05-22 14:03:28 -0700235}
Jason Sams1dda6752011-01-18 18:12:26 -0800236
237void LocklessCommandFifo::printDebugData() const {
238 dumpState("printing fifo debug");
239 const uint32_t *pptr = (const uint32_t *)mGet;
240 pptr -= 8 * 4;
241 if (mGet < mBuffer) {
242 pptr = (const uint32_t *)mBuffer;
243 }
244
245
246 for (int ct=0; ct < 16; ct++) {
Steve Block65982012011-10-20 11:56:00 +0100247 ALOGV("fifo %p = 0x%08x 0x%08x 0x%08x 0x%08x", pptr, pptr[0], pptr[1], pptr[2], pptr[3]);
Jason Sams1dda6752011-01-18 18:12:26 -0800248 pptr += 4;
249 }
250
251}