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 | |
| 24 | LocklessCommandFifo::LocklessCommandFifo() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | LocklessCommandFifo::~LocklessCommandFifo() |
| 29 | { |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 30 | if (!mInShutdown) { |
| 31 | shutdown(); |
| 32 | } |
| 33 | free(mBuffer); |
| 34 | } |
| 35 | |
| 36 | void LocklessCommandFifo::shutdown() |
| 37 | { |
| 38 | mInShutdown = true; |
| 39 | mSignalToWorker.set(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | bool 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 Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 51 | if (!mSignalToControl.init() || !mSignalToWorker.init()) { |
| 52 | LOGE("Signal setup failed"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 53 | free(mBuffer); |
| 54 | return false; |
| 55 | } |
| 56 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 57 | mInShutdown = false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 58 | mSize = sizeInBytes; |
| 59 | mPut = mBuffer; |
| 60 | mGet = mBuffer; |
| 61 | mEnd = mBuffer + (sizeInBytes) - 1; |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 62 | //dumpState("init"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 63 | return true; |
| 64 | } |
| 65 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 66 | uint32_t LocklessCommandFifo::getFreeSpace() const |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 67 | { |
| 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 Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 80 | return freeSpace; |
| 81 | } |
| 82 | |
| 83 | bool LocklessCommandFifo::isEmpty() const |
| 84 | { |
| 85 | return mPut == mGet; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | void * LocklessCommandFifo::reserve(uint32_t sizeInBytes) |
| 90 | { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 91 | // Add space for command header and loop token; |
| 92 | sizeInBytes += 8; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 93 | |
| 94 | //dumpState("reserve"); |
| 95 | if (getFreeSpace() < sizeInBytes) { |
| 96 | makeSpace(sizeInBytes); |
| 97 | } |
| 98 | |
| 99 | return mPut + 4; |
| 100 | } |
| 101 | |
| 102 | void LocklessCommandFifo::commit(uint32_t command, uint32_t sizeInBytes) |
| 103 | { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 104 | if (mInShutdown) { |
| 105 | return; |
| 106 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 107 | //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 Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 112 | mSignalToWorker.set(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void LocklessCommandFifo::commitSync(uint32_t command, uint32_t sizeInBytes) |
| 116 | { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 117 | if (mInShutdown) { |
| 118 | return; |
| 119 | } |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 120 | |
| 121 | //char buf[1024]; |
| 122 | //sprintf(buf, "RenderScript LocklessCommandFifo::commitSync %p %i %i", this, command, sizeInBytes); |
| 123 | //StopWatch compileTimer(buf); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 124 | commit(command, sizeInBytes); |
| 125 | flush(); |
| 126 | } |
| 127 | |
| 128 | void LocklessCommandFifo::flush() |
| 129 | { |
| 130 | //dumpState("flush 1"); |
| 131 | while(mPut != mGet) { |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 132 | mSignalToControl.wait(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 133 | } |
| 134 | //dumpState("flush 2"); |
| 135 | } |
| 136 | |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 137 | void LocklessCommandFifo::wait() |
| 138 | { |
| 139 | while(isEmpty() && !mInShutdown) { |
| 140 | mSignalToControl.set(); |
| 141 | mSignalToWorker.wait(); |
| 142 | } |
| 143 | } |
| 144 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 145 | const void * LocklessCommandFifo::get(uint32_t *command, uint32_t *bytesData) |
| 146 | { |
| 147 | while(1) { |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 148 | //dumpState("get"); |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 149 | wait(); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 150 | if (mInShutdown) { |
| 151 | *command = 0; |
| 152 | *bytesData = 0; |
| 153 | return 0; |
| 154 | } |
| 155 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 156 | *command = reinterpret_cast<const uint16_t *>(mGet)[0]; |
| 157 | *bytesData = reinterpret_cast<const uint16_t *>(mGet)[1]; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 158 | if (*command) { |
| 159 | // non-zero command is valid |
| 160 | return mGet+4; |
| 161 | } |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 162 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 163 | // zero command means reset to beginning. |
| 164 | mGet = mBuffer; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void LocklessCommandFifo::next() |
| 169 | { |
| 170 | uint32_t bytes = reinterpret_cast<const uint16_t *>(mGet)[1]; |
| 171 | mGet += ((bytes + 3) & ~3) + 4; |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 172 | if (isEmpty()) { |
| 173 | mSignalToControl.set(); |
| 174 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 175 | //dumpState("next"); |
| 176 | } |
| 177 | |
Jason Sams | d0cb106 | 2010-08-18 12:38:03 -0700 | [diff] [blame] | 178 | bool 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 Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 202 | void LocklessCommandFifo::makeSpace(uint32_t bytes) |
| 203 | { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 204 | //dumpState("make space"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 205 | if ((mPut+bytes) > mEnd) { |
| 206 | // Need to loop regardless of where get is. |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 207 | while((mGet > mPut) && (mBuffer+4 >= mGet)) { |
Jason Sams | e60446b | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 208 | usleep(100); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 209 | } |
| 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 Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 214 | mPut = mBuffer; |
Jason Sams | d0cb106 | 2010-08-18 12:38:03 -0700 | [diff] [blame] | 215 | mSignalToWorker.set(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // it will fit here so we just need to wait for space. |
| 219 | while(getFreeSpace() < bytes) { |
Jason Sams | e60446b | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 220 | usleep(100); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 221 | } |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 222 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void LocklessCommandFifo::dumpState(const char *s) const |
| 226 | { |
Jason Sams | d0cb106 | 2010-08-18 12:38:03 -0700 | [diff] [blame] | 227 | 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] | 228 | } |
| 229 | |