[Libreoffice-commits] core.git: comphelper/source

Noel Grandin (via logerrit) logerrit at kemper.freedesktop.org
Sat Aug 21 06:15:51 UTC 2021


 comphelper/source/misc/accessibleeventnotifier.cxx |   47 +++++++++++----------
 comphelper/source/misc/asyncnotification.cxx       |   14 ++++--
 comphelper/source/misc/configuration.cxx           |   16 +++----
 comphelper/source/misc/random.cxx                  |   15 +++---
 comphelper/source/misc/threadpool.cxx              |   15 +++---
 5 files changed, 58 insertions(+), 49 deletions(-)

New commits:
commit 5922c885a27512717080186273799dab5830ab3f
Author:     Noel Grandin <noelgrandin at gmail.com>
AuthorDate: Fri Aug 20 21:11:11 2021 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Sat Aug 21 08:15:18 2021 +0200

    rtl::Static to thread-safe static
    
    Change-Id: Id63bc7dada4ba09389f5a1ebd83c00c8e55faf7d
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/120795
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/comphelper/source/misc/accessibleeventnotifier.cxx b/comphelper/source/misc/accessibleeventnotifier.cxx
index f14176a03528..d2694ab6120b 100644
--- a/comphelper/source/misc/accessibleeventnotifier.cxx
+++ b/comphelper/source/misc/accessibleeventnotifier.cxx
@@ -19,7 +19,6 @@
 
 #include <comphelper/accessibleeventnotifier.hxx>
 #include <com/sun/star/accessibility/XAccessibleEventListener.hpp>
-#include <rtl/instance.hxx>
 #include <comphelper/interfacecontainer2.hxx>
 
 #include <map>
@@ -43,24 +42,30 @@ typedef std::map< AccessibleEventNotifier::TClientId,
 typedef std::map<AccessibleEventNotifier::TClientId,
             AccessibleEventNotifier::TClientId> IntervalMap;
 
-struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {};
+::osl::Mutex& GetLocalMutex()
+{
+    static ::osl::Mutex MUTEX;
+    return MUTEX;
+}
 
-struct Clients : public rtl::Static< ClientMap, Clients > {};
+ClientMap gaClients;
 
-struct FreeIntervals : public rtl::StaticWithInit<IntervalMap, FreeIntervals>
+IntervalMap& GetFreeIntervals()
 {
-    IntervalMap operator() ()
+    static IntervalMap MAP =
+    []()
     {
         IntervalMap map;
         map.insert(std::make_pair(
             std::numeric_limits<AccessibleEventNotifier::TClientId>::max(), 1));
         return map;
-    }
-};
+    }();
+    return MAP;
+}
 
 void releaseId(AccessibleEventNotifier::TClientId const nId)
 {
-    IntervalMap & rFreeIntervals(FreeIntervals::get());
+    IntervalMap & rFreeIntervals(GetFreeIntervals());
     IntervalMap::iterator const upper(rFreeIntervals.upper_bound(nId));
     assert(upper != rFreeIntervals.end());
     assert(nId < upper->second); // second is start of the interval!
@@ -89,7 +94,7 @@ void releaseId(AccessibleEventNotifier::TClientId const nId)
 /// generates a new client id
 AccessibleEventNotifier::TClientId generateId()
 {
-    IntervalMap & rFreeIntervals(FreeIntervals::get());
+    IntervalMap & rFreeIntervals(GetFreeIntervals());
     assert(!rFreeIntervals.empty());
     IntervalMap::iterator const iter(rFreeIntervals.begin());
     AccessibleEventNotifier::TClientId const nFirst = iter->first;
@@ -104,7 +109,7 @@ AccessibleEventNotifier::TClientId generateId()
         rFreeIntervals.erase(iter); // remove 1-element interval
     }
 
-    assert(Clients::get().end() == Clients::get().find(nFreeId));
+    assert(gaClients.end() == gaClients.find(nFreeId));
 
     return nFreeId;
 }
@@ -129,7 +134,7 @@ bool implLookupClient(
         ClientMap::iterator& rPos )
 {
     // look up this client
-    ClientMap &rClients = Clients::get();
+    ClientMap &rClients = gaClients;
     rPos = rClients.find( nClient );
     assert( rClients.end() != rPos &&
         "AccessibleEventNotifier::implLookupClient: invalid client id "
@@ -144,21 +149,21 @@ namespace comphelper {
 
 AccessibleEventNotifier::TClientId AccessibleEventNotifier::registerClient()
 {
-    ::osl::MutexGuard aGuard( lclMutex::get() );
+    ::osl::MutexGuard aGuard( GetLocalMutex() );
 
     // generate a new client id
     TClientId nNewClientId = generateId( );
 
     // the event listeners for the new client
     ::comphelper::OInterfaceContainerHelper2 *const pNewListeners =
-        new ::comphelper::OInterfaceContainerHelper2( lclMutex::get() );
+        new ::comphelper::OInterfaceContainerHelper2( GetLocalMutex() );
         // note that we're using our own mutex here, so the listener containers for all
         // our clients share this same mutex.
         // this is a reminiscence to the days where the notifier was asynchronous. Today this is
         // completely nonsense, and potentially slowing down the Office me thinks...
 
     // add the client
-    Clients::get().emplace( nNewClientId, pNewListeners );
+    gaClients.emplace( nNewClientId, pNewListeners );
 
     // outta here
     return nNewClientId;
@@ -166,7 +171,7 @@ AccessibleEventNotifier::TClientId AccessibleEventNotifier::registerClient()
 
 void AccessibleEventNotifier::revokeClient( const TClientId _nClient )
 {
-    ::osl::MutexGuard aGuard( lclMutex::get() );
+    ::osl::MutexGuard aGuard( GetLocalMutex() );
 
     ClientMap::iterator aClientPos;
     if ( !implLookupClient( _nClient, aClientPos ) )
@@ -175,7 +180,7 @@ void AccessibleEventNotifier::revokeClient( const TClientId _nClient )
 
     // remove it from the clients map
     delete aClientPos->second;
-    Clients::get().erase( aClientPos );
+    gaClients.erase( aClientPos );
     releaseId(_nClient);
 }
 
@@ -186,7 +191,7 @@ void AccessibleEventNotifier::revokeClientNotifyDisposing(
 
     {
         // rhbz#1001768 drop the mutex before calling disposeAndClear
-        ::osl::MutexGuard aGuard( lclMutex::get() );
+        ::osl::MutexGuard aGuard( GetLocalMutex() );
 
         ClientMap::iterator aClientPos;
         if (!implLookupClient(_nClient, aClientPos))
@@ -200,7 +205,7 @@ void AccessibleEventNotifier::revokeClientNotifyDisposing(
         // (do this before actually notifying, because some client
         // implementations have re-entrance problems and call into
         // revokeClient while we are notifying from here)
-        Clients::get().erase(aClientPos);
+        gaClients.erase(aClientPos);
         releaseId(_nClient);
     }
 
@@ -215,7 +220,7 @@ void AccessibleEventNotifier::revokeClientNotifyDisposing(
 sal_Int32 AccessibleEventNotifier::addEventListener(
     const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener )
 {
-    ::osl::MutexGuard aGuard( lclMutex::get() );
+    ::osl::MutexGuard aGuard( GetLocalMutex() );
 
     ClientMap::iterator aClientPos;
     if ( !implLookupClient( _nClient, aClientPos ) )
@@ -231,7 +236,7 @@ sal_Int32 AccessibleEventNotifier::addEventListener(
 sal_Int32 AccessibleEventNotifier::removeEventListener(
     const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener )
 {
-    ::osl::MutexGuard aGuard( lclMutex::get() );
+    ::osl::MutexGuard aGuard( GetLocalMutex() );
 
     ClientMap::iterator aClientPos;
     if ( !implLookupClient( _nClient, aClientPos ) )
@@ -249,7 +254,7 @@ void AccessibleEventNotifier::addEvent( const TClientId _nClient, const Accessib
     std::vector< Reference< XInterface > > aListeners;
 
     {
-        ::osl::MutexGuard aGuard( lclMutex::get() );
+        ::osl::MutexGuard aGuard( GetLocalMutex() );
 
         ClientMap::iterator aClientPos;
         if ( !implLookupClient( _nClient, aClientPos ) )
diff --git a/comphelper/source/misc/asyncnotification.cxx b/comphelper/source/misc/asyncnotification.cxx
index 0c7f6391db8b..0fd0c338af53 100644
--- a/comphelper/source/misc/asyncnotification.cxx
+++ b/comphelper/source/misc/asyncnotification.cxx
@@ -20,7 +20,7 @@
 #include <comphelper/asyncnotification.hxx>
 #include <mutex>
 #include <condition_variable>
-#include <rtl/instance.hxx>
+#include <osl/mutex.hxx>
 
 #include <cassert>
 #include <stdexcept>
@@ -163,7 +163,11 @@ namespace comphelper
 
     namespace {
 
-    struct theNotifiersMutex : public rtl::Static<osl::Mutex, theNotifiersMutex> {};
+    osl::Mutex& GetTheNotifiersMutex()
+    {
+        static osl::Mutex MUTEX;
+        return MUTEX;
+    }
 
     }
 
@@ -173,7 +177,7 @@ namespace comphelper
     {
         std::vector<std::weak_ptr<AsyncEventNotifierAutoJoin>> notifiers;
         {
-            ::osl::MutexGuard g(theNotifiersMutex::get());
+            ::osl::MutexGuard g(GetTheNotifiersMutex());
             notifiers = g_Notifiers;
         }
         for (std::weak_ptr<AsyncEventNotifierAutoJoin> const& wNotifier : notifiers)
@@ -197,7 +201,7 @@ namespace comphelper
 
     AsyncEventNotifierAutoJoin::~AsyncEventNotifierAutoJoin()
     {
-        ::osl::MutexGuard g(theNotifiersMutex::get());
+        ::osl::MutexGuard g(GetTheNotifiersMutex());
         // note: this doesn't happen atomically with the refcount
         // hence it's possible this deletes > 1 or 0 elements
         g_Notifiers.erase(
@@ -213,7 +217,7 @@ namespace comphelper
     {
         std::shared_ptr<AsyncEventNotifierAutoJoin> const ret(
                 new AsyncEventNotifierAutoJoin(name));
-        ::osl::MutexGuard g(theNotifiersMutex::get());
+        ::osl::MutexGuard g(GetTheNotifiersMutex());
         g_Notifiers.push_back(ret);
         return ret;
     }
diff --git a/comphelper/source/misc/configuration.cxx b/comphelper/source/misc/configuration.cxx
index 873c34b93a91..a03855b41c4b 100644
--- a/comphelper/source/misc/configuration.cxx
+++ b/comphelper/source/misc/configuration.cxx
@@ -28,7 +28,6 @@
 #include <comphelper/solarmutex.hxx>
 #include <comphelper/configuration.hxx>
 #include <comphelper/configurationlistener.hxx>
-#include <rtl/instance.hxx>
 #include <rtl/ustring.hxx>
 #include <sal/log.hxx>
 #include <i18nlangtag/languagetag.hxx>
@@ -37,12 +36,11 @@ namespace com::sun::star::uno { class XComponentContext; }
 
 namespace {
 
-struct TheConfigurationWrapper:
-    public rtl::StaticWithArg<
-        comphelper::detail::ConfigurationWrapper,
-        css::uno::Reference< css::uno::XComponentContext >,
-        TheConfigurationWrapper >
-{};
+comphelper::detail::ConfigurationWrapper& GetTheConfigurationWrapper(const css::uno::Reference< css::uno::XComponentContext >& xContext)
+{
+    static comphelper::detail::ConfigurationWrapper WRAPPER(xContext);
+    return WRAPPER;
+}
 
 OUString getDefaultLocale(
     css::uno::Reference< css::uno::XComponentContext > const & context)
@@ -70,7 +68,7 @@ std::shared_ptr< comphelper::ConfigurationChanges >
 comphelper::ConfigurationChanges::create(
     css::uno::Reference< css::uno::XComponentContext > const & context)
 {
-    return TheConfigurationWrapper::get(context).createChanges();
+    return GetTheConfigurationWrapper(context).createChanges();
 }
 
 comphelper::ConfigurationChanges::~ConfigurationChanges() {}
@@ -110,7 +108,7 @@ comphelper::detail::ConfigurationWrapper const &
 comphelper::detail::ConfigurationWrapper::get(
     css::uno::Reference< css::uno::XComponentContext > const & context)
 {
-    return TheConfigurationWrapper::get(context);
+    return GetTheConfigurationWrapper(context);
 }
 
 comphelper::detail::ConfigurationWrapper::ConfigurationWrapper(
diff --git a/comphelper/source/misc/random.cxx b/comphelper/source/misc/random.cxx
index ea9e7273756b..c8c87888bcae 100644
--- a/comphelper/source/misc/random.cxx
+++ b/comphelper/source/misc/random.cxx
@@ -11,7 +11,6 @@
  */
 
 #include <comphelper/random.hxx>
-#include <rtl/instance.hxx>
 #include <sal/log.hxx>
 #include <assert.h>
 #include <time.h>
@@ -74,16 +73,18 @@ struct RandomNumberGenerator
     }
 };
 
-class theRandomNumberGenerator : public rtl::Static<RandomNumberGenerator, theRandomNumberGenerator>
+RandomNumberGenerator& GetTheRandomNumberGenerator()
 {
-};
+    static RandomNumberGenerator RANDOM;
+    return RANDOM;
+}
 }
 
 // uniform ints [a,b] distribution
 int uniform_int_distribution(int a, int b)
 {
     std::uniform_int_distribution<int> dist(a, b);
-    auto& gen = theRandomNumberGenerator::get();
+    auto& gen = GetTheRandomNumberGenerator();
     std::scoped_lock<std::mutex> g(gen.mutex);
     return dist(gen.global_rng);
 }
@@ -92,7 +93,7 @@ int uniform_int_distribution(int a, int b)
 unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
 {
     std::uniform_int_distribution<unsigned int> dist(a, b);
-    auto& gen = theRandomNumberGenerator::get();
+    auto& gen = GetTheRandomNumberGenerator();
     std::scoped_lock<std::mutex> g(gen.mutex);
     return dist(gen.global_rng);
 }
@@ -101,7 +102,7 @@ unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
 size_t uniform_size_distribution(size_t a, size_t b)
 {
     std::uniform_int_distribution<size_t> dist(a, b);
-    auto& gen = theRandomNumberGenerator::get();
+    auto& gen = GetTheRandomNumberGenerator();
     std::scoped_lock<std::mutex> g(gen.mutex);
     return dist(gen.global_rng);
 }
@@ -111,7 +112,7 @@ double uniform_real_distribution(double a, double b)
 {
     assert(a < b);
     std::uniform_real_distribution<double> dist(a, b);
-    auto& gen = theRandomNumberGenerator::get();
+    auto& gen = GetTheRandomNumberGenerator();
     std::scoped_lock<std::mutex> g(gen.mutex);
     return dist(gen.global_rng);
 }
diff --git a/comphelper/source/misc/threadpool.cxx b/comphelper/source/misc/threadpool.cxx
index a89c8e1610b6..48ff4af2d7c6 100644
--- a/comphelper/source/misc/threadpool.cxx
+++ b/comphelper/source/misc/threadpool.cxx
@@ -13,7 +13,6 @@
 #include <config_options.h>
 #include <sal/config.h>
 #include <sal/log.hxx>
-#include <rtl/instance.hxx>
 #include <salhelper/thread.hxx>
 #include <algorithm>
 #include <memory>
@@ -111,20 +110,22 @@ ThreadPool::~ThreadPool()
 
 namespace {
 
-struct ThreadPoolStatic : public rtl::StaticWithInit< std::shared_ptr< ThreadPool >,
-                                                      ThreadPoolStatic >
+std::shared_ptr< ThreadPool >& GetStaticThreadPool()
 {
-    std::shared_ptr< ThreadPool > operator () () {
+    static std::shared_ptr< ThreadPool > POOL =
+    []()
+    {
         const sal_Int32 nThreads = ThreadPool::getPreferredConcurrency();
         return std::make_shared< ThreadPool >( nThreads );
-    };
-};
+    }();
+    return POOL;
+}
 
 }
 
 ThreadPool& ThreadPool::getSharedOptimalPool()
 {
-    return *ThreadPoolStatic::get();
+    return *GetStaticThreadPool();
 }
 
 sal_Int32 ThreadPool::getPreferredConcurrency()


More information about the Libreoffice-commits mailing list