Fix wiimote thread issues.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6943 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Jordan Woyak 2011-01-28 03:48:53 +00:00
parent 2a7f305d45
commit cb91be81ed
2 changed files with 25 additions and 27 deletions

View File

@ -44,7 +44,7 @@ volatile bool g_run_wiimote_thread = false;
std::thread g_wiimote_threads[MAX_WIIMOTES] = {};
Common::CriticalSection g_refresh_critsec;
void WiimoteThreadFunc(Wiimote& arg);
void WiimoteThreadFunc(Wiimote* arg);
void StartWiimoteThreads();
void StopWiimoteThreads();
@ -425,6 +425,8 @@ void Refresh()
if (WIIMOTE_SRC_REAL & g_wiimote_sources[i])
++wanted_wiimotes;
StopWiimoteThreads();
g_refresh_critsec.Enter();
// Remove wiimotes that are paired with slots no longer configured for a
@ -433,20 +435,14 @@ void Refresh()
if (g_wiimotes[i] && (!(WIIMOTE_SRC_REAL & g_wiimote_sources[i]) ||
!g_wiimotes[i]->IsConnected()))
{
// TODO: this looks broken
delete g_wiimotes[i];
g_wiimotes[i] = NULL;
g_wiimote_threads[i].join();
--g_wiimotes_found;
}
// Don't scan for wiimotes if we don't want any more
if (wanted_wiimotes <= g_wiimotes_found)
// Scan for wiimotes if we want more
if (wanted_wiimotes > g_wiimotes_found)
{
g_refresh_critsec.Leave();
return;
}
// Scan for wiimotes
unsigned int num_wiimotes = FindWiimotes(g_wiimotes, wanted_wiimotes);
@ -458,7 +454,9 @@ void Refresh()
DEBUG_LOG(WIIMOTE, "Connected to %i additional Real Wiimotes", num_new_wiimotes);
#endif
g_wiimotes_found = num_wiimotes;
}
g_refresh_critsec.Leave();
@ -510,8 +508,8 @@ void StartWiimoteThreads()
{
g_run_wiimote_thread = true;
for (unsigned int i = 0; i < MAX_WIIMOTES; ++i)
if (g_wiimotes[i])
g_wiimote_threads[i] = std::thread(WiimoteThreadFunc, *g_wiimotes[i]);
if (g_wiimotes[i] && !g_wiimote_threads[i].joinable())
g_wiimote_threads[i] = std::thread(WiimoteThreadFunc, g_wiimotes[i]);
}
void StopWiimoteThreads()
@ -522,35 +520,35 @@ void StopWiimoteThreads()
g_wiimote_threads[i].join();
}
void WiimoteThreadFunc(Wiimote& wiimote)
void WiimoteThreadFunc(Wiimote* wiimote)
{
#ifdef __APPLE__
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
#endif
char thname[] = "Wiimote # Thread";
thname[8] = (char)('1' + wiimote.index);
thname[8] = (char)('1' + wiimote->index);
Common::SetCurrentThreadName(thname);
// rumble briefly
wiimote.Rumble();
wiimote->Rumble();
Host_ConnectWiimote(wiimote.index, true);
Host_ConnectWiimote(wiimote->index, true);
// main loop
while (g_run_wiimote_thread && wiimote.IsConnected())
while (g_run_wiimote_thread && wiimote->IsConnected())
{
// hopefully this is alright
while (wiimote.Write()) {}
while (wiimote->Write()) {}
#ifndef __APPLE__
// sleep if there was nothing to read
if (false == wiimote.Read())
if (false == wiimote->Read())
#endif
Common::SleepCurrentThread(1);
}
Host_ConnectWiimote(wiimote.index, false);
Host_ConnectWiimote(wiimote->index, false);
#ifdef __APPLE__
[pool release];

View File

@ -35,7 +35,7 @@ typedef std::pair<u8*,u8> Report;
namespace WiimoteReal
{
class Wiimote
class Wiimote : NonCopyable
{
friend class WiimoteEmu::Wiimote;
public: