2011-07-14 00:25:40 +00:00
|
|
|
//
|
2014-08-13 15:44:31 +00:00
|
|
|
// Copyright 2011,2014 Ettus Research LLC
|
2018-02-19 23:30:32 +00:00
|
|
|
// Copyright 2018 Ettus Research, a National Instruments Company
|
2011-07-14 00:25:40 +00:00
|
|
|
//
|
2018-02-19 23:30:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2011-07-14 00:25:40 +00:00
|
|
|
//
|
|
|
|
|
|
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/exception.hpp>
|
|
|
|
|
#include <uhd/utils/log.hpp>
|
2013-11-05 21:56:00 +00:00
|
|
|
#include <uhd/utils/msg_task.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/utils/tasks.hpp>
|
2017-04-18 23:46:44 +00:00
|
|
|
#include <uhd/utils/thread.hpp>
|
2011-07-14 00:25:40 +00:00
|
|
|
#include <boost/thread/barrier.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/thread.hpp>
|
|
|
|
|
#include <atomic>
|
2011-07-14 00:25:40 +00:00
|
|
|
#include <exception>
|
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 <functional>
|
2011-07-14 00:25:40 +00:00
|
|
|
#include <iostream>
|
2017-04-22 05:33:16 +00:00
|
|
|
#include <thread>
|
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 <vector>
|
2011-07-14 00:25:40 +00:00
|
|
|
|
|
|
|
|
using namespace uhd;
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
class task_impl : public task
|
|
|
|
|
{
|
2011-07-14 00:25:40 +00:00
|
|
|
public:
|
2020-03-02 23:25:13 +00:00
|
|
|
task_impl(const task_fcn_type& task_fcn, const std::string& name) : _exit(false)
|
2011-07-26 18:15:04 +00:00
|
|
|
{
|
2020-03-02 23:25:13 +00:00
|
|
|
_task = std::thread([this, task_fcn]() { this->task_loop(task_fcn); });
|
2017-04-18 23:46:44 +00:00
|
|
|
if (not name.empty()) {
|
2019-11-22 18:52:55 +00:00
|
|
|
set_thread_name(&_task, name);
|
2017-04-18 23:46:44 +00:00
|
|
|
}
|
2011-07-14 00:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
~task_impl(void)
|
|
|
|
|
{
|
2017-04-22 05:33:16 +00:00
|
|
|
_exit = true;
|
|
|
|
|
if (_task.joinable()) {
|
|
|
|
|
_task.join();
|
|
|
|
|
}
|
2011-07-14 00:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2020-03-02 23:25:13 +00:00
|
|
|
void task_loop(const task_fcn_type& task_fcn)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
while (!_exit) {
|
2011-07-14 00:25:40 +00:00
|
|
|
task_fcn();
|
|
|
|
|
}
|
2020-03-02 23:25:13 +00:00
|
|
|
} catch (const std::exception& e) {
|
2011-07-14 00:25:40 +00:00
|
|
|
do_error_msg(e.what());
|
2020-03-02 23:25:13 +00:00
|
|
|
} catch (...) {
|
2017-04-22 05:33:16 +00:00
|
|
|
UHD_THROW_INVALID_CODE_PATH();
|
2011-07-14 00:25:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
void do_error_msg(const std::string& msg)
|
|
|
|
|
{
|
2017-02-08 00:37:25 +00:00
|
|
|
UHD_LOGGER_ERROR("UHD")
|
2017-04-22 05:33:16 +00:00
|
|
|
<< "An unexpected exception was caught in a task loop."
|
2020-03-02 23:25:13 +00:00
|
|
|
<< "The task loop will now exit, things may not work." << msg;
|
2011-07-14 00:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
2017-04-22 05:33:16 +00:00
|
|
|
std::atomic<bool> _exit;
|
|
|
|
|
std::thread _task;
|
2011-07-14 00:25:40 +00:00
|
|
|
};
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
task::sptr task::make(const task_fcn_type& task_fcn, const std::string& name)
|
|
|
|
|
{
|
2017-04-18 23:46:44 +00:00
|
|
|
return task::sptr(new task_impl(task_fcn, name));
|
2011-07-14 00:25:40 +00:00
|
|
|
}
|
2013-11-05 21:56:00 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
msg_task::~msg_task(void)
|
|
|
|
|
{
|
2014-08-13 15:44:31 +00:00
|
|
|
/* NOP */
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-05 21:56:00 +00:00
|
|
|
/*
|
|
|
|
|
* During shutdown pointers to queues for radio_ctrl_core might not be available anymore.
|
|
|
|
|
* msg_task_impl provides a dump_queue for such messages.
|
|
|
|
|
* ctrl_cores can check this queue for stranded messages.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
class msg_task_impl : public msg_task
|
|
|
|
|
{
|
2013-11-05 21:56:00 +00:00
|
|
|
public:
|
2020-03-02 23:25:13 +00:00
|
|
|
msg_task_impl(const task_fcn_type& task_fcn) : _spawn_barrier(2)
|
2013-11-05 21:56:00 +00:00
|
|
|
{
|
2020-03-02 23:25:13 +00:00
|
|
|
(void)_thread_group.create_thread(
|
|
|
|
|
std::bind(&msg_task_impl::task_loop, this, task_fcn));
|
2013-11-05 21:56:00 +00:00
|
|
|
_spawn_barrier.wait();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
~msg_task_impl(void)
|
|
|
|
|
{
|
2013-11-05 21:56:00 +00:00
|
|
|
_running = false;
|
|
|
|
|
_thread_group.interrupt_all();
|
|
|
|
|
_thread_group.join_all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Returns the first message for the given SID.
|
2020-03-02 23:25:13 +00:00
|
|
|
* This way a radio_ctrl_core doesn't have to die in timeout but can check for
|
|
|
|
|
* stranded messages here. This might happen during shutdown when dtors are called.
|
2013-11-05 21:56:00 +00:00
|
|
|
* See also: comments in b200_io_impl->handle_async_task
|
|
|
|
|
*/
|
2016-10-31 21:30:52 +00:00
|
|
|
msg_payload_t get_msg_from_dump_queue(uint32_t sid)
|
2013-11-05 21:56:00 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(_mutex);
|
|
|
|
|
msg_payload_t b;
|
|
|
|
|
for (size_t i = 0; i < _dump_queue.size(); i++) {
|
|
|
|
|
if (sid == _dump_queue[i].first) {
|
|
|
|
|
b = _dump_queue[i].second;
|
|
|
|
|
_dump_queue.erase(_dump_queue.begin() + i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2020-03-02 23:25:13 +00:00
|
|
|
void task_loop(const task_fcn_type& task_fcn)
|
|
|
|
|
{
|
2013-11-05 21:56:00 +00:00
|
|
|
_running = true;
|
|
|
|
|
_spawn_barrier.wait();
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
try {
|
|
|
|
|
while (_running) {
|
|
|
|
|
boost::optional<msg_type_t> buff = task_fcn();
|
|
|
|
|
if (buff != boost::none) {
|
|
|
|
|
/*
|
|
|
|
|
* If a message gets stranded it is returned by task_fcn and then
|
|
|
|
|
* pushed to the dump_queue. This way ctrl_cores can check dump_queue
|
|
|
|
|
* for missing messages.
|
|
|
|
|
*/
|
|
|
|
|
boost::mutex::scoped_lock lock(_mutex);
|
|
|
|
|
_dump_queue.push_back(buff.get());
|
|
|
|
|
}
|
2013-11-05 21:56:00 +00:00
|
|
|
}
|
2020-03-02 23:25:13 +00:00
|
|
|
} catch (const boost::thread_interrupted&) {
|
|
|
|
|
// this is an ok way to exit the task loop
|
|
|
|
|
} catch (const std::exception& e) {
|
2013-11-05 21:56:00 +00:00
|
|
|
do_error_msg(e.what());
|
2020-03-02 23:25:13 +00:00
|
|
|
} catch (...) {
|
|
|
|
|
// FIXME
|
|
|
|
|
// Unfortunately, this is also an ok way to end a task,
|
|
|
|
|
// because on some systems boost throws uncatchables.
|
2013-11-05 21:56:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
void do_error_msg(const std::string& msg)
|
|
|
|
|
{
|
2017-02-08 00:37:25 +00:00
|
|
|
UHD_LOGGER_ERROR("UHD")
|
2020-03-02 23:25:13 +00:00
|
|
|
<< "An unexpected exception was caught in a task loop."
|
|
|
|
|
<< "The task loop will now exit, things may not work." << msg;
|
2013-11-05 21:56:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boost::mutex _mutex;
|
|
|
|
|
boost::thread_group _thread_group;
|
|
|
|
|
boost::barrier _spawn_barrier;
|
|
|
|
|
bool _running;
|
|
|
|
|
|
|
|
|
|
/*
|
2020-03-02 23:25:13 +00:00
|
|
|
* This queue holds stranded messages until a radio_ctrl_core grabs them via
|
|
|
|
|
* 'get_msg_from_dump_queue'.
|
2013-11-05 21:56:00 +00:00
|
|
|
*/
|
2020-03-02 23:25:13 +00:00
|
|
|
std::vector<msg_type_t> _dump_queue;
|
2013-11-05 21:56:00 +00:00
|
|
|
};
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
msg_task::sptr msg_task::make(const task_fcn_type& task_fcn)
|
|
|
|
|
{
|
2013-11-05 21:56:00 +00:00
|
|
|
return msg_task::sptr(new msg_task_impl(task_fcn));
|
|
|
|
|
}
|