aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/utils/tasks.cpp
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2017-04-21 22:33:16 -0700
committerMartin Braun <martin.braun@ettus.com>2017-06-28 15:54:39 -0700
commitcb1649d201e41c85ed77256712309706ed1a805d (patch)
tree1aaa11ac2bba1eb95edc9303b3ad42fc254913d5 /host/lib/utils/tasks.cpp
parentf19e4602f10fb86dceb4d65d75741f98a054a7df (diff)
downloaduhd-cb1649d201e41c85ed77256712309706ed1a805d.tar.gz
uhd-cb1649d201e41c85ed77256712309706ed1a805d.tar.bz2
uhd-cb1649d201e41c85ed77256712309706ed1a805d.zip
uhd: tasks now use std::threads under the hood, and can't be interrupted
USRP1 and USRP2 used tasks that relied on Boost thread interruption mechanisms. These were replaced with explicit atomics.
Diffstat (limited to 'host/lib/utils/tasks.cpp')
-rw-r--r--host/lib/utils/tasks.cpp38
1 files changed, 16 insertions, 22 deletions
diff --git a/host/lib/utils/tasks.cpp b/host/lib/utils/tasks.cpp
index 5dac729c8..38d19502e 100644
--- a/host/lib/utils/tasks.cpp
+++ b/host/lib/utils/tasks.cpp
@@ -18,11 +18,14 @@
#include <uhd/utils/tasks.hpp>
#include <uhd/utils/msg_task.hpp>
#include <uhd/utils/log.hpp>
+#include <uhd/exception.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/barrier.hpp>
#include <exception>
#include <iostream>
#include <vector>
+#include <thread>
+#include <atomic>
using namespace uhd;
@@ -30,53 +33,44 @@ class task_impl : public task{
public:
task_impl(const task_fcn_type &task_fcn):
- _spawn_barrier(2)
+ _exit(false)
{
- (void)_thread_group.create_thread(boost::bind(&task_impl::task_loop, this, task_fcn));
- _spawn_barrier.wait();
+ _task = std::thread([this, task_fcn](){ this->task_loop(task_fcn); });
}
~task_impl(void){
- _running = false;
- _thread_group.interrupt_all();
- _thread_group.join_all();
+ _exit = true;
+ if (_task.joinable()) {
+ _task.join();
+ }
}
private:
void task_loop(const task_fcn_type &task_fcn){
- _running = true;
- _spawn_barrier.wait();
-
try{
- while (_running){
+ while (!_exit){
task_fcn();
}
}
- catch(const boost::thread_interrupted &){
- //this is an ok way to exit the task loop
- }
catch(const std::exception &e){
do_error_msg(e.what());
}
catch(...){
- //FIXME
- //Unfortunately, this is also an ok way to end a task,
- //because on some systems boost throws uncatchables.
+ UHD_THROW_INVALID_CODE_PATH();
}
}
void do_error_msg(const std::string &msg){
UHD_LOGGER_ERROR("UHD")
- << "An unexpected exception was caught in a task loop."
- << "The task loop will now exit, things may not work."
- << msg
+ << "An unexpected exception was caught in a task loop."
+ << "The task loop will now exit, things may not work."
+ << msg
;
}
- boost::thread_group _thread_group;
- boost::barrier _spawn_barrier;
- bool _running;
+ std::atomic<bool> _exit;
+ std::thread _task;
};
task::sptr task::make(const task_fcn_type &task_fcn){