Added logic in the emulator thread to check if requests to acquire the mutex have been made by the gui. If requests are found, the emulator thread will sleep so that the gui thread can gain access and service the requests.

This commit is contained in:
Matthew Budd 2020-07-17 22:14:20 -04:00
parent 6fb247d785
commit 2d4451a43c
1 changed files with 12 additions and 0 deletions

View File

@ -57,6 +57,7 @@ static int noconfig=0;
static int frameskip=0; static int frameskip=0;
static int periodic_saves = 0; static int periodic_saves = 0;
static int mutexLocks = 0; static int mutexLocks = 0;
static int mutexPending = 0;
static bool emulatorHasMutux = 0; static bool emulatorHasMutux = 0;
extern double g_fpsScale; extern double g_fpsScale;
@ -912,7 +913,9 @@ static void DoFun(int frameskip, int periodic_saves)
void fceuWrapperLock(void) void fceuWrapperLock(void)
{ {
mutexPending++;
consoleWindow->mutex->lock(); consoleWindow->mutex->lock();
mutexPending--;
mutexLocks++; mutexLocks++;
} }
@ -920,7 +923,9 @@ bool fceuWrapperTryLock(int timeout)
{ {
bool lockAcq; bool lockAcq;
mutexPending++;
lockAcq = consoleWindow->mutex->tryLock( timeout ); lockAcq = consoleWindow->mutex->tryLock( timeout );
mutexPending--;
if ( lockAcq ) if ( lockAcq )
{ {
@ -951,6 +956,13 @@ int fceuWrapperUpdate( void )
{ {
bool lock_acq; bool lock_acq;
// If a request is pending,
// sleep to allow request to be serviced.
if ( mutexPending > 0 )
{
usleep( 100000 );
}
lock_acq = fceuWrapperTryLock(); lock_acq = fceuWrapperTryLock();
if ( !lock_acq ) if ( !lock_acq )