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 threadEn_ = false;
83 queue_.stop();
84 thread_->join();
85 delete thread_;
86}
87
89std::size_t ris::Fifo::size() {
90 return queue_.size();
91}
92
94std::size_t ris::Fifo::dropCnt() const {
95 return dropFrameCnt_;
96}
97
99void ris::Fifo::clearCnt() {
100 dropFrameCnt_ = 0;
101}
102
104void ris::Fifo::acceptFrame(ris::FramePtr frame) {
105 uint32_t size;
106 ris::FramePtr nFrame;
109
110 // FIFO is full, drop frame
111 if (queue_.busy()) {
112 ++dropFrameCnt_;
113 return;
114 }
115
116 rogue::GilRelease noGil;
117 ris::FrameLockPtr lock = frame->lock();
118
119 // Do we copy the frame?
120 if (noCopy_) {
121 nFrame = frame;
122 } else {
123 // Get size, adjust if trim is enabled
124 size = frame->getPayload();
125 if (trimSize_ != 0 && trimSize_ < size) size = trimSize_;
126
127 // Request a new frame to hold the data
128 nFrame = reqFrame(size, true);
129 nFrame->setPayload(size);
130
131 // Get destination pointer
132 src = frame->begin();
133 dst = nFrame->begin();
134
135 // Copy the frame
136 ris::copyFrame(src, size, dst);
137 nFrame->setError(frame->getError());
138 nFrame->setChannel(frame->getChannel());
139 nFrame->setFlags(frame->getFlags());
140 }
141
142 // Append to buffer
143 queue_.push(nFrame);
144}
145
147void ris::Fifo::runThread() {
148 ris::FramePtr frame;
149 log_->logThreadId();
150
151 while (threadEn_) {
152 if ((frame = queue_.pop()) != NULL) sendFrame(frame);
153 }
154}
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
Structured Rogue logging helper.
Definition Logging.h:58
void setThold(uint32_t thold)
Sets busy-threshold depth used by busy().
Definition Queue.h:84
Stream frame FIFO.
Definition Fifo.h:53
std::size_t size()
Returns current FIFO queue depth.
Definition Fifo.cpp:89
std::size_t dropCnt() const
Returns the total dropped-frame counter.
Definition Fifo.cpp:94
void clearCnt()
Clears FIFO counters (including dropped-frame count).
Definition Fifo.cpp:99
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:133
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