Use semaphores on Windows for backward compatibility.

This commit is contained in:
Brandon Wright 2018-04-12 19:10:58 -05:00
parent a817eff105
commit 4ede4b7447
1 changed files with 17 additions and 21 deletions

View File

@ -260,32 +260,31 @@ static uint8 *resample_buffer = NULL;
static HANDLE worker_thread; static HANDLE worker_thread;
static DWORD worker_thread_id; static DWORD worker_thread_id;
static CONDITION_VARIABLE thread_running_cond; static HANDLE thread_running_semaphore;
static CRITICAL_SECTION thread_running_mutex; static HANDLE thread_waiting_semaphore;
static bool8 thread_running = FALSE; static bool8 thread_running = FALSE;
static bool8 thread_exit = FALSE; static bool8 thread_exit = FALSE;
static DWORD WINAPI S9xAPUWorkerThread(LPVOID lpParam) static DWORD WINAPI S9xAPUWorkerThread(LPVOID lpParam)
{ {
DWORD result;
while (!thread_exit) while (!thread_exit)
{ {
EnterCriticalSection(&thread_running_mutex); result = WaitForSingleObject(thread_waiting_semaphore, INFINITE);
while (!thread_running)
{ if (thread_exit)
SleepConditionVariableCS(&thread_running_cond, &thread_running_mutex, INFINITE); break;
if (thread_exit)
break; if (!thread_running || result != WAIT_OBJECT_0)
} continue;
LeaveCriticalSection(&thread_running_mutex);
S9xAPUExecute(); S9xAPUExecute();
SNES::dsp.synchronize(); SNES::dsp.synchronize();
EnterCriticalSection(&thread_running_mutex);
thread_running = FALSE; thread_running = FALSE;
WakeAllConditionVariable(&thread_running_cond); ReleaseSemaphore(thread_running_semaphore, 1, NULL);
LeaveCriticalSection(&thread_running_mutex);
} }
return 0; return 0;
@ -293,26 +292,22 @@ static DWORD WINAPI S9xAPUWorkerThread(LPVOID lpParam)
static void S9xAPUThreadRun(void) static void S9xAPUThreadRun(void)
{ {
EnterCriticalSection(&thread_running_mutex);
thread_running = TRUE; thread_running = TRUE;
WakeAllConditionVariable(&thread_running_cond); ReleaseSemaphore(thread_waiting_semaphore, 1, 0);
LeaveCriticalSection(&thread_running_mutex);
} }
static void S9xAPUThreadWait(void) static void S9xAPUThreadWait(void)
{ {
EnterCriticalSection(&thread_running_mutex);
while (thread_running) while (thread_running)
{ {
SleepConditionVariableCS (&thread_running_cond, &thread_running_mutex, INFINITE); WaitForSingleObject(thread_running_semaphore, INFINITE);
} }
LeaveCriticalSection(&thread_running_mutex);
} }
static void S9xAPUThreadInit(void) static void S9xAPUThreadInit(void)
{ {
InitializeConditionVariable(&thread_running_cond); thread_running_semaphore = CreateSemaphore(NULL, 0, 1, NULL);
InitializeCriticalSection (&thread_running_mutex); thread_waiting_semaphore = CreateSemaphore(NULL, 0, 1, NULL);
thread_running = FALSE; thread_running = FALSE;
worker_thread = CreateThread(NULL, 0,S9xAPUWorkerThread, NULL, 0, &worker_thread_id); worker_thread = CreateThread(NULL, 0,S9xAPUWorkerThread, NULL, 0, &worker_thread_id);
} }
@ -323,6 +318,7 @@ static void S9xAPUThreadDeinit(void)
thread_exit = TRUE; thread_exit = TRUE;
S9xAPUThreadRun(); S9xAPUThreadRun();
WaitForSingleObject(worker_thread, INFINITE); WaitForSingleObject(worker_thread, INFINITE);
CloseHandle(worker_thread);
} }
#else #else