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 | #ifndef ANDROID_RS_LOCKLESS_FIFO_H |
| 18 | #define ANDROID_RS_LOCKLESS_FIFO_H |
| 19 | |
| 20 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 21 | #include "rsUtils.h" |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 22 | #include "rsSignal.h" |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 25 | namespace renderscript { |
Jason Sams | d19f10d | 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 | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 30 | // will not require locking. It is not threadsafe for multiple |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 31 | // readers or writers by design. |
| 32 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 33 | class LocklessCommandFifo |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | { |
| 35 | public: |
| 36 | bool init(uint32_t size); |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 37 | void shutdown(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 38 | |
| 39 | LocklessCommandFifo(); |
| 40 | ~LocklessCommandFifo(); |
| 41 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 42 | protected: |
| 43 | uint8_t * volatile mPut; |
| 44 | uint8_t * volatile mGet; |
| 45 | uint8_t * mBuffer; |
| 46 | uint8_t * mEnd; |
| 47 | uint8_t mSize; |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 48 | bool mInShutdown; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 49 | |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 50 | Signal mSignalToWorker; |
| 51 | Signal mSignalToControl; |
| 52 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 53 | public: |
| 54 | void * reserve(uint32_t bytes); |
| 55 | void commit(uint32_t command, uint32_t bytes); |
| 56 | void commitSync(uint32_t command, uint32_t bytes); |
| 57 | |
| 58 | void flush(); |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 59 | void wait(); |
| 60 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 61 | const void * get(uint32_t *command, uint32_t *bytesData); |
| 62 | void next(); |
| 63 | |
| 64 | void makeSpace(uint32_t bytes); |
Jason Sams | d0cb106 | 2010-08-18 12:38:03 -0700 | [diff] [blame] | 65 | bool makeSpaceNonBlocking(uint32_t bytes); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 66 | |
| 67 | bool isEmpty() const; |
| 68 | uint32_t getFreeSpace() const; |
| 69 | |
| 70 | |
| 71 | private: |
| 72 | void dumpState(const char *) const; |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | } |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 77 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 78 | #endif |