-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
GCAdapter: improve thread safety #3805
Conversation
a566e86
to
cea56d2
Compare
Well it fixes the crash. But the multithreading code still looks a bit messy. |
Well, the multithreading code is a bit spotty but I don’t see any easy way around it as we can’t block the CPU thread and need another thread to fetch data from the controller; doing away with the scanning thread is kind of difficult if we don’t want users to have a manual interaction (we probably could make it run only when there is no adapter detected, although it may cause issues). We could put everything which isn’t on the CPU thread behind a mutex, but it may be a brutal thing to do. |
What's worrying me is I looked up libusb, and it's supposed to be thread safe. Yet somehow we are getting errors. Also s_handle and s_detected are set/accessed on multiple threads and should be atomic. |
Maybe you should glean some ideas of how to make it stable by looking at my Android implementation? :P |
Fixes my crash finally. I'm happy. |
@@ -37,6 +37,7 @@ static std::atomic<int> s_controller_payload_size = {0}; | |||
static std::thread s_adapter_thread; | |||
static Common::Flag s_adapter_thread_running; | |||
|
|||
static std::recursive_mutex s_init_mutex; |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Are we going to review/merge this, or pursue a better solution? |
@mathieui: Is this still WIP? |
67d9a6c
to
9915b1d
Compare
@mimimi085181 This should be fine now, one last code review to make sure I haven’t missed a spot, and maybe another test from @JMC47 before merging. |
It's crashed in master about 6 more times since I last reviewed it. I really need to just generally use this build in master to know for sure the crash is completely gone. I think it is though. |
Is this ready for review? |
This actually fixes both of my GC Adapter crashes. The one running two dolphin's and closing the first one that has the GC adapter used to crash the second Dolphin. It no longer does that in this pull request. |
Review status: 0 of 1 files reviewed at latest revision, 2 unresolved discussions. Source/Core/InputCommon/GCAdapter.cpp, line 476 [r3] (raw file):
Unnecessary extra line. Comments from Reviewable |
9915b1d
to
a62e457
Compare
Review status: 0 of 1 files reviewed at latest revision, 2 unresolved discussions. Source/Core/InputCommon/GCAdapter.cpp, line 476 [r3] (raw file):
|
Reviewed 1 of 1 files at r4. Comments from Reviewable |
LGTM |
Anything holding this up from merge? |
Review status: all files reviewed at latest revision, 2 unresolved discussions. Source/Core/InputCommon/GCAdapter.cpp, line 454 [r4] (raw file):
May you add a comment why this is required? eg in which path is it already locked, and why it's not needed to call this function here. Comments from Reviewable |
Reviewed 1 of 1 files at r4. Comments from Reviewable |
Source/Core/InputCommon/GCAdapter.cpp, line 456 [r4] (raw file):
This name is kind of misleading. It has "NoLock" in the name, yet is being used between a mutex that is trying to be locked, effectively stating the opposite of what the function name intends. Comments from Reviewable |
Review status: all files reviewed at latest revision, 5 unresolved discussions. Source/Core/InputCommon/GCAdapter.cpp, line 338 [r4] (raw file):
Now that I look at this again, this can just be: std::unique_lock<std::mutex> lock(s_init_mutex, std::defer_lock);
if (!lock.try_lock())
return; which would let you get rid of the explicit locking and unlocking elsewhere. Source/Core/InputCommon/GCAdapter.cpp, line 454 [r4] (raw file):
This can also be: std::unique_lock<std::mutex> lock(s_init_mutex, std::defer_lock);
if (!lock.try_lock())
return; Comments from Reviewable |
a62e457
to
f7dc777
Compare
make sure Reset() can’t be run concurrently with AddGCAdapter() or ResetRumble() (which is called on other threads) which can cause crashes (issue dolphin-emu#9462)
f7dc777
to
54b4eff
Compare
Review status: 0 of 1 files reviewed at latest revision, 5 unresolved discussions. Source/Core/InputCommon/GCAdapter.cpp, line 338 [r4] (raw file):
|
Reviewed 1 of 1 files at r6. Comments from Reviewable |
Reset() and Setup() are not used outside of this namespace
849b84d
to
1bbbe92
Compare
|
make sure Reset() can’t be run concurrently with AddGCAdapter(), which can cause crashes (issue #9462).
As the crash happens only on windows, I can’t test this.
This change is![Reviewable](https://camo.githubusercontent.com/23b05f5fb48215c989e92cc44cf6512512d083132bd3daf689867c8d9d386888/68747470733a2f2f72657669657761626c652e696f2f7265766965775f627574746f6e2e737667)