[Libreoffice-commits] core.git: stoc/source svl/source svtools/source ucb/source unotools/source

Jochen Nitschke j.nitschke+logerrit at ok.de
Sat Jun 17 13:44:05 UTC 2017


 stoc/source/javaloader/javaloader.cxx       |   14 +++-----------
 svl/source/numbers/zforlist.cxx             |   19 ++++++-------------
 svtools/source/config/helpopt.cxx           |   13 ++-----------
 svtools/source/config/menuoptions.cxx       |   21 +++------------------
 svtools/source/config/printoptions.cxx      |   21 +++------------------
 ucb/source/sorter/sortdynres.cxx            |   13 ++-----------
 ucb/source/sorter/sortresult.cxx            |   13 ++-----------
 unotools/source/config/syslocaleoptions.cxx |   19 ++++++-------------
 unotools/source/misc/syslocale.cxx          |   19 ++++++-------------
 9 files changed, 33 insertions(+), 119 deletions(-)

New commits:
commit 6149da20ddee5c0c50b445fb6a4e3e81b5ff900b
Author: Jochen Nitschke <j.nitschke+logerrit at ok.de>
Date:   Sat Jun 17 14:01:36 2017 +0200

    replace misc double checked locking patterns
    
    ... with thread safe local statics
    
    Change-Id: Ie3c8023776a388846b989f00a0be185273c0d5da
    Reviewed-on: https://gerrit.libreoffice.org/38907
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/stoc/source/javaloader/javaloader.cxx b/stoc/source/javaloader/javaloader.cxx
index db850bdb5e6a..01000b0ab827 100644
--- a/stoc/source/javaloader/javaloader.cxx
+++ b/stoc/source/javaloader/javaloader.cxx
@@ -330,17 +330,9 @@ css::uno::Reference<XInterface> SAL_CALL JavaComponentLoader::activate(
 
 static Mutex & getInitMutex()
 {
-    static Mutex * pMutex = nullptr;
-    if( ! pMutex )
-    {
-        MutexGuard guard( Mutex::getGlobalMutex() );
-        if( ! pMutex )
-        {
-            static Mutex mutex;
-            pMutex = &mutex;
-        }
-    }
-    return *pMutex;
+    static Mutex ourMutex;
+
+    return ourMutex;
 }
 
 /// @throws Exception
diff --git a/svl/source/numbers/zforlist.cxx b/svl/source/numbers/zforlist.cxx
index 876dfbde187e..1b77382bc6bd 100644
--- a/svl/source/numbers/zforlist.cxx
+++ b/svl/source/numbers/zforlist.cxx
@@ -350,19 +350,12 @@ void SvNumberFormatter::ChangeIntl(LanguageType eLnge)
 // static
 ::osl::Mutex& SvNumberFormatter::GetMutex()
 {
-    static ::osl::Mutex* pMutex = nullptr;
-    if( !pMutex )
-    {
-        ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
-        if( !pMutex )
-        {
-            // #i77768# Due to a static reference in the toolkit lib
-            // we need a mutex that lives longer than the svl library.
-            // Otherwise the dtor would use a destructed mutex!!
-            pMutex = new ::osl::Mutex;
-        }
-    }
-    return *pMutex;
+    // #i77768# Due to a static reference in the toolkit lib
+    // we need a mutex that lives longer than the svl library.
+    // Otherwise the dtor would use a destructed mutex!!
+    static ::osl::Mutex persistantMutex;
+
+    return persistantMutex;
 }
 
 
diff --git a/svtools/source/config/helpopt.cxx b/svtools/source/config/helpopt.cxx
index 381d67f7f0a4..bc1ac4d014c1 100644
--- a/svtools/source/config/helpopt.cxx
+++ b/svtools/source/config/helpopt.cxx
@@ -98,18 +98,9 @@ Sequence< OUString > const & SvtHelpOptions_Impl::GetPropertyNames()
 
 ::osl::Mutex & SvtHelpOptions_Impl::getInitMutex()
 {
-    static ::osl::Mutex *pMutex = nullptr;
+    static ::osl::Mutex ourMutex;
 
-    if( ! pMutex )
-    {
-        ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
-        if( ! pMutex )
-        {
-            static ::osl::Mutex mutex;
-            pMutex = &mutex;
-        }
-    }
-    return *pMutex;
+    return ourMutex;
 }
 
 SvtHelpOptions_Impl::SvtHelpOptions_Impl()
diff --git a/svtools/source/config/menuoptions.cxx b/svtools/source/config/menuoptions.cxx
index cc8b64b760bc..8bec940d4000 100644
--- a/svtools/source/config/menuoptions.cxx
+++ b/svtools/source/config/menuoptions.cxx
@@ -410,24 +410,9 @@ void SvtMenuOptions::SetContextMenuShortcuts(TriState eState)
 
 Mutex& SvtMenuOptions::GetOwnStaticMutex()
 {
-    // Initialize static mutex only for one time!
-    static Mutex* pMutex = nullptr;
-    // If these method first called (Mutex not already exist!) ...
-    if( pMutex == nullptr )
-    {
-        // ... we must create a new one. Protect follow code with the global mutex -
-        // It must be - we create a static variable!
-        MutexGuard aGuard( Mutex::getGlobalMutex() );
-        // We must check our pointer again - because it can be that another instance of our class will be faster than these!
-        if( pMutex == nullptr )
-        {
-            // Create the new mutex and set it for return on static variable.
-            static Mutex aMutex;
-            pMutex = &aMutex;
-        }
-    }
-    // Return new created or already existing mutex object.
-    return *pMutex;
+    static Mutex ourMutex;
+
+    return ourMutex;
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/svtools/source/config/printoptions.cxx b/svtools/source/config/printoptions.cxx
index be4a73644d4d..05ac6875d4a4 100644
--- a/svtools/source/config/printoptions.cxx
+++ b/svtools/source/config/printoptions.cxx
@@ -508,24 +508,9 @@ SvtBasePrintOptions::~SvtBasePrintOptions()
 
 Mutex& SvtBasePrintOptions::GetOwnStaticMutex()
 {
-    // Initialize static mutex only for one time!
-    static Mutex* pMutex = nullptr;
-    // If these method first called (Mutex not already exist!) ...
-    if( pMutex == nullptr )
-    {
-        // ... we must create a new one. Protect follow code with the global mutex -
-        // It must be - we create a static variable!
-        MutexGuard aGuard( Mutex::getGlobalMutex() );
-        // We must check our pointer again - because it can be that another instance of our class will be faster than these!
-        if( pMutex == nullptr )
-        {
-            // Create the new mutex and set it for return on static variable.
-            static Mutex aMutex;
-            pMutex = &aMutex;
-        }
-    }
-    // Return new created or already existing mutex object.
-    return *pMutex;
+    static Mutex ourMutex;
+
+    return ourMutex;
 }
 
 bool SvtBasePrintOptions::IsReduceTransparency() const
diff --git a/ucb/source/sorter/sortdynres.cxx b/ucb/source/sorter/sortdynres.cxx
index caa5f97afc72..c62f102cc0d2 100644
--- a/ucb/source/sorter/sortdynres.cxx
+++ b/ucb/source/sorter/sortdynres.cxx
@@ -40,18 +40,9 @@ using namespace comphelper;
 //  The mutex to synchronize access to containers.
 static osl::Mutex& getContainerMutex()
 {
-    static osl::Mutex* pMutex = nullptr;
-    if( !pMutex )
-    {
-        osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
-        if( !pMutex )
-        {
-            static osl::Mutex aMutex;
-            pMutex = &aMutex;
-        }
-    }
+    static osl::Mutex ourMutex;
 
-    return *pMutex;
+    return ourMutex;
 }
 
 
diff --git a/ucb/source/sorter/sortresult.cxx b/ucb/source/sorter/sortresult.cxx
index 154dc099831c..f842c45f78a6 100644
--- a/ucb/source/sorter/sortresult.cxx
+++ b/ucb/source/sorter/sortresult.cxx
@@ -49,18 +49,9 @@ using namespace cppu;
 //  The mutex to synchronize access to containers.
 static osl::Mutex& getContainerMutex()
 {
-    static osl::Mutex* pMutex = nullptr;
-    if( !pMutex )
-    {
-        osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
-        if( !pMutex )
-        {
-            static osl::Mutex aMutex;
-            pMutex = &aMutex;
-        }
-    }
+    static osl::Mutex ourMutex;
 
-    return *pMutex;
+    return ourMutex;
 }
 
 
diff --git a/unotools/source/config/syslocaleoptions.cxx b/unotools/source/config/syslocaleoptions.cxx
index 1bfb1be81109..ed1675ffaed1 100644
--- a/unotools/source/config/syslocaleoptions.cxx
+++ b/unotools/source/config/syslocaleoptions.cxx
@@ -523,19 +523,12 @@ SvtSysLocaleOptions::~SvtSysLocaleOptions()
 // static
 Mutex& SvtSysLocaleOptions::GetMutex()
 {
-    static Mutex* pMutex = nullptr;
-    if( !pMutex )
-    {
-        MutexGuard aGuard( Mutex::getGlobalMutex() );
-        if( !pMutex )
-        {
-            // #i77768# Due to a static reference in the toolkit lib
-            // we need a mutex that lives longer than the svl library.
-            // Otherwise the dtor would use a destructed mutex!!
-            pMutex = new Mutex;
-        }
-    }
-    return *pMutex;
+    // #i77768# Due to a static reference in the toolkit lib
+    // we need a mutex that lives longer than the svl library.
+    // Otherwise the dtor would use a destructed mutex!!
+    static Mutex persistentMutex;
+
+    return persistentMutex;
 }
 
 bool SvtSysLocaleOptions::IsModified()
diff --git a/unotools/source/misc/syslocale.cxx b/unotools/source/misc/syslocale.cxx
index 0955090391a2..0dcecca29392 100644
--- a/unotools/source/misc/syslocale.cxx
+++ b/unotools/source/misc/syslocale.cxx
@@ -134,19 +134,12 @@ SvtSysLocale::~SvtSysLocale()
 // static
 Mutex& SvtSysLocale::GetMutex()
 {
-    static Mutex* pMutex = nullptr;
-    if( !pMutex )
-    {
-        MutexGuard aGuard( Mutex::getGlobalMutex() );
-        if( !pMutex )
-        {
-            // #i77768# Due to a static reference in the toolkit lib
-            // we need a mutex that lives longer than the svl library.
-            // Otherwise the dtor would use a destructed mutex!!
-            pMutex = new Mutex;
-        }
-    }
-    return *pMutex;
+    // #i77768# Due to a static reference in the toolkit lib
+    // we need a mutex that lives longer than the svl library.
+    // Otherwise the dtor would use a destructed mutex!!
+    static Mutex persistentMutex;
+
+    return persistentMutex;
 }
 
 const LocaleDataWrapper& SvtSysLocale::GetLocaleData() const


More information about the Libreoffice-commits mailing list