blob: 10b38afa2d1462f6cc868f4b6535ad5e164e0f0a [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
24LocklessCommandFifo::LocklessCommandFifo()
25{
26}
27
28LocklessCommandFifo::~LocklessCommandFifo()
29{
Jason Sams8c0ee652009-08-25 14:49:07 -070030 if (!mInShutdown) {
31 shutdown();
32 }
33 free(mBuffer);
34}
35
36void LocklessCommandFifo::shutdown()
37{
38 mInShutdown = true;
39 mSignalToWorker.set();
Jason Sams326e0dd2009-05-22 14:03:28 -070040}
41
42bool LocklessCommandFifo::init(uint32_t sizeInBytes)
43{
44 // Add room for a buffer reset command
45 mBuffer = static_cast<uint8_t *>(malloc(sizeInBytes + 4));
46 if (!mBuffer) {
47 LOGE("LocklessFifo allocation failure");
48 return false;
49 }
50
Jason Sams732f1c02009-06-18 16:58:42 -070051 if (!mSignalToControl.init() || !mSignalToWorker.init()) {
52 LOGE("Signal setup failed");
Jason Sams326e0dd2009-05-22 14:03:28 -070053 free(mBuffer);
54 return false;
55 }
56
Jason Sams8c0ee652009-08-25 14:49:07 -070057 mInShutdown = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070058 mSize = sizeInBytes;
59 mPut = mBuffer;
60 mGet = mBuffer;
61 mEnd = mBuffer + (sizeInBytes) - 1;
Jason Sams613cad12009-11-12 15:10:25 -080062 //dumpState("init");
Jason Sams326e0dd2009-05-22 14:03:28 -070063 return true;
64}
65
Jason Sams8c0ee652009-08-25 14:49:07 -070066uint32_t LocklessCommandFifo::getFreeSpace() const
Jason Sams326e0dd2009-05-22 14:03:28 -070067{
68 int32_t freeSpace = 0;
69 //dumpState("getFreeSpace");
70
71 if (mPut >= mGet) {
72 freeSpace = mEnd - mPut;
73 } else {
74 freeSpace = mGet - mPut;
75 }
76
77 if (freeSpace < 0) {
78 freeSpace = 0;
79 }
Jason Sams326e0dd2009-05-22 14:03:28 -070080 return freeSpace;
81}
82
83bool LocklessCommandFifo::isEmpty() const
84{
85 return mPut == mGet;
86}
87
88
89void * LocklessCommandFifo::reserve(uint32_t sizeInBytes)
90{
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
102void LocklessCommandFifo::commit(uint32_t command, uint32_t sizeInBytes)
103{
Jason Sams8c401ef2009-10-06 13:58:47 -0700104 if (mInShutdown) {
105 return;
106 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700107 //dumpState("commit 1");
108 reinterpret_cast<uint16_t *>(mPut)[0] = command;
109 reinterpret_cast<uint16_t *>(mPut)[1] = sizeInBytes;
110 mPut += ((sizeInBytes + 3) & ~3) + 4;
111 //dumpState("commit 2");
Jason Sams732f1c02009-06-18 16:58:42 -0700112 mSignalToWorker.set();
Jason Sams326e0dd2009-05-22 14:03:28 -0700113}
114
115void LocklessCommandFifo::commitSync(uint32_t command, uint32_t sizeInBytes)
116{
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
128void LocklessCommandFifo::flush()
129{
130 //dumpState("flush 1");
131 while(mPut != mGet) {
Jason Sams732f1c02009-06-18 16:58:42 -0700132 mSignalToControl.wait();
Jason Sams326e0dd2009-05-22 14:03:28 -0700133 }
134 //dumpState("flush 2");
135}
136
Jason Sams12b14ae2010-03-18 11:39:44 -0700137void LocklessCommandFifo::wait()
138{
139 while(isEmpty() && !mInShutdown) {
140 mSignalToControl.set();
141 mSignalToWorker.wait();
142 }
143}
144
Jason Sams326e0dd2009-05-22 14:03:28 -0700145const void * LocklessCommandFifo::get(uint32_t *command, uint32_t *bytesData)
146{
147 while(1) {
Jason Sams732f1c02009-06-18 16:58:42 -0700148 //dumpState("get");
Jason Sams12b14ae2010-03-18 11:39:44 -0700149 wait();
Jason Samse514b452009-09-25 14:51:22 -0700150 if (mInShutdown) {
151 *command = 0;
152 *bytesData = 0;
153 return 0;
154 }
155
Jason Sams326e0dd2009-05-22 14:03:28 -0700156 *command = reinterpret_cast<const uint16_t *>(mGet)[0];
157 *bytesData = reinterpret_cast<const uint16_t *>(mGet)[1];
Jason Sams326e0dd2009-05-22 14:03:28 -0700158 if (*command) {
159 // non-zero command is valid
160 return mGet+4;
161 }
Jason Sams8c0ee652009-08-25 14:49:07 -0700162
Jason Sams326e0dd2009-05-22 14:03:28 -0700163 // zero command means reset to beginning.
164 mGet = mBuffer;
165 }
166}
167
168void LocklessCommandFifo::next()
169{
170 uint32_t bytes = reinterpret_cast<const uint16_t *>(mGet)[1];
171 mGet += ((bytes + 3) & ~3) + 4;
Jason Sams732f1c02009-06-18 16:58:42 -0700172 if (isEmpty()) {
173 mSignalToControl.set();
174 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700175 //dumpState("next");
176}
177
Jason Sams8c46b102010-08-18 12:38:03 -0700178bool LocklessCommandFifo::makeSpaceNonBlocking(uint32_t bytes)
179{
180 //dumpState("make space non-blocking");
181 if ((mPut+bytes) > mEnd) {
182 // Need to loop regardless of where get is.
183 if((mGet > mPut) && (mBuffer+4 >= mGet)) {
184 return false;
185 }
186
187 // Toss in a reset then the normal wait for space will do the rest.
188 reinterpret_cast<uint16_t *>(mPut)[0] = 0;
189 reinterpret_cast<uint16_t *>(mPut)[1] = 0;
190 mPut = mBuffer;
191 mSignalToWorker.set();
192 }
193
194 // it will fit here so we just need to wait for space.
195 if(getFreeSpace() < bytes) {
196 return false;
197 }
198
199 return true;
200}
201
Jason Sams326e0dd2009-05-22 14:03:28 -0700202void LocklessCommandFifo::makeSpace(uint32_t bytes)
203{
Jason Sams565ac362009-06-03 16:04:54 -0700204 //dumpState("make space");
Jason Sams326e0dd2009-05-22 14:03:28 -0700205 if ((mPut+bytes) > mEnd) {
206 // Need to loop regardless of where get is.
Jason Sams565ac362009-06-03 16:04:54 -0700207 while((mGet > mPut) && (mBuffer+4 >= mGet)) {
Jason Samsada7f272009-09-24 14:55:38 -0700208 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700209 }
210
211 // Toss in a reset then the normal wait for space will do the rest.
212 reinterpret_cast<uint16_t *>(mPut)[0] = 0;
213 reinterpret_cast<uint16_t *>(mPut)[1] = 0;
Jason Sams565ac362009-06-03 16:04:54 -0700214 mPut = mBuffer;
Jason Sams8c46b102010-08-18 12:38:03 -0700215 mSignalToWorker.set();
Jason Sams326e0dd2009-05-22 14:03:28 -0700216 }
217
218 // it will fit here so we just need to wait for space.
219 while(getFreeSpace() < bytes) {
Jason Samsada7f272009-09-24 14:55:38 -0700220 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700221 }
Jason Sams8c0ee652009-08-25 14:49:07 -0700222
Jason Sams326e0dd2009-05-22 14:03:28 -0700223}
224
225void LocklessCommandFifo::dumpState(const char *s) const
226{
Jason Sams8c46b102010-08-18 12:38:03 -0700227 LOGV("%s %p put %p, get %p, buf %p, end %p", s, this, mPut, mGet, mBuffer, mEnd);
Jason Sams326e0dd2009-05-22 14:03:28 -0700228}
229