From d2f4efa1bf8fd558f132b4234e9c4a3232bf3416 Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Mon, 5 Jul 2021 21:40:31 +0200 Subject: [PATCH] [misc] Centralize thread naming plus name websocket thread --- src/cache.c | 9 +-------- src/httpd.c | 9 +-------- src/input.c | 9 +-------- src/inputs/libspotify/libspotify.c | 9 +-------- src/inputs/pipe.c | 9 +-------- src/inputs/spotify_librespotc.c | 9 +-------- src/library.c | 9 +-------- src/misc.c | 13 +++++++++++++ src/misc.h | 4 ++++ src/mpd.c | 9 +-------- src/player.c | 10 ++-------- src/websocket.c | 7 ++++--- src/worker.c | 10 ++-------- 13 files changed, 33 insertions(+), 83 deletions(-) diff --git a/src/cache.c b/src/cache.c index 05b75eb0..fe4eedf7 100644 --- a/src/cache.c +++ b/src/cache.c @@ -30,9 +30,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_NP_H -# include -#endif #include #include @@ -1640,11 +1637,7 @@ cache_init(void) goto thread_fail; } -#if defined(HAVE_PTHREAD_SETNAME_NP) - pthread_setname_np(tid_cache, "cache"); -#elif defined(HAVE_PTHREAD_SET_NAME_NP) - pthread_set_name_np(tid_cache, "cache"); -#endif + thread_setname(tid_cache, "cache"); return 0; diff --git a/src/httpd.c b/src/httpd.c index 6c1d5968..c2613732 100644 --- a/src/httpd.c +++ b/src/httpd.c @@ -28,9 +28,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_NP_H -# include -#endif #include #include #include @@ -1789,11 +1786,7 @@ httpd_init(const char *webroot) goto thread_fail; } -#if defined(HAVE_PTHREAD_SETNAME_NP) - pthread_setname_np(tid_httpd, "httpd"); -#elif defined(HAVE_PTHREAD_SET_NAME_NP) - pthread_set_name_np(tid_httpd, "httpd"); -#endif + thread_setname(tid_httpd, "httpd"); return 0; diff --git a/src/input.c b/src/input.c index 34974917..828d400f 100644 --- a/src/input.c +++ b/src/input.c @@ -32,9 +32,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_NP_H -# include -#endif #include "misc.h" #include "logger.h" @@ -927,11 +924,7 @@ input_init(void) goto thread_fail; } -#if defined(HAVE_PTHREAD_SETNAME_NP) - pthread_setname_np(tid_input, "input"); -#elif defined(HAVE_PTHREAD_SET_NAME_NP) - pthread_set_name_np(tid_input, "input"); -#endif + thread_setname(tid_input, "input"); return 0; diff --git a/src/inputs/libspotify/libspotify.c b/src/inputs/libspotify/libspotify.c index d4252451..7f418751 100644 --- a/src/inputs/libspotify/libspotify.c +++ b/src/inputs/libspotify/libspotify.c @@ -35,9 +35,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_NP_H -# include -#endif #include #include @@ -1575,11 +1572,7 @@ libspotify_init(void) goto thread_fail; } -#if defined(HAVE_PTHREAD_SETNAME_NP) - pthread_setname_np(tid_spotify, "spotify"); -#elif defined(HAVE_PTHREAD_SET_NAME_NP) - pthread_set_name_np(tid_spotify, "spotify"); -#endif + thread_setname(tid_spotify, "spotify"); DPRINTF(E_DBG, L_SPOTIFY, "Spotify init complete\n"); return 0; diff --git a/src/inputs/pipe.c b/src/inputs/pipe.c index 76a38a01..2b2ade88 100644 --- a/src/inputs/pipe.c +++ b/src/inputs/pipe.c @@ -45,9 +45,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_NP_H -# include -#endif #include #include #include @@ -854,11 +851,7 @@ pipe_thread_start(void) CHECK_NULL(L_PLAYER, cmdbase = commands_base_new(evbase_pipe, NULL)); CHECK_ERR(L_PLAYER, pthread_create(&tid_pipe, NULL, pipe_thread_run, NULL)); -#if defined(HAVE_PTHREAD_SETNAME_NP) - pthread_setname_np(tid_pipe, "pipe"); -#elif defined(HAVE_PTHREAD_SET_NAME_NP) - pthread_set_name_np(tid_pipe, "pipe"); -#endif + thread_setname(tid_pipe, "pipe"); } static void diff --git a/src/inputs/spotify_librespotc.c b/src/inputs/spotify_librespotc.c index eb97eaa8..c2bf5ec4 100644 --- a/src/inputs/spotify_librespotc.c +++ b/src/inputs/spotify_librespotc.c @@ -24,9 +24,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_NP_H -# include -#endif #include @@ -229,11 +226,7 @@ tcp_disconnect(int fd) static void thread_name_set(pthread_t thread) { -#if defined(HAVE_PTHREAD_SETNAME_NP) - pthread_setname_np(thread, "spotify"); -#elif defined(HAVE_PTHREAD_SET_NAME_NP) - pthread_set_name_np(thread, "spotify"); -#endif + thread_setname(thread, "spotify"); } static void diff --git a/src/library.c b/src/library.c index c704958e..3af4845d 100644 --- a/src/library.c +++ b/src/library.c @@ -26,9 +26,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_NP_H -# include -#endif #include #include #include @@ -882,11 +879,7 @@ library_init(void) CHECK_ERR(L_LIB, pthread_create(&tid_library, NULL, library, NULL)); -#if defined(HAVE_PTHREAD_SETNAME_NP) - pthread_setname_np(tid_library, "library"); -#elif defined(HAVE_PTHREAD_SET_NAME_NP) - pthread_set_name_np(tid_library, "library"); -#endif + thread_setname(tid_library, "library"); return 0; } diff --git a/src/misc.c b/src/misc.c index 099b94c9..40625257 100644 --- a/src/misc.c +++ b/src/misc.c @@ -44,6 +44,9 @@ #ifdef HAVE_UUID #include #endif +#ifdef HAVE_PTHREAD_NP_H +# include +#endif #include #include @@ -1545,6 +1548,16 @@ mutex_init(pthread_mutex_t *mutex) return err; } +void +thread_setname(pthread_t thread, const char *name) +{ +#if defined(HAVE_PTHREAD_SETNAME_NP) + pthread_setname_np(thread, name); +#elif defined(HAVE_PTHREAD_SET_NAME_NP) + pthread_set_name_np(thread, name); +#endif +} + #ifdef HAVE_UUID void uuid_make(char *str) diff --git a/src/misc.h b/src/misc.h index 56d0f404..2510f5eb 100644 --- a/src/misc.h +++ b/src/misc.h @@ -284,6 +284,10 @@ buildopts_get(void); int mutex_init(pthread_mutex_t *mutex); +// wrapper for pthread_setname_np/pthread_set_name_np +void +thread_setname(pthread_t thread, const char *name); + void uuid_make(char *str); diff --git a/src/mpd.c b/src/mpd.c index aee17cfe..d93f23f9 100644 --- a/src/mpd.c +++ b/src/mpd.c @@ -29,9 +29,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_NP_H -# include -#endif #include #include #include @@ -4846,11 +4843,7 @@ mpd_init(void) goto thread_fail; } -#if defined(HAVE_PTHREAD_SETNAME_NP) - pthread_setname_np(tid_mpd, "mpd"); -#elif defined(HAVE_PTHREAD_SET_NAME_NP) - pthread_set_name_np(tid_mpd, "mpd"); -#endif + thread_setname(tid_mpd, "mpd"); mpd_clients = NULL; listener_add(mpd_listener_cb, MPD_ALL_IDLE_LISTENER_EVENTS); diff --git a/src/player.c b/src/player.c index 8be8f6e1..23598596 100644 --- a/src/player.c +++ b/src/player.c @@ -59,9 +59,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_NP_H -# include -#endif #ifdef HAVE_TIMERFD # include @@ -3708,11 +3705,8 @@ player_init(void) DPRINTF(E_FATAL, L_PLAYER, "Could not spawn player thread: %s\n", strerror(errno)); goto error_input_deinit; } -#if defined(HAVE_PTHREAD_SETNAME_NP) - pthread_setname_np(tid_player, "player"); -#elif defined(HAVE_PTHREAD_SET_NAME_NP) - pthread_set_name_np(tid_player, "player"); -#endif + + thread_setname(tid_player, "player"); return 0; diff --git a/src/websocket.c b/src/websocket.c index a0ec1a37..ab4a0a29 100644 --- a/src/websocket.c +++ b/src/websocket.c @@ -19,12 +19,10 @@ #ifdef HAVE_CONFIG_H # include #endif + #include #include #include -#ifdef HAVE_PTHREAD_NP_H -# include -#endif #include #include #include @@ -34,6 +32,7 @@ #include "conffile.h" #include "listener.h" #include "logger.h" +#include "misc.h" static struct lws_context *context; @@ -523,6 +522,8 @@ websocket_init(void) return -1; } + thread_setname(tid_websocket, "websocket"); + return 0; } diff --git a/src/worker.c b/src/worker.c index 354a1f92..23ad95c4 100644 --- a/src/worker.c +++ b/src/worker.c @@ -30,9 +30,6 @@ #include #include #include -#ifdef HAVE_PTHREAD_NP_H -# include -#endif #include @@ -40,6 +37,7 @@ #include "logger.h" #include "worker.h" #include "commands.h" +#include "misc.h" struct worker_arg @@ -191,11 +189,7 @@ worker_init(void) goto thread_fail; } -#if defined(HAVE_PTHREAD_SETNAME_NP) - pthread_setname_np(tid_worker, "worker"); -#elif defined(HAVE_PTHREAD_SET_NAME_NP) - pthread_set_name_np(tid_worker, "worker"); -#endif + thread_setname(tid_worker, "worker"); return 0;