diff options
Diffstat (limited to 'host/lib/utils/tasks.cpp')
-rw-r--r-- | host/lib/utils/tasks.cpp | 63 |
1 files changed, 31 insertions, 32 deletions
diff --git a/host/lib/utils/tasks.cpp b/host/lib/utils/tasks.cpp index 661315ae8..fb9a3052e 100644 --- a/host/lib/utils/tasks.cpp +++ b/host/lib/utils/tasks.cpp @@ -17,70 +17,69 @@ #include <uhd/utils/tasks.hpp> #include <uhd/utils/msg_task.hpp> -#include <uhd/utils/msg.hpp> +#include <uhd/utils/thread.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; class task_impl : public task{ public: - task_impl(const task_fcn_type &task_fcn): - _spawn_barrier(2) + task_impl(const task_fcn_type &task_fcn, const std::string &name): + _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); }); + if (not name.empty()) { +#ifdef HAVE_PTHREAD_SETNAME + pthread_setname_np(_task->native_handle(), name.substr(0,16).c_str()); +#endif /* HAVE_PTHREAD_SETNAME */ + } } ~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_MSG(error) - << "An unexpected exception was caught in a task loop." << std::endl - << "The task loop will now exit, things may not work." << std::endl - << msg << std::endl + UHD_LOGGER_ERROR("UHD") + << "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){ - return task::sptr(new task_impl(task_fcn)); +task::sptr task::make(const task_fcn_type &task_fcn, const std::string &name){ + return task::sptr(new task_impl(task_fcn, name)); } msg_task::~msg_task(void){ @@ -162,10 +161,10 @@ private: } void do_error_msg(const std::string &msg){ - UHD_MSG(error) - << "An unexpected exception was caught in a task loop." << std::endl - << "The task loop will now exit, things may not work." << std::endl - << msg << std::endl + UHD_LOGGER_ERROR("UHD") + << "An unexpected exception was caught in a task loop." + << "The task loop will now exit, things may not work." + << msg ; } |