Home | History | Annotate | Download | only in rs
      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 
     21 #include "rsUtils.h"
     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
     28 // will not require locking.  It is not threadsafe for multiple
     29 // readers or writers by design.
     30 
     31 class LocklessCommandFifo
     32 {
     33 public:
     34     bool init(uint32_t size);
     35     void shutdown();
     36 
     37     LocklessCommandFifo();
     38     ~LocklessCommandFifo();
     39 
     40 
     41 protected:
     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 
     58     uint8_t * volatile mPut;
     59     uint8_t * volatile mGet;
     60     uint8_t * mBuffer;
     61     uint8_t * mEnd;
     62     uint8_t mSize;
     63     bool mInShutdown;
     64 
     65     Signal mSignalToWorker;
     66     Signal mSignalToControl;
     67 
     68 
     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
     92