1 /* 2 * Copyright 2015 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 FIFO_FIFO_CONTROLLER_H 18 #define FIFO_FIFO_CONTROLLER_H 19 20 #include <stdint.h> 21 #include <atomic> 22 23 #include "FifoControllerBase.h" 24 25 namespace android { 26 27 /** 28 * A FIFO with counters contained in the class. 29 */ 30 class FifoController : public FifoControllerBase 31 { 32 public: FifoController(fifo_frames_t bufferSize,fifo_frames_t threshold)33 FifoController(fifo_frames_t bufferSize, fifo_frames_t threshold) 34 : FifoControllerBase(bufferSize, threshold) 35 , mReadCounter(0) 36 , mWriteCounter(0) 37 {} 38 ~FifoController()39 virtual ~FifoController() {} 40 41 // TODO review use of memory barriers, probably incorrect getReadCounter()42 virtual fifo_counter_t getReadCounter() override { 43 return mReadCounter.load(std::memory_order_acquire); 44 } setReadCounter(fifo_counter_t n)45 virtual void setReadCounter(fifo_counter_t n) override { 46 mReadCounter.store(n, std::memory_order_release); 47 } getWriteCounter()48 virtual fifo_counter_t getWriteCounter() override { 49 return mWriteCounter.load(std::memory_order_acquire); 50 } setWriteCounter(fifo_counter_t n)51 virtual void setWriteCounter(fifo_counter_t n) override { 52 mWriteCounter.store(n, std::memory_order_release); 53 } 54 55 private: 56 std::atomic<fifo_counter_t> mReadCounter; 57 std::atomic<fifo_counter_t> mWriteCounter; 58 }; 59 60 } // android 61 62 #endif //FIFO_FIFO_CONTROLLER_H 63