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 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 33 | class LocklessCommandFifo { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | public: |
| 35 | bool init(uint32_t size); |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 36 | void shutdown(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | |
Jason Sams | d1ac981 | 2011-01-18 18:12:26 -0800 | [diff] [blame] | 38 | void printDebugData() const; |
| 39 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 40 | LocklessCommandFifo(); |
| 41 | ~LocklessCommandFifo(); |
| 42 | |
Jason Sams | d19f10d | 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 | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 49 | bool mInShutdown; |
Alex Sakhartchouk | a8bb921 | 2011-08-19 09:43:18 -0700 | [diff] [blame^] | 50 | bool mInitialized; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 51 | |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 52 | Signal mSignalToWorker; |
| 53 | Signal mSignalToControl; |
| 54 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 55 | public: |
| 56 | void * reserve(uint32_t bytes); |
| 57 | void commit(uint32_t command, uint32_t bytes); |
| 58 | void commitSync(uint32_t command, uint32_t bytes); |
| 59 | |
| 60 | void flush(); |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 61 | bool wait(uint64_t timeout = 0); |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 62 | |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 63 | const void * get(uint32_t *command, uint32_t *bytesData, uint64_t timeout = 0); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 64 | void next(); |
| 65 | |
| 66 | void makeSpace(uint32_t bytes); |
Jason Sams | d0cb106 | 2010-08-18 12:38:03 -0700 | [diff] [blame] | 67 | bool makeSpaceNonBlocking(uint32_t bytes); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 68 | |
| 69 | bool isEmpty() const; |
| 70 | uint32_t getFreeSpace() const; |
| 71 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 72 | private: |
| 73 | void dumpState(const char *) const; |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | } |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 78 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 79 | #endif |