Skip to content

Commit

Permalink
WiimoteReal: Remove unneeded refresh mutex
Browse files Browse the repository at this point in the history
Now that the scan code only ever runs on one thread, I don't think
the g_refresh_lock mutex is necessary anymore.
  • Loading branch information
leoetlino committed Jul 10, 2016
1 parent c827fdd commit c1b2530
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 55 deletions.
2 changes: 0 additions & 2 deletions Source/Core/Core/HW/WiimoteEmu/EmuSubroutines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ void Wiimote::RequestStatus(const wm_request_status* const rs)
{
using namespace WiimoteReal;

std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);

if (g_wiimotes[m_index])
{
wm_request_status rpt = {};
Expand Down
1 change: 0 additions & 1 deletion Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,6 @@ void Wiimote::Update()
{
using namespace WiimoteReal;

std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);
if (g_wiimotes[m_index])
{
const Report& rpt = g_wiimotes[m_index]->ProcessReadQueue();
Expand Down
4 changes: 0 additions & 4 deletions Source/Core/Core/HW/WiimoteReal/IOdarwin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,6 @@ - (void)l2capChannelData:(IOBluetoothL2CAPChannel*)l2capChannel
IOBluetoothDevice* device = [l2capChannel device];
WiimoteReal::WiimoteDarwin* wm = nullptr;

std::lock_guard<std::recursive_mutex> lk(WiimoteReal::g_refresh_lock);

for (int i = 0; i < MAX_WIIMOTES; i++)
{
wm = static_cast<WiimoteReal::WiimoteDarwin*>(WiimoteReal::g_wiimotes[i]);
Expand Down Expand Up @@ -541,8 +539,6 @@ - (void)l2capChannelClosed:(IOBluetoothL2CAPChannel*)l2capChannel
IOBluetoothDevice* device = [l2capChannel device];
WiimoteReal::WiimoteDarwin* wm = nullptr;

std::lock_guard<std::recursive_mutex> lk(WiimoteReal::g_refresh_lock);

for (int i = 0; i < MAX_WIIMOTES; i++)
{
wm = static_cast<WiimoteReal::WiimoteDarwin*>(WiimoteReal::g_wiimotes[i]);
Expand Down
51 changes: 4 additions & 47 deletions Source/Core/Core/HW/WiimoteReal/WiimoteReal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ void DoneWithWiimote(int index);

static bool g_real_wiimotes_initialized = false;

std::recursive_mutex g_refresh_lock;

Wiimote* g_wiimotes[MAX_BBMOTES];
WiimoteScanner g_wiimote_scanner;

Expand Down Expand Up @@ -455,8 +453,6 @@ void WiimoteScanner::SetScanMode(WiimoteScanMode scan_mode)

static void CheckForDisconnectedWiimotes()
{
std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);

for (unsigned int i = 0; i < MAX_BBMOTES; ++i)
if (g_wiimotes[i] && !g_wiimotes[i]->IsConnected())
HandleWiimoteDisconnect(i);
Expand Down Expand Up @@ -618,8 +614,6 @@ void Initialize(::Wiimote::InitializeMode init_mode)
else
g_wiimote_scanner.SetScanMode(WiimoteScanMode::DO_NOT_SCAN);

std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);

// wait for connection because it should exist before state load
if (init_mode == ::Wiimote::InitializeMode::DO_WAIT_FOR_WIIMOTES)
{
Expand Down Expand Up @@ -653,8 +647,6 @@ void Shutdown()
{
g_wiimote_scanner.StopThread();

std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);

NOTICE_LOG(WIIMOTE, "WiimoteReal::Shutdown");

for (unsigned int i = 0; i < MAX_BBMOTES; ++i)
Expand All @@ -677,12 +669,9 @@ void Pause()

void ChangeWiimoteSource(unsigned int index, int source)
{
{
std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);
g_wiimote_sources[index] = source;
// kill real connection (or swap to different slot)
DoneWithWiimote(index);
}
g_wiimote_sources[index] = source;
// kill real connection (or swap to different slot)
DoneWithWiimote(index);

// reconnect to the emulator
Host_ConnectWiimote(index, false);
Expand All @@ -707,8 +696,6 @@ static bool TryToConnectWiimoteN(Wiimote* wm, unsigned int i)

void TryToConnectWiimote(Wiimote* wm)
{
std::unique_lock<std::recursive_mutex> lk(g_refresh_lock);

for (unsigned int i = 0; i < MAX_WIIMOTES; ++i)
{
if (TryToConnectWiimoteN(wm, i))
Expand All @@ -717,30 +704,21 @@ void TryToConnectWiimote(Wiimote* wm)
break;
}
}

lk.unlock();

delete wm;
}

void TryToConnectBalanceBoard(Wiimote* wm)
{
std::unique_lock<std::recursive_mutex> lk(g_refresh_lock);

if (TryToConnectWiimoteN(wm, WIIMOTE_BALANCE_BOARD))
{
wm = nullptr;
}

lk.unlock();

delete wm;
}

void DoneWithWiimote(int index)
{
std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);

Wiimote* wm = g_wiimotes[index];

if (wm)
Expand All @@ -757,11 +735,7 @@ void DoneWithWiimote(int index)
void HandleWiimoteDisconnect(int index)
{
Wiimote* wm = nullptr;

{
std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);
std::swap(wm, g_wiimotes[index]);
}
std::swap(wm, g_wiimotes[index]);

if (wm)
{
Expand All @@ -784,27 +758,19 @@ void Refresh()

void InterruptChannel(int _WiimoteNumber, u16 _channelID, const void* _pData, u32 _Size)
{
std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);
if (g_wiimotes[_WiimoteNumber])
g_wiimotes[_WiimoteNumber]->InterruptChannel(_channelID, _pData, _Size);
}

void ControlChannel(int _WiimoteNumber, u16 _channelID, const void* _pData, u32 _Size)
{
std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);

if (g_wiimotes[_WiimoteNumber])
g_wiimotes[_WiimoteNumber]->ControlChannel(_channelID, _pData, _Size);
}

// Read the Wiimote once
void Update(int _WiimoteNumber)
{
// Try to get a lock and return without doing anything if we fail
// This avoids deadlocks when adding a Wiimote during continuous scan
if (!g_refresh_lock.try_lock())
return;

if (g_wiimotes[_WiimoteNumber])
g_wiimotes[_WiimoteNumber]->Update();

Expand All @@ -813,25 +779,16 @@ void Update(int _WiimoteNumber)
{
Host_ConnectWiimote(_WiimoteNumber, false);
}
g_refresh_lock.unlock();
}

void ConnectOnInput(int _WiimoteNumber)
{
// see Update() above
if (!g_refresh_lock.try_lock())
return;

if (g_wiimotes[_WiimoteNumber])
g_wiimotes[_WiimoteNumber]->ConnectOnInput();

g_refresh_lock.unlock();
}

void StateChange(EMUSTATE_CHANGE newState)
{
// std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);

// TODO: disable/enable auto reporting, maybe
}

Expand Down
1 change: 0 additions & 1 deletion Source/Core/Core/HW/WiimoteReal/WiimoteReal.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ class WiimoteScanner
#endif
};

extern std::recursive_mutex g_refresh_lock;
extern WiimoteScanner g_wiimote_scanner;
extern Wiimote* g_wiimotes[MAX_BBMOTES];

Expand Down

0 comments on commit c1b2530

Please sign in to comment.