xref: /AOO41X/main/comphelper/source/misc/asyncnotification.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
30*cdf0e10cSrcweir #include <comphelper/asyncnotification.hxx>
31*cdf0e10cSrcweir #include <osl/diagnose.h>
32*cdf0e10cSrcweir #include <osl/mutex.hxx>
33*cdf0e10cSrcweir #include <osl/conditn.hxx>
34*cdf0e10cSrcweir #include <comphelper/guarding.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <deque>
37*cdf0e10cSrcweir #include <set>
38*cdf0e10cSrcweir #include <functional>
39*cdf0e10cSrcweir #include <algorithm>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir //........................................................................
42*cdf0e10cSrcweir namespace comphelper
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir //........................................................................
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     //====================================================================
47*cdf0e10cSrcweir     //= AnyEvent
48*cdf0e10cSrcweir     //====================================================================
49*cdf0e10cSrcweir     //--------------------------------------------------------------------
50*cdf0e10cSrcweir     AnyEvent::AnyEvent()
51*cdf0e10cSrcweir         :m_refCount( 0 )
52*cdf0e10cSrcweir     {
53*cdf0e10cSrcweir     }
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir     //--------------------------------------------------------------------
56*cdf0e10cSrcweir     AnyEvent::~AnyEvent()
57*cdf0e10cSrcweir     {
58*cdf0e10cSrcweir     }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     //--------------------------------------------------------------------
61*cdf0e10cSrcweir     oslInterlockedCount SAL_CALL AnyEvent::acquire()
62*cdf0e10cSrcweir     {
63*cdf0e10cSrcweir         return osl_incrementInterlockedCount( &m_refCount );
64*cdf0e10cSrcweir     }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     //--------------------------------------------------------------------
67*cdf0e10cSrcweir     oslInterlockedCount SAL_CALL AnyEvent::release()
68*cdf0e10cSrcweir     {
69*cdf0e10cSrcweir         if ( 0 == osl_decrementInterlockedCount( &m_refCount ) )
70*cdf0e10cSrcweir         {
71*cdf0e10cSrcweir             delete this;
72*cdf0e10cSrcweir             return 0;
73*cdf0e10cSrcweir         }
74*cdf0e10cSrcweir         return m_refCount;
75*cdf0e10cSrcweir     }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     //====================================================================
78*cdf0e10cSrcweir     //= ProcessableEvent
79*cdf0e10cSrcweir     //====================================================================
80*cdf0e10cSrcweir     struct ProcessableEvent
81*cdf0e10cSrcweir     {
82*cdf0e10cSrcweir         AnyEventRef                         aEvent;
83*cdf0e10cSrcweir         ::rtl::Reference< IEventProcessor > xProcessor;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir         ProcessableEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
86*cdf0e10cSrcweir             :aEvent( _rEvent )
87*cdf0e10cSrcweir             ,xProcessor( _xProcessor )
88*cdf0e10cSrcweir         {
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         ProcessableEvent( const ProcessableEvent& _rRHS )
92*cdf0e10cSrcweir             :aEvent( _rRHS.aEvent )
93*cdf0e10cSrcweir             ,xProcessor( _rRHS.xProcessor )
94*cdf0e10cSrcweir         {
95*cdf0e10cSrcweir         }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir         ProcessableEvent& operator=( const ProcessableEvent& _rRHS )
98*cdf0e10cSrcweir         {
99*cdf0e10cSrcweir             aEvent = _rRHS.aEvent;
100*cdf0e10cSrcweir             xProcessor = _rRHS.xProcessor;
101*cdf0e10cSrcweir             return *this;
102*cdf0e10cSrcweir         }
103*cdf0e10cSrcweir     };
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     //====================================================================
106*cdf0e10cSrcweir     typedef ::std::deque< ProcessableEvent >    EventQueue;
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     //====================================================================
109*cdf0e10cSrcweir     struct EqualProcessor : public ::std::unary_function< ProcessableEvent, bool >
110*cdf0e10cSrcweir     {
111*cdf0e10cSrcweir         const ::rtl::Reference< IEventProcessor >&  rProcessor;
112*cdf0e10cSrcweir         EqualProcessor( const ::rtl::Reference< IEventProcessor >& _rProcessor ) :rProcessor( _rProcessor ) { }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         bool operator()( const ProcessableEvent& _rEvent )
115*cdf0e10cSrcweir         {
116*cdf0e10cSrcweir             return _rEvent.xProcessor.get() == rProcessor.get();
117*cdf0e10cSrcweir         }
118*cdf0e10cSrcweir     };
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     //====================================================================
121*cdf0e10cSrcweir     //= EventNotifierImpl
122*cdf0e10cSrcweir     //====================================================================
123*cdf0e10cSrcweir     struct EventNotifierImpl
124*cdf0e10cSrcweir     {
125*cdf0e10cSrcweir         ::osl::Mutex        aMutex;
126*cdf0e10cSrcweir         oslInterlockedCount m_refCount;
127*cdf0e10cSrcweir         ::osl::Condition    aPendingActions;
128*cdf0e10cSrcweir         EventQueue          aEvents;
129*cdf0e10cSrcweir         ::std::set< ::rtl::Reference< IEventProcessor > >
130*cdf0e10cSrcweir                             m_aDeadProcessors;
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         EventNotifierImpl()
133*cdf0e10cSrcweir             :m_refCount( 0 )
134*cdf0e10cSrcweir         {
135*cdf0e10cSrcweir         }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     private:
138*cdf0e10cSrcweir         EventNotifierImpl( const EventNotifierImpl& );              // never implemented
139*cdf0e10cSrcweir         EventNotifierImpl& operator=( const EventNotifierImpl& );   // never implemented
140*cdf0e10cSrcweir     };
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     //====================================================================
143*cdf0e10cSrcweir     //= AsyncEventNotifier
144*cdf0e10cSrcweir     //====================================================================
145*cdf0e10cSrcweir     //--------------------------------------------------------------------
146*cdf0e10cSrcweir     AsyncEventNotifier::AsyncEventNotifier()
147*cdf0e10cSrcweir         :m_pImpl( new EventNotifierImpl )
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     //--------------------------------------------------------------------
152*cdf0e10cSrcweir     AsyncEventNotifier::~AsyncEventNotifier()
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir     }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     //--------------------------------------------------------------------
157*cdf0e10cSrcweir     void AsyncEventNotifier::removeEventsForProcessor( const ::rtl::Reference< IEventProcessor >& _xProcessor )
158*cdf0e10cSrcweir     {
159*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_pImpl->aMutex );
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir         // remove all events for this processor
162*cdf0e10cSrcweir         ::std::remove_if( m_pImpl->aEvents.begin(), m_pImpl->aEvents.end(), EqualProcessor( _xProcessor ) );
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir         // and just in case that an event for exactly this processor has just been
165*cdf0e10cSrcweir         // popped from the queue, but not yet processed: remember it:
166*cdf0e10cSrcweir         m_pImpl->m_aDeadProcessors.insert( _xProcessor );
167*cdf0e10cSrcweir     }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     //--------------------------------------------------------------------
170*cdf0e10cSrcweir     void SAL_CALL AsyncEventNotifier::terminate()
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_pImpl->aMutex );
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         // remember the termination request
175*cdf0e10cSrcweir         AsyncEventNotifier_TBASE::terminate();
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir         // awake the thread
178*cdf0e10cSrcweir         m_pImpl->aPendingActions.set();
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     //--------------------------------------------------------------------
182*cdf0e10cSrcweir     void AsyncEventNotifier::addEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
183*cdf0e10cSrcweir     {
184*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_pImpl->aMutex );
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         OSL_TRACE( "AsyncEventNotifier(%p): adding %p\n", this, _rEvent.get() );
187*cdf0e10cSrcweir         // remember this event
188*cdf0e10cSrcweir         m_pImpl->aEvents.push_back( ProcessableEvent( _rEvent, _xProcessor ) );
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir         // awake the thread
191*cdf0e10cSrcweir         m_pImpl->aPendingActions.set();
192*cdf0e10cSrcweir     }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     //--------------------------------------------------------------------
195*cdf0e10cSrcweir     void AsyncEventNotifier::run()
196*cdf0e10cSrcweir     {
197*cdf0e10cSrcweir         acquire();
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir         // keep us alive, in case we're terminated in the mid of the following
200*cdf0e10cSrcweir         ::rtl::Reference< AsyncEventNotifier > xKeepAlive( this );
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir         do
203*cdf0e10cSrcweir         {
204*cdf0e10cSrcweir             AnyEventRef aNextEvent;
205*cdf0e10cSrcweir             ::rtl::Reference< IEventProcessor > xNextProcessor;
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir             ::osl::ClearableMutexGuard aGuard( m_pImpl->aMutex );
208*cdf0e10cSrcweir             while ( m_pImpl->aEvents.size() > 0 )
209*cdf0e10cSrcweir             {
210*cdf0e10cSrcweir                 ProcessableEvent aEvent( m_pImpl->aEvents.front() );
211*cdf0e10cSrcweir                 aNextEvent = aEvent.aEvent;
212*cdf0e10cSrcweir                 xNextProcessor = aEvent.xProcessor;
213*cdf0e10cSrcweir                 m_pImpl->aEvents.pop_front();
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir                 OSL_TRACE( "AsyncEventNotifier(%p): popping %p\n", this, aNextEvent.get() );
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir                 if ( !aNextEvent.get() )
218*cdf0e10cSrcweir                     continue;
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir                 // process the event, but only if it's processor did not die inbetween
221*cdf0e10cSrcweir                 ::std::set< ::rtl::Reference< IEventProcessor > >::iterator deadPos = m_pImpl->m_aDeadProcessors.find( xNextProcessor );
222*cdf0e10cSrcweir                 if ( deadPos != m_pImpl->m_aDeadProcessors.end() )
223*cdf0e10cSrcweir                 {
224*cdf0e10cSrcweir                     m_pImpl->m_aDeadProcessors.erase( xNextProcessor );
225*cdf0e10cSrcweir                     xNextProcessor.clear();
226*cdf0e10cSrcweir                     OSL_TRACE( "AsyncEventNotifier(%p): removing %p\n", this, aNextEvent.get() );
227*cdf0e10cSrcweir                 }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir                 // if there was a termination request (->terminate), respect it
230*cdf0e10cSrcweir                 if ( !schedule() )
231*cdf0e10cSrcweir                     return;
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir                 {
234*cdf0e10cSrcweir                     ::comphelper::MutexRelease aReleaseOnce( m_pImpl->aMutex );
235*cdf0e10cSrcweir                     if ( xNextProcessor.get() )
236*cdf0e10cSrcweir                         xNextProcessor->processEvent( *aNextEvent.get() );
237*cdf0e10cSrcweir                 }
238*cdf0e10cSrcweir             }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir             // if there was a termination request (->terminate), respect it
241*cdf0e10cSrcweir             if ( !schedule() )
242*cdf0e10cSrcweir                 return;
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir             // wait for new events to process
245*cdf0e10cSrcweir             aGuard.clear();
246*cdf0e10cSrcweir             m_pImpl->aPendingActions.reset();
247*cdf0e10cSrcweir             m_pImpl->aPendingActions.wait();
248*cdf0e10cSrcweir         }
249*cdf0e10cSrcweir         while ( sal_True );
250*cdf0e10cSrcweir     }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir     //--------------------------------------------------------------------
253*cdf0e10cSrcweir     void SAL_CALL AsyncEventNotifier::onTerminated()
254*cdf0e10cSrcweir     {
255*cdf0e10cSrcweir         AsyncEventNotifier_TBASE::onTerminated();
256*cdf0e10cSrcweir         // when we were started (->run), we aquired ourself. Release this now
257*cdf0e10cSrcweir         // that we were finally terminated
258*cdf0e10cSrcweir         release();
259*cdf0e10cSrcweir     }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir     //--------------------------------------------------------------------
262*cdf0e10cSrcweir 	oslInterlockedCount SAL_CALL AsyncEventNotifier::acquire()
263*cdf0e10cSrcweir     {
264*cdf0e10cSrcweir         return osl_incrementInterlockedCount( &m_pImpl->m_refCount );
265*cdf0e10cSrcweir     }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir     //--------------------------------------------------------------------
268*cdf0e10cSrcweir 	oslInterlockedCount SAL_CALL AsyncEventNotifier::release()
269*cdf0e10cSrcweir     {
270*cdf0e10cSrcweir         if ( 0 == osl_decrementInterlockedCount( &m_pImpl->m_refCount ) )
271*cdf0e10cSrcweir         {
272*cdf0e10cSrcweir             delete this;
273*cdf0e10cSrcweir             return 0;
274*cdf0e10cSrcweir         }
275*cdf0e10cSrcweir         return m_pImpl->m_refCount;
276*cdf0e10cSrcweir     }
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir //........................................................................
279*cdf0e10cSrcweir } // namespace comphelper
280*cdf0e10cSrcweir //........................................................................
281*cdf0e10cSrcweir 
282