Skip to content

Commit

Permalink
The ThreadUtility singleton will live longer than any FileWatchdog
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-webb committed Dec 20, 2024
1 parent 48113fa commit b3bc38c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/main/cpp/aprinitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,21 @@ APRInitializer::APRInitializer() :

APRInitializer::~APRInitializer()
{
deleteWatchDogs();
stopWatchDogs();
isDestructed = true;
#if APR_HAS_THREADS
std::lock_guard<std::mutex> lock(m_priv->mutex);
apr_threadkey_private_delete(m_priv->tlsKey);
#endif
}

void APRInitializer::deleteWatchDogs()
void APRInitializer::stopWatchDogs()
{
std::lock_guard<std::mutex> lock(m_priv->mutex);

while (!m_priv->watchdogs.empty())
{
m_priv->watchdogs.back()->stop();
delete m_priv->watchdogs.back();
m_priv->watchdogs.pop_back();
}
Expand All @@ -126,7 +127,7 @@ void APRInitializer::deleteWatchDogs()
void APRInitializer::unregisterAll()
{
FileWatchdog::stopAll();
getInstance().deleteWatchDogs();
getInstance().stopWatchDogs();
}

APRInitializer& APRInitializer::getInstance()
Expand Down
21 changes: 11 additions & 10 deletions src/main/cpp/filewatchdog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ struct FileWatchdog::FileWatchdogPrivate{
FileWatchdogPrivate(const File& file1) :
file(file1), delay(DEFAULT_DELAY), lastModif(0),
warnedAlready(false),
taskName{LOG4CXX_STR("WatchDog_") + file1.getName()}
taskName{ LOG4CXX_STR("WatchDog_") + file1.getName() },
taskManager{ ThreadUtility::instance() }
{ }


Expand All @@ -59,6 +60,7 @@ struct FileWatchdog::FileWatchdogPrivate{
std::mutex interrupt_mutex;
#endif
LogString taskName;
ThreadUtility* taskManager;
};

FileWatchdog::FileWatchdog(const File& file1)
Expand All @@ -68,17 +70,18 @@ FileWatchdog::FileWatchdog(const File& file1)

FileWatchdog::~FileWatchdog()
{
stop();
}


bool FileWatchdog::is_active()
{
return ThreadUtility::instance()->hasPeriodicTask(m_priv->taskName);
return m_priv->taskManager->hasPeriodicTask(m_priv->taskName);
}

void FileWatchdog::stop()
{
ThreadUtility::instance()->removePeriodicTask(m_priv->taskName);
m_priv->taskManager->removePeriodicTask(m_priv->taskName);
}

/**
Expand Down Expand Up @@ -130,9 +133,8 @@ void FileWatchdog::checkAndConfigure()

void FileWatchdog::start()
{
auto p = ThreadUtility::instance();
checkAndConfigure();
if (!p->hasPeriodicTask(m_priv->taskName))
if (!m_priv->taskManager->hasPeriodicTask(m_priv->taskName))
{
if (LogLog::isDebugEnabled())
{
Expand All @@ -144,7 +146,7 @@ void FileWatchdog::start()
msg += LOG4CXX_STR(" ms interval");
LogLog::debug(msg);
}
p->addPeriodicTask(m_priv->taskName
m_priv->taskManager->addPeriodicTask(m_priv->taskName
, std::bind(&FileWatchdog::checkAndConfigure, this)
, std::chrono::milliseconds(m_priv->delay)
);
Expand All @@ -153,11 +155,10 @@ void FileWatchdog::start()

void FileWatchdog::setDelay(long delay1){
m_priv->delay = delay1;
auto p = ThreadUtility::instance();
if (p->hasPeriodicTask(m_priv->taskName))
if (m_priv->taskManager->hasPeriodicTask(m_priv->taskName))
{
p->removePeriodicTask(m_priv->taskName);
p->addPeriodicTask(m_priv->taskName
m_priv->taskManager->removePeriodicTask(m_priv->taskName);
m_priv->taskManager->addPeriodicTask(m_priv->taskName
, std::bind(&FileWatchdog::checkAndConfigure, this)
, std::chrono::milliseconds(m_priv->delay)
);
Expand Down
2 changes: 1 addition & 1 deletion src/main/include/log4cxx/helpers/aprinitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class APRInitializer
private: // Modifiers
void addObject(size_t key, const ObjectPtr& pObject);
const ObjectPtr& findOrAddObject(size_t key, std::function<ObjectPtr()> creator);
void deleteWatchDogs();
void stopWatchDogs();
private: // Attributes
LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(APRInitializerPrivate, m_priv)
private: // Class methods
Expand Down

0 comments on commit b3bc38c

Please sign in to comment.