From e368cc61fa715e98fbc448c5b0edc781915f8385 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Mon, 10 May 2021 19:08:08 +0200 Subject: [PATCH] reduce cost of locking in SvtModuleOptions Change-Id: If9e0c275822b733d339845d16edfbc5942b4aa6f Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115354 Tested-by: Jenkins Reviewed-by: Noel Grandin --- unotools/source/config/moduleoptions.cxx | 36 +++++++++++++----------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/unotools/source/config/moduleoptions.cxx b/unotools/source/config/moduleoptions.cxx index ea3db01585c2..e9d9e84a8c06 100644 --- a/unotools/source/config/moduleoptions.cxx +++ b/unotools/source/config/moduleoptions.cxx @@ -779,23 +779,25 @@ osl::Mutex& impl_GetOwnStaticMutex() *//*-*************************************************************************************************************/ SvtModuleOptions::SvtModuleOptions() { - // Global access, must be guarded (multithreading!) - ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); - + // no need to take the mutex yet, shared_ptr/weak_ptr are thread-safe m_pImpl = g_pModuleOptions.lock(); if( !m_pImpl ) { - m_pImpl = std::make_shared(); - g_pModuleOptions = m_pImpl; - ItemHolder1::holdConfigItem(EItem::ModuleOptions); + // take the mutex, so we don't accidentally create more than one + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + + m_pImpl = g_pModuleOptions.lock(); + if( !m_pImpl ) + { + m_pImpl = std::make_shared(); + g_pModuleOptions = m_pImpl; + ItemHolder1::holdConfigItem(EItem::ModuleOptions); + } } } SvtModuleOptions::~SvtModuleOptions() { - // Global access, must be guarded (multithreading!) - ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); - m_pImpl.reset(); } @@ -809,13 +811,13 @@ SvtModuleOptions::~SvtModuleOptions() *//*-*************************************************************************************************************/ bool SvtModuleOptions::IsModuleInstalled( EModule eModule ) const { - ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + // doesn't need mutex, never modified return m_pImpl->IsModuleInstalled( eModule ); } OUString SvtModuleOptions::GetFactoryName( EFactory eFactory ) const { - ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + // doesn't need mutex, never modified return m_pImpl->GetFactoryName( eFactory ); } @@ -873,25 +875,25 @@ void SvtModuleOptions::SetFactoryDefaultFilter( EFactory eFactory, bool SvtModuleOptions::IsMath() const { - ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + // doesn't need mutex, never modified return m_pImpl->IsModuleInstalled( EModule::MATH ); } bool SvtModuleOptions::IsChart() const { - ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + // doesn't need mutex, never modified return m_pImpl->IsModuleInstalled( EModule::CHART ); } bool SvtModuleOptions::IsCalc() const { - ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + // doesn't need mutex, never modified return m_pImpl->IsModuleInstalled( EModule::CALC ); } bool SvtModuleOptions::IsDraw() const { - ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + // doesn't need mutex, never modified return m_pImpl->IsModuleInstalled( EModule::DRAW ); } @@ -903,13 +905,13 @@ bool SvtModuleOptions::IsWriter() const bool SvtModuleOptions::IsImpress() const { - ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + // doesn't need mutex, never modified return m_pImpl->IsModuleInstalled( EModule::IMPRESS ); } bool SvtModuleOptions::IsDataBase() const { - ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + // doesn't need mutex, never modified return m_pImpl->IsModuleInstalled( EModule::DATABASE ); }