Kaydet (Commit) 9b7f17dd authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Fix races in AsyncEventNotifier::execute

* m_aDeadProcessors was useless; for one, removeEventsForProcessor could never
  have run (and set m_aDeadProcessors) between execute's reading from aEvents
  and checking m_aDeadProcessors (because of the use of aMutex in both
  functions), and if that were addressed, there would always be a race that
  execute would run a processor that has just been removed.  Clients have to be
  aware that a call to removeEventsForProcessor is just an optimization hint,
  but does not guarantee that the given processor is not called from the execute
  thread after removeEventsForProcessor returns.

* The sequence

    aGuard.clear(); aPendingActions.reset(); aPanedingActions.wait();

  could cause calls to aPendingActions.set() to get lost, both for signalling
  new content in the queue and for signalling termination.

Change-Id: I43293e3d5090c2d46db8bc8ed6fb9c71e049d55c
üst ddfe9eec
...@@ -23,8 +23,8 @@ ...@@ -23,8 +23,8 @@
#include <osl/conditn.hxx> #include <osl/conditn.hxx>
#include <comphelper/guarding.hxx> #include <comphelper/guarding.hxx>
#include <cassert>
#include <deque> #include <deque>
#include <set>
#include <functional> #include <functional>
#include <algorithm> #include <algorithm>
...@@ -72,23 +72,14 @@ namespace comphelper ...@@ -72,23 +72,14 @@ namespace comphelper
AnyEventRef aEvent; AnyEventRef aEvent;
::rtl::Reference< IEventProcessor > xProcessor; ::rtl::Reference< IEventProcessor > xProcessor;
ProcessableEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor ) ProcessableEvent()
:aEvent( _rEvent )
,xProcessor( _xProcessor )
{ {
} }
ProcessableEvent( const ProcessableEvent& _rRHS ) ProcessableEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
:aEvent( _rRHS.aEvent ) :aEvent( _rEvent )
,xProcessor( _rRHS.xProcessor ) ,xProcessor( _xProcessor )
{
}
ProcessableEvent& operator=( const ProcessableEvent& _rRHS )
{ {
aEvent = _rRHS.aEvent;
xProcessor = _rRHS.xProcessor;
return *this;
} }
}; };
...@@ -113,20 +104,14 @@ namespace comphelper ...@@ -113,20 +104,14 @@ namespace comphelper
struct EventNotifierImpl struct EventNotifierImpl
{ {
::osl::Mutex aMutex; ::osl::Mutex aMutex;
oslInterlockedCount m_refCount;
::osl::Condition aPendingActions; ::osl::Condition aPendingActions;
EventQueue aEvents; EventQueue aEvents;
::std::set< ::rtl::Reference< IEventProcessor > > bool bTerminate;
m_aDeadProcessors;
EventNotifierImpl() EventNotifierImpl()
:m_refCount( 0 ) :bTerminate( false )
{ {
} }
private:
EventNotifierImpl( const EventNotifierImpl& ); // never implemented
EventNotifierImpl& operator=( const EventNotifierImpl& ); // never implemented
}; };
...@@ -150,10 +135,6 @@ namespace comphelper ...@@ -150,10 +135,6 @@ namespace comphelper
// remove all events for this processor // remove all events for this processor
::std::remove_if( m_pImpl->aEvents.begin(), m_pImpl->aEvents.end(), EqualProcessor( _xProcessor ) ); ::std::remove_if( m_pImpl->aEvents.begin(), m_pImpl->aEvents.end(), EqualProcessor( _xProcessor ) );
// and just in case that an event for exactly this processor has just been
// popped from the queue, but not yet processed: remember it:
m_pImpl->m_aDeadProcessors.insert( _xProcessor );
} }
...@@ -162,7 +143,7 @@ namespace comphelper ...@@ -162,7 +143,7 @@ namespace comphelper
::osl::MutexGuard aGuard( m_pImpl->aMutex ); ::osl::MutexGuard aGuard( m_pImpl->aMutex );
// remember the termination request // remember the termination request
Thread::terminate(); m_pImpl->bTerminate = true;
// awake the thread // awake the thread
m_pImpl->aPendingActions.set(); m_pImpl->aPendingActions.set();
...@@ -184,57 +165,36 @@ namespace comphelper ...@@ -184,57 +165,36 @@ namespace comphelper
void AsyncEventNotifier::execute() void AsyncEventNotifier::execute()
{ {
do for (;;)
{ {
AnyEventRef aNextEvent; m_pImpl->aPendingActions.wait();
::rtl::Reference< IEventProcessor > xNextProcessor; ProcessableEvent aEvent;
::osl::ClearableMutexGuard aGuard( m_pImpl->aMutex );
while ( m_pImpl->aEvents.size() > 0 )
{ {
ProcessableEvent aEvent( m_pImpl->aEvents.front() ); osl::MutexGuard aGuard(m_pImpl->aMutex);
aNextEvent = aEvent.aEvent; if (m_pImpl->bTerminate)
xNextProcessor = aEvent.xProcessor;
m_pImpl->aEvents.pop_front();
OSL_TRACE( "AsyncEventNotifier(%p): popping %p", this, aNextEvent.get() );
if ( !aNextEvent.get() )
continue;
// process the event, but only if it's processor did not die inbetween
::std::set< ::rtl::Reference< IEventProcessor > >::iterator deadPos = m_pImpl->m_aDeadProcessors.find( xNextProcessor );
if ( deadPos != m_pImpl->m_aDeadProcessors.end() )
{ {
m_pImpl->m_aDeadProcessors.erase( xNextProcessor ); break;
xNextProcessor.clear();
OSL_TRACE( "AsyncEventNotifier(%p): removing %p", this, aNextEvent.get() );
} }
if (!m_pImpl->aEvents.empty())
// if there was a termination request (->terminate), respect it {
if ( !schedule() ) aEvent = m_pImpl->aEvents.front();
return; m_pImpl->aEvents.pop_front();
OSL_TRACE(
"AsyncEventNotifier(%p): popping %p", this,
aEvent.aEvent.get());
}
if (m_pImpl->aEvents.empty())
{ {
::comphelper::MutexRelease aReleaseOnce( m_pImpl->aMutex ); m_pImpl->aPendingActions.reset();
if ( xNextProcessor.get() )
xNextProcessor->processEvent( *aNextEvent.get() );
} }
} }
if (aEvent.aEvent.is()) {
// if there was a termination request (->terminate), respect it assert(aEvent.xProcessor.is());
if ( !schedule() ) aEvent.xProcessor->processEvent(*aEvent.aEvent);
return; }
// wait for new events to process
aGuard.clear();
m_pImpl->aPendingActions.reset();
m_pImpl->aPendingActions.wait();
} }
while ( true );
} }
} // namespace comphelper } // namespace comphelper
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment