core: terminate the poll thread (#150)

* core: terminate the poll thread

* core: use sigaction instead of signal

* core: use VA_RESTART for the unlock signal
This commit is contained in:
Maximilian Seidler 2024-03-08 23:54:34 +01:00 committed by GitHub
parent b4a1c8ccbb
commit 5eaa7c0834
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -292,6 +292,14 @@ void CHyprlock::onGlobalRemoved(void* data, struct wl_registry* registry, uint32
// end wl_registry // end wl_registry
static void registerSignalAction(int sig, void (*handler)(int), int sa_flags = 0) {
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = sa_flags;
sigaction(sig, &sa, NULL);
}
static void handleUnlockSignal(int sig) { static void handleUnlockSignal(int sig) {
if (sig == SIGUSR1) { if (sig == SIGUSR1) {
Debug::log(LOG, "Unlocking with a SIGUSR1"); Debug::log(LOG, "Unlocking with a SIGUSR1");
@ -299,6 +307,10 @@ static void handleUnlockSignal(int sig) {
} }
} }
static void handlePollTerminate(int sig) {
;
}
static void handleCriticalSignal(int sig) { static void handleCriticalSignal(int sig) {
g_pHyprlock->attemptRestoreOnDeath(); g_pHyprlock->attemptRestoreOnDeath();
abort(); abort();
@ -342,9 +354,10 @@ void CHyprlock::run() {
lockSession(); lockSession();
signal(SIGUSR1, handleUnlockSignal); registerSignalAction(SIGUSR1, handleUnlockSignal, SA_RESTART);
signal(SIGSEGV, handleCriticalSignal); registerSignalAction(SIGUSR2, handlePollTerminate);
signal(SIGABRT, handleCriticalSignal); registerSignalAction(SIGSEGV, handleCriticalSignal);
registerSignalAction(SIGABRT, handleCriticalSignal);
pollfd pollfds[] = { pollfd pollfds[] = {
{ {
@ -355,7 +368,7 @@ void CHyprlock::run() {
std::thread pollThr([this, &pollfds]() { std::thread pollThr([this, &pollfds]() {
while (!m_bTerminate) { while (!m_bTerminate) {
int ret = poll(pollfds, 1, 5000 /* 5 seconds, reasonable. It's because we might need to terminate */); int ret = poll(pollfds, 1, 5000 /* 5 seconds, reasonable. Just in case we need to terminate and the signal fails */);
if (ret < 0) { if (ret < 0) {
Debug::log(CRIT, "[core] Polling fds failed with {}", errno); Debug::log(CRIT, "[core] Polling fds failed with {}", errno);
@ -477,6 +490,8 @@ void CHyprlock::run() {
wl_display_disconnect(m_sWaylandState.display); wl_display_disconnect(m_sWaylandState.display);
pthread_kill(pollThr.native_handle(), SIGUSR2);
// wait for threads to exit cleanly to avoid a coredump // wait for threads to exit cleanly to avoid a coredump
pollThr.join(); pollThr.join();
timersThr.join(); timersThr.join();