player/core: add core thread handle to MPContext

This change removes convoluted core thread extraction through dispatch
added in 500ce69a06.

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
This commit is contained in:
Kacper Michajłow 2024-07-06 19:39:22 +02:00
parent 09b25771e8
commit c505f77dda
4 changed files with 3 additions and 23 deletions

View File

@ -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))

View File

@ -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;

View File

@ -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);

View File

@ -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;