This is an automated email from the ASF dual-hosted git repository. rmiddleton pushed a commit to branch LOGCXX-431 in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git
commit 39bbf1b3bc17b60c7fc54c7f3e20ffa18ae2770d Author: Robert Middleton <[email protected]> AuthorDate: Fri Aug 20 23:19:01 2021 -0400 Turned the thread utility into a singleton --- src/main/cpp/asyncappender.cpp | 2 +- src/main/cpp/filewatchdog.cpp | 2 +- src/main/cpp/socketappenderskeleton.cpp | 2 +- src/main/cpp/sockethubappender.cpp | 2 +- src/main/cpp/telnetappender.cpp | 2 +- src/main/cpp/threadutility.cpp | 66 +++++++++++++---------- src/main/include/CMakeLists.txt | 2 + src/main/include/log4cxx/helpers/threadutility.h | 68 +++++++++++++----------- 8 files changed, 80 insertions(+), 66 deletions(-) diff --git a/src/main/cpp/asyncappender.cpp b/src/main/cpp/asyncappender.cpp index c9f87ad..7ab05b7 100644 --- a/src/main/cpp/asyncappender.cpp +++ b/src/main/cpp/asyncappender.cpp @@ -51,7 +51,7 @@ AsyncAppender::AsyncAppender() locationInfo(false), blocking(true) { - dispatcher = ThreadUtility::createThread( LOG4CXX_STR("AyncAppend"), &AsyncAppender::dispatch, this ); + dispatcher = ThreadUtility::instance()->createThread( LOG4CXX_STR("AyncAppend"), &AsyncAppender::dispatch, this ); } AsyncAppender::~AsyncAppender() diff --git a/src/main/cpp/filewatchdog.cpp b/src/main/cpp/filewatchdog.cpp index 14942cd..3848ed3 100644 --- a/src/main/cpp/filewatchdog.cpp +++ b/src/main/cpp/filewatchdog.cpp @@ -93,7 +93,7 @@ void FileWatchdog::start() { checkAndConfigure(); - thread = ThreadUtility::createThread( LOG4CXX_STR("Filewatchdog"), &FileWatchdog::run, this ); + thread = ThreadUtility::instance()->createThread( LOG4CXX_STR("Filewatchdog"), &FileWatchdog::run, this ); } bool FileWatchdog::is_interrupted() diff --git a/src/main/cpp/socketappenderskeleton.cpp b/src/main/cpp/socketappenderskeleton.cpp index a310cba..e3e1375 100644 --- a/src/main/cpp/socketappenderskeleton.cpp +++ b/src/main/cpp/socketappenderskeleton.cpp @@ -163,7 +163,7 @@ void SocketAppenderSkeleton::fireConnector() { LogLog::debug(LOG4CXX_STR("Connector thread not alive: starting monitor.")); - thread = ThreadUtility::createThread( LOG4CXX_STR("SocketAppend"), &SocketAppenderSkeleton::monitor, this ); + thread = ThreadUtility::instance()->createThread( LOG4CXX_STR("SocketAppend"), &SocketAppenderSkeleton::monitor, this ); } } diff --git a/src/main/cpp/sockethubappender.cpp b/src/main/cpp/sockethubappender.cpp index 73d821a..7d83a23 100644 --- a/src/main/cpp/sockethubappender.cpp +++ b/src/main/cpp/sockethubappender.cpp @@ -178,7 +178,7 @@ void SocketHubAppender::append(const spi::LoggingEventPtr& event, Pool& p) void SocketHubAppender::startServer() { - thread = ThreadUtility::createThread( LOG4CXX_STR("SocketHub"), &SocketHubAppender::monitor, this ); + thread = ThreadUtility::instance()->createThread( LOG4CXX_STR("SocketHub"), &SocketHubAppender::monitor, this ); } void SocketHubAppender::monitor() diff --git a/src/main/cpp/telnetappender.cpp b/src/main/cpp/telnetappender.cpp index 9dc813a..7e1d6f5 100644 --- a/src/main/cpp/telnetappender.cpp +++ b/src/main/cpp/telnetappender.cpp @@ -62,7 +62,7 @@ void TelnetAppender::activateOptions(Pool& /* p */) serverSocket->setSoTimeout(1000); } - sh = ThreadUtility::createThread( LOG4CXX_STR("TelnetAppend"), &TelnetAppender::acceptConnections, this ); + sh = ThreadUtility::instance()->createThread( LOG4CXX_STR("TelnetAppend"), &TelnetAppender::acceptConnections, this ); } void TelnetAppender::setOption(const LogString& option, diff --git a/src/main/cpp/threadutility.cpp b/src/main/cpp/threadutility.cpp index c7990d9..165803b 100644 --- a/src/main/cpp/threadutility.cpp +++ b/src/main/cpp/threadutility.cpp @@ -11,35 +11,36 @@ using log4cxx::ThreadUtility; -log4cxx::pre_thread_start ThreadUtility::pre_start = ThreadUtility::preThreadDoNothing; -log4cxx::thread_started ThreadUtility::thread_start = ThreadUtility::threadStartedDoNothing; -log4cxx::post_thread_start ThreadUtility::post_start = ThreadUtility::postThreadDoNothing; +struct ThreadUtility::priv_data{ + priv_data(){ + pre_start = nullptr; + thread_start = nullptr; + post_start = nullptr; + } -ThreadUtility::ThreadUtility(){} + log4cxx::pre_thread_start pre_start; + log4cxx::thread_started thread_start; + log4cxx::post_thread_start post_start; +}; -void ThreadUtility::configureThreadFunctions( pre_thread_start pre_start1, - thread_started started, - post_thread_start post_start1 ){ - if( pre_start1 == nullptr ){ - pre_start = ThreadUtility::preThreadDoNothing; - }else{ - pre_start = pre_start1; - } +ThreadUtility::ThreadUtility() : + m_priv( new priv_data() ) +{} - if( started == nullptr ){ - thread_start = ThreadUtility::threadStartedDoNothing; - }else{ - thread_start = started; - } +ThreadUtility::~ThreadUtility(){} - if( post_start1 == nullptr ){ - post_start = ThreadUtility::postThreadDoNothing; - }else{ - post_start = pre_start1; - } +std::shared_ptr<ThreadUtility> ThreadUtility::instance(){ + static std::shared_ptr<ThreadUtility> instance( new ThreadUtility() ); + return instance; } -void ThreadUtility::preThreadDoNothing(){} +void ThreadUtility::configureThreadFunctions( pre_thread_start pre_start1, + thread_started started, + post_thread_start post_start1 ){ + m_priv->pre_start = pre_start1; + m_priv->thread_start = started; + m_priv->post_start = post_start1; +} void ThreadUtility::preThreadBlockSignals(){ #if LOG4CXX_HAS_PTHREAD_SIGMASK @@ -51,10 +52,6 @@ void ThreadUtility::preThreadBlockSignals(){ #endif /* LOG4CXX_HAS_PTHREAD_SIGMASK */ } -void ThreadUtility::threadStartedDoNothing(LogString, - std::thread::id, - std::thread::native_handle_type){} - void ThreadUtility::threadStartedNameThread(LogString threadName, std::thread::id /*thread_id*/, std::thread::native_handle_type native_handle){ @@ -70,8 +67,6 @@ void ThreadUtility::threadStartedNameThread(LogString threadName, #endif } -void ThreadUtility::postThreadDoNothing(){} - void ThreadUtility::postThreadUnblockSignals(){ #if LOG4CXX_HAS_PTHREAD_SIGMASK sigset_t set; @@ -81,3 +76,16 @@ void ThreadUtility::postThreadUnblockSignals(){ } #endif /* LOG4CXX_HAS_PTHREAD_SIGMASK */ } + + +log4cxx::pre_thread_start ThreadUtility::preStartFunction(){ + return m_priv->pre_start; +} + +log4cxx::thread_started ThreadUtility::threadStartedFunction(){ + return m_priv->thread_start; +} + +log4cxx::post_thread_start ThreadUtility::postStartFunction(){ + return m_priv->post_start; +} diff --git a/src/main/include/CMakeLists.txt b/src/main/include/CMakeLists.txt index 558eaeb..9905434 100644 --- a/src/main/include/CMakeLists.txt +++ b/src/main/include/CMakeLists.txt @@ -96,6 +96,8 @@ if(UNIX) # it anyway, we're only going to worry about linux. include(${LOG4CXX_SOURCE_DIR}/src/cmake/pthread/log4cxx-pthread.cmake) set(HAS_PTHREAD_SETNAME ${PTHREAD_SETNAME_NP_FOUND}) + + message("setname np: ${PTHREAD_SETNAME_NP_FOUND}") endif(UNIX) if(WIN32) diff --git a/src/main/include/log4cxx/helpers/threadutility.h b/src/main/include/log4cxx/helpers/threadutility.h index de4ffad..4111a0d 100644 --- a/src/main/include/log4cxx/helpers/threadutility.h +++ b/src/main/include/log4cxx/helpers/threadutility.h @@ -3,6 +3,7 @@ #include <thread> #include <functional> +#include <memory> #include "log4cxx/logstring.h" @@ -34,79 +35,82 @@ typedef std::function<void( LogString threadName, */ typedef std::function<void()> post_thread_start; +class ThreadUtility; +LOG4CXX_PTR_DEF(ThreadUtility); + class LOG4CXX_EXPORT ThreadUtility { +private: + ThreadUtility(); + + log4cxx::pre_thread_start preStartFunction(); + log4cxx::thread_started threadStartedFunction(); + log4cxx::post_thread_start postStartFunction(); + + struct priv_data; + std::unique_ptr<priv_data> m_priv; + public: + ~ThreadUtility(); + + static std::shared_ptr<ThreadUtility> instance(); + /** * Configure the thread functions that log4cxx will use. * Note that setting any of these parameters to nullptr will cause the * default function to be used. * */ - static void configureThreadFunctions( pre_thread_start pre_start, + void configureThreadFunctions( pre_thread_start pre_start, thread_started started, post_thread_start post_start ); /** - * A pre-start thread function that does nothing - */ - static void preThreadDoNothing(); - - /** * A pre-start thread function that blocks signals to the new thread * (if the system has pthreads). If the system does not have pthreads, * is equivalent to preThreadDoNothing(); */ - static void preThreadBlockSignals(); - - /** - * A thread_started function that does nothing when the thread starts. - */ - static void threadStartedDoNothing(LogString threadName, - std::thread::id thread_id, - std::thread::native_handle_type native_handle); + void preThreadBlockSignals(); /** * A thread_started function that names the thread using the appropriate * system call. */ - static void threadStartedNameThread(LogString threadName, + void threadStartedNameThread(LogString threadName, std::thread::id thread_id, std::thread::native_handle_type native_handle); /** - * A post-start thread function that does nothing. - */ - static void postThreadDoNothing(); - - /** * A post-start thread function that unblocks signals that preThreadBlockSignals * blocked before starting the thread. If the system does not have pthreads, * is equivalent to postThreadDoNothing(); */ - static void postThreadUnblockSignals(); + void postThreadUnblockSignals(); /** * Start a thread */ template<class Function, class... Args> - static std::thread createThread(LogString name, + std::thread createThread(LogString name, Function&& f, Args&&... args){ - pre_start(); + log4cxx::pre_thread_start pre_start = preStartFunction(); + log4cxx::thread_started thread_start = threadStartedFunction(); + log4cxx::post_thread_start post_start = postStartFunction(); + + if( pre_start ){ + pre_start(); + } std::thread t( f, args... ); - thread_start( name, + if( thread_start ){ + thread_start( name, t.get_id(), t.native_handle() ); - post_start(); + } + if( post_start ){ + post_start(); + } return t; } - -private: - ThreadUtility(); - - static log4cxx::pre_thread_start pre_start; - static log4cxx::thread_started thread_start; - static log4cxx::post_thread_start post_start; }; }
