Skip to content

Commit

Permalink
Make ThreadUtility 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 a1fc4bc commit 42e04d4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/main/cpp/aprinitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ APRInitializer::APRInitializer() :

APRInitializer::~APRInitializer()
{
stopWatchDogs();
isDestructed = true;
#if APR_HAS_THREADS
std::lock_guard<std::mutex> lock(m_priv->mutex);
Expand Down
12 changes: 10 additions & 2 deletions src/main/cpp/threadutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#if LOG4CXX_EVENTS_AT_EXIT
#include <log4cxx/private/atexitregistry.h>
#endif
#if !defined(LOG4CXX)
#define LOG4CXX 1
#endif
#include <log4cxx/helpers/aprinitializer.h>

namespace LOG4CXX_NS
{
Expand Down Expand Up @@ -117,8 +121,12 @@ ThreadUtility::~ThreadUtility() {}

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

void ThreadUtility::configure( ThreadConfigurationType type )
Expand Down
3 changes: 0 additions & 3 deletions src/main/include/log4cxx/helpers/filewatchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
#include <time.h>
#include <log4cxx/helpers/pool.h>
#include <log4cxx/file.h>
#include <atomic>
#include <thread>
#include <condition_variable>

namespace LOG4CXX_NS
{
Expand Down
52 changes: 52 additions & 0 deletions src/main/include/log4cxx/helpers/singletonholder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef LOG4CXX_SINGLETON_HOLDER_H
#define LOG4CXX_SINGLETON_HOLDER_H

#include <log4cxx/helpers/object.h>

namespace LOG4CXX_NS
{
namespace helpers
{

/** Wraps any singleton object so it can be added to APRInitializer
*/
template <class T>
class SingletonHolder : public Object
{
using ThisType = SingletonHolder<T>;
T m_data;
struct Unused : public helpers::Class
{
LogString getName() const override { return LOG4CXX_STR("SingletonHolder"); }
};
public: // Object method stubs
const helpers::Class& getClass() const override { static Unused notUsed; return notUsed; }
BEGIN_LOG4CXX_CAST_MAP()
LOG4CXX_CAST_ENTRY(ThisType)
END_LOG4CXX_CAST_MAP()

public: // Accessors
T& value() { return m_data; }
};

} // namespace helpers
} // namespace LOG4CXX_NS

#endif //LOG4CXX_SINGLETON_HOLDER_H
3 changes: 2 additions & 1 deletion src/main/include/log4cxx/helpers/threadutility.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <chrono>

#include "log4cxx/logstring.h"
#include "widelife.h"
#include "singletonholder.h"

namespace LOG4CXX_NS
{
Expand Down Expand Up @@ -72,6 +72,7 @@ class LOG4CXX_EXPORT ThreadUtility
{
private:
friend class LOG4CXX_NS::helpers::WideLife<ThreadUtility>;
friend class LOG4CXX_NS::helpers::SingletonHolder<ThreadUtility>;
ThreadUtility();

LOG4CXX_NS::helpers::ThreadStartPre preStartFunction();
Expand Down

0 comments on commit 42e04d4

Please sign in to comment.