From d9d30dd782870b2ab6584fb54b7c19a2a9ae4c78 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 29 May 2014 01:30:04 +0200 Subject: =?UTF-8?q?Use=20a=20timed=20event=20to=20force=20the=20exit=202?= =?UTF-8?q?=E2=80=AFseconds=20after=20an=20exit=20signal=20is=20received?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix #2469 --- src/main.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 40825c9..b5abad9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -17,6 +18,13 @@ static volatile std::atomic reload(false); // flag is set and all connections are closed, we can exit properly. static bool exiting = false; +// The global (and only) TimedEventsManager. It can be accessed by any +// class to add new TimedEvents, and it is used in the main loop to provide +// a timeout to the poller, this way the method execute_expired_events can +// be called on time, without having to do an active “poll” on it every n +// seconds. +TimedEventsManager timed_events; + /** * Provide an helpful message to help the user write a minimal working * configuration file. @@ -32,8 +40,11 @@ int config_help(const std::string& missing_option) return 1; } -static void sigint_handler(int, siginfo_t*, void*) +static void sigint_handler(int sig, siginfo_t*, void*) { + // In 2 seconds, repeat the same signal, to force the exit + timed_events.add_event(TimedEvent(std::chrono::steady_clock::now() + 2s, + [sig]() { raise(sig); })); stop.store(true); } @@ -90,9 +101,10 @@ int main(int ac, char** av) xmpp_component->start(); - const std::chrono::milliseconds timeout(-1); + auto timeout = timed_events.get_timeout(); while (p->poll(timeout) != -1) { + timed_events.execute_expired_events(); // Check for empty irc_clients (not connected, or with no joined // channel) and remove them xmpp_component->clean(); @@ -131,6 +143,7 @@ int main(int ac, char** av) xmpp_component->close(); if (exiting && p->size() == 1 && xmpp_component->is_document_open()) xmpp_component->close_document(); + timeout = timed_events.get_timeout(); } log_info("All connection cleanely closed, have a nice day."); return 0; -- cgit v1.2.3