AARE
Data analysis library for PSI hybrid detectors
Loading...
Searching...
No Matches
CircularFifo.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <fmt/color.h>
5#include <fmt/format.h>
6#include <memory>
7#include <thread>
8
10
11namespace aare {
12
13template <class ItemType> class CircularFifo {
14 uint32_t fifo_size;
17
18 public:
20 CircularFifo(uint32_t size) : fifo_size(size), free_slots(size + 1), filled_slots(size + 1) {
21
22 // TODO! how do we deal with alignment for writing? alignas???
23 // Do we give the user a chance to provide memory locations?
24 // Templated allocator?
25 for (size_t i = 0; i < fifo_size; ++i) {
26 free_slots.write(ItemType{});
27 }
28 }
29
30 bool next() {
31 // TODO! avoid default constructing ItemType
32 ItemType it;
33 if (!filled_slots.read(it))
34 return false;
35 if (!free_slots.write(std::move(it)))
36 return false;
37 return true;
38 }
39
41
42 using value_type = ItemType;
43
44 auto numFilledSlots() const noexcept { return filled_slots.sizeGuess(); }
45 auto numFreeSlots() const noexcept { return free_slots.sizeGuess(); }
46 auto isFull() const noexcept { return filled_slots.isFull(); }
47
48 ItemType pop_free() {
49 ItemType v;
50 while (!free_slots.read(v))
51 ;
52 return std::move(v);
53 // return v;
54 }
55
56 bool try_pop_free(ItemType &v) { return free_slots.read(v); }
57
58 ItemType pop_value(std::chrono::nanoseconds wait, std::atomic<bool> &stopped) {
59 ItemType v;
60 while (!filled_slots.read(v) && !stopped) {
61 std::this_thread::sleep_for(wait);
62 }
63 return std::move(v);
64 }
65
66 ItemType pop_value() {
67 ItemType v;
68 while (!filled_slots.read(v))
69 ;
70 return std::move(v);
71 }
72
73 ItemType *frontPtr() { return filled_slots.frontPtr(); }
74
75 // TODO! Add function to move item from filled to free to be used
76 // with the frontPtr function
77
78 template <class... Args> void push_value(Args &&...recordArgs) {
79 while (!filled_slots.write(std::forward<Args>(recordArgs)...))
80 ;
81 }
82
83 template <class... Args> bool try_push_value(Args &&...recordArgs) {
84 return filled_slots.write(std::forward<Args>(recordArgs)...);
85 }
86
87 template <class... Args> void push_free(Args &&...recordArgs) {
88 while (!free_slots.write(std::forward<Args>(recordArgs)...))
89 ;
90 }
91
92 template <class... Args> bool try_push_free(Args &&...recordArgs) {
93 return free_slots.write(std::forward<Args>(recordArgs)...);
94 }
95};
96
97} // namespace aare
Definition CircularFifo.hpp:13
void push_free(Args &&...recordArgs)
Definition CircularFifo.hpp:87
bool try_pop_free(ItemType &v)
Definition CircularFifo.hpp:56
folly::ProducerConsumerQueue< ItemType > filled_slots
Definition CircularFifo.hpp:16
~CircularFifo()
Definition CircularFifo.hpp:40
ItemType pop_value()
Definition CircularFifo.hpp:66
ItemType value_type
Definition CircularFifo.hpp:42
ItemType pop_free()
Definition CircularFifo.hpp:48
CircularFifo()
Definition CircularFifo.hpp:19
auto numFreeSlots() const noexcept
Definition CircularFifo.hpp:45
bool try_push_value(Args &&...recordArgs)
Definition CircularFifo.hpp:83
CircularFifo(uint32_t size)
Definition CircularFifo.hpp:20
ItemType * frontPtr()
Definition CircularFifo.hpp:73
folly::ProducerConsumerQueue< ItemType > free_slots
Definition CircularFifo.hpp:15
void push_value(Args &&...recordArgs)
Definition CircularFifo.hpp:78
bool try_push_free(Args &&...recordArgs)
Definition CircularFifo.hpp:92
auto isFull() const noexcept
Definition CircularFifo.hpp:46
ItemType pop_value(std::chrono::nanoseconds wait, std::atomic< bool > &stopped)
Definition CircularFifo.hpp:58
uint32_t fifo_size
Definition CircularFifo.hpp:14
bool next()
Definition CircularFifo.hpp:30
auto numFilledSlots() const noexcept
Definition CircularFifo.hpp:44
Frame class to represent a single frame of data model class should be able to work with streams comin...
Definition CircularFifo.hpp:11
Definition ProducerConsumerQueue.hpp:41
T * frontPtr()
Definition ProducerConsumerQueue.hpp:114
bool read(T &record)
Definition ProducerConsumerQueue.hpp:95
bool isFull() const
Definition ProducerConsumerQueue.hpp:140
size_t sizeGuess() const
Definition ProducerConsumerQueue.hpp:157
bool write(Args &&...recordArgs)
Definition ProducerConsumerQueue.hpp:78