From a51d8677602345e79b17dcb00d10440d92872854 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Mon, 2 May 2011 09:37:37 +0200 Subject: [PATCH] Added a mutex to avoid data race conditions in the shared link status object --- src/link/linkstatusanalyzer.cpp | 12 +++++++++++- src/link/linkstatusanalyzer.h | 4 +++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/link/linkstatusanalyzer.cpp b/src/link/linkstatusanalyzer.cpp index b85d77a..8e55a44 100644 --- a/src/link/linkstatusanalyzer.cpp +++ b/src/link/linkstatusanalyzer.cpp @@ -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 ) diff --git a/src/link/linkstatusanalyzer.h b/src/link/linkstatusanalyzer.h index b08f376..ffd2930 100644 --- a/src/link/linkstatusanalyzer.h +++ b/src/link/linkstatusanalyzer.h @@ -5,6 +5,7 @@ #include #include +#include #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; }; -- 1.7.1