From 2c50cf686854e4be671aed5a5c290ecad475b948 Mon Sep 17 00:00:00 2001 From: Shuotian Cheng Date: Mon, 26 Sep 2016 17:15:58 -0700 Subject: [PATCH] orchagent: Adding mutex while accessing the database (#93) --- orchagent/main.cpp | 4 ++++ orchagent/notifications.cpp | 7 +++++++ orchagent/orch.cpp | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/orchagent/main.cpp b/orchagent/main.cpp index bd8019e44d43..34506f8dbf01 100644 --- a/orchagent/main.cpp +++ b/orchagent/main.cpp @@ -9,6 +9,7 @@ extern "C" { #include #include +#include #include #include @@ -48,6 +49,9 @@ sai_object_id_t gVirtualRouterId; sai_object_id_t gUnderlayIfId; MacAddress gMacAddress; +/* Global database mutex */ +mutex gDbMutex; + const char *test_profile_get_value ( _In_ sai_switch_profile_id_t profile_id, _In_ const char *variable) diff --git a/orchagent/notifications.cpp b/orchagent/notifications.cpp index b4adae81e1d5..f52ea6f86af4 100644 --- a/orchagent/notifications.cpp +++ b/orchagent/notifications.cpp @@ -1,3 +1,5 @@ +#include + #include "portsorch.h" extern "C" { @@ -6,10 +8,15 @@ extern "C" { #include "logger.h" +extern mutex gDbMutex; extern PortsOrch *gPortsOrch; void on_port_state_change(uint32_t count, sai_port_oper_status_notification_t *data) { + SWSS_LOG_ENTER(); + + lock_guard lock(gDbMutex); + /* Wait until gPortsOrch is initialized */ if (!gPortsOrch || !gPortsOrch->isInitDone()) return; diff --git a/orchagent/orch.cpp b/orchagent/orch.cpp index c016d12f4cf8..a9044d671bc1 100644 --- a/orchagent/orch.cpp +++ b/orchagent/orch.cpp @@ -1,4 +1,5 @@ #include +#include #include "orch.h" #include "portsorch.h" @@ -7,6 +8,7 @@ using namespace swss; +extern mutex gDbMutex; extern PortsOrch *gPortsOrch; Orch::Orch(DBConnector *db, string tableName) : @@ -56,6 +58,8 @@ bool Orch::execute(string tableName) { SWSS_LOG_ENTER(); + lock_guard lock(gDbMutex); + auto consumer_it = m_consumerMap.find(tableName); if (consumer_it == m_consumerMap.end()) {