Clean thread shutdown
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 3 May 2011 13:30:03 +0000 (15:30 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 3 May 2011 13:30:03 +0000 (15:30 +0200)
src/host/pingscheduler.cpp
src/main.cpp

index f5252ee..75bb927 100644 (file)
@@ -96,6 +96,7 @@ bool PingScheduler::start_pinging()
 
 void PingScheduler::stop_pinging()
 {
+    // This is thread safe
     IoService.stop();
 }
 
index ee66765..ea440d9 100644 (file)
@@ -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;