Skip to content

Commit

Permalink
Remove any dependence on singleton destruction order
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-webb committed Dec 20, 2024
1 parent b80e6ec commit 6fca7cb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
8 changes: 5 additions & 3 deletions src/main/cpp/fileappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ FileAppender::FileAppender(std::unique_ptr<FileAppenderPriv> priv)
FileAppender::~FileAppender()
{
finalize();
if (_priv->bufferedIO && 0 < _priv->bufferedSeconds)
ThreadUtility::instance()->removePeriodicTask(getName());
if (auto p = _priv->taskManager.lock())
p->value().removePeriodicTask(getName());
}

void FileAppender::setAppend(bool fileAppend1)
Expand Down Expand Up @@ -197,10 +197,12 @@ void FileAppender::activateOptionsInternal(Pool& p)
;
else if (0 < _priv->bufferedSeconds)
{
ThreadUtility::instance()->addPeriodicTask(getName()
auto taskManager = ThreadUtility::instancePtr();
taskManager->value().addPeriodicTask(getName()
, std::bind(&WriterAppenderPriv::flush, _priv)
, std::chrono::seconds(_priv->bufferedSeconds)
);
_priv->taskManager = taskManager;
}
else if (0 == _priv->bufferedSeconds)
{
Expand Down
26 changes: 16 additions & 10 deletions src/main/cpp/filewatchdog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ struct FileWatchdog::FileWatchdogPrivate{
FileWatchdogPrivate(const File& file1) :
file(file1), delay(DEFAULT_DELAY), lastModif(0),
warnedAlready(false),
taskName{ LOG4CXX_STR("WatchDog_") + file1.getName() },
taskManager{ ThreadUtility::instance() }
taskName{ LOG4CXX_STR("WatchDog_") + file1.getName() }
{ }


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

FileWatchdog::FileWatchdog(const File& file1)
Expand All @@ -76,12 +75,16 @@ FileWatchdog::~FileWatchdog()

bool FileWatchdog::is_active()
{
return m_priv->taskManager->hasPeriodicTask(m_priv->taskName);
bool result = false;
if (auto p = m_priv->taskManager.lock())
result = p->value().hasPeriodicTask(m_priv->taskName);
return result;
}

void FileWatchdog::stop()
{
m_priv->taskManager->removePeriodicTask(m_priv->taskName);
if (auto p = m_priv->taskManager.lock())
p->value().removePeriodicTask(m_priv->taskName);
}

/**
Expand Down Expand Up @@ -134,7 +137,8 @@ void FileWatchdog::checkAndConfigure()
void FileWatchdog::start()
{
checkAndConfigure();
if (!m_priv->taskManager->hasPeriodicTask(m_priv->taskName))
auto taskManager = ThreadUtility::instancePtr();
if (taskManager->value().hasPeriodicTask(m_priv->taskName))
{
if (LogLog::isDebugEnabled())
{
Expand All @@ -146,19 +150,21 @@ void FileWatchdog::start()
msg += LOG4CXX_STR(" ms interval");
LogLog::debug(msg);
}
m_priv->taskManager->addPeriodicTask(m_priv->taskName
taskManager->value().addPeriodicTask(m_priv->taskName
, std::bind(&FileWatchdog::checkAndConfigure, this)
, std::chrono::milliseconds(m_priv->delay)
);
m_priv->taskManager = taskManager;
}
}

void FileWatchdog::setDelay(long delay1){
m_priv->delay = delay1;
if (m_priv->taskManager->hasPeriodicTask(m_priv->taskName))
auto p = m_priv->taskManager.lock();
if (p && p->value().hasPeriodicTask(m_priv->taskName))
{
m_priv->taskManager->removePeriodicTask(m_priv->taskName);
m_priv->taskManager->addPeriodicTask(m_priv->taskName
p->value().removePeriodicTask(m_priv->taskName);
p->value().addPeriodicTask(m_priv->taskName
, std::bind(&FileWatchdog::checkAndConfigure, this)
, std::chrono::milliseconds(m_priv->delay)
);
Expand Down
14 changes: 9 additions & 5 deletions src/main/cpp/threadutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,18 @@ ThreadUtility::ThreadUtility()

ThreadUtility::~ThreadUtility() {}

ThreadUtility* ThreadUtility::instance()
auto ThreadUtility::instancePtr() -> ManagerPtr
{
using ThreadUtilityHolder = SingletonHolder<ThreadUtility>;
auto result = APRInitializer::getOrAddUnique<ThreadUtilityHolder>
auto result = APRInitializer::getOrAddUnique<Manager>
( []() -> ObjectPtr
{ return std::make_shared<ThreadUtilityHolder>(); }
{ return std::make_shared<Manager>(); }
);
return &result->value();
return result;
}

ThreadUtility* ThreadUtility::instance()
{
return &instancePtr()->value();
}

void ThreadUtility::configure( ThreadConfigurationType type )
Expand Down
10 changes: 6 additions & 4 deletions src/main/include/log4cxx/helpers/threadutility.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,10 @@ enum class ThreadConfigurationType
BlockSignalsAndNameThread,
};

class ThreadUtility;
LOG4CXX_PTR_DEF(ThreadUtility);

class LOG4CXX_EXPORT ThreadUtility
{
private:
friend class LOG4CXX_NS::helpers::SingletonHolder<ThreadUtility>;
friend class SingletonHolder<ThreadUtility>;
ThreadUtility();

LOG4CXX_NS::helpers::ThreadStartPre preStartFunction();
Expand Down Expand Up @@ -176,6 +173,11 @@ class LOG4CXX_EXPORT ThreadUtility
* Remove any periodic task matching \c namePrefix
*/
void removePeriodicTasksMatching(const LogString& namePrefix);

using Manager = SingletonHolder<ThreadUtility>;
LOG4CXX_PTR_DEF(Manager);

static ManagerPtr instancePtr();
};

} /* namespace helpers */
Expand Down
9 changes: 8 additions & 1 deletion src/main/include/log4cxx/private/fileappender_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <log4cxx/private/writerappender_priv.h>
#include <log4cxx/fileappender.h>
#include <log4cxx/helpers/threadutility.h>

namespace LOG4CXX_NS
{
Expand Down Expand Up @@ -63,9 +64,15 @@ struct FileAppender::FileAppenderPriv : public WriterAppender::WriterAppenderPri

/**
The number of seconds between each asynchronous output buffer flush.
Only active when <code>bufferedIO == true</code>.
Only used when <code>bufferedIO == true</code>.
*/
int bufferedSeconds{ 5 };

/**
Manages asynchronous output buffer flush.
Only used when <code>bufferedIO == true</code>.
*/
helpers::ThreadUtility::ManagerWeakPtr taskManager;
};

}
Expand Down

0 comments on commit 6fca7cb

Please sign in to comment.