Skip to content

Commit

Permalink
Added lock guards around the DSP LLE cycle count updating.
Browse files Browse the repository at this point in the history
Moved the check outside of the flag setting.
Changed the isRunning flag to a Common::Flag.
  • Loading branch information
skidau committed Dec 21, 2014
1 parent 9b29093 commit d6016c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
43 changes: 28 additions & 15 deletions Source/Core/Core/HW/DSPLLE/DSPLLE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@

DSPLLE::DSPLLE()
{
m_bIsRunning = false;
m_bIsRunning.Clear();
{
std::lock_guard<std::mutex> lk(m_csDSPCycleCountActive);
m_cycle_count = 0;
}
}

static Common::Event dspEvent;
Expand Down Expand Up @@ -88,9 +91,14 @@ void DSPLLE::dsp_thread(DSPLLE *dsp_lle)
{
Common::SetCurrentThreadName("DSP thread");

while (dsp_lle->m_bIsRunning)
while (dsp_lle->m_bIsRunning.IsSet())
{
int cycles = (int)dsp_lle->m_cycle_count;
int cycles = 0;
{
std::lock_guard<std::mutex> lk(dsp_lle->m_csDSPCycleCountActive);
cycles = (int)dsp_lle->m_cycle_count;
}

if (cycles > 0)
{
std::lock_guard<std::mutex> lk(dsp_lle->m_csDSPThreadActive);
Expand All @@ -102,7 +110,10 @@ void DSPLLE::dsp_thread(DSPLLE *dsp_lle)
{
DSPInterpreter::RunCyclesThread(cycles);
}
Common::AtomicStore(dsp_lle->m_cycle_count, 0);
{
std::lock_guard<std::mutex> lk(dsp_lle->m_csDSPCycleCountActive);
dsp_lle->m_cycle_count = 0;
}
}
else
{
Expand Down Expand Up @@ -177,7 +188,7 @@ bool DSPLLE::Initialize(bool bWii, bool bDSPThread)
g_dsp.cpu_ram = Memory::base;
DSPCore_Reset();

m_bIsRunning = true;
m_bIsRunning.Set(true);

InitInstructionTable();

Expand All @@ -192,7 +203,7 @@ bool DSPLLE::Initialize(bool bWii, bool bDSPThread)
void DSPLLE::DSP_StopSoundStream()
{
DSPInterpreter::Stop();
m_bIsRunning = false;
m_bIsRunning.Clear();
if (m_bDSPThread)
{
ppcEvent.Set();
Expand All @@ -212,15 +223,15 @@ u16 DSPLLE::DSP_WriteControlRegister(u16 _uFlag)

// Check if the CPU has set an external interrupt (CR_EXTERNAL_INT)
// and immediately process it, if it has.
if (_uFlag & 2)
if (m_bDSPThread)
{
if (m_bDSPThread)
{
// External interrupt pending: this is the zelda ucode.
// Disable the DSP thread because there is no performance gain.
requestDisableThread = true;
}
// External interrupt pending: this is the zelda ucode.
// Disable the DSP thread because there is no performance gain.
requestDisableThread = true;
}

if (_uFlag & 2)
{
if (!m_bDSPThread)
{
DSPCore_CheckExternalInterrupt();
Expand Down Expand Up @@ -338,9 +349,11 @@ void DSPLLE::DSP_Update(int cycles)
{
// Wait for dsp thread to complete its cycle. Note: this logic should be thought through.
ppcEvent.Wait();
Common::AtomicStore(m_cycle_count, dsp_cycles);
{
std::lock_guard<std::mutex> lk(m_csDSPCycleCountActive);
m_cycle_count += dsp_cycles;
}
dspEvent.Set();

}
}

Expand Down
5 changes: 3 additions & 2 deletions Source/Core/Core/HW/DSPLLE/DSPLLE.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ class DSPLLE : public DSPEmulator

std::thread m_hDSPThread;
std::mutex m_csDSPThreadActive;
std::mutex m_csDSPCycleCountActive;
bool m_bWii;
bool m_bDSPThread;
bool m_bIsRunning;
volatile u32 m_cycle_count;
Common::Flag m_bIsRunning;
u32 m_cycle_count;
};

0 comments on commit d6016c9

Please sign in to comment.