diff options
author | Andrej Rode <andrej.rode@ettus.com> | 2017-04-18 16:46:44 -0700 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2017-06-29 13:43:05 -0700 |
commit | c33928d2bbdd27688c3475e77fc461e7d16eba5a (patch) | |
tree | d5bffc49696e2bc03c9b8576864e387373b950fb /host/lib/utils | |
parent | 47cdd6319c74a7b823843aad5ff3fa370ed1e6ef (diff) | |
download | uhd-c33928d2bbdd27688c3475e77fc461e7d16eba5a.tar.gz uhd-c33928d2bbdd27688c3475e77fc461e7d16eba5a.tar.bz2 uhd-c33928d2bbdd27688c3475e77fc461e7d16eba5a.zip |
utils: add set_thread_name API call, move thread_priority to thread
Diffstat (limited to 'host/lib/utils')
-rw-r--r-- | host/lib/utils/CMakeLists.txt | 36 | ||||
-rw-r--r-- | host/lib/utils/tasks.cpp | 13 | ||||
-rw-r--r-- | host/lib/utils/thread.cpp (renamed from host/lib/utils/thread_priority.cpp) | 14 | ||||
-rw-r--r-- | host/lib/utils/thread_priority_c.cpp | 2 |
4 files changed, 56 insertions, 9 deletions
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 <pthread.h> + 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 <uhd/utils/tasks.hpp> #include <uhd/utils/msg_task.hpp> +#include <uhd/utils/thread.hpp> #include <uhd/utils/log.hpp> #include <uhd/exception.hpp> #include <boost/thread/thread.hpp> @@ -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_priority.cpp b/host/lib/utils/thread.cpp index 729edcf0a..9100cfd7b 100644 --- a/host/lib/utils/thread_priority.cpp +++ b/host/lib/utils/thread.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // -#include <uhd/utils/thread_priority.hpp> +#include <uhd/utils/thread.hpp> #include <uhd/utils/log.hpp> #include <uhd/exception.hpp> #include <boost/format.hpp> @@ -108,3 +108,15 @@ static void check_priority_range(float priority){ } #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_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 <uhd/error.h> #include <uhd/utils/thread_priority.h> -#include <uhd/utils/thread_priority.hpp> +#include <uhd/utils/thread.hpp> #include <uhd/utils/log.hpp> #include <uhd/exception.hpp> #include <boost/format.hpp> |