update credits
[LibreOffice.git] / comphelper / source / misc / asyncnotification.cxx
blob8b1d81a804d1b45f6456f53bc9bdbee3159f0f17
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
26 #include <deque>
27 #include <set>
28 #include <functional>
29 #include <algorithm>
31 //........................................................................
32 namespace comphelper
34 //........................................................................
36 //====================================================================
37 //= AnyEvent
38 //====================================================================
39 //--------------------------------------------------------------------
40 AnyEvent::AnyEvent()
41 :m_refCount( 0 )
45 //--------------------------------------------------------------------
46 AnyEvent::~AnyEvent()
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 ) )
61 delete this;
62 return 0;
64 return m_refCount;
67 //====================================================================
68 //= ProcessableEvent
69 //====================================================================
70 struct ProcessableEvent
72 AnyEventRef aEvent;
73 ::rtl::Reference< IEventProcessor > xProcessor;
75 ProcessableEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
76 :aEvent( _rEvent )
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;
91 return *this;
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
115 ::osl::Mutex aMutex;
116 oslInterlockedCount m_refCount;
117 ::osl::Condition aPendingActions;
118 EventQueue aEvents;
119 ::std::set< ::rtl::Reference< IEventProcessor > >
120 m_aDeadProcessors;
122 EventNotifierImpl()
123 :m_refCount( 0 )
127 private:
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
165 Thread::terminate();
167 // awake the thread
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 ) );
180 // awake the thread
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() )
203 continue;
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
215 if ( !schedule() )
216 return;
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
226 if ( !schedule() )
227 return;
229 // wait for new events to process
230 aGuard.clear();
231 m_pImpl->aPendingActions.reset();
232 m_pImpl->aPendingActions.wait();
234 while ( true );
237 //........................................................................
238 } // namespace comphelper
239 //........................................................................
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */