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" |
| 18 | |
| 19 | using namespace android; |
| 20 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 21 | |
| 22 | LocklessCommandFifo::LocklessCommandFifo() |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | LocklessCommandFifo::~LocklessCommandFifo() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | bool LocklessCommandFifo::init(uint32_t sizeInBytes) |
| 31 | { |
| 32 | // Add room for a buffer reset command |
| 33 | mBuffer = static_cast<uint8_t *>(malloc(sizeInBytes + 4)); |
| 34 | if (!mBuffer) { |
| 35 | LOGE("LocklessFifo allocation failure"); |
| 36 | return false; |
| 37 | } |
| 38 | |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 39 | if (!mSignalToControl.init() || !mSignalToWorker.init()) { |
| 40 | LOGE("Signal setup failed"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 41 | free(mBuffer); |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | mSize = sizeInBytes; |
| 46 | mPut = mBuffer; |
| 47 | mGet = mBuffer; |
| 48 | mEnd = mBuffer + (sizeInBytes) - 1; |
| 49 | dumpState("init"); |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | uint32_t LocklessCommandFifo::getFreeSpace() const |
| 54 | { |
| 55 | int32_t freeSpace = 0; |
| 56 | //dumpState("getFreeSpace"); |
| 57 | |
| 58 | if (mPut >= mGet) { |
| 59 | freeSpace = mEnd - mPut; |
| 60 | } else { |
| 61 | freeSpace = mGet - mPut; |
| 62 | } |
| 63 | |
| 64 | if (freeSpace < 0) { |
| 65 | freeSpace = 0; |
| 66 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 67 | return freeSpace; |
| 68 | } |
| 69 | |
| 70 | bool LocklessCommandFifo::isEmpty() const |
| 71 | { |
| 72 | return mPut == mGet; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | void * LocklessCommandFifo::reserve(uint32_t sizeInBytes) |
| 77 | { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 78 | // Add space for command header and loop token; |
| 79 | sizeInBytes += 8; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 80 | |
| 81 | //dumpState("reserve"); |
| 82 | if (getFreeSpace() < sizeInBytes) { |
| 83 | makeSpace(sizeInBytes); |
| 84 | } |
| 85 | |
| 86 | return mPut + 4; |
| 87 | } |
| 88 | |
| 89 | void LocklessCommandFifo::commit(uint32_t command, uint32_t sizeInBytes) |
| 90 | { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 91 | //dumpState("commit 1"); |
| 92 | reinterpret_cast<uint16_t *>(mPut)[0] = command; |
| 93 | reinterpret_cast<uint16_t *>(mPut)[1] = sizeInBytes; |
| 94 | mPut += ((sizeInBytes + 3) & ~3) + 4; |
| 95 | //dumpState("commit 2"); |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 96 | mSignalToWorker.set(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void LocklessCommandFifo::commitSync(uint32_t command, uint32_t sizeInBytes) |
| 100 | { |
| 101 | commit(command, sizeInBytes); |
| 102 | flush(); |
| 103 | } |
| 104 | |
| 105 | void LocklessCommandFifo::flush() |
| 106 | { |
| 107 | //dumpState("flush 1"); |
| 108 | while(mPut != mGet) { |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 109 | mSignalToControl.wait(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 110 | } |
| 111 | //dumpState("flush 2"); |
| 112 | } |
| 113 | |
| 114 | const void * LocklessCommandFifo::get(uint32_t *command, uint32_t *bytesData) |
| 115 | { |
| 116 | while(1) { |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 117 | //dumpState("get"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 118 | while(isEmpty()) { |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 119 | mSignalToControl.set(); |
| 120 | mSignalToWorker.wait(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 121 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 122 | |
| 123 | *command = reinterpret_cast<const uint16_t *>(mGet)[0]; |
| 124 | *bytesData = reinterpret_cast<const uint16_t *>(mGet)[1]; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 125 | if (*command) { |
| 126 | // non-zero command is valid |
| 127 | return mGet+4; |
| 128 | } |
| 129 | |
| 130 | // zero command means reset to beginning. |
| 131 | mGet = mBuffer; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void LocklessCommandFifo::next() |
| 136 | { |
| 137 | uint32_t bytes = reinterpret_cast<const uint16_t *>(mGet)[1]; |
| 138 | mGet += ((bytes + 3) & ~3) + 4; |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 139 | if (isEmpty()) { |
| 140 | mSignalToControl.set(); |
| 141 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 142 | //dumpState("next"); |
| 143 | } |
| 144 | |
| 145 | void LocklessCommandFifo::makeSpace(uint32_t bytes) |
| 146 | { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 147 | //dumpState("make space"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 148 | if ((mPut+bytes) > mEnd) { |
| 149 | // Need to loop regardless of where get is. |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 150 | while((mGet > mPut) && (mBuffer+4 >= mGet)) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 151 | sleep(1); |
| 152 | } |
| 153 | |
| 154 | // Toss in a reset then the normal wait for space will do the rest. |
| 155 | reinterpret_cast<uint16_t *>(mPut)[0] = 0; |
| 156 | reinterpret_cast<uint16_t *>(mPut)[1] = 0; |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 157 | mPut = mBuffer; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // it will fit here so we just need to wait for space. |
| 161 | while(getFreeSpace() < bytes) { |
| 162 | sleep(1); |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
| 167 | void LocklessCommandFifo::dumpState(const char *s) const |
| 168 | { |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 169 | LOGV("%s put %p, get %p, buf %p, end %p", s, mPut, mGet, mBuffer, mEnd); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 172 | LocklessCommandFifo::Signal::Signal() |
| 173 | { |
| 174 | mSet = true; |
| 175 | } |
| 176 | |
| 177 | LocklessCommandFifo::Signal::~Signal() |
| 178 | { |
| 179 | pthread_mutex_destroy(&mMutex); |
| 180 | pthread_cond_destroy(&mCondition); |
| 181 | } |
| 182 | |
| 183 | bool LocklessCommandFifo::Signal::init() |
| 184 | { |
| 185 | int status = pthread_mutex_init(&mMutex, NULL); |
| 186 | if (status) { |
| 187 | LOGE("LocklessFifo mutex init failure"); |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | status = pthread_cond_init(&mCondition, NULL); |
| 192 | if (status) { |
| 193 | LOGE("LocklessFifo condition init failure"); |
| 194 | pthread_mutex_destroy(&mMutex); |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | void LocklessCommandFifo::Signal::set() |
| 202 | { |
| 203 | int status; |
| 204 | |
| 205 | status = pthread_mutex_lock(&mMutex); |
| 206 | if (status) { |
| 207 | LOGE("LocklessCommandFifo: error %i locking for set condition.", status); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | mSet = true; |
| 212 | |
| 213 | status = pthread_cond_signal(&mCondition); |
| 214 | if (status) { |
| 215 | LOGE("LocklessCommandFifo: error %i on set condition.", status); |
| 216 | } |
| 217 | |
| 218 | status = pthread_mutex_unlock(&mMutex); |
| 219 | if (status) { |
| 220 | LOGE("LocklessCommandFifo: error %i unlocking for set condition.", status); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void LocklessCommandFifo::Signal::wait() |
| 225 | { |
| 226 | int status; |
| 227 | |
| 228 | status = pthread_mutex_lock(&mMutex); |
| 229 | if (status) { |
| 230 | LOGE("LocklessCommandFifo: error %i locking for condition.", status); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | if (!mSet) { |
| 235 | status = pthread_cond_wait(&mCondition, &mMutex); |
| 236 | if (status) { |
| 237 | LOGE("LocklessCommandFifo: error %i waiting on condition.", status); |
| 238 | } |
| 239 | } |
| 240 | mSet = false; |
| 241 | |
| 242 | status = pthread_mutex_unlock(&mMutex); |
| 243 | if (status) { |
| 244 | LOGE("LocklessCommandFifo: error %i unlocking for condition.", status); |
| 245 | } |
| 246 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 247 | |