From 8671e985e132b8e7fc2659b9bef0eb0b0560a251 Mon Sep 17 00:00:00 2001 From: Vaclav Haisman Date: Fri, 22 Dec 2023 18:38:28 +0100 Subject: [PATCH] Revert commits changing log4cplus mutexes to standard C++ mutexes. The reason we have the wrappers is also that it allows us to compile a single threaded variant of log4cplus without the overhead of the mutexes at run time. The wrappers have to stay for this reason. --- include/log4cplus/consoleappender.h | 8 ++-- include/log4cplus/helpers/connectorthread.h | 16 +++---- include/log4cplus/helpers/loglog.h | 4 +- include/log4cplus/helpers/pointer.h | 2 +- include/log4cplus/socketappender.h | 2 +- include/log4cplus/spi/objectregistry.h | 6 +-- include/log4cplus/syslogappender.h | 2 +- src/appender.cxx | 14 +++--- src/connectorthread.cxx | 10 ++--- src/consoleappender.cxx | 8 ++-- src/fileappender.cxx | 2 +- src/global-init.cxx | 4 +- src/loglog.cxx | 8 ++-- src/objectregistry.cxx | 49 +++------------------ src/socketappender.cxx | 2 +- src/syslogappender.cxx | 4 +- 16 files changed, 52 insertions(+), 89 deletions(-) diff --git a/include/log4cplus/consoleappender.h b/include/log4cplus/consoleappender.h index f3a0049ef..3da24c817 100644 --- a/include/log4cplus/consoleappender.h +++ b/include/log4cplus/consoleappender.h @@ -48,7 +48,7 @@ namespace log4cplus { *
ImmediateFlush
*
When it is set true, output stream will be flushed after * each appended event.
- * + * *
Locale
*
This property specifies a locale name that will be imbued * into output stream. Locale can be specified either by system @@ -61,11 +61,11 @@ namespace log4cplus { * spi::LocaleFactory in * spi::LocaleFactoryRegistry. * \sa spi::getLocaleFactoryRegistry(). - * + * * Note: if Locale is set, ImmediateFlush will * be set to true automatically. *
- * + * * * \sa Appender */ @@ -83,7 +83,7 @@ namespace log4cplus { //! This mutex is used by ConsoleAppender and helpers::LogLog //! classes to synchronize output to console. - static std::recursive_mutex & getOutputMutex(); + static log4cplus::thread::Mutex const & getOutputMutex(); protected: virtual void append(const spi::InternalLoggingEvent& event); diff --git a/include/log4cplus/helpers/connectorthread.h b/include/log4cplus/helpers/connectorthread.h index 5c516e3f7..6df3ce52e 100644 --- a/include/log4cplus/helpers/connectorthread.h +++ b/include/log4cplus/helpers/connectorthread.h @@ -1,16 +1,16 @@ // -*- C++ -*- // Copyright (C) 2013-2017, Vaclav Zeman. All rights reserved. -// +// // Redistribution and use in source and binary forms, with or without modifica- // tion, are permitted provided that the following conditions are met: -// +// // 1. Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. -// +// // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. -// +// // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE @@ -51,7 +51,7 @@ class LOG4CPLUS_EXPORT IConnectorThreadClient //! \return Mutex for synchronization between ConnectorThread and //! its client object. This is usually SharedObject::access_mutex. - virtual std::recursive_mutex & ctcGetAccessMutex () const = 0; + virtual thread::Mutex const & ctcGetAccessMutex () const = 0; //! \return Socket variable in ConnectorThread client to maintain. virtual helpers::Socket & ctcGetSocket () = 0; @@ -76,7 +76,7 @@ class LOG4CPLUS_EXPORT ConnectorThread //! \param client reference to ConnectorThread's client object ConnectorThread (IConnectorThreadClient & client); virtual ~ConnectorThread (); - + virtual void run(); //! Call this function to terminate ConnectorThread. The function @@ -87,7 +87,7 @@ class LOG4CPLUS_EXPORT ConnectorThread //! This function triggers (`trigger_ev`) connection check and //! attempt to re-connect a broken connection, when necessary. void trigger (); - + protected: //! reference to ConnectorThread's client IConnectorThreadClient & ctc; @@ -95,7 +95,7 @@ class LOG4CPLUS_EXPORT ConnectorThread //! This event is the re-connection trigger. thread::ManualResetEvent trigger_ev; - //! When this variable set to true when ConnectorThread is signaled to + //! When this variable set to true when ConnectorThread is signaled to bool exit_flag; }; diff --git a/include/log4cplus/helpers/loglog.h b/include/log4cplus/helpers/loglog.h index 7c68c8ba2..4f92ef835 100644 --- a/include/log4cplus/helpers/loglog.h +++ b/include/log4cplus/helpers/loglog.h @@ -70,7 +70,7 @@ namespace log4cplus { /** * In quite mode no LogLog generates strictly no output, not even - * for errors. + * for errors. * * @param quietMode A true for not */ @@ -129,7 +129,7 @@ namespace log4cplus { // Data mutable TriState debugEnabled; mutable TriState quietMode; - mutable std::mutex mutex; + thread::Mutex mutex; LOG4CPLUS_PRIVATE LogLog(const LogLog&); LOG4CPLUS_PRIVATE LogLog & operator = (LogLog const &); diff --git a/include/log4cplus/helpers/pointer.h b/include/log4cplus/helpers/pointer.h index 744d22695..d9ccfd47a 100644 --- a/include/log4cplus/helpers/pointer.h +++ b/include/log4cplus/helpers/pointer.h @@ -81,7 +81,7 @@ namespace log4cplus { SharedObject& operator=(SharedObject &&) LOG4CPLUS_NOEXCEPT { return *this; } public: - mutable std::recursive_mutex access_mutex; + thread::Mutex access_mutex; private: #if defined (LOG4CPLUS_SINGLE_THREADED) diff --git a/include/log4cplus/socketappender.h b/include/log4cplus/socketappender.h index 299efb06f..f0dd564a8 100644 --- a/include/log4cplus/socketappender.h +++ b/include/log4cplus/socketappender.h @@ -134,7 +134,7 @@ namespace log4cplus bool ipv6 = false; #if ! defined (LOG4CPLUS_SINGLE_THREADED) - virtual std::recursive_mutex & ctcGetAccessMutex () const; + virtual thread::Mutex const & ctcGetAccessMutex () const; virtual helpers::Socket & ctcGetSocket (); virtual helpers::Socket ctcConnect (); virtual void ctcSetConnected (); diff --git a/include/log4cplus/spi/objectregistry.h b/include/log4cplus/spi/objectregistry.h index 5ab775ec8..a63cecdd2 100644 --- a/include/log4cplus/spi/objectregistry.h +++ b/include/log4cplus/spi/objectregistry.h @@ -95,12 +95,12 @@ namespace log4cplus { typedef std::map ObjectMap; // Data - mutable std::mutex mutex; + thread::Mutex mutex; ObjectMap data; private: - ObjectRegistryBase (ObjectRegistryBase const &) = delete; - ObjectRegistryBase & operator = (ObjectRegistryBase const &) = delete; + ObjectRegistryBase (ObjectRegistryBase const &); + ObjectRegistryBase & operator = (ObjectRegistryBase const &); bool volatile locking; }; diff --git a/include/log4cplus/syslogappender.h b/include/log4cplus/syslogappender.h index 4c769d1b3..4f9276442 100644 --- a/include/log4cplus/syslogappender.h +++ b/include/log4cplus/syslogappender.h @@ -141,7 +141,7 @@ namespace log4cplus void openSocket (); #if ! defined (LOG4CPLUS_SINGLE_THREADED) - virtual std::recursive_mutex & ctcGetAccessMutex () const; + virtual thread::Mutex const & ctcGetAccessMutex () const; virtual helpers::Socket & ctcGetSocket (); virtual helpers::Socket ctcConnect (); virtual void ctcSetConnected (); diff --git a/src/appender.cxx b/src/appender.cxx index e886b91c2..971cc3411 100644 --- a/src/appender.cxx +++ b/src/appender.cxx @@ -347,7 +347,7 @@ Appender::asyncDoAppend(const log4cplus::spi::InternalLoggingEvent& event) void Appender::syncDoAppend(const log4cplus::spi::InternalLoggingEvent& event) { - std::lock_guard guard {access_mutex}; + thread::MutexGuard guard (access_mutex); if(closed) { helpers::getLogLog().error( @@ -434,7 +434,7 @@ Appender::setErrorHandler(std::unique_ptr eh) return; } - std::lock_guard guard {access_mutex}; + thread::MutexGuard guard (access_mutex); this->errorHandler = std::move(eh); } @@ -444,7 +444,7 @@ Appender::setErrorHandler(std::unique_ptr eh) void Appender::setLayout(std::unique_ptr lo) { - std::lock_guard guard {access_mutex}; + thread::MutexGuard guard (access_mutex); this->layout = std::move(lo); } @@ -454,7 +454,7 @@ Appender::setLayout(std::unique_ptr lo) Layout* Appender::getLayout() { - std::lock_guard guard {access_mutex}; + thread::MutexGuard guard (access_mutex); return layout.get(); } @@ -463,7 +463,7 @@ Appender::getLayout() void Appender::setFilter(log4cplus::spi::FilterPtr f) { - std::lock_guard guard {access_mutex}; + thread::MutexGuard guard (access_mutex); filter = std::move (f); } @@ -472,7 +472,7 @@ Appender::setFilter(log4cplus::spi::FilterPtr f) log4cplus::spi::FilterPtr Appender::getFilter() const { - std::lock_guard guard {access_mutex}; + thread::MutexGuard guard (access_mutex); return filter; } @@ -481,7 +481,7 @@ Appender::getFilter() const void Appender::addFilter (log4cplus::spi::FilterPtr f) { - std::lock_guard guard {access_mutex}; + thread::MutexGuard guard (access_mutex); log4cplus::spi::FilterPtr filterChain = getFilter (); if (filterChain) diff --git a/src/connectorthread.cxx b/src/connectorthread.cxx index fa313631b..64a3bf28c 100644 --- a/src/connectorthread.cxx +++ b/src/connectorthread.cxx @@ -59,7 +59,7 @@ ConnectorThread::run () // Check exit condition as the very first thing. { - std::lock_guard guard {access_mutex}; + thread::MutexGuard guard (access_mutex); if (exit_flag) return; trigger_ev.reset (); @@ -68,9 +68,9 @@ ConnectorThread::run () // Do not try to re-open already open socket. helpers::Socket & client_socket = ctc.ctcGetSocket (); - std::recursive_mutex & client_access_mutex = ctc.ctcGetAccessMutex (); + thread::Mutex const & client_access_mutex = ctc.ctcGetAccessMutex (); { - std::lock_guard guard {client_access_mutex}; + thread::MutexGuard guard (client_access_mutex); if (client_socket.isOpen ()) continue; } @@ -95,7 +95,7 @@ ConnectorThread::run () // Connection was successful, move the socket into client. { - std::lock_guard guard {client_access_mutex}; + thread::MutexGuard guard (client_access_mutex); client_socket = std::move (new_socket); ctc.ctcSetConnected (); } @@ -107,7 +107,7 @@ void ConnectorThread::terminate () { { - std::lock_guard guard {access_mutex}; + thread::MutexGuard guard (access_mutex); exit_flag = true; trigger_ev.signal (); } diff --git a/src/consoleappender.cxx b/src/consoleappender.cxx index d132d587b..a0cf25259 100644 --- a/src/consoleappender.cxx +++ b/src/consoleappender.cxx @@ -37,12 +37,12 @@ namespace log4cplus namespace helpers { -extern std::recursive_mutex & getConsoleOutputMutex (); +extern log4cplus::thread::Mutex const & getConsoleOutputMutex (); } // namespace helpers -std::recursive_mutex & +log4cplus::thread::Mutex const & ConsoleAppender::getOutputMutex () { return helpers::getConsoleOutputMutex (); @@ -93,7 +93,7 @@ ConsoleAppender::~ConsoleAppender() // ConsoleAppender public methods ////////////////////////////////////////////////////////////////////////////// -void +void ConsoleAppender::close() { helpers::getLogLog().debug( @@ -110,7 +110,7 @@ ConsoleAppender::close() void ConsoleAppender::append(const spi::InternalLoggingEvent& event) { - std::lock_guard guard {getOutputMutex ()}; + thread::MutexGuard guard (getOutputMutex ()); tostream& output = (logToStdErr ? tcerr : tcout); diff --git a/src/fileappender.cxx b/src/fileappender.cxx index 9421c0e34..d18473dd5 100644 --- a/src/fileappender.cxx +++ b/src/fileappender.cxx @@ -289,7 +289,7 @@ FileAppenderBase::init() void FileAppenderBase::close() { - std::lock_guard guard {access_mutex}; + thread::MutexGuard guard (access_mutex); out.close(); buffer.reset (); diff --git a/src/global-init.cxx b/src/global-init.cxx index 808c9e509..8c180cff6 100644 --- a/src/global-init.cxx +++ b/src/global-init.cxx @@ -168,7 +168,7 @@ struct ThreadPoolHolder //! Default context. struct DefaultContext { - std::recursive_mutex console_mutex; + log4cplus::thread::Mutex console_mutex; helpers::LogLog loglog; LogLevelManager log_level_manager; internal::CustomLogLevelManager custom_log_level_manager; @@ -306,7 +306,7 @@ namespace helpers { -std::recursive_mutex & +log4cplus::thread::Mutex const & getConsoleOutputMutex () { return get_dc ()->console_mutex; diff --git a/src/loglog.cxx b/src/loglog.cxx index 80f3bfb03..b1b1dae1c 100644 --- a/src/loglog.cxx +++ b/src/loglog.cxx @@ -60,7 +60,7 @@ LogLog::~LogLog() = default; void LogLog::setInternalDebugging(bool enabled) { - std::lock_guard guard {mutex}; + thread::MutexGuard guard (mutex); debugEnabled = enabled ? TriTrue : TriFalse; } @@ -69,7 +69,7 @@ LogLog::setInternalDebugging(bool enabled) void LogLog::setQuietMode(bool quietModeVal) { - std::lock_guard guard {mutex}; + thread::MutexGuard guard (mutex); quietMode = quietModeVal ? TriTrue : TriFalse; } @@ -168,7 +168,7 @@ LogLog::logging_worker (tostream & os, bool (LogLog:: * cond) () const, { bool output; { - std::lock_guard guard {mutex}; + thread::MutexGuard guard (mutex); output = (this->*cond) (); } @@ -176,7 +176,7 @@ LogLog::logging_worker (tostream & os, bool (LogLog:: * cond) () const, { // XXX This is potential recursive lock of // ConsoleAppender::outputMutex. - std::lock_guard outputGuard {ConsoleAppender::getOutputMutex ()}; + thread::MutexGuard outputGuard (ConsoleAppender::getOutputMutex ()); os << prefix << msg << std::endl; } diff --git a/src/objectregistry.cxx b/src/objectregistry.cxx index 44a7662bf..2f1f89113 100644 --- a/src/objectregistry.cxx +++ b/src/objectregistry.cxx @@ -22,10 +22,6 @@ #include #include -#if defined (LOG4CPLUS_WITH_UNIT_TESTS) -#include -#endif - namespace log4cplus::spi { @@ -50,7 +46,7 @@ ObjectRegistryBase::~ObjectRegistryBase() = default; bool ObjectRegistryBase::exists(const tstring& name) const { - std::unique_lock guard {mutex}; + thread::MutexGuard guard (mutex); return data.find(name) != data.end(); } @@ -62,7 +58,7 @@ ObjectRegistryBase::getAllNames() const std::vector tmp; { - std::unique_lock guard {mutex}; + thread::MutexGuard guard (mutex); tmp.reserve (data.size ()); for (auto const & kv : data) tmp.emplace_back(kv.first); @@ -84,9 +80,9 @@ ObjectRegistryBase::putVal(const tstring& name, void* object) std::pair ret; { - std::unique_lock guard; + thread::MutexGuard guard; if (locking) - guard = std::unique_lock {mutex}; + guard.attach_and_lock (mutex); ret = data.insert(std::move (value)); } @@ -101,7 +97,7 @@ ObjectRegistryBase::putVal(const tstring& name, void* object) void* ObjectRegistryBase::getVal(const tstring& name) const { - std::unique_lock guard {mutex}; + thread::MutexGuard guard (mutex); auto it (data.find (name)); if (it != data.end ()) @@ -116,7 +112,7 @@ ObjectRegistryBase::getVal(const tstring& name) const void ObjectRegistryBase::clear() { - std::unique_lock guard {mutex}; + thread::MutexGuard guard (mutex); for (auto const & kv : data) deleteObject (kv.second); @@ -130,37 +126,4 @@ ObjectRegistryBase::_enableLocking (bool enable) } -#if defined (LOG4CPLUS_WITH_UNIT_TESTS) -CATCH_TEST_CASE ("ObjectRegistryBase") -{ - - class TestObjectRegistry : public ObjectRegistryBase - { - public: - using ObjectRegistryBase::putVal; - using ObjectRegistryBase::getVal; - using ObjectRegistryBase::clear; - - virtual void deleteObject(void *object) const - { - delete static_cast(object); - } - }; - - CATCH_SECTION ("put-get") - { - TestObjectRegistry reg; - CATCH_REQUIRE (reg.getVal (LOG4CPLUS_TEXT ("doesnotexist")) == nullptr); - std::string * const str = new std::string ("test"); - CATCH_REQUIRE (reg.putVal (LOG4CPLUS_TEXT ("a"), str)); - CATCH_REQUIRE (!reg.putVal (LOG4CPLUS_TEXT ("a"), str)); - std::string * str2 = new std::string ("test2"); - CATCH_REQUIRE (reg.putVal (LOG4CPLUS_TEXT ("b"), str2)); - CATCH_REQUIRE (reg.getVal (LOG4CPLUS_TEXT ("a")) == str); - CATCH_REQUIRE (reg.getVal (LOG4CPLUS_TEXT ("b")) == str2); - } - -} -#endif // defined (LOG4CPLUS_WITH_UNIT_TESTS) - } // namespace log4cplus::spi diff --git a/src/socketappender.cxx b/src/socketappender.cxx index 43db28272..5cdafd452 100644 --- a/src/socketappender.cxx +++ b/src/socketappender.cxx @@ -170,7 +170,7 @@ SocketAppender::append(const spi::InternalLoggingEvent& event) #if ! defined (LOG4CPLUS_SINGLE_THREADED) -std::recursive_mutex & +thread::Mutex const & SocketAppender::ctcGetAccessMutex () const { return access_mutex; diff --git a/src/syslogappender.cxx b/src/syslogappender.cxx index 29e039ebd..efa0b95c5 100644 --- a/src/syslogappender.cxx +++ b/src/syslogappender.cxx @@ -336,7 +336,7 @@ SysLogAppender::close() { helpers::getLogLog().debug( LOG4CPLUS_TEXT("Entering SysLogAppender::close()...")); - std::lock_guard guard {access_mutex}; + thread::MutexGuard guard (access_mutex); if (host.empty ()) { @@ -495,7 +495,7 @@ SysLogAppender::appendRemote(const spi::InternalLoggingEvent& event) #if ! defined (LOG4CPLUS_SINGLE_THREADED) -std::recursive_mutex & +thread::Mutex const & SysLogAppender::ctcGetAccessMutex () const { return access_mutex;