From f7a8c3cc4dc9a316b39e8680a2af330d8f8ba296 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Thu, 18 Nov 2021 20:24:57 +0200 Subject: [PATCH] rtl::Static->thread-safe static in unotools Change-Id: I2a8c9ca7c45a8e20f3d114722a0677b856a13d8c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/125495 Tested-by: Jenkins Reviewed-by: Noel Grandin --- unotools/source/config/bootstrap.cxx | 11 ++++--- unotools/source/config/cmdoptions.cxx | 9 ++---- unotools/source/config/configmgr.cxx | 8 ++---- unotools/source/config/fontcfg.cxx | 24 +++------------- unotools/source/config/lingucfg.cxx | 32 +++++++++++---------- unotools/source/config/moduleoptions.cxx | 1 - unotools/source/config/pathoptions.cxx | 14 ++++++--- unotools/source/config/syslocaleoptions.cxx | 10 +++---- unotools/source/i18n/textsearch.cxx | 1 - unotools/source/ucbhelper/tempfile.cxx | 2 +- 10 files changed, 47 insertions(+), 65 deletions(-) diff --git a/unotools/source/config/bootstrap.cxx b/unotools/source/config/bootstrap.cxx index 51d3625e4af0..09c52157024b 100644 --- a/unotools/source/config/bootstrap.cxx +++ b/unotools/source/config/bootstrap.cxx @@ -32,7 +32,6 @@ #include #include -#include #include // #define this to true, if remembering defaults is not supported properly @@ -112,12 +111,16 @@ private: // implementation namespace { - class theImpl : public rtl::Static {}; + Bootstrap::Impl& theImpl() + { + static Bootstrap::Impl SINGLETON; + return SINGLETON; + } } const Bootstrap::Impl& Bootstrap::data() { - return theImpl::get(); + return theImpl(); } bool Bootstrap::getProcessWorkingDir(OUString &rUrl) @@ -146,7 +149,7 @@ bool Bootstrap::getProcessWorkingDir(OUString &rUrl) void Bootstrap::reloadData() { - theImpl::get().initialize(); + theImpl().initialize(); } // helper diff --git a/unotools/source/config/cmdoptions.cxx b/unotools/source/config/cmdoptions.cxx index b61e49840390..d9e061224a4a 100644 --- a/unotools/source/config/cmdoptions.cxx +++ b/unotools/source/config/cmdoptions.cxx @@ -27,7 +27,6 @@ #include #include #include -#include #include "itemholder1.hxx" @@ -340,16 +339,12 @@ void SvtCommandOptions::EstablishFrameCallback(const css::uno::Reference< css::f m_pImpl->EstablishFrameCallback(xFrame); } -namespace -{ - class theCommandOptionsMutex : public rtl::Static{}; -} - // private method Mutex& SvtCommandOptions::GetOwnStaticMutex() { - return theCommandOptionsMutex::get(); + static osl::Mutex theCommandOptionsMutex; + return theCommandOptionsMutex; } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/configmgr.cxx b/unotools/source/config/configmgr.cxx index 84ab42cc795d..5b109788d2cd 100644 --- a/unotools/source/config/configmgr.cxx +++ b/unotools/source/config/configmgr.cxx @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include @@ -67,10 +66,6 @@ getConfigurationProvider() { return css::configuration::theDefaultProvider::get( comphelper::getProcessComponentContext() ); } -struct theConfigManager: - public rtl::Static< utl::ConfigManager, theConfigManager > -{}; - } OUString utl::ConfigManager::getAboutBoxProductVersion() { @@ -114,7 +109,8 @@ void utl::ConfigManager::storeConfigItems() { } utl::ConfigManager & utl::ConfigManager::getConfigManager() { - return theConfigManager::get(); + static utl::ConfigManager theConfigManager; + return theConfigManager; } css::uno::Reference< css::container::XHierarchicalNameAccess > diff --git a/unotools/source/config/fontcfg.cxx b/unotools/source/config/fontcfg.cxx index cfa6705a41c6..ef94981d7746 100644 --- a/unotools/source/config/fontcfg.cxx +++ b/unotools/source/config/fontcfg.cxx @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -83,18 +82,10 @@ static const char* getKeyType( DefaultFontType nKeyType ) } } -namespace -{ - class theDefaultFontConfiguration - : public rtl::Static - { - }; -} - DefaultFontConfiguration& DefaultFontConfiguration::get() { - return theDefaultFontConfiguration::get(); + static DefaultFontConfiguration theDefaultFontConfiguration; + return theDefaultFontConfiguration; } DefaultFontConfiguration::DefaultFontConfiguration() @@ -297,17 +288,10 @@ OUString DefaultFontConfiguration::getUserInterfaceFont( const LanguageTag& rLan * FontSubstConfigItem::get */ -namespace -{ - class theFontSubstConfiguration - : public rtl::Static - { - }; -} - FontSubstConfiguration& FontSubstConfiguration::get() { - return theFontSubstConfiguration::get(); + static FontSubstConfiguration theFontSubstConfiguration; + return theFontSubstConfiguration; } /* diff --git a/unotools/source/config/lingucfg.cxx b/unotools/source/config/lingucfg.cxx index eaf4310ca314..f37fffb5734a 100644 --- a/unotools/source/config/lingucfg.cxx +++ b/unotools/source/config/lingucfg.cxx @@ -25,7 +25,6 @@ #include #include #include -#include #include #include #include @@ -47,8 +46,11 @@ constexpr OUStringLiteral FILE_PROTOCOL = u"file:///"; namespace { - class theSvtLinguConfigItemMutex : - public rtl::Static< osl::Mutex, theSvtLinguConfigItemMutex > {}; + osl::Mutex& theSvtLinguConfigItemMutex() + { + static osl::Mutex SINGLETON; + return SINGLETON; + } } static bool lcl_SetLocale( LanguageType &rLanguage, const uno::Any &rVal ) @@ -319,7 +321,7 @@ bool SvtLinguConfigItem::GetHdlByName( uno::Any SvtLinguConfigItem::GetProperty( const OUString &rPropertyName ) const { - osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex()); sal_Int32 nHdl; return GetHdlByName( nHdl, rPropertyName ) ? GetProperty( nHdl ) : uno::Any(); @@ -327,7 +329,7 @@ uno::Any SvtLinguConfigItem::GetProperty( const OUString &rPropertyName ) const uno::Any SvtLinguConfigItem::GetProperty( sal_Int32 nPropertyHandle ) const { - osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex()); uno::Any aRes; @@ -409,7 +411,7 @@ uno::Any SvtLinguConfigItem::GetProperty( sal_Int32 nPropertyHandle ) const bool SvtLinguConfigItem::SetProperty( const OUString &rPropertyName, const uno::Any &rValue ) { - osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex()); bool bSucc = false; sal_Int32 nHdl; @@ -420,7 +422,7 @@ bool SvtLinguConfigItem::SetProperty( const OUString &rPropertyName, const uno:: bool SvtLinguConfigItem::SetProperty( sal_Int32 nPropertyHandle, const uno::Any &rValue ) { - osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex()); bool bSucc = false; if (!rValue.hasValue()) @@ -559,13 +561,13 @@ bool SvtLinguConfigItem::SetProperty( sal_Int32 nPropertyHandle, const uno::Any const SvtLinguOptions& SvtLinguConfigItem::GetOptions() const { - osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex()); return aOpt; } void SvtLinguConfigItem::LoadOptions( const uno::Sequence< OUString > &rProperyNames ) { - osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex()); bool bRes = false; @@ -684,7 +686,7 @@ bool SvtLinguConfigItem::SaveOptions( const uno::Sequence< OUString > &rProperyN if (!IsModified()) return true; - osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex()); bool bRet = false; @@ -746,7 +748,7 @@ bool SvtLinguConfigItem::SaveOptions( const uno::Sequence< OUString > &rProperyN bool SvtLinguConfigItem::IsReadOnly( const OUString &rPropertyName ) const { - osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex()); bool bReadOnly = false; sal_Int32 nHdl; @@ -757,7 +759,7 @@ bool SvtLinguConfigItem::IsReadOnly( const OUString &rPropertyName ) const bool SvtLinguConfigItem::IsReadOnly( sal_Int32 nPropertyHandle ) const { - osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex()); bool bReadOnly = false; @@ -808,13 +810,13 @@ constexpr OUStringLiteral aG_Dictionaries = u"Dictionaries"; SvtLinguConfig::SvtLinguConfig() { // Global access, must be guarded (multithreading) - osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex()); ++nCfgItemRefCount; } SvtLinguConfig::~SvtLinguConfig() { - osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex()); if (pCfgItem && pCfgItem->IsModified()) pCfgItem->Commit(); @@ -829,7 +831,7 @@ SvtLinguConfig::~SvtLinguConfig() SvtLinguConfigItem & SvtLinguConfig::GetConfigItem() { // Global access, must be guarded (multithreading) - osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex()); if (!pCfgItem) { pCfgItem = new SvtLinguConfigItem; diff --git a/unotools/source/config/moduleoptions.cxx b/unotools/source/config/moduleoptions.cxx index 0af82141d30a..324736359b6e 100644 --- a/unotools/source/config/moduleoptions.cxx +++ b/unotools/source/config/moduleoptions.cxx @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include diff --git a/unotools/source/config/pathoptions.cxx b/unotools/source/config/pathoptions.cxx index 59e8a978d0b1..60335e00dfb9 100644 --- a/unotools/source/config/pathoptions.cxx +++ b/unotools/source/config/pathoptions.cxx @@ -37,7 +37,6 @@ #include #include #include -#include #include #include "itemholder1.hxx" @@ -433,12 +432,19 @@ SvtPathOptions_Impl::SvtPathOptions_Impl() // class SvtPathOptions -------------------------------------------------- -namespace { struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {}; } +namespace +{ + ::osl::Mutex& lclMutex() + { + static ::osl::Mutex SINGLETON; + return SINGLETON; + } +} SvtPathOptions::SvtPathOptions() { // Global access, must be guarded (multithreading) - ::osl::MutexGuard aGuard( lclMutex::get() ); + ::osl::MutexGuard aGuard( lclMutex() ); pImpl = g_pOptions.lock(); if ( !pImpl ) { @@ -451,7 +457,7 @@ SvtPathOptions::SvtPathOptions() SvtPathOptions::~SvtPathOptions() { // Global access, must be guarded (multithreading) - ::osl::MutexGuard aGuard( lclMutex::get() ); + ::osl::MutexGuard aGuard( lclMutex() ); pImpl.reset(); } diff --git a/unotools/source/config/syslocaleoptions.cxx b/unotools/source/config/syslocaleoptions.cxx index 7e38237ec3b2..8f08202a7a23 100644 --- a/unotools/source/config/syslocaleoptions.cxx +++ b/unotools/source/config/syslocaleoptions.cxx @@ -18,7 +18,6 @@ */ #include -#include #include #include #include @@ -41,8 +40,7 @@ using namespace com::sun::star::lang; namespace { std::weak_ptr g_pSysLocaleOptions; - struct CurrencyChangeLink - : public rtl::Static, CurrencyChangeLink> {}; + Link g_CurrencyChangeLink; Mutex& GetMutex() { @@ -669,15 +667,15 @@ OUString SvtSysLocaleOptions::CreateCurrencyConfigString( void SvtSysLocaleOptions::SetCurrencyChangeLink( const Link& rLink ) { MutexGuard aGuard( GetMutex() ); - DBG_ASSERT( !CurrencyChangeLink::get().IsSet(), "SvtSysLocaleOptions::SetCurrencyChangeLink: already set" ); - CurrencyChangeLink::get() = rLink; + DBG_ASSERT( !g_CurrencyChangeLink.IsSet(), "SvtSysLocaleOptions::SetCurrencyChangeLink: already set" ); + g_CurrencyChangeLink = rLink; } // static const Link& SvtSysLocaleOptions::GetCurrencyChangeLink() { MutexGuard aGuard( GetMutex() ); - return CurrencyChangeLink::get(); + return g_CurrencyChangeLink; } void SvtSysLocaleOptions::ConfigurationChanged( utl::ConfigurationBroadcaster* p, ConfigurationHints nHint ) diff --git a/unotools/source/i18n/textsearch.cxx b/unotools/source/i18n/textsearch.cxx index ba75b0b2319b..0ed0539b0e17 100644 --- a/unotools/source/i18n/textsearch.cxx +++ b/unotools/source/i18n/textsearch.cxx @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include diff --git a/unotools/source/ucbhelper/tempfile.cxx b/unotools/source/ucbhelper/tempfile.cxx index 22ce434b0e3e..02770fda8744 100644 --- a/unotools/source/ucbhelper/tempfile.cxx +++ b/unotools/source/ucbhelper/tempfile.cxx @@ -24,7 +24,7 @@ #include #include -#include +#include #include #include #include