Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
Signed-off-by: Nuno Sá <nuno.sa@analog.com>
  • Loading branch information
nunojsa committed Jan 31, 2025
1 parent efd0d84 commit a925fa3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions iio-config.h.cmakein
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
36 changes: 30 additions & 6 deletions lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down

0 comments on commit a925fa3

Please sign in to comment.