From efd0d84d6fc501f760ba0c245077701e4131bc92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20S=C3=A1?= Date: Thu, 30 Jan 2025 13:17:36 +0000 Subject: [PATCH] lock: support naming threads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add support for naming threads under Unix. It's done with pthread_setname_np(3) so we need to make sure it's available. On top of that, we also need to compile lock.c with _GNU_SOURCE. Signed-off-by: Nuno Sá --- CMakeLists.txt | 7 ++++++- lock.c | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5eee82f7f..d1a39ed19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -597,8 +597,13 @@ else () target_link_libraries(iio PRIVATE ${PTHREAD_LIBRARIES}) endif() - + check_symbol_exists(pthread_setname_np "pthread.h" HAS_PTHREAD_SETNAME_NP) + if (HAS_PTHREAD_SETNAME_NP) + # still need to define _GNU_SOURCE for pthread_setname_np + set_source_files_properties(lock.c PROPERTIES COMPILE_FLAGS -D_GNU_SOURCE) + endif() target_sources(iio PRIVATE lock.c) + endif() if (IIOD_CLIENT OR WITH_IIOD OR WITH_LIBTINYIIOD) diff --git a/lock.c b/lock.c index 87e29cb35..1b1d2a6af 100644 --- a/lock.c +++ b/lock.c @@ -134,7 +134,12 @@ struct iio_thrd * iio_thrd_create(int (*thrd)(void *), return iio_ptr(ret); } - /* TODO: Set name */ + if (HAS_PTHREAD_SETNAME_NP) { + ret = pthread_setname_np(iio_thrd->thid, name); + if (ret) + fprintf(stderr, "[WARNING]: Setting thread name(%s) failed\n", + name); + } return iio_thrd; }