From c505f77ddaaef00b216c84374eab90c4c7b7c2ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Sat, 6 Jul 2024 19:39:22 +0200 Subject: [PATCH] player/core: add core thread handle to MPContext This change removes convoluted core thread extraction through dispatch added in 500ce69a06be2cb2b79f310e638e4c4ceabe447e. Currently we fully control this thread, create it and join, there is no reason not to keep the handle of it in the player context. As a bonus to code simplification this also fixes thread handle leak on Windows. Fixes: #14472 --- osdep/threads-posix.h | 1 - osdep/threads-win32.h | 11 ----------- player/client.c | 13 ++----------- player/core.h | 1 + 4 files changed, 3 insertions(+), 23 deletions(-) diff --git a/osdep/threads-posix.h b/osdep/threads-posix.h index 4b3bb902ac..44f812dd6a 100644 --- a/osdep/threads-posix.h +++ b/osdep/threads-posix.h @@ -219,7 +219,6 @@ static inline int mp_cond_timedwait_until(mp_cond *cond, mp_mutex *mutex, int64_ #define mp_thread_create(t, f, a) pthread_create(t, NULL, f, a) #define mp_thread_join(t) pthread_join(t, NULL) -#define mp_thread_join_id(t) pthread_join(t, NULL) #define mp_thread_detach pthread_detach #define mp_thread_current_id pthread_self #define mp_thread_id_equal(a, b) ((a) == (b)) diff --git a/osdep/threads-win32.h b/osdep/threads-win32.h index ec90fe9387..e13e8a6f9b 100644 --- a/osdep/threads-win32.h +++ b/osdep/threads-win32.h @@ -175,17 +175,6 @@ static inline int mp_thread_join(mp_thread thread) return 0; } -static inline int mp_thread_join_id(mp_thread_id id) -{ - mp_thread thread = OpenThread(SYNCHRONIZE, FALSE, id); - if (!thread) - return ESRCH; - int ret = mp_thread_join(thread); - if (ret) - CloseHandle(thread); - return ret; -} - static inline int mp_thread_detach(mp_thread thread) { return CloseHandle(thread) ? 0 : EINVAL; diff --git a/player/client.c b/player/client.c index 85854e31ff..bcee9c4246 100644 --- a/player/client.c +++ b/player/client.c @@ -422,11 +422,6 @@ static void abort_async(struct MPContext *mpctx, mpv_handle *ctx, mp_mutex_unlock(&mpctx->abort_lock); } -static void get_thread_id(void *ptr) -{ - *(mp_thread_id *)ptr = mp_thread_current_id(); -} - static void mp_destroy_client(mpv_handle *ctx, bool terminate) { if (!ctx) @@ -521,9 +516,6 @@ static void mp_destroy_client(mpv_handle *ctx, bool terminate) mpctx->stop_play = PT_QUIT; mp_dispatch_unlock(mpctx->dispatch); - mp_thread_id playthread; - mp_dispatch_run(mpctx->dispatch, get_thread_id, &playthread); - // Ask the core thread to stop. mp_mutex_lock(&clients->lock); clients->terminate_core_thread = true; @@ -531,7 +523,7 @@ static void mp_destroy_client(mpv_handle *ctx, bool terminate) mp_wakeup_core(mpctx); // Blocking wait for all clients and core thread to terminate. - mp_thread_join_id(playthread); + mp_thread_join(mpctx->core_thread); mp_destroy(mpctx); } @@ -629,8 +621,7 @@ mpv_handle *mpv_create(void) return NULL; } - mp_thread thread; - if (mp_thread_create(&thread, core_thread, mpctx) != 0) { + if (mp_thread_create(&mpctx->core_thread, core_thread, mpctx) != 0) { ctx->clients->have_terminator = true; // avoid blocking mpv_terminate_destroy(ctx); mp_destroy(mpctx); diff --git a/player/core.h b/player/core.h index 35d6348e39..28acc26c5d 100644 --- a/player/core.h +++ b/player/core.h @@ -232,6 +232,7 @@ extern const int num_ptracks[STREAM_TYPE_COUNT]; typedef struct MPContext { bool initialized; bool is_cli; + mp_thread core_thread; struct mpv_global *global; struct MPOpts *opts; struct mp_log *log;