From a925fa38ea511f808ee053cd10b012b8e32bd158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20S=C3=A1?= Date: Fri, 31 Jan 2025 09:51:10 +0000 Subject: [PATCH] test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nuno Sá --- CMakeLists.txt | 10 ++++++++++ iio-config.h.cmakein | 1 + lock.c | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d1a39ed19..6b2e33fa0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -597,8 +597,18 @@ else () target_link_libraries(iio PRIVATE ${PTHREAD_LIBRARIES}) endif() + + include(CheckSymbolExists) + set(CMAKE_REQUIRED_LIBRARIES ${PTHREAD_LIBRARIES}) + set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) + set(TMP_FLAGS "${CMAKE_C_FLAGS}") + set(CMAKE_C_FLAGS "") check_symbol_exists(pthread_setname_np "pthread.h" HAS_PTHREAD_SETNAME_NP) + set(CMAKE_C_FLAGS "${TMP_FLAGS}") + set(CMAKE_REQUIRED_LIBRARIES) + set(CMAKE_REQUIRED_DEFINITIONS) if (HAS_PTHREAD_SETNAME_NP) + message("pthread_setname_np is available") # still need to define _GNU_SOURCE for pthread_setname_np set_source_files_properties(lock.c PROPERTIES COMPILE_FLAGS -D_GNU_SOURCE) endif() diff --git a/iio-config.h.cmakein b/iio-config.h.cmakein index e3ea030a0..2216dfb86 100644 --- a/iio-config.h.cmakein +++ b/iio-config.h.cmakein @@ -52,6 +52,7 @@ #cmakedefine HAS_NEWLOCALE #cmakedefine HAS_PTHREAD_SETNAME_NP #cmakedefine HAVE_IPV6 +#cmakedefine HAS_PTHREAD_SETNAME_NP #define IF_ENABLED(cfg, ptr) ((cfg) ? (ptr) : NULL) diff --git a/lock.c b/lock.c index 1b1d2a6af..2bb6b5ffa 100644 --- a/lock.c +++ b/lock.c @@ -27,9 +27,24 @@ struct iio_cond { struct iio_thrd { pthread_t thid; void *d; + /* + * pthread_setname_np(3) does not allow names bigger than 16 + * (at least on linux). + */ + char name[16]; int (*func)(void *); }; +#if defined(HAS_PTHREAD_SETNAME_NP) +#if defined(__linux__) +#define iio_thrd_create_set_name(thid, name) pthread_setname_np(thid, name) +#elif defined(__APPLE__) +#define iio_thrd_create_set_name(thid, name) pthread_setname_np(name) +#endif +#else +#define iio_thrd_create_set_name(thid, name) 0 +#endif + struct iio_mutex * iio_mutex_create(void) { struct iio_mutex *lock = malloc(sizeof(*lock)); @@ -107,6 +122,18 @@ void iio_cond_signal(struct iio_cond *cond) static void * iio_thrd_wrapper(void *d) { struct iio_thrd *thrd = d; + /* + * For Mac, it seems we need to name the thread from the thread + * itself. + */ + if (thrd->name[0] != '\0') { + int ret; + + ret = iio_thrd_create_set_name(thrd->thid, thrd->name); + if (ret) + fprintf(stderr, "[WARNING]: Failed to set thread name: %s\n", + thrd->name); + } return (void *)(intptr_t) thrd->func(thrd->d); } @@ -126,6 +153,8 @@ struct iio_thrd * iio_thrd_create(int (*thrd)(void *), iio_thrd->d = d; iio_thrd->func = thrd; + if (name) + iio_strlcpy(iio_thrd->name, name, sizeof(iio_thrd->name)); ret = pthread_create(&iio_thrd->thid, NULL, iio_thrd_wrapper, iio_thrd); @@ -134,12 +163,7 @@ struct iio_thrd * iio_thrd_create(int (*thrd)(void *), return iio_ptr(ret); } - 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; }