From cb829879af8f2c93fd77c47f12c162d4ce580e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Sun, 22 Oct 2023 20:59:34 +0200 Subject: [PATCH] mp_threads: rename threads for consistent naming across all of them I'd like some names to be more descriptive, but to work with 15 chars limit we have to make some sacrifice. Also because of the limit, remove the `mpv/` prefix and prioritize actuall thread name. --- audio/out/ao_audiotrack.c | 2 +- audio/out/ao_wasapi.c | 2 +- common/msg.c | 2 +- filters/f_decoder_wrapper.c | 6 +++--- input/input.c | 2 +- input/ipc-unix.c | 6 ++++-- input/ipc-win.c | 6 ++++-- osdep/macosx_application.m | 3 +-- osdep/terminal-unix.c | 2 +- osdep/terminal-win.c | 2 +- osdep/threads.c | 11 +++++------ player/client.c | 2 +- player/main.c | 2 ++ player/scripting.c | 6 +++--- video/out/w32_common.c | 2 +- 15 files changed, 30 insertions(+), 26 deletions(-) diff --git a/audio/out/ao_audiotrack.c b/audio/out/ao_audiotrack.c index 9620e45bd68fb..321e3cb8511c2 100644 --- a/audio/out/ao_audiotrack.c +++ b/audio/out/ao_audiotrack.c @@ -554,7 +554,7 @@ static void *playthread(void *arg) struct ao *ao = arg; struct priv *p = ao->priv; JNIEnv *env = MP_JNI_GET_ENV(ao); - mpthread_set_name("audiotrack"); + mpthread_set_name("ao/audiotrack"); pthread_mutex_lock(&p->lock); while (!p->thread_terminate) { int state = AudioTrack.PLAYSTATE_PAUSED; diff --git a/audio/out/ao_wasapi.c b/audio/out/ao_wasapi.c index a2460ab513301..5f6468f33e41a 100644 --- a/audio/out/ao_wasapi.c +++ b/audio/out/ao_wasapi.c @@ -199,7 +199,7 @@ static DWORD __stdcall AudioThread(void *lpParameter) { struct ao *ao = lpParameter; struct wasapi_state *state = ao->priv; - mpthread_set_name("wasapi event"); + mpthread_set_name("ao/wasapi"); CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); state->init_ok = wasapi_thread_init(ao); diff --git a/common/msg.c b/common/msg.c index 48767e89ec3a0..66a7a6f1fff04 100644 --- a/common/msg.c +++ b/common/msg.c @@ -541,7 +541,7 @@ static void *log_file_thread(void *p) { struct mp_log_root *root = p; - mpthread_set_name("log-file"); + mpthread_set_name("log"); pthread_mutex_lock(&root->log_file_lock); diff --git a/filters/f_decoder_wrapper.c b/filters/f_decoder_wrapper.c index 2830ada5d50b8..ad090ecd7f895 100644 --- a/filters/f_decoder_wrapper.c +++ b/filters/f_decoder_wrapper.c @@ -1102,10 +1102,10 @@ static void *dec_thread(void *ptr) { struct priv *p = ptr; - char *t_name = "?"; + char *t_name = "dec/?"; switch (p->header->type) { - case STREAM_VIDEO: t_name = "vdec"; break; - case STREAM_AUDIO: t_name = "adec"; break; + case STREAM_VIDEO: t_name = "dec/video"; break; + case STREAM_AUDIO: t_name = "dec/audio"; break; } mpthread_set_name(t_name); diff --git a/input/input.c b/input/input.c index 9d50abb50d8fa..4b13841224526 100644 --- a/input/input.c +++ b/input/input.c @@ -1622,7 +1622,7 @@ static void *input_src_thread(void *ptr) void (*loop_fn)(struct mp_input_src *src, void *ctx) = args[1]; void *ctx = args[2]; - mpthread_set_name("input source"); + mpthread_set_name("input"); src->in->thread_running = true; diff --git a/input/ipc-unix.c b/input/ipc-unix.c index 5a638ea49fd01..2cb53be546134 100644 --- a/input/ipc-unix.c +++ b/input/ipc-unix.c @@ -105,7 +105,9 @@ static void *client_thread(void *p) struct client_arg *arg = p; bstr client_msg = { talloc_strdup(NULL, ""), 0 }; - mpthread_set_name(arg->client_name); + char *tname = talloc_asprintf(NULL, "ipc/%s", arg->client_name); + mpthread_set_name(tname); + talloc_free(tname); int pipe_fd = mpv_get_wakeup_pipe(arg->client); if (pipe_fd < 0) { @@ -302,7 +304,7 @@ static void *ipc_thread(void *p) struct mp_ipc_ctx *arg = p; - mpthread_set_name("ipc socket listener"); + mpthread_set_name("ipc/socket"); MP_VERBOSE(arg, "Starting IPC master\n"); diff --git a/input/ipc-win.c b/input/ipc-win.c index aec754a281f9e..42a519ecf0de0 100644 --- a/input/ipc-win.c +++ b/input/ipc-win.c @@ -210,7 +210,9 @@ static void *client_thread(void *p) DWORD ioerr = 0; DWORD r; - mpthread_set_name(arg->client_name); + char *tname = talloc_asprintf(NULL, "ipc/%s", arg->client_name); + mpthread_set_name(tname); + talloc_free(tname); arg->write_ol.hEvent = CreateEventW(NULL, TRUE, TRUE, NULL); if (!wakeup_event || !ol.hEvent || !arg->write_ol.hEvent) { @@ -356,7 +358,7 @@ static void *ipc_thread(void *p) HANDLE client = INVALID_HANDLE_VALUE; int client_num = 0; - mpthread_set_name("ipc named pipe listener"); + mpthread_set_name("ipc/named-pipe"); MP_VERBOSE(arg, "Starting IPC master\n"); SECURITY_ATTRIBUTES sa = { diff --git a/osdep/macosx_application.m b/osdep/macosx_application.m index bf60e05389679..78fcb5cb485f0 100644 --- a/osdep/macosx_application.m +++ b/osdep/macosx_application.m @@ -275,7 +275,7 @@ static void cocoa_run_runloop(void) static void *playback_thread(void *ctx_obj) { - mpthread_set_name("playback core (OSX)"); + mpthread_set_name("core/playback"); @autoreleasepool { struct playback_thread_ctx *ctx = (struct playback_thread_ctx*) ctx_obj; int r = mpv_main(*ctx->argc, *ctx->argv); @@ -377,4 +377,3 @@ int cocoa_main(int argc, char *argv[]) return 1; } } - diff --git a/osdep/terminal-unix.c b/osdep/terminal-unix.c index d31e1e753f3d7..43a74e6bdb513 100644 --- a/osdep/terminal-unix.c +++ b/osdep/terminal-unix.c @@ -408,7 +408,7 @@ static void quit_request_sighandler(int signum) static void *terminal_thread(void *ptr) { - mpthread_set_name("terminal"); + mpthread_set_name("terminal/input"); bool stdin_ok = read_terminal; // if false, we still wait for SIGTERM while (1) { getch2_poll(); diff --git a/osdep/terminal-win.c b/osdep/terminal-win.c index 4e88f29679ca8..406829ed0226b 100644 --- a/osdep/terminal-win.c +++ b/osdep/terminal-win.c @@ -161,7 +161,7 @@ static void read_input(HANDLE in) static void *input_thread_fn(void *ptr) { - mpthread_set_name("terminal"); + mpthread_set_name("terminal/input"); HANDLE in = ptr; HANDLE stuff[2] = {in, death}; while (1) { diff --git a/osdep/threads.c b/osdep/threads.c index 0b96216cc75f6..1802fea3cef69 100644 --- a/osdep/threads.c +++ b/osdep/threads.c @@ -40,17 +40,16 @@ int mpthread_mutex_init_recursive(pthread_mutex_t *mutex) void mpthread_set_name(const char *name) { - char tname[80]; - snprintf(tname, sizeof(tname), "mpv/%s", name); #if HAVE_GLIBC_THREAD_NAME - if (pthread_setname_np(pthread_self(), tname) == ERANGE) { - tname[15] = '\0'; // glibc-checked kernel limit + if (pthread_setname_np(pthread_self(), name) == ERANGE) { + char tname[16] = {0}; // glibc-checked kernel limit + strncpy(tname, name, sizeof(tname) - 1); pthread_setname_np(pthread_self(), tname); } #elif HAVE_WIN32_INTERNAL_PTHREADS || HAVE_BSD_THREAD_NAME - pthread_set_name_np(pthread_self(), tname); + pthread_set_name_np(pthread_self(), name); #elif HAVE_OSX_THREAD_NAME - pthread_setname_np(tname); + pthread_setname_np(name); #endif } diff --git a/player/client.c b/player/client.c index a14d2e99a37c2..68cc055597fa6 100644 --- a/player/client.c +++ b/player/client.c @@ -602,7 +602,7 @@ static void *core_thread(void *p) { struct MPContext *mpctx = p; - mpthread_set_name("mpv core"); + mpthread_set_name("core"); while (!mpctx->initialized && mpctx->stop_play != PT_QUIT) mp_idle(mpctx); diff --git a/player/main.c b/player/main.c index cd84026730ddf..8f98754f2b380 100644 --- a/player/main.c +++ b/player/main.c @@ -34,6 +34,7 @@ #include "misc/thread_pool.h" #include "osdep/io.h" #include "osdep/terminal.h" +#include "osdep/threads.h" #include "osdep/timer.h" #include "osdep/main-fn.h" @@ -419,6 +420,7 @@ int mp_initialize(struct MPContext *mpctx, char **options) int mpv_main(int argc, char *argv[]) { + mpthread_set_name("main"); struct MPContext *mpctx = mp_create(); if (!mpctx) return 1; diff --git a/player/scripting.c b/player/scripting.c index 9a23a0dae0c2a..15f35337382cf 100644 --- a/player/scripting.c +++ b/player/scripting.c @@ -85,10 +85,10 @@ static char *script_name_from_filename(void *talloc_ctx, const char *fname) static void run_script(struct mp_script_args *arg) { - char name[90]; - snprintf(name, sizeof(name), "%s (%s)", arg->backend->name, - mpv_client_name(arg->client)); + char *name = talloc_asprintf(NULL, "%s/%s", arg->backend->name, + mpv_client_name(arg->client)); mpthread_set_name(name); + talloc_free(name); if (arg->backend->load(arg) < 0) MP_ERR(arg, "Could not load %s %s\n", arg->backend->name, arg->filename); diff --git a/video/out/w32_common.c b/video/out/w32_common.c index f58149e3c9f13..424369d5d0041 100644 --- a/video/out/w32_common.c +++ b/video/out/w32_common.c @@ -1730,7 +1730,7 @@ static void *gui_thread(void *ptr) bool ole_ok = false; int res = 0; - mpthread_set_name("win32 window"); + mpthread_set_name("window"); w32_api_load(w32);