blob: 1aaf9ea22e9ddcde288babbbb7297b70f7cbd366 [file] [log] [blame]
Phil Burkfd911c12017-01-03 17:15:39 -08001/*
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/**
26 * A FifoControllerBase with counters external to the class.
27 *
28 * The actual copunters may be stored in separate regions of shared memory
29 * with different access rights.
30 */
31class FifoControllerIndirect : public FifoControllerBase {
32
33public:
34 FifoControllerIndirect(fifo_frames_t capacity,
35 fifo_frames_t threshold,
36 fifo_counter_t * readCounterAddress,
37 fifo_counter_t * writeCounterAddress)
38 : FifoControllerBase(capacity, threshold)
39 , mReadCounterAddress((std::atomic<fifo_counter_t> *) readCounterAddress)
40 , mWriteCounterAddress((std::atomic<fifo_counter_t> *) writeCounterAddress)
41 {
42 setReadCounter(0);
43 setWriteCounter(0);
44 }
45 virtual ~FifoControllerIndirect() {};
46
47 // TODO review use of memory barriers, probably incorrect
48 virtual fifo_counter_t getReadCounter() override {
49 return mReadCounterAddress->load(std::memory_order_acquire);
50 }
51
52 virtual void setReadCounter(fifo_counter_t count) override {
53 mReadCounterAddress->store(count, std::memory_order_release);
54 }
55
56 virtual fifo_counter_t getWriteCounter() override {
57 return mWriteCounterAddress->load(std::memory_order_acquire);
58 }
59
60 virtual void setWriteCounter(fifo_counter_t count) override {
61 mWriteCounterAddress->store(count, std::memory_order_release);
62 }
63
64private:
65 std::atomic<fifo_counter_t> * mReadCounterAddress;
66 std::atomic<fifo_counter_t> * mWriteCounterAddress;
67};
68
69#endif //FIFO_FIFO_CONTROLLER_INDIRECT_H