Skip to content

Commit

Permalink
orchagent: Adding mutex while accessing the database (sonic-net#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
stcheng authored Sep 27, 2016
1 parent 6784f7b commit 2c50cf6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions orchagent/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern "C" {

#include <iostream>
#include <map>
#include <mutex>
#include <thread>
#include <chrono>

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions orchagent/notifications.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <mutex>

#include "portsorch.h"

extern "C" {
Expand All @@ -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<mutex> lock(gDbMutex);

/* Wait until gPortsOrch is initialized */
if (!gPortsOrch || !gPortsOrch->isInitDone())
return;
Expand Down
4 changes: 4 additions & 0 deletions orchagent/orch.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <mutex>

#include "orch.h"
#include "portsorch.h"
Expand All @@ -7,6 +8,7 @@

using namespace swss;

extern mutex gDbMutex;
extern PortsOrch *gPortsOrch;

Orch::Orch(DBConnector *db, string tableName) :
Expand Down Expand Up @@ -56,6 +58,8 @@ bool Orch::execute(string tableName)
{
SWSS_LOG_ENTER();

lock_guard<mutex> lock(gDbMutex);

auto consumer_it = m_consumerMap.find(tableName);
if (consumer_it == m_consumerMap.end())
{
Expand Down

0 comments on commit 2c50cf6

Please sign in to comment.