Skip to content

Commit

Permalink
Use selectable event to terminate logger thread (#848)
Browse files Browse the repository at this point in the history
settingThread of Logger select DB change with 1000ms timeout. It causes up to 1 second wait while destroy the logger instance. The 1 second wait is not necessary and can be replaced by SelectableEvent
  • Loading branch information
Junchao-Mellanox authored Jan 16, 2024
1 parent b563580 commit 2711f6f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
14 changes: 11 additions & 3 deletions common/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,21 @@ void Logger::terminateSettingThread()

if (m_settingThread)
{
m_runSettingThread = false;
m_stopEvent->notify();

m_settingThread->join();

m_settingThread = nullptr;

m_stopEvent = nullptr;
}
}

void Logger::restartSettingThread()
{
terminateSettingThread();

m_runSettingThread = true;
m_stopEvent = std::make_unique<SelectableEvent>(0);

m_settingThread.reset(new std::thread(&Logger::settingThread, this));
}
Expand Down Expand Up @@ -195,8 +197,9 @@ void Logger::settingThread()
auto table = std::make_shared<SubscriberStateTable>(&db, CFG_LOGGER_TABLE_NAME);
selectables.emplace(CFG_LOGGER_TABLE_NAME, table);
select.addSelectable(table.get());
select.addSelectable(m_stopEvent.get());

while (m_runSettingThread)
while (1)
{

Selectable *selectable = nullptr;
Expand All @@ -216,6 +219,11 @@ void Logger::settingThread()
continue;
}

if (selectable == m_stopEvent.get())
{
break;
}

KeyOpFieldsValuesTuple koValues;
SubscriberStateTable *subscriberStateTable = NULL;
subscriberStateTable = dynamic_cast<SubscriberStateTable *>(selectable);
Expand Down
3 changes: 2 additions & 1 deletion common/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <functional>

#include "concurrentmap.h"
#include "selectableevent.h"

namespace swss {

Expand Down Expand Up @@ -161,7 +162,7 @@ class Logger
std::atomic<Output> m_output = { SWSS_SYSLOG };
std::unique_ptr<std::thread> m_settingThread;
std::mutex m_mutex;
volatile bool m_runSettingThread = true;
std::unique_ptr<SelectableEvent> m_stopEvent;
};

}

0 comments on commit 2711f6f

Please sign in to comment.