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