comphelper/source/misc/mimeconfighelper.cxx    |   20 ++--
 i18nlangtag/source/languagetag/languagetag.cxx |  107 ++++++++++++++-----------
 include/comphelper/mimeconfighelper.hxx        |    6 +
 3 files changed, 78 insertions(+), 55 deletions(-)

New commits:
commit 626296cb6cb1e81d40b9554fb6f15b8912d410ff
Author:     Noel Grandin <[email protected]>
AuthorDate: Sun Nov 21 18:32:29 2021 +0200
Commit:     Noel Grandin <[email protected]>
CommitDate: Mon Nov 22 09:52:34 2021 +0100

    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 <[email protected]>

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 <rtl/bootstrap.hxx>
 #include <sal/log.hxx>
 #include <osl/file.hxx>
-#include <osl/mutex.hxx>
-#include <rtl/instance.hxx>
 #include <rtl/locale.h>
 #include <tools/long.hxx>
 #include <algorithm>
 #include <map>
+#include <mutex>
 #include <string_view>
 #include <unordered_set>
 
@@ -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<LanguageTagImpl>( *this);
-            pImpl = rDontKnow;
+            if (!theDontKnow)
+                theDontKnow = std::make_shared<LanguageTagImpl>( *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()
commit ef1e087dc94cf83b293e05e2cacbac69fa92bbdc
Author:     Noel Grandin <[email protected]>
AuthorDate: Sun Nov 21 18:39:25 2021 +0200
Commit:     Noel Grandin <[email protected]>
CommitDate: Mon Nov 22 09:52:18 2021 +0100

    osl::Mutex->std::mutex in MimeConfigurationHelper
    
    Change-Id: Ib9a628d42448aea858271094ef5bdaac022b0f21
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/125633
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <[email protected]>

diff --git a/comphelper/source/misc/mimeconfighelper.cxx 
b/comphelper/source/misc/mimeconfighelper.cxx
index 82867b7bae48..4bc4410c225c 100644
--- a/comphelper/source/misc/mimeconfighelper.cxx
+++ b/comphelper/source/misc/mimeconfighelper.cxx
@@ -116,8 +116,12 @@ uno::Sequence< sal_Int8 > 
MimeConfigurationHelper::GetSequenceClassIDRepresentat
 
 uno::Reference< container::XNameAccess > 
MimeConfigurationHelper::GetConfigurationByPath( const OUString& aPath )
 {
-    osl::MutexGuard aGuard( m_aMutex );
+    std::unique_lock aGuard( m_aMutex );
+    return GetConfigurationByPathImpl(aPath);
+}
 
+uno::Reference< container::XNameAccess > 
MimeConfigurationHelper::GetConfigurationByPathImpl( const OUString& aPath )
+{
     uno::Reference< container::XNameAccess > xConfig;
 
     try
@@ -143,10 +147,10 @@ uno::Reference< container::XNameAccess > 
MimeConfigurationHelper::GetConfigurati
 
 uno::Reference< container::XNameAccess > 
MimeConfigurationHelper::GetObjConfiguration()
 {
-    osl::MutexGuard aGuard( m_aMutex );
+    std::unique_lock aGuard( m_aMutex );
 
     if ( !m_xObjectConfig.is() )
-        m_xObjectConfig = GetConfigurationByPath(
+        m_xObjectConfig = GetConfigurationByPathImpl(
                                          
"/org.openoffice.Office.Embedding/Objects" );
 
     return m_xObjectConfig;
@@ -155,10 +159,10 @@ uno::Reference< container::XNameAccess > 
MimeConfigurationHelper::GetObjConfigur
 
 uno::Reference< container::XNameAccess > 
MimeConfigurationHelper::GetVerbsConfiguration()
 {
-    osl::MutexGuard aGuard( m_aMutex );
+    std::unique_lock aGuard( m_aMutex );
 
     if ( !m_xVerbsConfig.is() )
-        m_xVerbsConfig = GetConfigurationByPath(
+        m_xVerbsConfig = GetConfigurationByPathImpl(
                                         
"/org.openoffice.Office.Embedding/Verbs");
 
     return m_xVerbsConfig;
@@ -167,10 +171,10 @@ uno::Reference< container::XNameAccess > 
MimeConfigurationHelper::GetVerbsConfig
 
 uno::Reference< container::XNameAccess > 
MimeConfigurationHelper::GetMediaTypeConfiguration()
 {
-    osl::MutexGuard aGuard( m_aMutex );
+    std::unique_lock aGuard( m_aMutex );
 
     if ( !m_xMediaTypeConfig.is() )
-        m_xMediaTypeConfig = GetConfigurationByPath(
+        m_xMediaTypeConfig = GetConfigurationByPathImpl(
                     
"/org.openoffice.Office.Embedding/MimeTypeClassIDRelations");
 
     return m_xMediaTypeConfig;
@@ -179,7 +183,7 @@ uno::Reference< container::XNameAccess > 
MimeConfigurationHelper::GetMediaTypeCo
 
 uno::Reference< container::XNameAccess > 
MimeConfigurationHelper::GetFilterFactory()
 {
-    osl::MutexGuard aGuard( m_aMutex );
+    std::unique_lock aGuard( m_aMutex );
 
     if ( !m_xFilterFactory.is() )
         m_xFilterFactory.set(
diff --git a/include/comphelper/mimeconfighelper.hxx 
b/include/comphelper/mimeconfighelper.hxx
index 5a5585f00d55..4e674538e29c 100644
--- a/include/comphelper/mimeconfighelper.hxx
+++ b/include/comphelper/mimeconfighelper.hxx
@@ -22,6 +22,7 @@
 
 #include <com/sun/star/uno/Reference.hxx>
 #include <comphelper/comphelperdllapi.h>
+#include <mutex>
 
 namespace com::sun::star::beans { struct NamedValue; }
 namespace com::sun::star::beans { struct PropertyValue; }
@@ -37,7 +38,7 @@ namespace comphelper {
 
 class COMPHELPER_DLLPUBLIC MimeConfigurationHelper
 {
-    ::osl::Mutex                                           m_aMutex;
+    std::mutex                                             m_aMutex;
     css::uno::Reference< css::uno::XComponentContext >     m_xContext;
     css::uno::Reference< css::lang::XMultiServiceFactory > m_xConfigProvider;
 
@@ -133,6 +134,9 @@ public:
     static css::uno::Sequence< sal_Int8 > GetSequenceClassID( sal_uInt32 n1, 
sal_uInt16 n2, sal_uInt16 n3,
                                                 sal_uInt8 b8, sal_uInt8 b9, 
sal_uInt8 b10, sal_uInt8 b11,
                                                 sal_uInt8 b12, sal_uInt8 b13, 
sal_uInt8 b14, sal_uInt8 b15 );
+private:
+    css::uno::Reference< css::container::XNameAccess >
+                                            GetConfigurationByPathImpl( const 
OUString& aPath );
 };
 
 }

Reply via email to