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"
28#include "rogue/ScopedGil.h"
35
37
38#ifndef NO_PYTHON
39 #include <boost/python.hpp>
40namespace bp = boost::python;
41#endif
42
44ris::FifoPtr ris::Fifo::create(uint32_t maxDepth, uint32_t trimSize, bool noCopy) {
45 ris::FifoPtr p = std::make_shared<ris::Fifo>(maxDepth, trimSize, noCopy);
46 return (p);
47}
48
50void ris::Fifo::setup_python() {
51#ifndef NO_PYTHON
52 bp::class_<ris::Fifo, ris::FifoPtr, bp::bases<ris::Master, ris::Slave>, boost::noncopyable>(
53 "Fifo",
54 bp::init<uint32_t, uint32_t, bool>())
55 .def("size", &Fifo::size)
56 .def("dropCnt", &Fifo::dropCnt)
57 .def("clearCnt", &Fifo::clearCnt)
58 .def("_stop", &Fifo::stop);
59#endif
60}
61
63ris::Fifo::Fifo(uint32_t maxDepth, uint32_t trimSize, bool noCopy)
64 : ris::Master(),
65 ris::Slave(),
66 log_(rogue::Logging::create("stream.Fifo")),
67 maxDepth_(maxDepth),
68 trimSize_(trimSize),
69 noCopy_(noCopy),
70 dropFrameCnt_(0),
71 threadEn_(true),
72 thread_(new std::thread(&ris::Fifo::runThread, this)) {
73 queue_.setThold(maxDepth);
74
75 // Set a thread name
76#ifndef __MACH__
77 pthread_setname_np(thread_->native_handle(), "Fifo");
78#endif
79}
80
82ris::Fifo::~Fifo() {
83 stop();
84}
85
87void ris::Fifo::stop() {
88 // No-op on a repeat call, or if the ctor threw before allocating thread_.
89 if (threadEn_.exchange(false) && thread_ != nullptr) {
91 queue_.stop();
92 thread_->join();
93 delete thread_;
94 thread_ = nullptr;
95 }
96}
97
99std::size_t ris::Fifo::size() {
100 return queue_.size();
101}
102
104std::size_t ris::Fifo::dropCnt() const {
105 return dropFrameCnt_;
106}
107
109void ris::Fifo::clearCnt() {
110 dropFrameCnt_ = 0;
111}
112
114void ris::Fifo::acceptFrame(ris::FramePtr frame) {
115 uint32_t size;
116 ris::FramePtr nFrame;
119
120 // FIFO is full, drop frame
121 if (queue_.busy()) {
122 ++dropFrameCnt_;
123 return;
124 }
125
126 rogue::GilRelease noGil;
127 ris::FrameLockPtr lock = frame->lock();
128
129 // Do we copy the frame?
130 if (noCopy_) {
131 nFrame = frame;
132 } else {
133 // Get size, adjust if trim is enabled
134 size = frame->getPayload();
135 if (trimSize_ != 0 && trimSize_ < size) size = trimSize_;
136
137 // Request a new frame to hold the data
138 nFrame = reqFrame(size, true);
139 nFrame->setPayload(size);
140
141 // Get destination pointer
142 src = frame->begin();
143 dst = nFrame->begin();
144
145 // Copy the frame
146 ris::copyFrame(src, size, dst);
147 nFrame->setError(frame->getError());
148 nFrame->setChannel(frame->getChannel());
149 nFrame->setFlags(frame->getFlags());
150 }
151
152 // Append to buffer
153 queue_.push(nFrame);
154}
155
157void ris::Fifo::runThread() {
158 ris::FramePtr frame;
159 log_->logThreadId();
160
161 while (threadEn_) {
162 if ((frame = queue_.pop()) != NULL) {
163 sendFrame(frame);
164
165 // Release here instead of at the next pop() overwrite or thread exit.
166 // With noCopy_ the queued frame is the caller's, so a Python-owned
167 // FramePtr carries a Boost.Python deleter that calls Py_DECREF without
168 // the GIL, which is fatal on this thread. Copy mode holds a reqFrame()
169 // frame with no Python state, so it needs no GIL, and skipping the
170 // acquire there avoids blocking a GIL-holding C++ caller.
171#ifndef NO_PYTHON
172 if (noCopy_ && Py_IsInitialized()) {
174 frame.reset();
175 } else {
176 frame.reset();
177 }
178#else
179 frame.reset();
180#endif
181 }
182 }
183}
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
RAII helper that acquires the Python GIL for a scope.
Definition ScopedGil.h:35
Stream frame FIFO.
Definition Fifo.h:54
void stop()
Stops the internal worker thread.
Definition Fifo.cpp:87
std::size_t size()
Returns current FIFO queue depth.
Definition Fifo.cpp:99
std::size_t dropCnt() const
Returns the total dropped-frame counter.
Definition Fifo.cpp:104
void clearCnt()
Clears FIFO counters (including dropped-frame count).
Definition Fifo.cpp:109
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:147
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