From 626296cb6cb1e81d40b9554fb6f15b8912d410ff Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Sun, 21 Nov 2021 18:32:29 +0200 Subject: osl::Mutex->std::recursive_mutex in LanguageTag Change-Id: I75e2d0b78ebf4390ed67d94eb1021145f245fe50 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/125631 Tested-by: Jenkins Reviewed-by: Noel Grandin --- i18nlangtag/source/languagetag/languagetag.cxx | 107 ++++++++++++++----------- 1 file changed, 61 insertions(+), 46 deletions(-) (limited to 'i18nlangtag') diff --git a/i18nlangtag/source/languagetag/languagetag.cxx b/i18nlangtag/source/languagetag/languagetag.cxx index 6f6a766e861f..ed1614059d64 100644 --- a/i18nlangtag/source/languagetag/languagetag.cxx +++ b/i18nlangtag/source/languagetag/languagetag.cxx @@ -17,12 +17,11 @@ #include #include #include -#include -#include #include #include #include #include +#include #include #include @@ -49,24 +48,22 @@ struct myLtError ~myLtError() { if (p) lt_error_unref( p); } }; -// "static" to be returned as const reference to an empty locale. -struct theEmptyLocale : public rtl::Static< lang::Locale, theEmptyLocale > {}; } -typedef std::unordered_set< OUString > KnownTagSet; namespace { -struct theKnowns : public rtl::Static< KnownTagSet, theKnowns > {}; -struct theMutex : public rtl::Static< osl::Mutex, theMutex > {}; +std::recursive_mutex& theMutex() +{ + static std::recursive_mutex SINGLETON; + return SINGLETON; +} } +typedef std::unordered_set< OUString > KnownTagSet; static const KnownTagSet & getKnowns() { - KnownTagSet & rKnowns = theKnowns::get(); - if (rKnowns.empty()) - { - osl::MutexGuard aGuard( theMutex::get()); - if (rKnowns.empty()) + static KnownTagSet theKnowns = []() { + KnownTagSet tmpSet; ::std::vector< MsLangId::LanguagetagMapping > aDefined( MsLangId::getDefinedLanguagetags()); for (auto const& elemDefined : aDefined) { @@ -76,12 +73,12 @@ static const KnownTagSet & getKnowns() ::std::vector< OUString > aFallbacks( LanguageTag( elemDefined.mnLang).getFallbackStrings( true)); for (auto const& fallback : aFallbacks) { - rKnowns.insert(fallback); + tmpSet.insert(fallback); } } - } - } - return rKnowns; + return tmpSet; + }(); + return theKnowns; } @@ -95,17 +92,28 @@ struct compareIgnoreAsciiCaseLess }; typedef ::std::map< OUString, LanguageTag::ImplPtr, compareIgnoreAsciiCaseLess > MapBcp47; typedef ::std::map< LanguageType, LanguageTag::ImplPtr > MapLangID; -struct theMapBcp47 : public rtl::Static< MapBcp47, theMapBcp47 > {}; -struct theMapLangID : public rtl::Static< MapLangID, theMapLangID > {}; -struct theDontKnow : public rtl::Static< LanguageTag::ImplPtr, theDontKnow > {}; -struct theSystemLocale : public rtl::Static< LanguageTag::ImplPtr, theSystemLocale > {}; +MapBcp47& theMapBcp47() +{ + static MapBcp47 SINGLETON; + return SINGLETON; +} +MapLangID& theMapLangID() +{ + static MapLangID SINGLETON; + return SINGLETON; +} +LanguageTag::ImplPtr& theSystemLocale() +{ + static LanguageTag::ImplPtr SINGLETON; + return SINGLETON; +} } static LanguageType getNextOnTheFlyLanguage() { static LanguageType nOnTheFlyLanguage(0); - osl::MutexGuard aGuard( theMutex::get()); + std::unique_lock aGuard( theMutex()); if (!nOnTheFlyLanguage) nOnTheFlyLanguage = MsLangId::makeLangID( LANGUAGE_ON_THE_FLY_SUB_START, LANGUAGE_ON_THE_FLY_START); else @@ -171,7 +179,11 @@ private: static void teardown(); }; -struct theDataRef : public rtl::Static< LiblangtagDataRef, theDataRef > {}; +LiblangtagDataRef& theDataRef() +{ + static LiblangtagDataRef SINGLETON; + return SINGLETON; +} } LiblangtagDataRef::LiblangtagDataRef() @@ -429,7 +441,7 @@ LanguageTagImpl::LanguageTagImpl( const LanguageTagImpl & rLanguageTagImpl ) mbCachedGlibcString( rLanguageTagImpl.mbCachedGlibcString) { if (mpImplLangtag) - theDataRef::get().init(); + theDataRef().init(); } @@ -465,7 +477,7 @@ LanguageTagImpl& LanguageTagImpl::operator=( const LanguageTagImpl & rLanguageTa mbCachedVariants = rLanguageTagImpl.mbCachedVariants; mbCachedGlibcString = rLanguageTagImpl.mbCachedGlibcString; if (mpImplLangtag && !oldTag) - theDataRef::get().init(); + theDataRef().init(); return *this; } @@ -596,9 +608,9 @@ LanguageTag::ImplPtr LanguageTagImpl::registerOnTheFly( LanguageType nRegisterID return pImpl; } - osl::MutexGuard aGuard( theMutex::get()); + std::unique_lock aGuard( theMutex()); - MapBcp47& rMapBcp47 = theMapBcp47::get(); + MapBcp47& rMapBcp47 = theMapBcp47(); MapBcp47::const_iterator it( rMapBcp47.find( maBcp47)); bool bOtherImpl = false; if (it != rMapBcp47.end()) @@ -634,7 +646,7 @@ LanguageTag::ImplPtr LanguageTagImpl::registerOnTheFly( LanguageType nRegisterID // different, otherwise we would end up with ambiguous assignments // of different language tags, for example for the same primary // LangID with "no", "nb" and "nn". - const MapLangID& rMapLangID = theMapLangID::get(); + const MapLangID& rMapLangID = theMapLangID(); MapLangID::const_iterator itID( rMapLangID.find( nRegisterID)); if (itID != rMapLangID.end()) { @@ -667,7 +679,7 @@ LanguageTag::ImplPtr LanguageTagImpl::registerOnTheFly( LanguageType nRegisterID } ::std::pair< MapLangID::const_iterator, bool > res( - theMapLangID::get().insert( ::std::make_pair( pImpl->mnLangID, pImpl))); + theMapLangID().insert( ::std::make_pair( pImpl->mnLangID, pImpl))); if (res.second) { SAL_INFO( "i18nlangtag", "LanguageTag::registerOnTheFly: cross-inserted 0x" @@ -686,7 +698,7 @@ LanguageTag::ImplPtr LanguageTagImpl::registerOnTheFly( LanguageType nRegisterID LanguageTag::ScriptType LanguageTag::getOnTheFlyScriptType( LanguageType nRegisterID ) { - const MapLangID& rMapLangID = theMapLangID::get(); + const MapLangID& rMapLangID = theMapLangID(); MapLangID::const_iterator itID( rMapLangID.find( nRegisterID)); if (itID != rMapLangID.end()) return (*itID).second->getScriptType(); @@ -709,7 +721,7 @@ void LanguageTag::setConfiguredSystemLanguage( LanguageType nLang ) MsLangId::LanguageTagAccess::setConfiguredSystemLanguage( nLang); // Reset system locale to none and let registerImpl() do the rest to // initialize a new one. - theSystemLocale::get().reset(); + theSystemLocale().reset(); LanguageTag aLanguageTag( LANGUAGE_SYSTEM); aLanguageTag.registerImpl(); } @@ -749,7 +761,7 @@ LanguageTag::ImplPtr LanguageTag::registerImpl() const // and take the system locale shortcut if possible. if (mbSystemLocale) { - pImpl = theSystemLocale::get(); + pImpl = theSystemLocale(); if (pImpl) { #if OSL_DEBUG_LEVEL > 0 @@ -771,13 +783,13 @@ LanguageTag::ImplPtr LanguageTag::registerImpl() const { if (mnLangID == LANGUAGE_DONTKNOW) { + static LanguageTag::ImplPtr theDontKnow; // Heavy usage of LANGUAGE_DONTKNOW, make it an own Impl for all the // conversion attempts. At the same time provide a central breakpoint // to inspect such places. - LanguageTag::ImplPtr& rDontKnow = theDontKnow::get(); - if (!rDontKnow) - rDontKnow = std::make_shared( *this); - pImpl = rDontKnow; + if (!theDontKnow) + theDontKnow = std::make_shared( *this); + pImpl = theDontKnow; #if OSL_DEBUG_LEVEL > 0 static size_t nCallsDontKnow = 0; ++nCallsDontKnow; @@ -788,7 +800,7 @@ LanguageTag::ImplPtr LanguageTag::registerImpl() const else { // A great share are calls for a system equal locale. - pImpl = theSystemLocale::get(); + pImpl = theSystemLocale(); if (pImpl && pImpl->mnLangID == mnLangID) { #if OSL_DEBUG_LEVEL > 0 @@ -819,7 +831,7 @@ LanguageTag::ImplPtr LanguageTag::registerImpl() const if (mbInitializedBcp47) { // A great share are calls for a system equal locale. - pImpl = theSystemLocale::get(); + pImpl = theSystemLocale(); if (pImpl && pImpl->maBcp47 == maBcp47) { #if OSL_DEBUG_LEVEL > 0 @@ -837,7 +849,7 @@ LanguageTag::ImplPtr LanguageTag::registerImpl() const SAL_INFO( "i18nlangtag", "LanguageTag::registerImpl: " << nCallsNonSystem << " non-system calls"); #endif - osl::MutexGuard aGuard( theMutex::get()); + std::unique_lock aGuard( theMutex()); #if OSL_DEBUG_LEVEL > 0 static tools::Long nRunning = 0; @@ -852,7 +864,7 @@ LanguageTag::ImplPtr LanguageTag::registerImpl() const // Prefer LangID map as find+insert needs less comparison work. if (mbInitializedLangID) { - MapLangID& rMap = theMapLangID::get(); + MapLangID& rMap = theMapLangID(); MapLangID::const_iterator it( rMap.find( mnLangID)); if (it != rMap.end()) { @@ -874,7 +886,7 @@ LanguageTag::ImplPtr LanguageTag::registerImpl() const if (!pImpl->mbInitializedBcp47) pImpl->convertLocaleToBcp47(); ::std::pair< MapBcp47::const_iterator, bool > res( - theMapBcp47::get().insert( ::std::make_pair( pImpl->maBcp47, pImpl))); + theMapBcp47().insert( ::std::make_pair( pImpl->maBcp47, pImpl))); if (res.second) { SAL_INFO( "i18nlangtag", "LanguageTag::registerImpl: cross-inserted '" << pImpl->maBcp47 << "' for 0x" << ::std::hex << mnLangID); @@ -895,7 +907,7 @@ LanguageTag::ImplPtr LanguageTag::registerImpl() const } else if (!maBcp47.isEmpty()) { - MapBcp47& rMap = theMapBcp47::get(); + MapBcp47& rMap = theMapBcp47(); MapBcp47::const_iterator it( rMap.find( maBcp47)); if (it != rMap.end()) { @@ -957,7 +969,7 @@ LanguageTag::ImplPtr LanguageTag::registerImpl() const if (bInsert) { ::std::pair< MapLangID::const_iterator, bool > res( - theMapLangID::get().insert( ::std::make_pair( pImpl->mnLangID, pImpl))); + theMapLangID().insert( ::std::make_pair( pImpl->mnLangID, pImpl))); if (res.second) { SAL_INFO( "i18nlangtag", "LanguageTag::registerImpl: cross-inserted 0x" @@ -989,7 +1001,7 @@ LanguageTag::ImplPtr LanguageTag::registerImpl() const // above, so add it. if (mbSystemLocale && mbInitializedLangID) { - theSystemLocale::get() = pImpl; + theSystemLocale() = pImpl; SAL_INFO( "i18nlangtag", "LanguageTag::registerImpl: added system locale 0x" << ::std::hex << pImpl->mnLangID << " '" << pImpl->maBcp47 << "'"); } @@ -1211,7 +1223,7 @@ bool LanguageTagImpl::canonicalize() if (!mpImplLangtag) { - theDataRef::get().init(); + theDataRef().init(); mpImplLangtag = lt_tag_new(); } @@ -1698,8 +1710,11 @@ OUString LanguageTagImpl::getVariantsFromLangtag() const css::lang::Locale & LanguageTag::getLocale( bool bResolveSystem ) const { + // "static" to be returned as const reference to an empty locale. + static lang::Locale theEmptyLocale; + if (!bResolveSystem && mbSystemLocale) - return theEmptyLocale::get(); + return theEmptyLocale; if (!mbInitializedLocale) syncVarsFromImpl(); if (!mbInitializedLocale) @@ -2832,7 +2847,7 @@ bool LanguageTag::isValidBcp47( const OUString& rString, OUString* o_pCanonicali lt_tag_t* mpLangtag; guard() { - theDataRef::get().init(); + theDataRef().init(); mpLangtag = lt_tag_new(); } ~guard() -- cgit