Skip to content

Commit da2554d

Browse files
committed
fix quitting behavior again
1 parent 8df902d commit da2554d

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/core/system.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ class System {
166166
bool running() { return m_running; }
167167
const bool *runningPtr() { return &m_running; }
168168
bool quitting() { return m_quitting; }
169+
bool terminating() { return m_terminating; }
169170
int exitCode() { return m_exitCode; }
170171
bool emergencyExit() { return m_emergencyExit; }
171172
[[gnu::cold]] void pause(bool exception = false) {
@@ -174,11 +175,16 @@ class System {
174175
m_eventBus->signal(Events::ExecutionFlow::Pause{exception});
175176
}
176177
void resume() {
177-
if (m_running) return;
178+
if (m_running || m_terminating) return;
178179
m_running = true;
179180
m_eventBus->signal(Events::ExecutionFlow::Run{});
180181
}
181182
virtual void testQuit(int code) = 0;
183+
[[gnu::cold]] void terminateSignalSafe() {
184+
// about the only thing signal handlers can do safely is switch a flag
185+
m_terminating = true;
186+
m_running = false;
187+
}
182188
[[gnu::cold]] void quit(int code = 0) {
183189
m_quitting = true;
184190
pause();
@@ -259,6 +265,7 @@ class System {
259265
std::string m_currentLocale;
260266
bool m_running = false;
261267
bool m_quitting = false;
268+
bool m_terminating = false;
262269
int m_exitCode = 0;
263270
struct LocaleInfo {
264271
const std::string filename;

src/main/main.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ struct Cleaner {
166166
std::function<void()> f;
167167
};
168168

169+
void handleSignal(auto signal) {
170+
PCSX::g_system->terminateSignalSafe();
171+
}
172+
169173
int pcsxMain(int argc, char **argv) {
170174
ZoneScoped;
171175
// Command line arguments are parsed after this point.
@@ -193,9 +197,8 @@ int pcsxMain(int argc, char **argv) {
193197
// enabled as much as possible.
194198
SystemImpl *system = new SystemImpl(args);
195199
PCSX::g_system = system;
196-
static std::atomic_bool scheduledQuit = false;
197-
auto sigint = std::signal(SIGINT, [](auto signal) { scheduledQuit = true; });
198-
auto sigterm = std::signal(SIGTERM, [](auto signal) { scheduledQuit = true; });
200+
auto sigint = std::signal(SIGINT, handleSignal);
201+
auto sigterm = std::signal(SIGTERM, handleSignal);
199202
#ifndef _WIN32
200203
signal(SIGPIPE, SIG_IGN);
201204
#endif
@@ -455,10 +458,10 @@ runner.init({
455458
// when the emulator is paused.
456459
s_ui->update();
457460
}
458-
if (scheduledQuit) {
459-
PCSX::g_system->quit(-1);
460-
return exitCode;
461-
}
461+
if (system->terminating()) {
462+
PCSX::g_system->quit(-1);
463+
return exitCode;
464+
}
462465
}
463466
} catch (...) {
464467
// This will ensure we don't do certain cleanups that are awaiting other tasks,

0 commit comments

Comments
 (0)