Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WiimoteReal: Don't block on refresh #3988

Merged
merged 2 commits into from Jul 10, 2016

Conversation

leoetlino
Copy link
Member

@leoetlino leoetlino commented Jul 7, 2016

This changes Refresh() to use the existing scanning thread to scan for devices, instead of running the scan on the UI thread and blocking it. (Should fix issue 8992)

This also makes the UI thread not block when Continuous Scanning is enabled then disabled, and removes a random 0-500ms delay on exit.


This change is Reviewable

@JMC47
Copy link
Contributor

JMC47 commented Jul 10, 2016

Very, very nice.

@delroth
Copy link
Member

delroth commented Jul 10, 2016

As I was just saying on IRC: it feels like the logic here is being overly complex and that it could be simplified much further. For example, if we're always going to do scanning in a thread, we can potentially get rid of a tons of things that were assuming scanning could happen in two different threads. We can also keep the thread running continuously instead of having StopScanning with two behaviours (stop / do not stop thread). etc.

This changes Refresh() to use the existing scanning thread to scan for
devices, instead of running the scan on the UI thread and blocking it.

Also makes the UI thread not block when Continuous Scanning is disabled
and removes duplicated code.

Should fix issue 8992.

Under the hood:
* The scanning thread is now always active, even when continuous
  scanning is disabled.
* The initialize code which waits for Wiimotes to be connected also
  uses the scanning thread instead of scanning on yet another thread.
* The scanning thread now always checks for disconnected devices, to
  avoid Dolphin thinking a Wiimote is still connected when it isn't. So
  we now check if we need new Wiimotes or a Balance Board at scan time.
@leoetlino
Copy link
Member Author

I have switched to enums (instead of booleans), and removed code that shouldn't be necessary anymore since scanning now only happens on one thread. Also made it clear that the scanning thread is always running.

@delroth
Copy link
Member

delroth commented Jul 10, 2016

I'm not convinced about the thread safety of the new code. For example:

void InterruptChannel(int _WiimoteNumber, u16 _channelID, const void* _pData, u32 _Size);
void ControlChannel(int _WiimoteNumber, u16 _channelID, const void* _pData, u32 _Size);

These two functions now perform unsynchronized accesses to g_wiimotes but nothing guarantees that they would be called from the scanning thread. In fact, g_wiimotes itself is global, which is a fairly terrible design...

Can you explain why removing the lock is safe? I understand that it was recursive before because of the potential "double scan" issue, and that's probably useless now, but still feels like it should be a simple mutex.

This replaces a recursive mutex with a normal mutex.
@leoetlino
Copy link
Member Author

You're right; that was not very safe. I have added back a simple mutex in places where they are needed.

@delroth delroth merged commit 3a895f8 into dolphin-emu:master Jul 10, 2016
@leoetlino leoetlino deleted the scanning-block branch July 13, 2016 11:49
@degasus
Copy link
Member

degasus commented Aug 8, 2016

@leoetlino
Copy link
Member Author

Huh, that's a weird regression… So basically stopping a game prevents it from working again?

My first guess would be that it relies on WiimoteReal::Refresh() (which is basically now a noop if continuous scanning is enabled) and for some reason it needs the scanning thread to really be stopped and then restarted. I can't test it on Android (no ARMv8 device), but I'll try to see if there is something I can do…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants