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 | |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | LocklessCommandFifo::LocklessCommandFifo() |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | LocklessCommandFifo::~LocklessCommandFifo() |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | bool LocklessCommandFifo::init(uint32_t sizeInBytes) |
| 32 | { |
| 33 | // Add room for a buffer reset command |
| 34 | mBuffer = static_cast<uint8_t *>(malloc(sizeInBytes + 4)); |
| 35 | if (!mBuffer) { |
| 36 | LOGE("LocklessFifo allocation failure"); |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | int status = pthread_mutex_init(&mMutex, NULL); |
| 41 | if (status) { |
| 42 | LOGE("LocklessFifo mutex init failure"); |
| 43 | free(mBuffer); |
| 44 | return false; |
| 45 | } |
| 46 | status = pthread_cond_init(&mCondition, NULL); |
| 47 | if (status) { |
| 48 | LOGE("LocklessFifo condition init failure"); |
| 49 | pthread_mutex_destroy(&mMutex); |
| 50 | free(mBuffer); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | mSize = sizeInBytes; |
| 55 | mPut = mBuffer; |
| 56 | mGet = mBuffer; |
| 57 | mEnd = mBuffer + (sizeInBytes) - 1; |
| 58 | dumpState("init"); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | uint32_t LocklessCommandFifo::getFreeSpace() const |
| 63 | { |
| 64 | int32_t freeSpace = 0; |
| 65 | //dumpState("getFreeSpace"); |
| 66 | |
| 67 | if (mPut >= mGet) { |
| 68 | freeSpace = mEnd - mPut; |
| 69 | } else { |
| 70 | freeSpace = mGet - mPut; |
| 71 | } |
| 72 | |
| 73 | if (freeSpace < 0) { |
| 74 | freeSpace = 0; |
| 75 | } |
| 76 | |
| 77 | return freeSpace; |
| 78 | } |
| 79 | |
| 80 | bool LocklessCommandFifo::isEmpty() const |
| 81 | { |
| 82 | return mPut == mGet; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | void * LocklessCommandFifo::reserve(uint32_t sizeInBytes) |
| 87 | { |
| 88 | // Add space for command header; |
| 89 | sizeInBytes += 4; |
| 90 | |
| 91 | //dumpState("reserve"); |
| 92 | if (getFreeSpace() < sizeInBytes) { |
| 93 | makeSpace(sizeInBytes); |
| 94 | } |
| 95 | |
| 96 | return mPut + 4; |
| 97 | } |
| 98 | |
| 99 | void LocklessCommandFifo::commit(uint32_t command, uint32_t sizeInBytes) |
| 100 | { |
| 101 | //LOGE("commit cmd %i size %i", command, sizeInBytes); |
| 102 | //dumpState("commit 1"); |
| 103 | reinterpret_cast<uint16_t *>(mPut)[0] = command; |
| 104 | reinterpret_cast<uint16_t *>(mPut)[1] = sizeInBytes; |
| 105 | mPut += ((sizeInBytes + 3) & ~3) + 4; |
| 106 | //dumpState("commit 2"); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | void LocklessCommandFifo::commitSync(uint32_t command, uint32_t sizeInBytes) |
| 111 | { |
| 112 | commit(command, sizeInBytes); |
| 113 | flush(); |
| 114 | } |
| 115 | |
| 116 | void LocklessCommandFifo::flush() |
| 117 | { |
| 118 | //dumpState("flush 1"); |
| 119 | while(mPut != mGet) { |
| 120 | usleep(1); |
| 121 | } |
| 122 | //dumpState("flush 2"); |
| 123 | } |
| 124 | |
| 125 | const void * LocklessCommandFifo::get(uint32_t *command, uint32_t *bytesData) |
| 126 | { |
| 127 | while(1) { |
| 128 | while(isEmpty()) { |
| 129 | usleep(10); |
| 130 | } |
| 131 | //dumpState("get 3"); |
| 132 | |
| 133 | *command = reinterpret_cast<const uint16_t *>(mGet)[0]; |
| 134 | *bytesData = reinterpret_cast<const uint16_t *>(mGet)[1]; |
| 135 | //LOGE("Got %i, %i", *command, *bytesData); |
| 136 | |
| 137 | if (*command) { |
| 138 | // non-zero command is valid |
| 139 | return mGet+4; |
| 140 | } |
| 141 | |
| 142 | // zero command means reset to beginning. |
| 143 | mGet = mBuffer; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void LocklessCommandFifo::next() |
| 148 | { |
| 149 | uint32_t bytes = reinterpret_cast<const uint16_t *>(mGet)[1]; |
| 150 | mGet += ((bytes + 3) & ~3) + 4; |
| 151 | //dumpState("next"); |
| 152 | } |
| 153 | |
| 154 | void LocklessCommandFifo::makeSpace(uint32_t bytes) |
| 155 | { |
| 156 | if ((mPut+bytes) > mEnd) { |
| 157 | // Need to loop regardless of where get is. |
| 158 | while((mGet > mPut) && (mPut+4 >= mGet)) { |
| 159 | sleep(1); |
| 160 | } |
| 161 | |
| 162 | // Toss in a reset then the normal wait for space will do the rest. |
| 163 | reinterpret_cast<uint16_t *>(mPut)[0] = 0; |
| 164 | reinterpret_cast<uint16_t *>(mPut)[1] = 0; |
| 165 | mPut += 4; |
| 166 | } |
| 167 | |
| 168 | // it will fit here so we just need to wait for space. |
| 169 | while(getFreeSpace() < bytes) { |
| 170 | sleep(1); |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | |
| 175 | void LocklessCommandFifo::dumpState(const char *s) const |
| 176 | { |
| 177 | LOGE("%s put %p, get %p, buf %p, end %p", s, mPut, mGet, mBuffer, mEnd); |
| 178 | } |
| 179 | |
| 180 | |