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

View File

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