From c33928d2bbdd27688c3475e77fc461e7d16eba5a Mon Sep 17 00:00:00 2001 From: Andrej Rode Date: Tue, 18 Apr 2017 16:46:44 -0700 Subject: utils: add set_thread_name API call, move thread_priority to thread --- host/examples/benchmark_rate.cpp | 11 +- host/examples/gpio.cpp | 2 +- host/examples/init_usrp/init_usrp.cpp | 2 +- host/examples/latency_test.cpp | 2 +- host/examples/network_relay.cpp | 2 +- host/examples/rx_ascii_art_dft.cpp | 2 +- host/examples/rx_multi_samples.cpp | 2 +- host/examples/rx_samples_to_file.cpp | 2 +- host/examples/rx_samples_to_udp.cpp | 2 +- host/examples/rx_timed_samples.cpp | 2 +- host/examples/sync_to_gps.cpp | 2 +- host/examples/test_clock_synch.cpp | 2 +- host/examples/test_dboard_coercion.cpp | 2 +- host/examples/test_messages.cpp | 2 +- host/examples/test_pps_input.cpp | 2 +- host/examples/test_timed_commands.cpp | 2 +- host/examples/twinrx_freq_hopping.cpp | 2 +- host/examples/tx_bursts.cpp | 2 +- host/examples/tx_samples_from_file.cpp | 2 +- host/examples/tx_timed_samples.cpp | 2 +- host/examples/tx_waveforms.cpp | 2 +- host/examples/txrx_loopback_to_file.cpp | 2 +- host/include/uhd/utils/CMakeLists.txt | 1 + host/include/uhd/utils/tasks.hpp | 6 +- host/include/uhd/utils/thread.hpp | 70 +++++++++++++ host/include/uhd/utils/thread_priority.hpp | 42 +------- host/lib/transport/super_send_packet_handler.hpp | 1 + host/lib/transport/zero_copy_recv_offload.cpp | 2 + host/lib/usrp/common/fifo_ctrl_excelsior.cpp | 2 +- host/lib/usrp/device3/device3_io_impl.cpp | 3 +- host/lib/usrp/usrp2/io_impl.cpp | 2 +- host/lib/usrp/x300/x300_impl.cpp | 2 +- host/lib/utils/CMakeLists.txt | 36 ++++++- host/lib/utils/tasks.cpp | 13 ++- host/lib/utils/thread.cpp | 122 +++++++++++++++++++++++ host/lib/utils/thread_priority.cpp | 110 -------------------- host/lib/utils/thread_priority_c.cpp | 2 +- host/utils/latency/lib/Responder.cpp | 2 +- host/utils/query_gpsdo_sensors.cpp | 2 +- host/utils/uhd_cal_rx_iq_balance.cpp | 2 +- host/utils/uhd_cal_tx_dc_offset.cpp | 2 +- host/utils/uhd_cal_tx_iq_balance.cpp | 2 +- 42 files changed, 285 insertions(+), 192 deletions(-) create mode 100644 host/include/uhd/utils/thread.hpp create mode 100644 host/lib/utils/thread.cpp delete mode 100644 host/lib/utils/thread_priority.cpp (limited to 'host') diff --git a/host/examples/benchmark_rate.cpp b/host/examples/benchmark_rate.cpp index 80fc0be6c..cb2cf251c 100644 --- a/host/examples/benchmark_rate.cpp +++ b/host/examples/benchmark_rate.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include +#include #include #include #include @@ -451,7 +451,8 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ uhd::stream_args_t stream_args(rx_cpu, rx_otw); stream_args.channels = rx_channel_nums; uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args); - thread_group.create_thread(boost::bind(&benchmark_rx_rate, usrp, rx_cpu, rx_stream, random_nsamps, boost::ref(burst_timer_elapsed))); + auto rx_thread = thread_group.create_thread(boost::bind(&benchmark_rx_rate, usrp, rx_cpu, rx_stream, random_nsamps, boost::ref(burst_timer_elapsed))); + uhd::set_thread_name(rx_thread, "bmark_rx_stream"); } //spawn the transmit test thread @@ -461,8 +462,10 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ uhd::stream_args_t stream_args(tx_cpu, tx_otw); stream_args.channels = tx_channel_nums; uhd::tx_streamer::sptr tx_stream = usrp->get_tx_stream(stream_args); - thread_group.create_thread(boost::bind(&benchmark_tx_rate, usrp, tx_cpu, tx_stream, boost::ref(burst_timer_elapsed), random_nsamps)); - thread_group.create_thread(boost::bind(&benchmark_tx_rate_async_helper, tx_stream, boost::ref(burst_timer_elapsed))); + auto tx_thread = thread_group.create_thread(boost::bind(&benchmark_tx_rate, usrp, tx_cpu, tx_stream, boost::ref(burst_timer_elapsed), random_nsamps)); + uhd::set_thread_name(tx_thread, "bmark_tx_stream"); + auto tx_async_thread = thread_group.create_thread(boost::bind(&benchmark_tx_rate_async_helper, tx_stream, boost::ref(burst_timer_elapsed))); + uhd::set_thread_name(tx_async_thread, "bmark_tx_helper"); } //sleep for the required duration diff --git a/host/examples/gpio.cpp b/host/examples/gpio.cpp index e47b2ab27..b3edc81d3 100644 --- a/host/examples/gpio.cpp +++ b/host/examples/gpio.cpp @@ -73,7 +73,7 @@ // mask - a mask indicating which bits in the specified attribute register are // to be changed (default is all bits). -#include +#include #include #include #include diff --git a/host/examples/init_usrp/init_usrp.cpp b/host/examples/init_usrp/init_usrp.cpp index c0a211767..46650a583 100644 --- a/host/examples/init_usrp/init_usrp.cpp +++ b/host/examples/init_usrp/init_usrp.cpp @@ -20,7 +20,7 @@ // The program itself only initializes a USRP. For more elaborate examples, // have a look at the files in host/examples/. -#include +#include #include #include #include diff --git a/host/examples/latency_test.cpp b/host/examples/latency_test.cpp index da06086f9..7282e70f5 100644 --- a/host/examples/latency_test.cpp +++ b/host/examples/latency_test.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include +#include #include #include #include diff --git a/host/examples/network_relay.cpp b/host/examples/network_relay.cpp index 7e354934a..c21b2696e 100644 --- a/host/examples/network_relay.cpp +++ b/host/examples/network_relay.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include +#include #include #include #include diff --git a/host/examples/rx_ascii_art_dft.cpp b/host/examples/rx_ascii_art_dft.cpp index 55b4eeebe..d8733ddd0 100644 --- a/host/examples/rx_ascii_art_dft.cpp +++ b/host/examples/rx_ascii_art_dft.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include +#include #include #include #include "ascii_art_dft.hpp" //implementation diff --git a/host/examples/rx_multi_samples.cpp b/host/examples/rx_multi_samples.cpp index 01c7332cd..9212ba4b0 100644 --- a/host/examples/rx_multi_samples.cpp +++ b/host/examples/rx_multi_samples.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include +#include #include #include #include diff --git a/host/examples/rx_samples_to_file.cpp b/host/examples/rx_samples_to_file.cpp index 444fd155c..f3de9f5de 100644 --- a/host/examples/rx_samples_to_file.cpp +++ b/host/examples/rx_samples_to_file.cpp @@ -16,7 +16,7 @@ // #include -#include +#include #include #include #include diff --git a/host/examples/rx_samples_to_udp.cpp b/host/examples/rx_samples_to_udp.cpp index a3f5624ca..409e40a56 100644 --- a/host/examples/rx_samples_to_udp.cpp +++ b/host/examples/rx_samples_to_udp.cpp @@ -16,7 +16,7 @@ // #include -#include +#include #include #include #include diff --git a/host/examples/rx_timed_samples.cpp b/host/examples/rx_timed_samples.cpp index a0f7e2821..4b348e672 100644 --- a/host/examples/rx_timed_samples.cpp +++ b/host/examples/rx_timed_samples.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include +#include #include #include #include diff --git a/host/examples/sync_to_gps.cpp b/host/examples/sync_to_gps.cpp index 3a9b5c7e4..006574cff 100644 --- a/host/examples/sync_to_gps.cpp +++ b/host/examples/sync_to_gps.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include +#include #include #include #include diff --git a/host/examples/test_clock_synch.cpp b/host/examples/test_clock_synch.cpp index b1af6790b..1756cdbb9 100644 --- a/host/examples/test_clock_synch.cpp +++ b/host/examples/test_clock_synch.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include namespace po = boost::program_options; diff --git a/host/examples/test_dboard_coercion.cpp b/host/examples/test_dboard_coercion.cpp index 0119f39cd..307728214 100644 --- a/host/examples/test_dboard_coercion.cpp +++ b/host/examples/test_dboard_coercion.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include +#include #include #include #include diff --git a/host/examples/test_messages.cpp b/host/examples/test_messages.cpp index 8a50e4c85..5047db0c5 100644 --- a/host/examples/test_messages.cpp +++ b/host/examples/test_messages.cpp @@ -16,7 +16,7 @@ // #include -#include +#include #include #include #include diff --git a/host/examples/test_pps_input.cpp b/host/examples/test_pps_input.cpp index 3e6c4ba9d..ef166f31c 100644 --- a/host/examples/test_pps_input.cpp +++ b/host/examples/test_pps_input.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include +#include #include #include #include diff --git a/host/examples/test_timed_commands.cpp b/host/examples/test_timed_commands.cpp index 3da4bc707..8c6e039dc 100644 --- a/host/examples/test_timed_commands.cpp +++ b/host/examples/test_timed_commands.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include +#include #include #include #include diff --git a/host/examples/twinrx_freq_hopping.cpp b/host/examples/twinrx_freq_hopping.cpp index 878129d92..1e9000b76 100644 --- a/host/examples/twinrx_freq_hopping.cpp +++ b/host/examples/twinrx_freq_hopping.cpp @@ -18,7 +18,7 @@ // FFT conversion #include "ascii_art_dft.hpp" -#include +#include #include #include diff --git a/host/examples/tx_bursts.cpp b/host/examples/tx_bursts.cpp index d174f4d6f..406b76c41 100644 --- a/host/examples/tx_bursts.cpp +++ b/host/examples/tx_bursts.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include +#include #include #include #include diff --git a/host/examples/tx_samples_from_file.cpp b/host/examples/tx_samples_from_file.cpp index b09efe454..7ef6ecfe9 100644 --- a/host/examples/tx_samples_from_file.cpp +++ b/host/examples/tx_samples_from_file.cpp @@ -16,7 +16,7 @@ // #include -#include +#include #include #include #include diff --git a/host/examples/tx_timed_samples.cpp b/host/examples/tx_timed_samples.cpp index 667ae66a9..a8f4acfeb 100644 --- a/host/examples/tx_timed_samples.cpp +++ b/host/examples/tx_timed_samples.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include +#include #include #include #include diff --git a/host/examples/tx_waveforms.cpp b/host/examples/tx_waveforms.cpp index 9e3c2cec6..4e824cf43 100644 --- a/host/examples/tx_waveforms.cpp +++ b/host/examples/tx_waveforms.cpp @@ -16,7 +16,7 @@ // #include "wavetable.hpp" -#include +#include #include #include #include diff --git a/host/examples/txrx_loopback_to_file.cpp b/host/examples/txrx_loopback_to_file.cpp index b0907f162..fedaed7da 100644 --- a/host/examples/txrx_loopback_to_file.cpp +++ b/host/examples/txrx_loopback_to_file.cpp @@ -17,7 +17,7 @@ #include "wavetable.hpp" #include -#include +#include #include #include #include diff --git a/host/include/uhd/utils/CMakeLists.txt b/host/include/uhd/utils/CMakeLists.txt index cf5bb8aa8..e176f2c77 100644 --- a/host/include/uhd/utils/CMakeLists.txt +++ b/host/include/uhd/utils/CMakeLists.txt @@ -39,6 +39,7 @@ UHD_INSTALL(FILES static.hpp tasks.hpp thread_priority.hpp + thread.hpp DESTINATION ${INCLUDE_DIR}/uhd/utils COMPONENT headers ) diff --git a/host/include/uhd/utils/tasks.hpp b/host/include/uhd/utils/tasks.hpp index 9b17ae08a..94bc508a1 100644 --- a/host/include/uhd/utils/tasks.hpp +++ b/host/include/uhd/utils/tasks.hpp @@ -40,9 +40,13 @@ namespace uhd{ * It may not block, or the destructor will also block. * * \param task_fcn the task callback function + * \param name Task name. Will be used as a thread name. * \return a new task object */ - static sptr make(const task_fcn_type &task_fcn); + static sptr make( + const task_fcn_type &task_fcn, + const std::string &name="" + ); }; } //namespace uhd diff --git a/host/include/uhd/utils/thread.hpp b/host/include/uhd/utils/thread.hpp new file mode 100644 index 000000000..23268fa01 --- /dev/null +++ b/host/include/uhd/utils/thread.hpp @@ -0,0 +1,70 @@ +// +// Copyright 2010,2017 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#ifndef INCLUDED_UHD_UTILS_THREAD_HPP +#define INCLUDED_UHD_UTILS_THREAD_HPP + +#include +#include +#include + +namespace uhd{ + + static const float default_thread_priority = float(0.5); + + /*! + * Set the scheduling priority on the current thread. + * + * A new thread or calling process should make this call + * with the defaults this to enable realtime scheduling. + * + * A priority of zero corresponds to normal priority. + * Positive priority values are higher than normal. + * Negative priority values are lower than normal. + * + * \param priority a value between -1 and 1 + * \param realtime true to use realtime mode + * \throw exception on set priority failure + */ + UHD_API void set_thread_priority( + float priority = default_thread_priority, + bool realtime = true + ); + + /*! + * Set the scheduling priority on the current thread. + * Same as set_thread_priority but does not throw on failure. + * \return true on success, false on failure + */ + UHD_API bool set_thread_priority_safe( + float priority = default_thread_priority, + bool realtime = true + ); + + /*! + * Set the thread name on the given boost thread. + * \param thread pointer to a boost thread + * \param name thread name with maximum length of 16 characters + */ + UHD_API void set_thread_name( + boost::thread *thread, + const std::string &name + ); + +} //namespace uhd + +#endif /* INCLUDED_UHD_UTILS_THREAD_HPP */ diff --git a/host/include/uhd/utils/thread_priority.hpp b/host/include/uhd/utils/thread_priority.hpp index c34ac5542..e9012c742 100644 --- a/host/include/uhd/utils/thread_priority.hpp +++ b/host/include/uhd/utils/thread_priority.hpp @@ -1,5 +1,5 @@ // -// Copyright 2010 Ettus Research LLC +// Copyright 2010,2017 Ettus Research LLC // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -17,42 +17,6 @@ #ifndef INCLUDED_UHD_UTILS_THREAD_PRIORITY_HPP #define INCLUDED_UHD_UTILS_THREAD_PRIORITY_HPP - -#include - -namespace uhd{ - - static const float default_thread_priority = float(0.5); - - /*! - * Set the scheduling priority on the current thread. - * - * A new thread or calling process should make this call - * with the defaults this to enable realtime scheduling. - * - * A priority of zero corresponds to normal priority. - * Positive priority values are higher than normal. - * Negative priority values are lower than normal. - * - * \param priority a value between -1 and 1 - * \param realtime true to use realtime mode - * \throw exception on set priority failure - */ - UHD_API void set_thread_priority( - float priority = default_thread_priority, - bool realtime = true - ); - - /*! - * Set the scheduling priority on the current thread. - * Same as set_thread_priority but does not throw on failure. - * \return true on success, false on failure - */ - UHD_API bool set_thread_priority_safe( - float priority = default_thread_priority, - bool realtime = true - ); - -} //namespace uhd - +#pragma message "This header is deprecated - please use instead." +#include #endif /* INCLUDED_UHD_UTILS_THREAD_PRIORITY_HPP */ diff --git a/host/lib/transport/super_send_packet_handler.hpp b/host/lib/transport/super_send_packet_handler.hpp index 12c053efb..75ba7260b 100644 --- a/host/lib/transport/super_send_packet_handler.hpp +++ b/host/lib/transport/super_send_packet_handler.hpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include diff --git a/host/lib/transport/zero_copy_recv_offload.cpp b/host/lib/transport/zero_copy_recv_offload.cpp index 5de2cd44d..e8f75be84 100644 --- a/host/lib/transport/zero_copy_recv_offload.cpp +++ b/host/lib/transport/zero_copy_recv_offload.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -54,6 +55,7 @@ public: _recv_thread = boost::thread( boost::bind(&zero_copy_recv_offload_impl::enqueue_recv, this) ); + set_thread_name(&_recv_thread, "zero_copy_recv"); } // Receive thread flags diff --git a/host/lib/usrp/common/fifo_ctrl_excelsior.cpp b/host/lib/usrp/common/fifo_ctrl_excelsior.cpp index c86380354..0a93f44e8 100644 --- a/host/lib/usrp/common/fifo_ctrl_excelsior.cpp +++ b/host/lib/usrp/common/fifo_ctrl_excelsior.cpp @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/host/lib/usrp/device3/device3_io_impl.cpp b/host/lib/usrp/device3/device3_io_impl.cpp index 374232972..9795afae5 100644 --- a/host/lib/usrp/device3/device3_io_impl.cpp +++ b/host/lib/usrp/device3/device3_io_impl.cpp @@ -849,7 +849,8 @@ tx_streamer::sptr device3_impl::get_tx_stream(const uhd::stream_args_t &args_) my_streamer->_async_xport.recv, xport.endianness, tick_rate_retriever - ) + ), + "tx_async_msgs_task" ); blk_ctrl->sr_write(uhd::rfnoc::SR_CLEAR_RX_FC, 0xc1ea12, block_port); diff --git a/host/lib/usrp/usrp2/io_impl.cpp b/host/lib/usrp/usrp2/io_impl.cpp index 8f6a67453..967b53c88 100644 --- a/host/lib/usrp/usrp2/io_impl.cpp +++ b/host/lib/usrp/usrp2/io_impl.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/host/lib/usrp/x300/x300_impl.cpp b/host/lib/usrp/x300/x300_impl.cpp index 40288b54c..1c8eeab51 100644 --- a/host/lib/usrp/x300/x300_impl.cpp +++ b/host/lib/usrp/x300/x300_impl.cpp @@ -712,7 +712,7 @@ void x300_impl::setup_mb(const size_t mb_i, const uhd::device_addr_t &dev_addr) if (not try_to_claim(mb.zpu_ctrl)) { throw uhd::runtime_error("Failed to claim device"); } - mb.claimer_task = uhd::task::make(boost::bind(&x300_impl::claimer_loop, this, mb.zpu_ctrl)); + mb.claimer_task = uhd::task::make(boost::bind(&x300_impl::claimer_loop, this, mb.zpu_ctrl), "x300_claimer"); //extract the FW path for the X300 //and live load fw over ethernet link diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt index f76c5763a..a053846c6 100644 --- a/host/lib/utils/CMakeLists.txt +++ b/host/lib/utils/CMakeLists.txt @@ -51,9 +51,11 @@ CHECK_CXX_SOURCE_COMPILES(" " HAVE_WIN_SETTHREADPRIORITY ) + + IF(HAVE_PTHREAD_SETSCHEDPARAM) MESSAGE(STATUS " Priority scheduling supported through pthread_setschedparam.") - SET(THREAD_PRIO_DEFS HAVE_PTHREAD_SETSCHEDPARAM) + LIST(APPEND THREAD_PRIO_DEFS HAVE_PTHREAD_SETSCHEDPARAM) LIBUHD_APPEND_LIBS(pthread) ELSEIF(HAVE_WIN_SETTHREADPRIORITY) MESSAGE(STATUS " Priority scheduling supported through windows SetThreadPriority.") @@ -63,8 +65,36 @@ ELSE() SET(THREAD_PRIO_DEFS HAVE_THREAD_PRIO_DUMMY) ENDIF() +SET(CMAKE_REQUIRED_LIBRARIES "pthread") + +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){ + pthread_t pt; + const char* pt_name = \"test\"; + pthread_setname_np(pt, pt_name); + return 0; + } + " HAVE_PTHREAD_SETNAME +) + +IF(CYGWIN) + #SCHED_RR non-operational on cygwin + SET(HAVE_PTHREAD_SETNAME False) +ENDIF(CYGWIN) + +IF(HAVE_PTHREAD_SETNAME) + MESSAGE(STATUS " Setting thread names is supported through pthread_setname_np.") + LIST(APPEND THREAD_PRIO_DEFS HAVE_PTHREAD_SETNAME) + LIBUHD_APPEND_LIBS(pthread) +ELSE() + MESSAGE(STATUS " Setting thread names is not supported.") + LIST(APPEND THREAD_PRIO_DEFS HAVE_THREAD_SETNAME_DUMMY) +ENDIF() + + SET_SOURCE_FILES_PROPERTIES( - ${CMAKE_CURRENT_SOURCE_DIR}/thread_priority.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/thread.cpp PROPERTIES COMPILE_DEFINITIONS "${THREAD_PRIO_DEFS}" ) @@ -146,7 +176,7 @@ LIBUHD_APPEND_SOURCES( ${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp ${CMAKE_CURRENT_SOURCE_DIR}/static.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tasks.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/thread_priority.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/thread.cpp ) IF(ENABLE_C_API) diff --git a/host/lib/utils/tasks.cpp b/host/lib/utils/tasks.cpp index 38d19502e..fb9a3052e 100644 --- a/host/lib/utils/tasks.cpp +++ b/host/lib/utils/tasks.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -32,10 +33,15 @@ using namespace uhd; class task_impl : public task{ public: - task_impl(const task_fcn_type &task_fcn): + task_impl(const task_fcn_type &task_fcn, const std::string &name): _exit(false) { _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){ @@ -46,7 +52,6 @@ public: } private: - void task_loop(const task_fcn_type &task_fcn){ try{ while (!_exit){ @@ -73,8 +78,8 @@ private: 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){ diff --git a/host/lib/utils/thread.cpp b/host/lib/utils/thread.cpp new file mode 100644 index 000000000..9100cfd7b --- /dev/null +++ b/host/lib/utils/thread.cpp @@ -0,0 +1,122 @@ +// +// Copyright 2010-2011,2015 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#include +#include +#include +#include +#include + +bool uhd::set_thread_priority_safe(float priority, bool realtime){ + try{ + set_thread_priority(priority, realtime); + return true; + }catch(const std::exception &e){ + UHD_LOGGER_WARNING("UHD") << boost::format( + "Unable to set the thread priority. Performance may be negatively affected.\n" + "Please see the general application notes in the manual for instructions.\n" + "%s" + ) % e.what(); + return false; + } +} + +static void check_priority_range(float priority){ + if (priority > +1.0 or priority < -1.0) + throw uhd::value_error("priority out of range [-1.0, +1.0]"); +} + +/*********************************************************************** + * Pthread API to set priority + **********************************************************************/ +#ifdef HAVE_PTHREAD_SETSCHEDPARAM + #include + + void uhd::set_thread_priority(float priority, bool realtime){ + check_priority_range(priority); + + //when realtime is not enabled, use sched other + int policy = (realtime)? SCHED_RR : SCHED_OTHER; + + //we cannot have below normal priority, set to zero + if (priority < 0) priority = 0; + + //get the priority bounds for the selected policy + int min_pri = sched_get_priority_min(policy); + int max_pri = sched_get_priority_max(policy); + if (min_pri == -1 or max_pri == -1) throw uhd::os_error("error in sched_get_priority_min/max"); + + //set the new priority and policy + sched_param sp; + sp.sched_priority = int(priority*(max_pri - min_pri)) + min_pri; + int ret = pthread_setschedparam(pthread_self(), policy, &sp); + if (ret != 0) throw uhd::os_error("error in pthread_setschedparam"); + } +#endif /* HAVE_PTHREAD_SETSCHEDPARAM */ + +/*********************************************************************** + * Windows API to set priority + **********************************************************************/ +#ifdef HAVE_WIN_SETTHREADPRIORITY + #include + + void uhd::set_thread_priority(float priority, UHD_UNUSED(bool realtime)){ + check_priority_range(priority); + + /* + * Process wide priority is no longer set. + * This is the responsibility of the application. + //set the priority class on the process + int pri_class = (realtime)? REALTIME_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS; + if (SetPriorityClass(GetCurrentProcess(), pri_class) == 0) + throw uhd::os_error("error in SetPriorityClass"); + */ + + //scale the priority value to the constants + int priorities[] = { + THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_NORMAL, + THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL + }; + size_t pri_index = size_t((priority+1.0)*6/2.0); // -1 -> 0, +1 -> 6 + + //set the thread priority on the thread + if (SetThreadPriority(GetCurrentThread(), priorities[pri_index]) == 0) + throw uhd::os_error("error in SetThreadPriority"); + } +#endif /* HAVE_WIN_SETTHREADPRIORITY */ + +/*********************************************************************** + * Unimplemented API to set priority + **********************************************************************/ +#ifdef HAVE_THREAD_PRIO_DUMMY + void uhd::set_thread_priority(float, bool){ + throw uhd::not_implemented_error("set thread priority not implemented"); + } + +#endif /* HAVE_THREAD_PRIO_DUMMY */ + +void uhd::set_thread_name( + boost::thread *thrd, + const std::string &name +) { +#ifdef HAVE_PTHREAD_SETNAME + pthread_setname_np(thrd->native_handle(), name.substr(0,16).c_str()); +#endif /* HAVE_PTHREAD_SETNAME */ +#ifdef HAVE_THREAD_SETNAME_DUMMY + UHD_LOG_DEBUG("UHD", "Setting thread name is not implemented; wanted to set to " << name); +#endif /* HAVE_THREAD_SETNAME_DUMMY */ +} diff --git a/host/lib/utils/thread_priority.cpp b/host/lib/utils/thread_priority.cpp deleted file mode 100644 index 729edcf0a..000000000 --- a/host/lib/utils/thread_priority.cpp +++ /dev/null @@ -1,110 +0,0 @@ -// -// Copyright 2010-2011,2015 Ettus Research LLC -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -// - -#include -#include -#include -#include -#include - -bool uhd::set_thread_priority_safe(float priority, bool realtime){ - try{ - set_thread_priority(priority, realtime); - return true; - }catch(const std::exception &e){ - UHD_LOGGER_WARNING("UHD") << boost::format( - "Unable to set the thread priority. Performance may be negatively affected.\n" - "Please see the general application notes in the manual for instructions.\n" - "%s" - ) % e.what(); - return false; - } -} - -static void check_priority_range(float priority){ - if (priority > +1.0 or priority < -1.0) - throw uhd::value_error("priority out of range [-1.0, +1.0]"); -} - -/*********************************************************************** - * Pthread API to set priority - **********************************************************************/ -#ifdef HAVE_PTHREAD_SETSCHEDPARAM - #include - - void uhd::set_thread_priority(float priority, bool realtime){ - check_priority_range(priority); - - //when realtime is not enabled, use sched other - int policy = (realtime)? SCHED_RR : SCHED_OTHER; - - //we cannot have below normal priority, set to zero - if (priority < 0) priority = 0; - - //get the priority bounds for the selected policy - int min_pri = sched_get_priority_min(policy); - int max_pri = sched_get_priority_max(policy); - if (min_pri == -1 or max_pri == -1) throw uhd::os_error("error in sched_get_priority_min/max"); - - //set the new priority and policy - sched_param sp; - sp.sched_priority = int(priority*(max_pri - min_pri)) + min_pri; - int ret = pthread_setschedparam(pthread_self(), policy, &sp); - if (ret != 0) throw uhd::os_error("error in pthread_setschedparam"); - } -#endif /* HAVE_PTHREAD_SETSCHEDPARAM */ - -/*********************************************************************** - * Windows API to set priority - **********************************************************************/ -#ifdef HAVE_WIN_SETTHREADPRIORITY - #include - - void uhd::set_thread_priority(float priority, UHD_UNUSED(bool realtime)){ - check_priority_range(priority); - - /* - * Process wide priority is no longer set. - * This is the responsibility of the application. - //set the priority class on the process - int pri_class = (realtime)? REALTIME_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS; - if (SetPriorityClass(GetCurrentProcess(), pri_class) == 0) - throw uhd::os_error("error in SetPriorityClass"); - */ - - //scale the priority value to the constants - int priorities[] = { - THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_NORMAL, - THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL - }; - size_t pri_index = size_t((priority+1.0)*6/2.0); // -1 -> 0, +1 -> 6 - - //set the thread priority on the thread - if (SetThreadPriority(GetCurrentThread(), priorities[pri_index]) == 0) - throw uhd::os_error("error in SetThreadPriority"); - } -#endif /* HAVE_WIN_SETTHREADPRIORITY */ - -/*********************************************************************** - * Unimplemented API to set priority - **********************************************************************/ -#ifdef HAVE_THREAD_PRIO_DUMMY - void uhd::set_thread_priority(float, bool){ - throw uhd::not_implemented_error("set thread priority not implemented"); - } - -#endif /* HAVE_THREAD_PRIO_DUMMY */ diff --git a/host/lib/utils/thread_priority_c.cpp b/host/lib/utils/thread_priority_c.cpp index b2de9970d..822126d4a 100644 --- a/host/lib/utils/thread_priority_c.cpp +++ b/host/lib/utils/thread_priority_c.cpp @@ -17,7 +17,7 @@ #include #include -#include +#include #include #include #include diff --git a/host/utils/latency/lib/Responder.cpp b/host/utils/latency/lib/Responder.cpp index ba278f338..517d5e1c1 100644 --- a/host/utils/latency/lib/Responder.cpp +++ b/host/utils/latency/lib/Responder.cpp @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include const std::string _eth_file("eths_info.txt"); diff --git a/host/utils/query_gpsdo_sensors.cpp b/host/utils/query_gpsdo_sensors.cpp index 6c2ec0bd1..2a707db9e 100644 --- a/host/utils/query_gpsdo_sensors.cpp +++ b/host/utils/query_gpsdo_sensors.cpp @@ -16,7 +16,7 @@ // #include -#include +#include #include #include #include diff --git a/host/utils/uhd_cal_rx_iq_balance.cpp b/host/utils/uhd_cal_rx_iq_balance.cpp index d599a4e03..7e53c22c4 100644 --- a/host/utils/uhd_cal_rx_iq_balance.cpp +++ b/host/utils/uhd_cal_rx_iq_balance.cpp @@ -16,7 +16,7 @@ // #include "usrp_cal_utils.hpp" -#include +#include #include #include #include diff --git a/host/utils/uhd_cal_tx_dc_offset.cpp b/host/utils/uhd_cal_tx_dc_offset.cpp index e3cdc087e..d89429f5d 100644 --- a/host/utils/uhd_cal_tx_dc_offset.cpp +++ b/host/utils/uhd_cal_tx_dc_offset.cpp @@ -16,7 +16,7 @@ // #include "usrp_cal_utils.hpp" -#include +#include #include #include #include diff --git a/host/utils/uhd_cal_tx_iq_balance.cpp b/host/utils/uhd_cal_tx_iq_balance.cpp index 445b86694..9ccf3de3e 100644 --- a/host/utils/uhd_cal_tx_iq_balance.cpp +++ b/host/utils/uhd_cal_tx_iq_balance.cpp @@ -16,7 +16,7 @@ // #include "usrp_cal_utils.hpp" -#include +#include #include #include #include -- cgit v1.2.3