Core: clang-format SysThreadBase.cpp

This commit is contained in:
TheLastRar 2020-11-18 16:13:34 +00:00 committed by refractionpcsx2
parent ba1497ca48
commit 9983bcfbe6
1 changed files with 87 additions and 71 deletions

View File

@ -24,9 +24,9 @@
// (Called form outside the context of this thread)
// --------------------------------------------------------------------------------------
SysThreadBase::SysThreadBase() :
m_ExecMode( ExecMode_NoThreadYet )
, m_ExecModeMutex()
SysThreadBase::SysThreadBase()
: m_ExecMode(ExecMode_NoThreadYet)
, m_ExecModeMutex()
{
}
@ -34,11 +34,10 @@ void SysThreadBase::Start()
{
_parent::Start();
Sleep( 1 );
Sleep(1);
pxAssertDev( (m_ExecMode == ExecMode_Closing) || (m_ExecMode == ExecMode_Closed),
"Unexpected thread status during SysThread startup."
);
pxAssertDev((m_ExecMode == ExecMode_Closing) || (m_ExecMode == ExecMode_Closed),
"Unexpected thread status during SysThread startup.");
m_sem_event.Post();
}
@ -46,13 +45,14 @@ void SysThreadBase::Start()
void SysThreadBase::OnStart()
{
if( !pxAssertDev( m_ExecMode == ExecMode_NoThreadYet, "SysSustainableThread:Start(): Invalid execution mode" ) ) return;
if (!pxAssertDev(m_ExecMode == ExecMode_NoThreadYet, "SysSustainableThread:Start(): Invalid execution mode"))
return;
m_sem_Resume.Reset();
m_sem_ChangingExecMode.Reset();
FrankenMutex( m_ExecModeMutex );
FrankenMutex( m_RunningLock );
FrankenMutex(m_ExecModeMutex);
FrankenMutex(m_RunningLock);
_parent::OnStart();
}
@ -76,48 +76,52 @@ void SysThreadBase::OnStart()
// Suspension must cancel itself forcefully or risk crashing whatever other action is
// in progress.
//
void SysThreadBase::Suspend( bool isBlocking )
void SysThreadBase::Suspend(bool isBlocking)
{
if (!pxAssertDev(!IsSelf(),"Suspend/Resume are not allowed from this thread.")) return;
if (!IsRunning()) return;
if (!pxAssertDev(!IsSelf(), "Suspend/Resume are not allowed from this thread."))
return;
if (!IsRunning())
return;
// shortcut ExecMode check to avoid deadlocking on redundant calls to Suspend issued
// from Resume or OnResumeReady code.
if( m_ExecMode == ExecMode_Closed ) return;
if (m_ExecMode == ExecMode_Closed)
return;
{
ScopedLock locker( m_ExecModeMutex );
ScopedLock locker(m_ExecModeMutex);
switch( m_ExecMode.load() )
switch (m_ExecMode.load())
{
// Invalid thread state, nothing to suspend
case ExecMode_NoThreadYet:
// Check again -- status could have changed since above.
case ExecMode_Closed: return;
case ExecMode_Closed:
return;
case ExecMode_Pausing:
case ExecMode_Paused:
if( !isBlocking )
throw Exception::CancelEvent( L"Cannot suspend in non-blocking fashion: Another thread is pausing the VM state." );
if (!isBlocking)
throw Exception::CancelEvent(L"Cannot suspend in non-blocking fashion: Another thread is pausing the VM state.");
m_ExecMode = ExecMode_Closing;
m_sem_Resume.Post();
m_sem_ChangingExecMode.Wait();
break;
break;
case ExecMode_Opened:
m_ExecMode = ExecMode_Closing;
break;
break;
case ExecMode_Closing:
break;
break;
}
pxAssertDev( m_ExecMode == ExecMode_Closing, "ExecMode should be nothing other than Closing..." );
pxAssertDev(m_ExecMode == ExecMode_Closing, "ExecMode should be nothing other than Closing...");
m_sem_event.Post();
}
if( isBlocking )
if (isBlocking)
m_RunningLock.Wait();
}
@ -127,22 +131,25 @@ void SysThreadBase::Suspend( bool isBlocking )
//
void SysThreadBase::Pause(bool debug)
{
if( IsSelf() || !IsRunning() ) return;
if (IsSelf() || !IsRunning())
return;
// shortcut ExecMode check to avoid deadlocking on redundant calls to Suspend issued
// from Resume or OnResumeReady code.
if( (m_ExecMode == ExecMode_Closed) || (m_ExecMode == ExecMode_Paused) ) return;
if ((m_ExecMode == ExecMode_Closed) || (m_ExecMode == ExecMode_Paused))
return;
{
ScopedLock locker( m_ExecModeMutex );
ScopedLock locker(m_ExecModeMutex);
// Check again -- status could have changed since above.
if( (m_ExecMode == ExecMode_Closed) || (m_ExecMode == ExecMode_Paused) ) return;
if ((m_ExecMode == ExecMode_Closed) || (m_ExecMode == ExecMode_Paused))
return;
if( m_ExecMode == ExecMode_Opened )
if (m_ExecMode == ExecMode_Opened)
m_ExecMode = ExecMode_Pausing;
pxAssertDev( m_ExecMode == ExecMode_Pausing, "ExecMode should be nothing other than Pausing..." );
pxAssertDev(m_ExecMode == ExecMode_Pausing, "ExecMode should be nothing other than Pausing...");
if (debug)
OnPauseDebug();
@ -156,14 +163,15 @@ void SysThreadBase::Pause(bool debug)
void SysThreadBase::PauseSelf()
{
if( !IsSelf() || !IsRunning() ) return;
if (!IsSelf() || !IsRunning())
return;
{
ScopedLock locker( m_ExecModeMutex );
if( m_ExecMode == ExecMode_Opened )
ScopedLock locker(m_ExecModeMutex);
if (m_ExecMode == ExecMode_Opened)
m_ExecMode = ExecMode_Pausing;
OnPause();
m_sem_event.Post();
}
@ -171,7 +179,8 @@ void SysThreadBase::PauseSelf()
void SysThreadBase::PauseSelfDebug()
{
if (!IsSelf() || !IsRunning()) return;
if (!IsSelf() || !IsRunning())
return;
{
ScopedLock locker(m_ExecModeMutex);
@ -200,10 +209,12 @@ void SysThreadBase::PauseSelfDebug()
//
void SysThreadBase::Resume()
{
if( IsSelf() ) return;
if( m_ExecMode == ExecMode_Opened ) return;
if (IsSelf())
return;
if (m_ExecMode == ExecMode_Opened)
return;
ScopedLock locker( m_ExecModeMutex );
ScopedLock locker(m_ExecModeMutex);
// Implementation Note:
// The entire state coming out of a Wait is indeterminate because of user input
@ -211,18 +222,20 @@ void SysThreadBase::Resume()
// sanity checks against m_ExecMode/m_Running status, and if something doesn't feel
// right, we should abort; the user may have canceled the action before it even finished.
switch( m_ExecMode.load() )
switch (m_ExecMode.load())
{
case ExecMode_Opened: return;
case ExecMode_Opened:
return;
case ExecMode_NoThreadYet:
{
Start();
if( !m_running || (m_ExecMode == ExecMode_NoThreadYet) )
if (!m_running || (m_ExecMode == ExecMode_NoThreadYet))
throw Exception::ThreadCreationError(this);
if( m_ExecMode == ExecMode_Opened ) return;
if (m_ExecMode == ExecMode_Opened)
return;
}
// fall through...
// fall through...
case ExecMode_Closing:
case ExecMode_Pausing:
@ -230,18 +243,21 @@ void SysThreadBase::Resume()
// state before continuing...
m_RunningLock.Wait();
if( !m_running ) return;
if( (m_ExecMode != ExecMode_Closed) && (m_ExecMode != ExecMode_Paused) ) return;
if( !GetCorePlugins().AreLoaded() ) return;
break;
if (!m_running)
return;
if ((m_ExecMode != ExecMode_Closed) && (m_ExecMode != ExecMode_Paused))
return;
if (!GetCorePlugins().AreLoaded())
return;
break;
case ExecMode_Paused:
case ExecMode_Closed:
break;
break;
}
pxAssertDev( (m_ExecMode == ExecMode_Closed) || (m_ExecMode == ExecMode_Paused),
"SysThreadBase is not in a closed/paused state? wtf!" );
pxAssertDev((m_ExecMode == ExecMode_Closed) || (m_ExecMode == ExecMode_Paused),
"SysThreadBase is not in a closed/paused state? wtf!");
OnResumeReady();
m_ExecMode = ExecMode_Opened;
@ -269,7 +285,7 @@ void SysThreadBase::OnCleanupInThread()
}
void SysThreadBase::OnSuspendInThread() {}
void SysThreadBase::OnResumeInThread( bool isSuspended ) {}
void SysThreadBase::OnResumeInThread(bool isSuspended) {}
// Tests for Pause and Suspend/Close requests. If the thread is trying to be paused or
// closed, it will enter a wait/holding pattern here in this method until the managing
@ -282,22 +298,22 @@ void SysThreadBase::OnResumeInThread( bool isSuspended ) {}
// continued execution unimpeded.
bool SysThreadBase::StateCheckInThread()
{
switch( m_ExecMode.load() )
switch (m_ExecMode.load())
{
#ifdef PCSX2_DEVBUILD // optimize out handlers for these cases in release builds.
#ifdef PCSX2_DEVBUILD // optimize out handlers for these cases in release builds.
case ExecMode_NoThreadYet:
// threads should never have this state set while the thread is in any way
// active or alive. (for obvious reasons!!)
pxFailDev( "Invalid execution state detected." );
return false;
#endif
pxFailDev("Invalid execution state detected.");
return false;
#endif
case ExecMode_Opened:
// Other cases don't need TestCancel() because its built into the various
// threading wait/signal actions.
TestCancel();
return false;
return false;
// -------------------------------------
case ExecMode_Pausing:
@ -309,21 +325,21 @@ bool SysThreadBase::StateCheckInThread()
[[fallthrough]];
case ExecMode_Paused:
while( m_ExecMode == ExecMode_Paused )
while (m_ExecMode == ExecMode_Paused)
m_sem_Resume.WaitWithoutYield();
m_RunningLock.Acquire();
if( m_ExecMode != ExecMode_Closing )
if (m_ExecMode != ExecMode_Closing)
{
if ( g_CDVDReset )
if (g_CDVDReset)
DoCDVDopen();
g_CDVDReset = false;
break;
}
m_sem_ChangingExecMode.Post();
[[fallthrough]];
// fallthrough if we're switching to closing state...
// -------------------------------------
case ExecMode_Closing:
@ -335,16 +351,16 @@ bool SysThreadBase::StateCheckInThread()
[[fallthrough]];
case ExecMode_Closed:
while( m_ExecMode == ExecMode_Closed )
while (m_ExecMode == ExecMode_Closed)
m_sem_Resume.WaitWithoutYield();
m_RunningLock.Acquire();
OnResumeInThread( true );
OnResumeInThread(true);
g_CDVDReset = false;
break;
break;
jNO_DEFAULT;
jNO_DEFAULT;
}
return true;
}