diff options
author | Noel Grandin <noelgrandin@gmail.com> | 2021-07-21 21:25:31 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2021-07-23 09:59:13 +0200 |
commit | ff61ec1ef2e408bb758bbce91d0d1840ef2b2883 (patch) | |
tree | 87e589d14b5bf755aadae19aa70e061a4938ea20 /comphelper | |
parent | 19d01fae16f4f7b46c168b983960547f4dd2c2aa (diff) |
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@collabora.co.uk>
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/source/misc/asyncnotification.cxx | 69 |
1 files changed, 23 insertions, 46 deletions
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(); } } |