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

Noel Grandin (via logerrit) logerrit at kemper.freedesktop.org
Fri Jul 23 07:59:47 UTC 2021


 comphelper/source/misc/asyncnotification.cxx |   69 +++++++++------------------
 1 file changed, 23 insertions(+), 46 deletions(-)

New commits:
commit ff61ec1ef2e408bb758bbce91d0d1840ef2b2883
Author:     Noel Grandin <noelgrandin at gmail.com>
AuthorDate: Wed Jul 21 21:25:31 2021 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Fri Jul 23 09:59:13 2021 +0200

    osl::Mutex->std::mutex in AsyncEventNotifier
    
    and use a vector instead of a deque, becuase then a std::swap makes a nice
    way of ping-ponging a data buffer back and forth between the
    producer and consumer sides
    
    Change-Id: I11f491b9fde87f0a495df05d306ccee57dcfc6da
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119345
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/comphelper/source/misc/asyncnotification.cxx b/comphelper/source/misc/asyncnotification.cxx
index a360b7207fe4..1a33c5a0b331 100644
--- a/comphelper/source/misc/asyncnotification.cxx
+++ b/comphelper/source/misc/asyncnotification.cxx
@@ -18,12 +18,11 @@
  */
 
 #include <comphelper/asyncnotification.hxx>
-#include <osl/mutex.hxx>
-#include <osl/conditn.hxx>
+#include <mutex>
+#include <condition_variable>
 #include <rtl/instance.hxx>
 
 #include <cassert>
-#include <deque>
 #include <stdexcept>
 #include <vector>
 #include <algorithm>
@@ -44,24 +43,8 @@ namespace comphelper
     {
         AnyEventRef                         aEvent;
         ::rtl::Reference< IEventProcessor > xProcessor;
-
-        ProcessableEvent()
-        {
-        }
-
-        ProcessableEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
-            :aEvent( _rEvent )
-            ,xProcessor( _xProcessor )
-        {
-        }
     };
 
-    }
-
-    typedef std::deque< ProcessableEvent >    EventQueue;
-
-    namespace {
-
     struct EqualProcessor
     {
         const ::rtl::Reference< IEventProcessor >&  rProcessor;
@@ -77,9 +60,9 @@ namespace comphelper
 
     struct EventNotifierImpl
     {
-        ::osl::Mutex        aMutex;
-        ::osl::Condition    aPendingActions;
-        EventQueue          aEvents;
+        std::mutex        aMutex;
+        std::condition_variable aPendingActions;
+        std::vector< ProcessableEvent > aEvents;
         bool                bTerminate;
         // only used for AsyncEventNotifierAutoJoin
         char const*         name;
@@ -105,7 +88,7 @@ namespace comphelper
 
     void AsyncEventNotifierBase::removeEventsForProcessor( const ::rtl::Reference< IEventProcessor >& _xProcessor )
     {
-        ::osl::MutexGuard aGuard( m_xImpl->aMutex );
+        std::lock_guard aGuard( m_xImpl->aMutex );
 
         // remove all events for this processor
         m_xImpl->aEvents.erase(std::remove_if( m_xImpl->aEvents.begin(), m_xImpl->aEvents.end(), EqualProcessor( _xProcessor ) ), m_xImpl->aEvents.end());
@@ -114,25 +97,25 @@ namespace comphelper
 
     void SAL_CALL AsyncEventNotifierBase::terminate()
     {
-        ::osl::MutexGuard aGuard( m_xImpl->aMutex );
+        std::lock_guard aGuard( m_xImpl->aMutex );
 
         // remember the termination request
         m_xImpl->bTerminate = true;
 
         // awake the thread
-        m_xImpl->aPendingActions.set();
+        m_xImpl->aPendingActions.notify_all();
     }
 
 
     void AsyncEventNotifierBase::addEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
     {
-        ::osl::MutexGuard aGuard( m_xImpl->aMutex );
+        std::lock_guard aGuard( m_xImpl->aMutex );
 
         // remember this event
-        m_xImpl->aEvents.emplace_back( _rEvent, _xProcessor );
+        m_xImpl->aEvents.emplace_back( ProcessableEvent {_rEvent, _xProcessor} );
 
         // awake the thread
-        m_xImpl->aPendingActions.set();
+        m_xImpl->aPendingActions.notify_all();
     }
 
 
@@ -140,28 +123,22 @@ namespace comphelper
     {
         for (;;)
         {
-            m_xImpl->aPendingActions.wait();
-            ProcessableEvent aEvent;
+            std::vector< ProcessableEvent > aEvents;
             {
-                osl::MutexGuard aGuard(m_xImpl->aMutex);
+                std::unique_lock aGuard(m_xImpl->aMutex);
+                m_xImpl->aPendingActions.wait(aGuard,
+                    [this] { return m_xImpl->bTerminate || !m_xImpl->aEvents.empty(); } );
                 if (m_xImpl->bTerminate)
-                {
-                    break;
-                }
-                if (!m_xImpl->aEvents.empty())
-                {
-                    aEvent = m_xImpl->aEvents.front();
-                    m_xImpl->aEvents.pop_front();
-                }
-                if (m_xImpl->aEvents.empty())
-                {
-                    m_xImpl->aPendingActions.reset();
-                }
+                    return;
+                else
+                    std::swap(aEvents, m_xImpl->aEvents);
             }
-            if (aEvent.aEvent.is()) {
-                assert(aEvent.xProcessor.is());
-                aEvent.xProcessor->processEvent(*aEvent.aEvent);
+            for (ProcessableEvent& rEvent : aEvents)
+            {
+                assert(rEvent.xProcessor.is());
+                rEvent.xProcessor->processEvent(*rEvent.aEvent);
             }
+            aEvents.clear();
         }
     }
 


More information about the Libreoffice-commits mailing list