Skip to content

Commit

Permalink
lock: support naming threads
Browse files Browse the repository at this point in the history
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á <nuno.sa@analog.com>
  • Loading branch information
nunojsa committed Jan 31, 2025
1 parent ee3a89b commit efd0d84
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit efd0d84

Please sign in to comment.