pj64team / Project64-Legacy

Finishing what we started.
79 stars 7 forks source link

Fix deadlock in CPU thread #50

Closed parasyte closed 1 year ago

parasyte commented 1 year ago

Finally found the culprit causing that "emulation thread failed to terminate plugins" error. It had nothing to do with plugins! The deadlock is caused by the CPU thread sending messages to the GUI thread, ALL INTERNAL to PJ64.

When this happens, the two threads wait on each other, and no progress can be made. Deadlock!

The proper fix is to make the GUI thread continue processing messages while it waits for the CPU thread to stop.

This does keep the disaster recovery code, even though we do not want it to be called, ever. It is probably best to just remove it. The new upper bound deadline for disaster recovery is 2 seconds. Which should be plenty of time for the threads to cooperate, even on heavily loaded (or very slow) systems.

oddMLan commented 1 year ago

Awesome job! 🥳🕺

parasyte commented 1 year ago

There is a case where plugins do actually block the CPU thread. It can happen especially with homebrew (icepir8 found the NEO Myth Menu hangs the CPU thread). The problem is that the RSP and the CPU are run cooperatively, but if the RSP program sits in a loop waiting for the CPU to do something, then the whole system hangs with 100% CPU usage.

CPU and RSP should definitely be run in parallel, but the synchronization required for it is difficult.