Skip to content

SDL: call SDL_PumpEvents from main thread - #17519

Merged
Megamouse merged 2 commits into
RPCS3:masterfrom
Megamouse:dates
Sep 21, 2025
Merged

Megamouse merged 2 commits into
RPCS3:masterfrom
Megamouse:dates

Conversation

@Megamouse

Copy link
Copy Markdown
Contributor

maybe fixes #17518
maybe fixes #17259
maybe fixes #17227
maybe fixes #16348
maybe fixes #15947
maybe fixes #15715

@ItsLaguna

ItsLaguna commented Sep 20, 2025 •

Copy link
Copy Markdown

Works fine on my end in Windows at least, will edit once I try on Linux.

Edit: Works in Linux aswell but there's a little issue (if it can be called that as it's very minor):

If you disconnect the controller while on Pads and Refresh, it won't get picked up again until you boot a game. This very same thing can sort of be reproduce while in-game, but upon pressing Save it picks the controller again.

Edit2: Just tried in Windows and it doesn't happen there so it seems to be Linux-specific.

@digant73

digant73 commented Sep 20, 2025 •

Copy link
Copy Markdown
Contributor

forcing the execution of SDL_PumpEvents on the main thread fixes the issues. That was the first test I did.
However, that solution was horrific being the opposite of using a fully decoupled thread (as it is currently) to manage pads inputs.
SDL seems to depend on some thread's resources where SDL_PumpEvents was running.
To fix the issues, it seems enough to always re-init the lib before SDL_PumpEvents is executed on a new thread. SDL_Init can continue to be initialized on the main thread while SDL_PumpEvents can be run on a different thread.
That is easily achieved with the following changes in initialize() (see the comment for the changes):

bool sdl_instance::initialize()
{
	std::lock_guard lock(m_instance_mutex);

	if (m_initialized)
	{
		SDL_Quit();                                          // reset the lib
		m_initialized = false;                           // change status
//		return true;                                         // continue with a new initialization
	}

...

This solution has been confirmed as working also by all the users in the reported bug reports.
It is working on Pad menu, game, opening Pad menu while in game etc.

@Megamouse

Copy link
Copy Markdown
Contributor Author

SDL init and quit are only supposed to be called once for the whole application, no matter how many threads you use, and Pumpevents is by documentation supposed to be run on the main thread.
Changing the init will only lead to the bugs we already had in the past.
And we can have multiple threads using SDL at the same time, so that's bound to lead to many other issues

@Megamouse
Megamouse merged commit 1667998 into RPCS3:master Sep 21, 2025
8 of 9 checks passed
@digant73

Copy link
Copy Markdown
Contributor

Yes, a safe solution. Maybe SDL will be improved to support this new technology called multi-threading.
There was a minor bug on SDL lib responsible for memory leaks possibly propagated on RPCS3. It has been immediately fixed by the SDL team.

libsdl-org/SDL#13998

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment