Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 18 | #include "utils/Timers.h" |
| 19 | #include "utils/StopWatch.h" |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 20 | |
| 21 | using namespace android; |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 22 | using namespace android::renderscript; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 23 | |
Alex Sakhartchouk | a8bb921 | 2011-08-19 09:43:18 -0700 | [diff] [blame] | 24 | LocklessCommandFifo::LocklessCommandFifo() : mBuffer(0), mInitialized(false) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 27 | LocklessCommandFifo::~LocklessCommandFifo() { |
Alex Sakhartchouk | a8bb921 | 2011-08-19 09:43:18 -0700 | [diff] [blame] | 28 | if (!mInShutdown && mInitialized) { |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 29 | shutdown(); |
| 30 | } |
Alex Sakhartchouk | 1bfccea | 2011-07-13 17:32:05 -0700 | [diff] [blame] | 31 | if (mBuffer) { |
| 32 | free(mBuffer); |
| 33 | } |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 36 | void LocklessCommandFifo::shutdown() { |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 37 | mInShutdown = true; |
| 38 | mSignalToWorker.set(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 41 | bool LocklessCommandFifo::init(uint32_t sizeInBytes) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 42 | // Add room for a buffer reset command |
| 43 | mBuffer = static_cast<uint8_t *>(malloc(sizeInBytes + 4)); |
| 44 | if (!mBuffer) { |
| 45 | LOGE("LocklessFifo allocation failure"); |
| 46 | return false; |
| 47 | } |
| 48 | |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 49 | if (!mSignalToControl.init() || !mSignalToWorker.init()) { |
| 50 | LOGE("Signal setup failed"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 51 | free(mBuffer); |
| 52 | return false; |
| 53 | } |
| 54 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 55 | mInShutdown = false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 56 | mSize = sizeInBytes; |
| 57 | mPut = mBuffer; |
| 58 | mGet = mBuffer; |
| 59 | mEnd = mBuffer + (sizeInBytes) - 1; |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 60 | //dumpState("init"); |
Alex Sakhartchouk | a8bb921 | 2011-08-19 09:43:18 -0700 | [diff] [blame] | 61 | mInitialized = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 65 | uint32_t LocklessCommandFifo::getFreeSpace() const { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 66 | int32_t freeSpace = 0; |
| 67 | //dumpState("getFreeSpace"); |
| 68 | |
| 69 | if (mPut >= mGet) { |
| 70 | freeSpace = mEnd - mPut; |
| 71 | } else { |
| 72 | freeSpace = mGet - mPut; |
| 73 | } |
| 74 | |
| 75 | if (freeSpace < 0) { |
| 76 | freeSpace = 0; |
| 77 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 78 | return freeSpace; |
| 79 | } |
| 80 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 81 | bool LocklessCommandFifo::isEmpty() const { |
Jason Sams | fcecc24 | 2011-01-24 17:33:21 -0800 | [diff] [blame] | 82 | uint32_t p = android_atomic_acquire_load((int32_t *)&mPut); |
| 83 | return ((uint8_t *)p) == mGet; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 87 | void * LocklessCommandFifo::reserve(uint32_t sizeInBytes) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 88 | // Add space for command header and loop token; |
| 89 | sizeInBytes += 8; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 90 | |
| 91 | //dumpState("reserve"); |
| 92 | if (getFreeSpace() < sizeInBytes) { |
| 93 | makeSpace(sizeInBytes); |
| 94 | } |
| 95 | |
| 96 | return mPut + 4; |
| 97 | } |
| 98 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 99 | void LocklessCommandFifo::commit(uint32_t command, uint32_t sizeInBytes) { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 100 | if (mInShutdown) { |
| 101 | return; |
| 102 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 103 | //dumpState("commit 1"); |
| 104 | reinterpret_cast<uint16_t *>(mPut)[0] = command; |
| 105 | reinterpret_cast<uint16_t *>(mPut)[1] = sizeInBytes; |
Jason Sams | 5012a8f | 2011-01-27 18:07:06 -0800 | [diff] [blame] | 106 | |
| 107 | int32_t s = ((sizeInBytes + 3) & ~3) + 4; |
| 108 | android_atomic_add(s, (int32_t *)&mPut); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 109 | //dumpState("commit 2"); |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 110 | mSignalToWorker.set(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 113 | void LocklessCommandFifo::commitSync(uint32_t command, uint32_t sizeInBytes) { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 114 | if (mInShutdown) { |
| 115 | return; |
| 116 | } |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 117 | |
| 118 | //char buf[1024]; |
| 119 | //sprintf(buf, "RenderScript LocklessCommandFifo::commitSync %p %i %i", this, command, sizeInBytes); |
| 120 | //StopWatch compileTimer(buf); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 121 | commit(command, sizeInBytes); |
| 122 | flush(); |
| 123 | } |
| 124 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 125 | void LocklessCommandFifo::flush() { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 126 | //dumpState("flush 1"); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 127 | while (mPut != mGet) { |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 128 | mSignalToControl.wait(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 129 | } |
| 130 | //dumpState("flush 2"); |
| 131 | } |
| 132 | |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 133 | bool LocklessCommandFifo::wait(uint64_t timeout) { |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 134 | while (isEmpty() && !mInShutdown) { |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 135 | mSignalToControl.set(); |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 136 | return mSignalToWorker.wait(timeout); |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 137 | } |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 138 | return true; |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 141 | const void * LocklessCommandFifo::get(uint32_t *command, uint32_t *bytesData, uint64_t timeout) { |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 142 | while (1) { |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 143 | //dumpState("get"); |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 144 | wait(timeout); |
| 145 | |
| 146 | if (isEmpty() || mInShutdown) { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 147 | *command = 0; |
| 148 | *bytesData = 0; |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 149 | return NULL; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 152 | *command = reinterpret_cast<const uint16_t *>(mGet)[0]; |
| 153 | *bytesData = reinterpret_cast<const uint16_t *>(mGet)[1]; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 154 | if (*command) { |
| 155 | // non-zero command is valid |
| 156 | return mGet+4; |
| 157 | } |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 158 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 159 | // zero command means reset to beginning. |
| 160 | mGet = mBuffer; |
| 161 | } |
| 162 | } |
| 163 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 164 | void LocklessCommandFifo::next() { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 165 | uint32_t bytes = reinterpret_cast<const uint16_t *>(mGet)[1]; |
Jason Sams | fcecc24 | 2011-01-24 17:33:21 -0800 | [diff] [blame] | 166 | |
| 167 | android_atomic_add(((bytes + 3) & ~3) + 4, (int32_t *)&mGet); |
| 168 | //mGet += ((bytes + 3) & ~3) + 4; |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 169 | if (isEmpty()) { |
| 170 | mSignalToControl.set(); |
| 171 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 172 | //dumpState("next"); |
| 173 | } |
| 174 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 175 | bool LocklessCommandFifo::makeSpaceNonBlocking(uint32_t bytes) { |
Jason Sams | d0cb106 | 2010-08-18 12:38:03 -0700 | [diff] [blame] | 176 | //dumpState("make space non-blocking"); |
| 177 | if ((mPut+bytes) > mEnd) { |
| 178 | // Need to loop regardless of where get is. |
Bryan Mawhinney | 8ea061b | 2010-12-05 17:21:07 +0000 | [diff] [blame] | 179 | if ((mGet > mPut) || (mBuffer+4 >= mGet)) { |
Jason Sams | d0cb106 | 2010-08-18 12:38:03 -0700 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | |
| 183 | // Toss in a reset then the normal wait for space will do the rest. |
| 184 | reinterpret_cast<uint16_t *>(mPut)[0] = 0; |
| 185 | reinterpret_cast<uint16_t *>(mPut)[1] = 0; |
| 186 | mPut = mBuffer; |
| 187 | mSignalToWorker.set(); |
| 188 | } |
| 189 | |
| 190 | // it will fit here so we just need to wait for space. |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 191 | if (getFreeSpace() < bytes) { |
Jason Sams | d0cb106 | 2010-08-18 12:38:03 -0700 | [diff] [blame] | 192 | return false; |
| 193 | } |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 198 | void LocklessCommandFifo::makeSpace(uint32_t bytes) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 199 | //dumpState("make space"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 200 | if ((mPut+bytes) > mEnd) { |
| 201 | // Need to loop regardless of where get is. |
Bryan Mawhinney | 8ea061b | 2010-12-05 17:21:07 +0000 | [diff] [blame] | 202 | while ((mGet > mPut) || (mBuffer+4 >= mGet)) { |
Jason Sams | e60446b | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 203 | usleep(100); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // Toss in a reset then the normal wait for space will do the rest. |
| 207 | reinterpret_cast<uint16_t *>(mPut)[0] = 0; |
| 208 | reinterpret_cast<uint16_t *>(mPut)[1] = 0; |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 209 | mPut = mBuffer; |
Jason Sams | d0cb106 | 2010-08-18 12:38:03 -0700 | [diff] [blame] | 210 | mSignalToWorker.set(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // it will fit here so we just need to wait for space. |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 214 | while (getFreeSpace() < bytes) { |
Jason Sams | e60446b | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 215 | usleep(100); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 216 | } |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 217 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 220 | void LocklessCommandFifo::dumpState(const char *s) const { |
Jason Sams | d0cb106 | 2010-08-18 12:38:03 -0700 | [diff] [blame] | 221 | LOGV("%s %p put %p, get %p, buf %p, end %p", s, this, mPut, mGet, mBuffer, mEnd); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 222 | } |
Jason Sams | d1ac981 | 2011-01-18 18:12:26 -0800 | [diff] [blame] | 223 | |
| 224 | void LocklessCommandFifo::printDebugData() const { |
| 225 | dumpState("printing fifo debug"); |
| 226 | const uint32_t *pptr = (const uint32_t *)mGet; |
| 227 | pptr -= 8 * 4; |
| 228 | if (mGet < mBuffer) { |
| 229 | pptr = (const uint32_t *)mBuffer; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | for (int ct=0; ct < 16; ct++) { |
| 234 | LOGV("fifo %p = 0x%08x 0x%08x 0x%08x 0x%08x", pptr, pptr[0], pptr[1], pptr[2], pptr[3]); |
| 235 | pptr += 4; |
| 236 | } |
| 237 | |
| 238 | } |