From 1d1ae364c80aecd1a7ffdd2ea5e26e41d54c5df4 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Tue, 3 May 2011 15:30:03 +0200 Subject: [PATCH] Clean thread shutdown --- src/host/pingscheduler.cpp | 1 + src/main.cpp | 40 ++++++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/host/pingscheduler.cpp b/src/host/pingscheduler.cpp index f5252ee..75bb927 100644 --- a/src/host/pingscheduler.cpp +++ b/src/host/pingscheduler.cpp @@ -96,6 +96,7 @@ bool PingScheduler::start_pinging() void PingScheduler::stop_pinging() { + // This is thread safe IoService.stop(); } diff --git a/src/main.cpp b/src/main.cpp index ee66765..ea440d9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -108,25 +108,25 @@ void start_pingers( << endl; } } - -#if 0 - // Main loop to handle scheduled ping requests - BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list ) - { - scheduler->wait_pinging_thread(); - } -#endif } void stop_pingers( const PingSchedulerList &scheduler_list ) { - // stop each ping scheduler + // Stop each ping scheduler + GlobalLogger.info() << "Telling all pingers to stop"; BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list ) { scheduler->stop_pinging_thread(); } + + GlobalLogger.info() << "Waiting for threads to shut down"; + BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list ) + { + scheduler->wait_pinging_thread(); + } + GlobalLogger.info() << "All ping threads are gone"; } /** @@ -163,13 +163,18 @@ void handle_signals() if (!err) { if (signal == SIGUSR1) - I2n::Logger::set_log_level( I2n::Logger::get_log_level()+1 ); //lint !e534 - else if (signal == SIGUSR2) + { + int new_log_level = I2n::Logger::get_log_level()+1; + I2n::Logger::set_log_level( new_log_level ); //lint !e534 + GlobalLogger.info() << "Increased log level to " << new_log_level; + } else if (signal == SIGUSR2) + { I2n::Logger::set_log_level( default_log_level ); //lint !e534 - else if (signal == SIGTERM || signal == SIGINT) + GlobalLogger.info() << "Reset log level to normal (" << default_log_level << ")"; + } else if (signal == SIGTERM || signal == SIGINT) + { want_quit = true; - else if (signal == SIGHUP) - std::cerr << "DEBUG: Caught HUP signal" << std::endl; + } } else want_quit = true; } @@ -181,10 +186,12 @@ int main( int argc, char *argv[] ) block_all_signals(); init_logger(); + GlobalLogger.notice() << "started"; ConfigurationItem configuration = get_configuration( argc, argv ); if ( configuration.get() != NULL ) { + bool daemon_mode = configuration->get_daemon(); if ( daemon_mode ) { @@ -201,8 +208,9 @@ int main( int argc, char *argv[] ) // React to signals (main thread will sleep) handle_signals(); - // TODO: Shut down threads - std::cerr << "DEBUG: Shutting down" << std::endl; + stop_pingers( scheduler_list ); + + GlobalLogger.notice() << "exiting"; } return 0; -- 1.7.1