Skip to content

Commit 7e7943f

Browse files
committed
Always initialize SDL from main thread
1 parent cb5fd17 commit 7e7943f

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

rpcs3/Input/sdl_instance.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifdef HAVE_SDL3
22

33
#include "stdafx.h"
4+
#include "sdl_instance.h"
5+
#include "Emu/System.h"
46

57
#ifndef _MSC_VER
68
#pragma GCC diagnostic push
@@ -11,10 +13,6 @@
1113
#pragma GCC diagnostic pop
1214
#endif
1315

14-
#include "util/logs.hpp"
15-
16-
#include "sdl_instance.h"
17-
1816
LOG_CHANNEL(sdl_log, "SDL");
1917

2018
sdl_instance::~sdl_instance()
@@ -35,9 +33,12 @@ sdl_instance& sdl_instance::get_instance()
3533

3634
void sdl_instance::pump_events()
3735
{
38-
const std::lock_guard<std::mutex> lock(m_instance_mutex);
36+
std::lock_guard lock(m_instance_mutex);
37+
3938
if (m_initialized)
39+
{
4040
SDL_PumpEvents();
41+
}
4142
}
4243

4344
void sdl_instance::set_hint(const char* name, const char* value)
@@ -50,7 +51,25 @@ void sdl_instance::set_hint(const char* name, const char* value)
5051

5152
bool sdl_instance::initialize()
5253
{
53-
const std::lock_guard<std::mutex> lock(m_instance_mutex);
54+
std::lock_guard lock(m_instance_mutex);
55+
56+
if (m_initialized)
57+
{
58+
return true;
59+
}
60+
61+
bool instance_success = false;
62+
63+
Emu.BlockingCallFromMainThread([this, &instance_success]()
64+
{
65+
instance_success = initialize_impl();
66+
});
67+
68+
return instance_success;
69+
}
70+
71+
bool sdl_instance::initialize_impl()
72+
{
5473
// Only init SDL once. SDL uses a global state internally...
5574
if (m_initialized)
5675
{

rpcs3/Input/sdl_instance.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct sdl_instance
88
{
99
public:
1010
sdl_instance() = default;
11-
~sdl_instance();
11+
virtual ~sdl_instance();
1212

1313
static sdl_instance& get_instance();
1414

@@ -17,6 +17,7 @@ struct sdl_instance
1717

1818
private:
1919
void set_hint(const char* name, const char* value);
20+
bool initialize_impl();
2021

2122
bool m_initialized = false;
2223
std::mutex m_instance_mutex;

rpcs3/Input/sdl_pad_handler.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
#include "stdafx.h"
44
#include "sdl_pad_handler.h"
5+
#include "sdl_instance.h"
56
#include "Emu/system_utils.hpp"
67
#include "Emu/system_config.h"
78
#include "Emu/System.h"
8-
#include "sdl_instance.h"
99

1010
#include <mutex>
1111

@@ -180,14 +180,7 @@ bool sdl_pad_handler::Init()
180180
if (m_is_init)
181181
return true;
182182

183-
bool instance_success;
184-
185-
Emu.BlockingCallFromMainThread([&instance_success]()
186-
{
187-
instance_success = sdl_instance::get_instance().initialize();
188-
});
189-
190-
if (!instance_success)
183+
if (!sdl_instance::get_instance().initialize())
191184
return false;
192185

193186
if (g_cfg.io.load_sdl_mappings)

0 commit comments

Comments
 (0)