From 09e21b3397f537a51916913be8b15e8b3c4dbe2c Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Tue, 26 May 2026 00:19:35 -0700 Subject: [PATCH] pcem: fix helper thread handles The PCem compatibility layer returned a raw uae_thread_id as a thread_t pointer and then waited on that value through the semaphore API. That was pointer-shaped on some targets, but it was not a valid wrapper around UAE's thread API. Store the uae_thread_id in an explicit PCem handle object, join it with uae_wait_thread(), and free the wrapper after wait or close. Reset and signal PCem events through the existing semaphore object instead of passing the address of a local pointer copy. --- pcem/pcemglue.cpp | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/pcem/pcemglue.cpp b/pcem/pcemglue.cpp index 170dc0b8..72388b9c 100644 --- a/pcem/pcemglue.cpp +++ b/pcem/pcemglue.cpp @@ -821,15 +821,28 @@ void thread_sleep(int n) sleep_millis(n); } -thread_t *thread_create(void (*thread_rout)(void *param), void *param) +typedef struct pcem_thread_handle { uae_thread_id tid; - uae_start_thread(_T("PCem helper"), thread_rout, param, &tid); - return tid; +} pcem_thread_handle; + +thread_t *thread_create(void (*thread_rout)(void *param), void *param) +{ + pcem_thread_handle *handle = xcalloc(pcem_thread_handle, 1); + if (!uae_start_thread(_T("PCem helper"), thread_rout, param, &handle->tid)) { + xfree(handle); + return NULL; + } + return (thread_t*)handle; } void thread_kill(thread_t *handle) { - uae_end_thread((uae_thread_id*)&handle); + if (!handle) { + return; + } + pcem_thread_handle *thread = (pcem_thread_handle*)handle; + uae_end_thread(&thread->tid); + xfree(thread); } event_t *thread_create_event(void) { @@ -842,25 +855,30 @@ int thread_wait(thread_t *arg) if (!arg) { return 0; } - uae_sem_wait((uae_sem_t*)&arg); + pcem_thread_handle *thread = (pcem_thread_handle*)arg; + uae_wait_thread(thread->tid); + xfree(thread); return 1; } void thread_set_event(event_t *event) { - uae_sem_post((uae_sem_t*)&event); + uae_sem_t sem = (uae_sem_t)event; + uae_sem_post(&sem); } void thread_reset_event(event_t *_event) { - uae_sem_init((uae_sem_t*)&_event, 1, 0); + uae_sem_t sem = (uae_sem_t)_event; + uae_sem_unpost(&sem); } int thread_wait_event(event_t *event, int timeout) { - uae_sem_trywait_delay((uae_sem_t*)&event, timeout < 0 ? INFINITE : timeout); - return 0; + uae_sem_t sem = (uae_sem_t)event; + return uae_sem_trywait_delay(&sem, timeout < 0 ? -1 : timeout); } void thread_destroy_event(event_t *_event) { - uae_sem_destroy((uae_sem_t*)&_event); + uae_sem_t sem = (uae_sem_t)_event; + uae_sem_destroy(&sem); } typedef struct win_mutex_t -- 2.47.3