| 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 | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 22 |  | 
 | 23 | namespace android { | 
 | 24 |  | 
 | 25 |  | 
 | 26 | // A simple FIFO to be used as a producer / consumer between two | 
 | 27 | // threads.  One is writer and one is reader.  The common cases | 
| Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 28 | // will not require locking.  It is not threadsafe for multiple | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 29 | // readers or writers by design. | 
 | 30 |  | 
| Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 31 | class LocklessCommandFifo | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 32 | { | 
 | 33 | public: | 
 | 34 |     bool init(uint32_t size); | 
| Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 35 |     void shutdown(); | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 36 |  | 
 | 37 |     LocklessCommandFifo(); | 
 | 38 |     ~LocklessCommandFifo(); | 
 | 39 |  | 
 | 40 |  | 
 | 41 | protected: | 
| Jason Sams | 732f1c0 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 42 |     class Signal { | 
 | 43 |     public: | 
 | 44 |         Signal(); | 
 | 45 |         ~Signal(); | 
 | 46 |  | 
 | 47 |         bool init(); | 
 | 48 |  | 
 | 49 |         void set(); | 
 | 50 |         void wait(); | 
 | 51 |  | 
 | 52 |     protected: | 
 | 53 |         bool mSet; | 
 | 54 |         pthread_mutex_t mMutex; | 
 | 55 |         pthread_cond_t mCondition; | 
 | 56 |     }; | 
 | 57 |  | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 58 |     uint8_t * volatile mPut; | 
 | 59 |     uint8_t * volatile mGet; | 
 | 60 |     uint8_t * mBuffer; | 
 | 61 |     uint8_t * mEnd; | 
 | 62 |     uint8_t mSize; | 
| Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 63 |     bool mInShutdown; | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 64 |  | 
| Jason Sams | 732f1c0 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 65 |     Signal mSignalToWorker; | 
 | 66 |     Signal mSignalToControl; | 
 | 67 |  | 
 | 68 |  | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 69 |  | 
 | 70 | public: | 
 | 71 |     void * reserve(uint32_t bytes); | 
 | 72 |     void commit(uint32_t command, uint32_t bytes); | 
 | 73 |     void commitSync(uint32_t command, uint32_t bytes); | 
 | 74 |  | 
 | 75 |     void flush(); | 
 | 76 |     const void * get(uint32_t *command, uint32_t *bytesData); | 
 | 77 |     void next(); | 
 | 78 |  | 
 | 79 |     void makeSpace(uint32_t bytes); | 
 | 80 |  | 
 | 81 |     bool isEmpty() const; | 
 | 82 |     uint32_t getFreeSpace() const; | 
 | 83 |  | 
 | 84 |  | 
 | 85 | private: | 
 | 86 |     void dumpState(const char *) const; | 
 | 87 | }; | 
 | 88 |  | 
 | 89 |  | 
 | 90 | } | 
 | 91 | #endif |