rogue
Loading...
Searching...
No Matches
Fifo.cpp
Go to the documentation of this file.
1
17#include "rogue/Directives.h"
18
20
21#include <stdint.h>
22
23#include <memory>
24#include <thread>
25
26#include "rogue/GilRelease.h"
27#include "rogue/Logging.h"
34
36
37#ifndef NO_PYTHON
38 #include <boost/python.hpp>
39namespace bp = boost::python;
40#endif
41
43ris::FifoPtr ris::Fifo::create(uint32_t maxDepth, uint32_t trimSize, bool noCopy) {
44 ris::FifoPtr p = std::make_shared<ris::Fifo>(maxDepth, trimSize, noCopy);
45 return (p);
46}
47
49void ris::Fifo::setup_python() {
50#ifndef NO_PYTHON
51 bp::class_<ris::Fifo, ris::FifoPtr, bp::bases<ris::Master, ris::Slave>, boost::noncopyable>(
52 "Fifo",
53 bp::init<uint32_t, uint32_t, bool>())
54 .def("size", &Fifo::size)
55 .def("dropCnt", &Fifo::dropCnt)
56 .def("clearCnt", &Fifo::clearCnt);
57#endif
58}
59
61ris::Fifo::Fifo(uint32_t maxDepth, uint32_t trimSize, bool noCopy)
62 : ris::Master(),
63 ris::Slave(),
64 log_(rogue::Logging::create("stream.Fifo")),
65 maxDepth_(maxDepth),
66 trimSize_(trimSize),
67 noCopy_(noCopy),
68 dropFrameCnt_(0),
69 threadEn_(true),
70 thread_(new std::thread(&ris::Fifo::runThread, this)) {
71 queue_.setThold(maxDepth);
72
73 // Set a thread name
74#ifndef __MACH__
75 pthread_setname_np(thread_->native_handle(), "Fifo");
76#endif
77}
78
80ris::Fifo::~Fifo() {
81 // No-op if the ctor threw before allocating thread_.
82 if (thread_ != nullptr) {
83 threadEn_ = false;
85 queue_.stop();
86 thread_->join();
87 delete thread_;
88 thread_ = nullptr;
89 }
90}
91
93std::size_t ris::Fifo::size() {
94 return queue_.size();
95}
96
98std::size_t ris::Fifo::dropCnt() const {
99 return dropFrameCnt_;
100}
101
103void ris::Fifo::clearCnt() {
104 dropFrameCnt_ = 0;
105}
106
108void ris::Fifo::acceptFrame(ris::FramePtr frame) {
109 uint32_t size;
110 ris::FramePtr nFrame;
113
114 // FIFO is full, drop frame
115 if (queue_.busy()) {
116 ++dropFrameCnt_;
117 return;
118 }
119
120 rogue::GilRelease noGil;
121 ris::FrameLockPtr lock = frame->lock();
122
123 // Do we copy the frame?
124 if (noCopy_) {
125 nFrame = frame;
126 } else {
127 // Get size, adjust if trim is enabled
128 size = frame->getPayload();
129 if (trimSize_ != 0 && trimSize_ < size) size = trimSize_;
130
131 // Request a new frame to hold the data
132 nFrame = reqFrame(size, true);
133 nFrame->setPayload(size);
134
135 // Get destination pointer
136 src = frame->begin();
137 dst = nFrame->begin();
138
139 // Copy the frame
140 ris::copyFrame(src, size, dst);
141 nFrame->setError(frame->getError());
142 nFrame->setChannel(frame->getChannel());
143 nFrame->setFlags(frame->getFlags());
144 }
145
146 // Append to buffer
147 queue_.push(nFrame);
148}
149
151void ris::Fifo::runThread() {
152 ris::FramePtr frame;
153 log_->logThreadId();
154
155 while (threadEn_) {
156 if ((frame = queue_.pop()) != NULL) sendFrame(frame);
157 }
158}
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
Structured Rogue logging helper.
Definition Logging.h:59
void setThold(uint32_t thold)
Sets busy-threshold depth used by busy().
Definition Queue.h:80
Stream frame FIFO.
Definition Fifo.h:54
std::size_t size()
Returns current FIFO queue depth.
Definition Fifo.cpp:93
std::size_t dropCnt() const
Returns the total dropped-frame counter.
Definition Fifo.cpp:98
void clearCnt()
Clears FIFO counters (including dropped-frame count).
Definition Fifo.cpp:103
Random-access byte iterator across a Frame payload.
Stream master endpoint.
Definition Master.h:65
Stream slave endpoint and default frame pool.
Definition Slave.h:72
std::shared_ptr< rogue::interfaces::stream::Fifo > FifoPtr
Shared pointer alias for Fifo.
Definition Fifo.h:137
static void copyFrame(rogue::interfaces::stream::FrameIterator &srcIter, uint32_t size, rogue::interfaces::stream::FrameIterator &dstIter)
Copies bytes between frame iterators.
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
Definition Frame.h:549
std::shared_ptr< rogue::interfaces::stream::FrameLock > FrameLockPtr
Shared pointer alias for FrameLock.
Definition FrameLock.h:110