blob: 5832d9cd8eec4bb438c876ebf62335ab44fae22d [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
Phil Burk7f6b40d2017-02-09 13:18:38 -080025namespace android {
26
Phil Burkfd911c12017-01-03 17:15:39 -080027/**
28 * A FifoControllerBase with counters external to the class.
29 *
30 * The actual copunters may be stored in separate regions of shared memory
31 * with different access rights.
32 */
33class FifoControllerIndirect : public FifoControllerBase {
34
35public:
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 }
47 virtual ~FifoControllerIndirect() {};
48
49 // TODO review use of memory barriers, probably incorrect
50 virtual fifo_counter_t getReadCounter() override {
51 return mReadCounterAddress->load(std::memory_order_acquire);
52 }
53
54 virtual void setReadCounter(fifo_counter_t count) override {
55 mReadCounterAddress->store(count, std::memory_order_release);
56 }
57
58 virtual fifo_counter_t getWriteCounter() override {
59 return mWriteCounterAddress->load(std::memory_order_acquire);
60 }
61
62 virtual void setWriteCounter(fifo_counter_t count) override {
63 mWriteCounterAddress->store(count, std::memory_order_release);
64 }
65
66private:
67 std::atomic<fifo_counter_t> * mReadCounterAddress;
68 std::atomic<fifo_counter_t> * mWriteCounterAddress;
69};
70
Phil Burk7f6b40d2017-02-09 13:18:38 -080071} // android
72
Phil Burkfd911c12017-01-03 17:15:39 -080073#endif //FIFO_FIFO_CONTROLLER_INDIRECT_H