1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <comphelper/asyncnotification.hxx>
21 #include <osl/diagnose.h>
22 #include <osl/mutex.hxx>
23 #include <osl/conditn.hxx>
24 #include <comphelper/guarding.hxx>
31 //........................................................................
34 //........................................................................
36 //====================================================================
38 //====================================================================
39 //--------------------------------------------------------------------
45 //--------------------------------------------------------------------
50 //--------------------------------------------------------------------
51 oslInterlockedCount SAL_CALL
AnyEvent::acquire()
53 return osl_atomic_increment( &m_refCount
);
56 //--------------------------------------------------------------------
57 oslInterlockedCount SAL_CALL
AnyEvent::release()
59 if ( 0 == osl_atomic_decrement( &m_refCount
) )
67 //====================================================================
69 //====================================================================
70 struct ProcessableEvent
73 ::rtl::Reference
< IEventProcessor
> xProcessor
;
75 ProcessableEvent( const AnyEventRef
& _rEvent
, const ::rtl::Reference
< IEventProcessor
>& _xProcessor
)
77 ,xProcessor( _xProcessor
)
81 ProcessableEvent( const ProcessableEvent
& _rRHS
)
82 :aEvent( _rRHS
.aEvent
)
83 ,xProcessor( _rRHS
.xProcessor
)
87 ProcessableEvent
& operator=( const ProcessableEvent
& _rRHS
)
89 aEvent
= _rRHS
.aEvent
;
90 xProcessor
= _rRHS
.xProcessor
;
95 //====================================================================
96 typedef ::std::deque
< ProcessableEvent
> EventQueue
;
98 //====================================================================
99 struct EqualProcessor
: public ::std::unary_function
< ProcessableEvent
, bool >
101 const ::rtl::Reference
< IEventProcessor
>& rProcessor
;
102 EqualProcessor( const ::rtl::Reference
< IEventProcessor
>& _rProcessor
) :rProcessor( _rProcessor
) { }
104 bool operator()( const ProcessableEvent
& _rEvent
)
106 return _rEvent
.xProcessor
.get() == rProcessor
.get();
110 //====================================================================
111 //= EventNotifierImpl
112 //====================================================================
113 struct EventNotifierImpl
116 oslInterlockedCount m_refCount
;
117 ::osl::Condition aPendingActions
;
119 ::std::set
< ::rtl::Reference
< IEventProcessor
> >
128 EventNotifierImpl( const EventNotifierImpl
& ); // never implemented
129 EventNotifierImpl
& operator=( const EventNotifierImpl
& ); // never implemented
132 //====================================================================
133 //= AsyncEventNotifier
134 //====================================================================
135 //--------------------------------------------------------------------
136 AsyncEventNotifier::AsyncEventNotifier(char const * name
):
137 Thread(name
), m_pImpl(new EventNotifierImpl
)
141 //--------------------------------------------------------------------
142 AsyncEventNotifier::~AsyncEventNotifier()
146 //--------------------------------------------------------------------
147 void AsyncEventNotifier::removeEventsForProcessor( const ::rtl::Reference
< IEventProcessor
>& _xProcessor
)
149 ::osl::MutexGuard
aGuard( m_pImpl
->aMutex
);
151 // remove all events for this processor
152 ::std::remove_if( m_pImpl
->aEvents
.begin(), m_pImpl
->aEvents
.end(), EqualProcessor( _xProcessor
) );
154 // and just in case that an event for exactly this processor has just been
155 // popped from the queue, but not yet processed: remember it:
156 m_pImpl
->m_aDeadProcessors
.insert( _xProcessor
);
159 //--------------------------------------------------------------------
160 void SAL_CALL
AsyncEventNotifier::terminate()
162 ::osl::MutexGuard
aGuard( m_pImpl
->aMutex
);
164 // remember the termination request
168 m_pImpl
->aPendingActions
.set();
171 //--------------------------------------------------------------------
172 void AsyncEventNotifier::addEvent( const AnyEventRef
& _rEvent
, const ::rtl::Reference
< IEventProcessor
>& _xProcessor
)
174 ::osl::MutexGuard
aGuard( m_pImpl
->aMutex
);
176 OSL_TRACE( "AsyncEventNotifier(%p): adding %p", this, _rEvent
.get() );
177 // remember this event
178 m_pImpl
->aEvents
.push_back( ProcessableEvent( _rEvent
, _xProcessor
) );
181 m_pImpl
->aPendingActions
.set();
184 //--------------------------------------------------------------------
185 void AsyncEventNotifier::execute()
189 AnyEventRef aNextEvent
;
190 ::rtl::Reference
< IEventProcessor
> xNextProcessor
;
192 ::osl::ClearableMutexGuard
aGuard( m_pImpl
->aMutex
);
193 while ( m_pImpl
->aEvents
.size() > 0 )
195 ProcessableEvent
aEvent( m_pImpl
->aEvents
.front() );
196 aNextEvent
= aEvent
.aEvent
;
197 xNextProcessor
= aEvent
.xProcessor
;
198 m_pImpl
->aEvents
.pop_front();
200 OSL_TRACE( "AsyncEventNotifier(%p): popping %p", this, aNextEvent
.get() );
202 if ( !aNextEvent
.get() )
205 // process the event, but only if it's processor did not die inbetween
206 ::std::set
< ::rtl::Reference
< IEventProcessor
> >::iterator deadPos
= m_pImpl
->m_aDeadProcessors
.find( xNextProcessor
);
207 if ( deadPos
!= m_pImpl
->m_aDeadProcessors
.end() )
209 m_pImpl
->m_aDeadProcessors
.erase( xNextProcessor
);
210 xNextProcessor
.clear();
211 OSL_TRACE( "AsyncEventNotifier(%p): removing %p", this, aNextEvent
.get() );
214 // if there was a termination request (->terminate), respect it
219 ::comphelper::MutexRelease
aReleaseOnce( m_pImpl
->aMutex
);
220 if ( xNextProcessor
.get() )
221 xNextProcessor
->processEvent( *aNextEvent
.get() );
225 // if there was a termination request (->terminate), respect it
229 // wait for new events to process
231 m_pImpl
->aPendingActions
.reset();
232 m_pImpl
->aPendingActions
.wait();
237 //........................................................................
238 } // namespace comphelper
239 //........................................................................
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */