Added a mutex to avoid data race conditions in the shared link status object
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Mon, 2 May 2011 07:37:37 +0000 (09:37 +0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Mon, 2 May 2011 09:35:17 +0000 (11:35 +0200)
src/link/linkstatusanalyzer.cpp
src/link/linkstatusanalyzer.h

index b85d77a..8e55a44 100644 (file)
@@ -9,6 +9,7 @@
 using namespace std;
 using boost::posix_time::microsec_clock;
 using boost::posix_time::ptime;
+using boost::mutex;
 using I2n::Logger::GlobalLogger;
 
 //-----------------------------------------------------------------------------
@@ -26,7 +27,8 @@ LinkStatusAnalyzer::LinkStatusAnalyzer(
     CurrentLinkStatus( LinkStatus_Down ),
     CurrentNotificationStatus( NotificationStatus_NotReported ),
     TimeLinkStatusChanged( microsec_clock::universal_time() ),
-    StatusNotifierCmd( status_notifier_cmd )
+    StatusNotifierCmd( status_notifier_cmd ),
+    Mutex()
 {
     BOOST_ASSERT( 0 <= hosts_down_limit );
     BOOST_ASSERT( 0 <= link_up_interval_in_min );
@@ -40,6 +42,8 @@ void LinkStatusAnalyzer::notify_host_up( const string &host_address )
 {
     BOOST_ASSERT( !host_address.empty() );
 
+    Mutex.lock();
+
     GlobalLogger.info() << "- Host up: " << host_address << endl;
 
     add_host_up( host_address );
@@ -51,12 +55,16 @@ void LinkStatusAnalyzer::notify_host_up( const string &host_address )
 
     // removed from the list?
     BOOST_ASSERT( HostsDownList.count( host_address ) == 0 );
+
+    Mutex.unlock();
 }
 
 void LinkStatusAnalyzer::notify_host_down( const string &host_address )
 {
     BOOST_ASSERT( !host_address.empty() );
 
+    Mutex.lock();
+
     GlobalLogger.info() << "- Host down: " << host_address << endl;
 
     add_host_down( host_address );
@@ -68,6 +76,8 @@ void LinkStatusAnalyzer::notify_host_down( const string &host_address )
 
     // inserted in the list?
     BOOST_ASSERT( HostsDownList.count( host_address ) == 1 );
+
+    Mutex.unlock();
 }
 
 void LinkStatusAnalyzer::add_host_up( const string &host_address )
index b08f376..ffd2930 100644 (file)
@@ -5,6 +5,7 @@
 #include <string>
 
 #include <boost/asio.hpp>
+#include <boost/thread/mutex.hpp>
 
 #include "link/statusnotifiercommand.h"
 
@@ -74,7 +75,8 @@ private:
     boost::posix_time::ptime TimeLinkStatusChanged;
     /// command used to notify the status of the link
     StatusNotifierCommand StatusNotifierCmd;
-
+    /// mutual exclusion variable to avoid data races
+    boost::mutex Mutex;
 
 };