Skip to content

Commit

Permalink
Prefer std::lock_guard when std::unique_lock features are not required (
Browse files Browse the repository at this point in the history
  • Loading branch information
swebb2066 authored Apr 23, 2024
1 parent d8048cc commit 57295d2
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/main/cpp/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Action::~Action()
*/
void Action::run(LOG4CXX_NS::helpers::Pool& pool1)
{
std::unique_lock<std::mutex> lock(m_priv->mutex);
std::lock_guard<std::mutex> lock(m_priv->mutex);

if (!m_priv->interrupted)
{
Expand All @@ -66,7 +66,7 @@ void Action::run(LOG4CXX_NS::helpers::Pool& pool1)
*/
void Action::close()
{
std::unique_lock<std::mutex> lock(m_priv->mutex);
std::lock_guard<std::mutex> lock(m_priv->mutex);
m_priv->interrupted = true;
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/cpp/aprinitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ APRInitializer::~APRInitializer()
stopWatchDogs();
isDestructed = true;
#if APR_HAS_THREADS
std::unique_lock<std::mutex> lock(m_priv->mutex);
std::lock_guard<std::mutex> lock(m_priv->mutex);
apr_threadkey_private_delete(m_priv->tlsKey);
#endif
}

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

while (!m_priv->watchdogs.empty())
{
Expand Down Expand Up @@ -146,14 +146,14 @@ apr_threadkey_t* APRInitializer::getTlsKey()
void APRInitializer::registerCleanup(FileWatchdog* watchdog)
{
APRInitializer& instance(getInstance());
std::unique_lock<std::mutex> lock(instance.m_priv->mutex);
std::lock_guard<std::mutex> lock(instance.m_priv->mutex);
instance.m_priv->watchdogs.push_back(watchdog);
}

void APRInitializer::unregisterCleanup(FileWatchdog* watchdog)
{
APRInitializer& instance(getInstance());
std::unique_lock<std::mutex> lock(instance.m_priv->mutex);
std::lock_guard<std::mutex> lock(instance.m_priv->mutex);

auto iter = std::find(instance.m_priv->watchdogs.begin(), instance.m_priv->watchdogs.end(), watchdog);
if (iter != instance.m_priv->watchdogs.end())
Expand All @@ -162,13 +162,13 @@ void APRInitializer::unregisterCleanup(FileWatchdog* watchdog)

void APRInitializer::addObject(size_t key, const ObjectPtr& pObject)
{
std::unique_lock<std::mutex> lock(m_priv->mutex);
std::lock_guard<std::mutex> lock(m_priv->mutex);
m_priv->objects[key] = pObject;
}

const ObjectPtr& APRInitializer::findOrAddObject(size_t key, std::function<ObjectPtr()> creator)
{
std::unique_lock<std::mutex> lock(m_priv->mutex);
std::lock_guard<std::mutex> lock(m_priv->mutex);
auto pItem = m_priv->objects.find(key);
if (m_priv->objects.end() == pItem)
pItem = m_priv->objects.emplace(key, creator()).first;
Expand Down
4 changes: 2 additions & 2 deletions src/main/cpp/aprserversocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ APRServerSocket::APRServerSocket(int port) :
}

void APRServerSocket::close(){
std::unique_lock<std::mutex> lock(_priv->mutex);
std::lock_guard<std::mutex> lock(_priv->mutex);

if (_priv->socket != 0)
{
Expand All @@ -103,7 +103,7 @@ accepts it
*/
SocketPtr APRServerSocket::accept()
{
std::unique_lock<std::mutex> lock(_priv->mutex);
std::lock_guard<std::mutex> lock(_priv->mutex);

if (_priv->socket == 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/asyncappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void AsyncAppender::append(const spi::LoggingEventPtr& event, Pool& p)

if (!priv->dispatcher.joinable())
{
std::unique_lock<std::mutex> lock(priv->bufferMutex);
std::lock_guard<std::mutex> lock(priv->bufferMutex);
if (!priv->dispatcher.joinable())
priv->dispatcher = ThreadUtility::instance()->createThread( LOG4CXX_STR("AsyncAppender"), &AsyncAppender::dispatch, this );
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/cpp/charsetdecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class APRCharsetDecoder : public CharsetDecoder
{
size_t outbytes_left = initial_outbytes_left;
{
std::unique_lock<std::mutex> lock(mutex);
std::lock_guard<std::mutex> lock(mutex);
stat = apr_xlate_conv_buffer((apr_xlate_t*) convset,
NULL, NULL, (char*) buf, &outbytes_left);
}
Expand All @@ -113,7 +113,7 @@ class APRCharsetDecoder : public CharsetDecoder
size_t pos = in.position();
apr_size_t outbytes_left = initial_outbytes_left;
{
std::unique_lock<std::mutex> lock(mutex);
std::lock_guard<std::mutex> lock(mutex);
stat = apr_xlate_conv_buffer((apr_xlate_t*) convset,
in.data() + pos,
&inbytes_left,
Expand Down
4 changes: 2 additions & 2 deletions src/main/cpp/charsetencoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class APRCharsetEncoder : public CharsetEncoder

if (iter == in.end())
{
std::unique_lock<std::mutex> lock(mutex);
std::lock_guard<std::mutex> lock(mutex);
stat = apr_xlate_conv_buffer(convset, NULL, NULL,
out.data() + position, &outbytes_left);
}
Expand All @@ -102,7 +102,7 @@ class APRCharsetEncoder : public CharsetEncoder
(in.size() - inOffset) * sizeof(LogString::value_type);
apr_size_t initial_inbytes_left = inbytes_left;
{
std::unique_lock<std::mutex> lock(mutex);
std::lock_guard<std::mutex> lock(mutex);
stat = apr_xlate_conv_buffer(convset,
(const char*) (in.data() + inOffset),
&inbytes_left,
Expand Down
6 changes: 1 addition & 5 deletions src/main/cpp/filewatchdog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ void FileWatchdog::stop()
{
LogLog::debug(LOG4CXX_STR("Stopping file watchdog"));
m_priv->interrupted = 0xFFFF;

{
std::unique_lock<std::mutex> lock(m_priv->interrupt_mutex);
m_priv->interrupt.notify_all();
}
m_priv->interrupt.notify_all();
m_priv->thread.join();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/cpp/hierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ bool Hierarchy::isDisabled(int level) const

void Hierarchy::ensureIsConfigured(std::function<void()> configurator)
{
std::unique_lock<std::mutex> lock(m_priv->configuredMutex);
std::lock_guard<std::mutex> lock(m_priv->configuredMutex);
if (!m_priv->configured)
{
configurator();
Expand Down Expand Up @@ -446,7 +446,7 @@ void Hierarchy::setConfigured(bool newValue)

bool Hierarchy::isConfigured()
{
std::unique_lock<std::mutex> lock(m_priv->configuredMutex); // Blocks while auto-configuration is active
std::lock_guard<std::mutex> lock(m_priv->configuredMutex); // Blocks while auto-configuration is active
return m_priv->configured;
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/cpp/loglog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ LogLog& LogLog::getInstance()
void LogLog::setInternalDebugging(bool debugEnabled1)
{
auto p = getInstance().m_priv.get();
std::unique_lock<std::mutex> lock(p->mutex);
std::lock_guard<std::mutex> lock(p->mutex);

p->debugEnabled = debugEnabled1;
}
Expand All @@ -84,7 +84,7 @@ void LogLog::debug(const LogString& msg)
return;
}

std::unique_lock<std::mutex> lock(p->mutex);
std::lock_guard<std::mutex> lock(p->mutex);

emit(msg);
}
Expand All @@ -98,7 +98,7 @@ void LogLog::debug(const LogString& msg, const std::exception& e)
if (!p->debugEnabled)
return;

std::unique_lock<std::mutex> lock(p->mutex);
std::lock_guard<std::mutex> lock(p->mutex);
emit(msg);
emit(e);
}
Expand All @@ -110,7 +110,7 @@ void LogLog::error(const LogString& msg)
auto p = getInstance().m_priv.get();
if (p && !p->quietMode) // Not deleted by onexit processing?
{
std::unique_lock<std::mutex> lock(p->mutex);
std::lock_guard<std::mutex> lock(p->mutex);

emit(msg);
}
Expand All @@ -121,7 +121,7 @@ void LogLog::error(const LogString& msg, const std::exception& e)
auto p = getInstance().m_priv.get();
if (p && !p->quietMode) // Not deleted by onexit processing?
{
std::unique_lock<std::mutex> lock(p->mutex);
std::lock_guard<std::mutex> lock(p->mutex);
emit(msg);
emit(e);
}
Expand All @@ -130,7 +130,7 @@ void LogLog::error(const LogString& msg, const std::exception& e)
void LogLog::setQuietMode(bool quietMode1)
{
auto p = getInstance().m_priv.get();
std::unique_lock<std::mutex> lock(p->mutex);
std::lock_guard<std::mutex> lock(p->mutex);

p->quietMode = quietMode1;
}
Expand All @@ -140,7 +140,7 @@ void LogLog::warn(const LogString& msg)
auto p = getInstance().m_priv.get();
if (p && !p->quietMode) // Not deleted by onexit processing?
{
std::unique_lock<std::mutex> lock(p->mutex);
std::lock_guard<std::mutex> lock(p->mutex);
emit(msg);
}
}
Expand All @@ -150,7 +150,7 @@ void LogLog::warn(const LogString& msg, const std::exception& e)
auto p = getInstance().m_priv.get();
if (p && !p->quietMode) // Not deleted by onexit processing?
{
std::unique_lock<std::mutex> lock(p->mutex);
std::lock_guard<std::mutex> lock(p->mutex);
emit(msg);
emit(e);
}
Expand Down
7 changes: 1 addition & 6 deletions src/main/cpp/socketappenderskeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,7 @@ void SocketAppenderSkeleton::close()

_priv->closed = true;
cleanUp(_priv->pool);

{
std::unique_lock<std::mutex> lock2(_priv->interrupt_mutex);
_priv->interrupt.notify_all();
}

_priv->interrupt.notify_all();
if ( _priv->thread.joinable() )
{
_priv->thread.join();
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/writerappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ void WriterAppender::writeHeader(Pool& p)

void WriterAppender::setWriter(const WriterPtr& newWriter)
{
std::unique_lock<std::recursive_mutex> lock(_priv->mutex);
std::lock_guard<std::recursive_mutex> lock(_priv->mutex);
setWriterInternal(newWriter);
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/cpp/asyncappendertestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class BlockableVectorAppender : public VectorAppender
*/
void append(const spi::LoggingEventPtr& event, log4cxx::helpers::Pool& p) override
{
std::unique_lock<std::mutex> lock( blocker );
std::lock_guard<std::mutex> lock( blocker );
VectorAppender::append(event, p);

//
Expand Down Expand Up @@ -332,7 +332,7 @@ class AsyncAppenderTestCase : public AppenderSkeletonTestCase
LOG4CXX_INFO(rootLogger, "Hello, World"); // This causes the dispatch thread creation
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) ); // Wait for the dispatch thread to be ready
{
std::unique_lock<std::mutex> sync(blockableAppender->getBlocker());
std::lock_guard<std::mutex> sync(blockableAppender->getBlocker());

for (int i = 0; i < 140; i++)
{
Expand Down
1 change: 0 additions & 1 deletion src/test/cpp/helpers/charsetencodertestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ LOGUNIT_CLASS(CharsetEncoderTestCase)

void signalAll()
{
std::unique_lock<std::mutex> sync(lock);
condition.notify_all();
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/cpp/throughput/throughput-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void benchmark_function( const std::string& name, void (*fn)(int), int ho
auto delta = high_resolution_clock::now() - start;
auto delta_d = duration_cast<duration<double>>(delta).count();

std::unique_lock<std::mutex> lk(results_mutex);
std::lock_guard<std::mutex> lk(results_mutex);
results.push_back( uint64_t(howmany / delta_d) );
LOG4CXX_INFO_FMT( console, "Log4cxx {} Elapsed: {:.4} secs {:L}/sec",
name,
Expand All @@ -73,7 +73,7 @@ static void benchmark_conversion_pattern( const std::string& name,
auto delta = high_resolution_clock::now() - start;
auto delta_d = duration_cast<duration<double>>(delta).count();

std::unique_lock<std::mutex> lk(results_mutex);
std::lock_guard<std::mutex> lk(results_mutex);
results.push_back( uint64_t(howmany / delta_d) );
LOG4CXX_INFO_FMT( console, "Log4cxx {} pattern: {} Elapsed: {:.4} secs {:L}/sec",
name,
Expand Down

0 comments on commit 57295d2

Please sign in to comment.