1 /*
2  * Copyright 2016 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_INDIRECT_H
18 #define FIFO_FIFO_CONTROLLER_INDIRECT_H
19 
20 #include <stdint.h>
21 #include <atomic>
22 
23 #include "FifoControllerBase.h"
24 
25 namespace android {
26 
27 /**
28  * A FifoControllerBase with counters external to the class.
29  *
30  * The actual counters may be stored in separate regions of shared memory
31  * with different access rights.
32  */
33 class FifoControllerIndirect : public FifoControllerBase {
34 
35 public:
FifoControllerIndirect(fifo_frames_t capacity,fifo_frames_t threshold,fifo_counter_t * readCounterAddress,fifo_counter_t * writeCounterAddress)36     FifoControllerIndirect(fifo_frames_t capacity,
37                            fifo_frames_t threshold,
38                            fifo_counter_t * readCounterAddress,
39                            fifo_counter_t * writeCounterAddress)
40         : FifoControllerBase(capacity, threshold)
41         , mReadCounterAddress((std::atomic<fifo_counter_t> *) readCounterAddress)
42         , mWriteCounterAddress((std::atomic<fifo_counter_t> *) writeCounterAddress)
43     {
44         setReadCounter(0);
45         setWriteCounter(0);
46     }
~FifoControllerIndirect()47     virtual ~FifoControllerIndirect() {};
48 
49     // TODO review use of memory barriers, probably incorrect
getReadCounter()50     virtual fifo_counter_t getReadCounter() override {
51         return mReadCounterAddress->load(std::memory_order_acquire);
52     }
53 
setReadCounter(fifo_counter_t count)54     virtual void setReadCounter(fifo_counter_t count) override {
55         mReadCounterAddress->store(count, std::memory_order_release);
56     }
57 
getWriteCounter()58     virtual fifo_counter_t getWriteCounter() override {
59         return mWriteCounterAddress->load(std::memory_order_acquire);
60     }
61 
setWriteCounter(fifo_counter_t count)62     virtual void setWriteCounter(fifo_counter_t count) override {
63         mWriteCounterAddress->store(count, std::memory_order_release);
64     }
65 
66 private:
67     std::atomic<fifo_counter_t> * mReadCounterAddress;
68     std::atomic<fifo_counter_t> * mWriteCounterAddress;
69 };
70 
71 }  // android
72 
73 #endif //FIFO_FIFO_CONTROLLER_INDIRECT_H
74