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