From c5db148e984a972643641fa7560ca1cba0e845d8 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Mon, 16 Nov 2015 15:24:58 -1000 Subject: [PATCH] Fixed main thread join issue --- lib/x11/ApplicationXlib.hpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/x11/ApplicationXlib.hpp b/lib/x11/ApplicationXlib.hpp index f905e5c..915ad01 100644 --- a/lib/x11/ApplicationXlib.hpp +++ b/lib/x11/ApplicationXlib.hpp @@ -221,15 +221,29 @@ public: return PLAT_XLIB; } + /* Empty handler for SIGTERM */ + static void _sigterm(int) {} + int run() { if (!m_xDisp) return 1; + /* SIGTERM will be used to terminate main thread when client thread ends */ + pthread_t mainThread = pthread_self(); + struct sigaction s; + s.sa_handler = _sigterm; + sigemptyset(&s.sa_mask); + s.sa_flags = 0; + sigaction(SIGTERM, &s, nullptr); + /* Spawn client thread */ int clientReturn = INT_MIN; std::thread clientThread([&]() - {clientReturn = m_callback.appMain(this);}); + { + clientReturn = m_callback.appMain(this); + pthread_kill(mainThread, SIGTERM); + }); /* Begin application event loop */ while (clientReturn == INT_MIN) @@ -238,7 +252,12 @@ public: FD_ZERO(&fds); FD_SET(m_xcbFd, &fds); FD_SET(m_dbusFd, &fds); - select(m_maxFd+1, &fds, NULL, NULL, NULL); + if (select(m_maxFd+1, &fds, NULL, NULL, NULL) < 0) + { + /* SIGTERM handled here */ + if (errno == EINTR) + break; + } if (FD_ISSET(m_xcbFd, &fds)) {