Jason Sams | 326e0dd | 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 | #ifndef ANDROID_RS_LOCKLESS_FIFO_H |
| 18 | #define ANDROID_RS_LOCKLESS_FIFO_H |
| 19 | |
| 20 | |
Jason Sams | 1aa5a4e | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 21 | #include "rsUtils.h" |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 22 | #include "rsSignal.h" |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 25 | namespace renderscript { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 26 | |
| 27 | |
| 28 | // A simple FIFO to be used as a producer / consumer between two |
| 29 | // threads. One is writer and one is reader. The common cases |
Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 30 | // will not require locking. It is not threadsafe for multiple |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 31 | // readers or writers by design. |
| 32 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 33 | class LocklessCommandFifo { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | public: |
| 35 | bool init(uint32_t size); |
Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 36 | void shutdown(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | |
Jason Sams | 1dda675 | 2011-01-18 18:12:26 -0800 | [diff] [blame] | 38 | void printDebugData() const; |
| 39 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 40 | LocklessCommandFifo(); |
| 41 | ~LocklessCommandFifo(); |
| 42 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 43 | protected: |
| 44 | uint8_t * volatile mPut; |
| 45 | uint8_t * volatile mGet; |
| 46 | uint8_t * mBuffer; |
| 47 | uint8_t * mEnd; |
| 48 | uint8_t mSize; |
Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 49 | bool mInShutdown; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 50 | |
Jason Sams | 732f1c0 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 51 | Signal mSignalToWorker; |
| 52 | Signal mSignalToControl; |
| 53 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 54 | public: |
| 55 | void * reserve(uint32_t bytes); |
| 56 | void commit(uint32_t command, uint32_t bytes); |
| 57 | void commitSync(uint32_t command, uint32_t bytes); |
| 58 | |
| 59 | void flush(); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 60 | void wait(); |
| 61 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 62 | const void * get(uint32_t *command, uint32_t *bytesData); |
| 63 | void next(); |
| 64 | |
| 65 | void makeSpace(uint32_t bytes); |
Jason Sams | 8c46b10 | 2010-08-18 12:38:03 -0700 | [diff] [blame] | 66 | bool makeSpaceNonBlocking(uint32_t bytes); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 67 | |
| 68 | bool isEmpty() const; |
| 69 | uint32_t getFreeSpace() const; |
| 70 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 71 | private: |
| 72 | void dumpState(const char *) const; |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | } |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 77 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 78 | #endif |