2011-06-15 00:25:22 +00:00
|
|
|
//
|
2012-02-02 23:15:54 +00:00
|
|
|
// Copyright 2011-2012 Ettus Research LLC
|
2018-02-19 23:30:32 +00:00
|
|
|
// Copyright 2018 Ettus Research, a National Instruments Company
|
2011-06-15 00:25:22 +00:00
|
|
|
//
|
2018-02-19 23:30:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2011-06-15 00:25:22 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include <uhd/transport/buffer_pool.hpp>
|
uhd: Replace all occurrences of boost::bind with std::bind
Note: Replacing everything with a lambda would be even better, but that
can't be easily scripted so we'll do this as a first step to reduce the
Boost footprint.
This also removes occurences of #include <boost/bind.hpp>, and makes
sure all usages of std::bind have an #include <functional>. clang-format
wasn't always applied to minimize the changeset in this commit, however,
it was applied to the blocks of #includes.
Due to conflicts with other Boost libraries, the placeholders _1, _2,
etc. could not be directly used, but had to be explicitly called out
(as std::placeholders::_1, etc.). This makes the use of std::bind even
uglier, which serves as another reminder that using std::bind (and even
more so, boost::bind) should be avoided.
nirio/rpc/rpc_client.cpp still contains a reference to boost::bind. It
was not possible to remove it by simply doing a search and replace, so
it will be removed in a separate commit.
2019-10-16 23:21:19 +00:00
|
|
|
#include <uhd/transport/usb_zero_copy.hpp>
|
2012-02-02 23:15:54 +00:00
|
|
|
#include <uhd/utils/byteswap.hpp>
|
2017-02-08 00:37:25 +00:00
|
|
|
#include <uhd/utils/log.hpp>
|
2012-05-30 20:33:09 +00:00
|
|
|
#include <uhd/utils/tasks.hpp>
|
2018-04-06 18:36:04 +00:00
|
|
|
#include <uhdlib/utils/atomic.hpp>
|
2012-05-30 20:33:09 +00:00
|
|
|
#include <boost/thread/condition_variable.hpp>
|
uhd: Replace all occurrences of boost::bind with std::bind
Note: Replacing everything with a lambda would be even better, but that
can't be easily scripted so we'll do this as a first step to reduce the
Boost footprint.
This also removes occurences of #include <boost/bind.hpp>, and makes
sure all usages of std::bind have an #include <functional>. clang-format
wasn't always applied to minimize the changeset in this commit, however,
it was applied to the blocks of #includes.
Due to conflicts with other Boost libraries, the placeholders _1, _2,
etc. could not be directly used, but had to be explicitly called out
(as std::placeholders::_1, etc.). This makes the use of std::bind even
uglier, which serves as another reminder that using std::bind (and even
more so, boost::bind) should be avoided.
nirio/rpc/rpc_client.cpp still contains a reference to boost::bind. It
was not possible to remove it by simply doing a search and replace, so
it will be removed in a separate commit.
2019-10-16 23:21:19 +00:00
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
|
#include <functional>
|
2011-06-15 00:25:22 +00:00
|
|
|
#include <iostream>
|
uhd: Replace all occurrences of boost::bind with std::bind
Note: Replacing everything with a lambda would be even better, but that
can't be easily scripted so we'll do this as a first step to reduce the
Boost footprint.
This also removes occurences of #include <boost/bind.hpp>, and makes
sure all usages of std::bind have an #include <functional>. clang-format
wasn't always applied to minimize the changeset in this commit, however,
it was applied to the blocks of #includes.
Due to conflicts with other Boost libraries, the placeholders _1, _2,
etc. could not be directly used, but had to be explicitly called out
(as std::placeholders::_1, etc.). This makes the use of std::bind even
uglier, which serves as another reminder that using std::bind (and even
more so, boost::bind) should be avoided.
nirio/rpc/rpc_client.cpp still contains a reference to boost::bind. It
was not possible to remove it by simply doing a search and replace, so
it will be removed in a separate commit.
2019-10-16 23:21:19 +00:00
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
2011-06-15 00:25:22 +00:00
|
|
|
|
2012-07-02 18:18:28 +00:00
|
|
|
using namespace uhd;
|
2011-06-15 00:25:22 +00:00
|
|
|
using namespace uhd::transport;
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
static const boost::posix_time::time_duration AUTOFLUSH_TIMEOUT(
|
|
|
|
|
boost::posix_time::milliseconds(1));
|
2012-05-30 20:33:09 +00:00
|
|
|
|
2011-06-15 00:25:22 +00:00
|
|
|
/***********************************************************************
|
|
|
|
|
* USB zero copy wrapper - managed receive buffer
|
|
|
|
|
**********************************************************************/
|
2020-03-02 23:25:13 +00:00
|
|
|
class usb_zero_copy_wrapper_mrb : public managed_recv_buffer
|
|
|
|
|
{
|
2011-06-15 00:25:22 +00:00
|
|
|
public:
|
2020-03-02 23:25:13 +00:00
|
|
|
usb_zero_copy_wrapper_mrb(void)
|
|
|
|
|
{ /*NOP*/
|
|
|
|
|
}
|
2011-06-15 00:25:22 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
void release(void)
|
|
|
|
|
{
|
|
|
|
|
_mrb.reset(); // decrement ref count, other MRB's may hold a ref
|
2012-04-23 20:45:23 +00:00
|
|
|
_claimer.release();
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
UHD_INLINE sptr get_new(managed_recv_buffer::sptr& mrb,
|
|
|
|
|
size_t& offset_bytes,
|
|
|
|
|
const double timeout,
|
|
|
|
|
size_t& index)
|
|
|
|
|
{
|
|
|
|
|
if (not mrb or not _claimer.claim_with_wait(timeout))
|
|
|
|
|
return sptr();
|
2012-04-23 20:45:23 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
index++; // advances the caller's buffer
|
2012-04-23 20:45:23 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// hold a copy of the buffer shared pointer
|
2014-08-21 08:26:42 +00:00
|
|
|
UHD_ASSERT_THROW(not _mrb);
|
2011-06-15 00:25:22 +00:00
|
|
|
_mrb = mrb;
|
2012-04-23 20:45:23 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// extract this packet's memory address and length in bytes
|
|
|
|
|
char* mem = mrb->cast<char*>() + offset_bytes;
|
|
|
|
|
const uint32_t* mem32 = reinterpret_cast<const uint32_t*>(mem);
|
|
|
|
|
const size_t words32 =
|
|
|
|
|
(uhd::wtohx(mem32[0]) & 0xffff); // length in words32 (from VRT header)
|
|
|
|
|
const size_t len = words32 * sizeof(uint32_t); // length in bytes
|
2012-04-23 20:45:23 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// check if this receive buffer has been exhausted
|
2012-04-23 20:45:23 +00:00
|
|
|
offset_bytes += len;
|
2020-03-02 23:25:13 +00:00
|
|
|
if (offset_bytes >= mrb->size())
|
|
|
|
|
mrb.reset(); // drop caller's ref
|
|
|
|
|
else if (uhd::wtohx(mem32[words32]) == 0)
|
|
|
|
|
mrb.reset();
|
2012-04-23 20:45:23 +00:00
|
|
|
|
|
|
|
|
return make(this, mem, len);
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
managed_recv_buffer::sptr _mrb;
|
2012-04-23 20:45:23 +00:00
|
|
|
simple_claimer _claimer;
|
2011-06-15 00:25:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* USB zero copy wrapper - managed send buffer
|
|
|
|
|
**********************************************************************/
|
2020-03-02 23:25:13 +00:00
|
|
|
class usb_zero_copy_wrapper_msb : public managed_send_buffer
|
|
|
|
|
{
|
2011-06-15 00:25:22 +00:00
|
|
|
public:
|
2020-03-02 23:25:13 +00:00
|
|
|
usb_zero_copy_wrapper_msb(
|
|
|
|
|
const zero_copy_if::sptr internal, const size_t fragmentation_size)
|
|
|
|
|
: _internal(internal), _fragmentation_size(fragmentation_size)
|
2012-05-30 20:33:09 +00:00
|
|
|
{
|
|
|
|
|
_ok_to_auto_flush = false;
|
uhd: Replace all occurrences of boost::bind with std::bind
Note: Replacing everything with a lambda would be even better, but that
can't be easily scripted so we'll do this as a first step to reduce the
Boost footprint.
This also removes occurences of #include <boost/bind.hpp>, and makes
sure all usages of std::bind have an #include <functional>. clang-format
wasn't always applied to minimize the changeset in this commit, however,
it was applied to the blocks of #includes.
Due to conflicts with other Boost libraries, the placeholders _1, _2,
etc. could not be directly used, but had to be explicitly called out
(as std::placeholders::_1, etc.). This makes the use of std::bind even
uglier, which serves as another reminder that using std::bind (and even
more so, boost::bind) should be avoided.
nirio/rpc/rpc_client.cpp still contains a reference to boost::bind. It
was not possible to remove it by simply doing a search and replace, so
it will be removed in a separate commit.
2019-10-16 23:21:19 +00:00
|
|
|
_task = uhd::task::make(std::bind(&usb_zero_copy_wrapper_msb::auto_flush, this));
|
2012-05-30 20:33:09 +00:00
|
|
|
}
|
2011-06-15 00:25:22 +00:00
|
|
|
|
2012-06-13 19:48:02 +00:00
|
|
|
~usb_zero_copy_wrapper_msb(void)
|
|
|
|
|
{
|
2020-03-02 23:25:13 +00:00
|
|
|
// ensure the task has exited before anything auto deconstructs
|
2012-06-13 19:48:02 +00:00
|
|
|
_task.reset();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
void release(void)
|
|
|
|
|
{
|
2012-05-30 20:33:09 +00:00
|
|
|
boost::mutex::scoped_lock lock(_mutex);
|
|
|
|
|
_ok_to_auto_flush = true;
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// get a reference to the VITA header before incrementing
|
|
|
|
|
const uint32_t vita_header =
|
|
|
|
|
reinterpret_cast<const uint32_t*>(_mem_buffer_tip)[0];
|
2012-02-02 23:15:54 +00:00
|
|
|
|
2012-04-23 20:45:23 +00:00
|
|
|
_bytes_in_buffer += size();
|
|
|
|
|
_mem_buffer_tip += size();
|
2012-02-02 23:15:54 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// extract VITA end of packet flag, we must force flush under eof conditions
|
|
|
|
|
const bool eop = (uhd::wtohx(vita_header) & (0x1 << 24)) != 0;
|
|
|
|
|
const bool full = _bytes_in_buffer
|
|
|
|
|
>= (_last_send_buff->size() - _fragmentation_size);
|
|
|
|
|
if (eop or full) {
|
2012-02-02 23:15:54 +00:00
|
|
|
_last_send_buff->commit(_bytes_in_buffer);
|
|
|
|
|
_last_send_buff.reset();
|
2012-05-30 20:33:09 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// notify the auto-flusher to restart its timed_wait
|
|
|
|
|
lock.unlock();
|
|
|
|
|
_cond.notify_one();
|
2012-02-02 23:15:54 +00:00
|
|
|
}
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
UHD_INLINE sptr get_new(const double timeout)
|
|
|
|
|
{
|
2012-05-30 20:33:09 +00:00
|
|
|
boost::mutex::scoped_lock lock(_mutex);
|
|
|
|
|
_ok_to_auto_flush = false;
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
if (not _last_send_buff) {
|
2012-02-02 23:15:54 +00:00
|
|
|
_last_send_buff = _internal->get_send_buff(timeout);
|
2020-03-02 23:25:13 +00:00
|
|
|
if (not _last_send_buff)
|
|
|
|
|
return sptr();
|
|
|
|
|
_mem_buffer_tip = _last_send_buff->cast<char*>();
|
2012-02-02 23:15:54 +00:00
|
|
|
_bytes_in_buffer = 0;
|
|
|
|
|
}
|
2012-05-30 20:33:09 +00:00
|
|
|
|
2012-04-23 20:45:23 +00:00
|
|
|
return make(this, _mem_buffer_tip, _fragmentation_size);
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2013-07-19 20:29:50 +00:00
|
|
|
zero_copy_if::sptr _internal;
|
2012-02-02 23:15:54 +00:00
|
|
|
const size_t _fragmentation_size;
|
|
|
|
|
managed_send_buffer::sptr _last_send_buff;
|
|
|
|
|
size_t _bytes_in_buffer;
|
2020-03-02 23:25:13 +00:00
|
|
|
char* _mem_buffer_tip;
|
2012-05-30 20:33:09 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// private variables for auto flusher
|
2012-05-30 20:33:09 +00:00
|
|
|
boost::mutex _mutex;
|
|
|
|
|
boost::condition_variable _cond;
|
|
|
|
|
uhd::task::sptr _task;
|
|
|
|
|
bool _ok_to_auto_flush;
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* The auto flusher ensures that buffers are force committed when
|
|
|
|
|
* the user has not called get_new() within a certain time window.
|
|
|
|
|
*/
|
|
|
|
|
void auto_flush(void)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(_mutex);
|
|
|
|
|
const bool timeout = not _cond.timed_wait(lock, AUTOFLUSH_TIMEOUT);
|
2020-03-02 23:25:13 +00:00
|
|
|
if (timeout and _ok_to_auto_flush and _last_send_buff and _bytes_in_buffer != 0) {
|
2012-05-30 20:33:09 +00:00
|
|
|
_last_send_buff->commit(_bytes_in_buffer);
|
|
|
|
|
_last_send_buff.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-15 00:25:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* USB zero copy wrapper implementation
|
|
|
|
|
**********************************************************************/
|
2020-03-02 23:25:13 +00:00
|
|
|
class usb_zero_copy_wrapper : public usb_zero_copy
|
|
|
|
|
{
|
2011-06-15 00:25:22 +00:00
|
|
|
public:
|
2020-03-02 23:25:13 +00:00
|
|
|
usb_zero_copy_wrapper(zero_copy_if::sptr usb_zc, const size_t frame_boundary)
|
|
|
|
|
: _internal_zc(usb_zc)
|
|
|
|
|
, _frame_boundary(frame_boundary)
|
|
|
|
|
, _last_recv_offset(0)
|
|
|
|
|
, _next_recv_buff_index(0)
|
2011-06-15 00:25:22 +00:00
|
|
|
{
|
2020-03-02 23:25:13 +00:00
|
|
|
for (size_t i = 0; i < this->get_num_recv_frames(); i++) {
|
2019-09-28 09:18:57 +00:00
|
|
|
_mrb_pool.push_back(std::make_shared<usb_zero_copy_wrapper_mrb>());
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|
2020-03-02 23:25:13 +00:00
|
|
|
_the_only_msb =
|
|
|
|
|
std::make_shared<usb_zero_copy_wrapper_msb>(usb_zc, frame_boundary);
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
managed_recv_buffer::sptr get_recv_buff(double timeout)
|
|
|
|
|
{
|
|
|
|
|
// lazy flush mechanism - negative timeout
|
|
|
|
|
if (timeout < 0.0) {
|
2013-02-20 05:54:30 +00:00
|
|
|
_last_recv_buff.reset();
|
2020-03-02 23:25:13 +00:00
|
|
|
while (_internal_zc->get_recv_buff()) {
|
|
|
|
|
}
|
2013-02-20 05:54:30 +00:00
|
|
|
return managed_recv_buffer::sptr();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// attempt to get a managed recv buffer
|
|
|
|
|
if (not _last_recv_buff) {
|
|
|
|
|
_last_recv_buff = _internal_zc->get_recv_buff(timeout);
|
|
|
|
|
_last_recv_offset = 0; // reset offset into buffer
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// get the buffer to be returned to the user
|
|
|
|
|
if (_next_recv_buff_index == _mrb_pool.size())
|
|
|
|
|
_next_recv_buff_index = 0;
|
2012-04-23 20:45:23 +00:00
|
|
|
return _mrb_pool[_next_recv_buff_index]->get_new(
|
2020-03-02 23:25:13 +00:00
|
|
|
_last_recv_buff, _last_recv_offset, timeout, _next_recv_buff_index);
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
size_t get_num_recv_frames(void) const
|
|
|
|
|
{
|
|
|
|
|
return (_internal_zc->get_num_recv_frames() * _internal_zc->get_recv_frame_size())
|
|
|
|
|
/ this->get_recv_frame_size();
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
size_t get_recv_frame_size(void) const
|
|
|
|
|
{
|
2012-02-15 00:34:36 +00:00
|
|
|
return std::min(_frame_boundary, _internal_zc->get_recv_frame_size());
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
managed_send_buffer::sptr get_send_buff(double timeout)
|
|
|
|
|
{
|
2012-04-23 20:45:23 +00:00
|
|
|
return _the_only_msb->get_new(timeout);
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
size_t get_num_send_frames(void) const
|
|
|
|
|
{
|
2011-06-15 00:25:22 +00:00
|
|
|
return _internal_zc->get_num_send_frames();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
size_t get_send_frame_size(void) const
|
|
|
|
|
{
|
2012-02-15 00:34:36 +00:00
|
|
|
return std::min(_frame_boundary, _internal_zc->get_send_frame_size());
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2013-07-19 20:29:50 +00:00
|
|
|
zero_copy_if::sptr _internal_zc;
|
2012-02-15 00:34:36 +00:00
|
|
|
size_t _frame_boundary;
|
2020-03-02 23:25:13 +00:00
|
|
|
std::vector<std::shared_ptr<usb_zero_copy_wrapper_mrb>> _mrb_pool;
|
2019-09-28 09:18:57 +00:00
|
|
|
std::shared_ptr<usb_zero_copy_wrapper_msb> _the_only_msb;
|
2012-02-02 23:15:54 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
// state for last recv buffer to create multiple managed buffers
|
2011-06-15 00:25:22 +00:00
|
|
|
managed_recv_buffer::sptr _last_recv_buff;
|
|
|
|
|
size_t _last_recv_offset;
|
2012-04-23 20:45:23 +00:00
|
|
|
size_t _next_recv_buff_index;
|
2011-06-15 00:25:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* USB zero copy wrapper factory function
|
|
|
|
|
**********************************************************************/
|
2013-07-19 20:29:50 +00:00
|
|
|
zero_copy_if::sptr usb_zero_copy_make_wrapper(
|
2020-03-02 23:25:13 +00:00
|
|
|
zero_copy_if::sptr usb_zc, size_t usb_frame_boundary)
|
|
|
|
|
{
|
2013-07-19 20:29:50 +00:00
|
|
|
return zero_copy_if::sptr(new usb_zero_copy_wrapper(usb_zc, usb_frame_boundary));
|
2011-06-15 00:25:22 +00:00
|
|
|
}
|