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 "LifeTime.hxx"
22 #include <osl/diagnose.h>
24 #include <com/sun/star/util/XModifyListener.hpp>
25 #include <com/sun/star/util/XCloseListener.hpp>
27 using namespace ::com::sun::star
;
32 LifeTimeManager::LifeTimeManager( lang::XComponent
* pComponent
, bool bLongLastingCallsCancelable
)
33 : m_aListenerContainer( m_aAccessMutex
)
34 , m_pComponent(pComponent
)
35 , m_bLongLastingCallsCancelable(bLongLastingCallsCancelable
)
40 void LifeTimeManager::impl_init()
45 m_nLongLastingCallCount
= 0;
46 m_aNoAccessCountCondition
.set();
47 m_aNoLongLastingCallCountCondition
.set();
50 LifeTimeManager::~LifeTimeManager()
54 bool LifeTimeManager::impl_isDisposed( bool bAssert
)
56 if( m_bDisposed
|| m_bInDispose
)
60 OSL_FAIL( "This component is already disposed " );
68 ::impl_canStartApiCall()
70 if( impl_isDisposed() )
71 return false; //behave passive if already disposed
78 ::impl_registerApiCall(bool bLongLastingCall
)
80 //only allowed if not disposed
81 //do not acquire the mutex here because it will be acquired already
84 //@todo? is it ok to wake some threads here while we have acquired the mutex?
85 m_aNoAccessCountCondition
.reset();
88 m_nLongLastingCallCount
++;
89 if(m_nLongLastingCallCount
==1)
90 m_aNoLongLastingCallCountCondition
.reset();
93 ::impl_unregisterApiCall(bool bLongLastingCall
)
95 //Mutex needs to be acquired exactly ones
96 //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
98 OSL_ENSURE( m_nAccessCount
>0, "access count mismatch" );
101 m_nLongLastingCallCount
--;
102 if( m_nLongLastingCallCount
==0 )
104 m_aNoLongLastingCallCountCondition
.set();
106 if( m_nAccessCount
== 0)
108 m_aNoAccessCountCondition
.set();
109 impl_apiCallCountReachedNull();
115 ::dispose() throw(uno::RuntimeException
)
119 osl::Guard
< osl::Mutex
> aGuard( m_aAccessMutex
);
121 if( m_bDisposed
|| m_bInDispose
)
123 OSL_TRACE( "This component is already disposed " );
124 return false; //behave passive if already disposed
128 //adding any listener is not allowed anymore
129 //new calls will not be accepted
130 //still running calls have the freedom to finish their work without crash
132 //no mutex is acquired
134 //--do the disposing of listeners after calling this method
136 uno::Reference
< lang::XComponent
> xComponent
=
137 uno::Reference
< lang::XComponent
>(m_pComponent
);;
140 // notify XCLoseListeners
141 lang::EventObject
aEvent( xComponent
);
142 m_aListenerContainer
.disposeAndClear( aEvent
);
146 //no mutex is acquired
148 osl::ClearableGuard
< osl::Mutex
> aGuard( m_aAccessMutex
);
149 OSL_ENSURE( !m_bDisposed
, "dispose was called already" );
153 //no mutex is acquired
155 //wait until all still running calls have finished
156 //the accessCount cannot grow anymore, because all calls will return after checking m_bDisposed
157 m_aNoAccessCountCondition
.wait();
159 //we are the only ones working on our data now
162 //--release all resources and references after calling this method successful
165 CloseableLifeTimeManager::CloseableLifeTimeManager( ::com::sun::star::util::XCloseable
* pCloseable
166 , ::com::sun::star::lang::XComponent
* pComponent
167 , bool bLongLastingCallsCancelable
)
168 : LifeTimeManager( pComponent
, bLongLastingCallsCancelable
)
169 , m_pCloseable(pCloseable
)
174 CloseableLifeTimeManager::~CloseableLifeTimeManager()
178 bool CloseableLifeTimeManager::impl_isDisposedOrClosed( bool bAssert
)
180 if( impl_isDisposed( bAssert
) )
187 OSL_FAIL( "This object is already closed" );
188 (void)(bAssert
);//avoid warnings
195 bool CloseableLifeTimeManager
196 ::g_close_startTryClose(bool bDeliverOwnership
)
197 throw ( uno::Exception
)
199 //no mutex is allowed to be acquired
201 osl::ResettableGuard
< osl::Mutex
> aGuard( m_aAccessMutex
);
202 if( impl_isDisposedOrClosed(false) )
205 //Mutex needs to be acquired exactly ones; will be released inbetween
206 if( !impl_canStartApiCall() )
210 //not closed already -> we try to close again
211 m_bInTryClose
= true;
212 m_aEndTryClosingCondition
.reset();
214 impl_registerApiCall(false);
217 //no mutex is acquired
219 //only remove listener calls will be worked on until end of tryclose
220 //all other new calls will wait till end of try close // @todo? is that really ok
222 //?? still running calls have the freedom to finish their work without crash
226 uno::Reference
< util::XCloseable
> xCloseable
=
227 uno::Reference
< util::XCloseable
>(m_pCloseable
);;
230 //--call queryClosing on all registered close listeners
231 ::cppu::OInterfaceContainerHelper
* pIC
= m_aListenerContainer
.getContainer(
232 cppu::UnoType
<util::XCloseListener
>::get());;
235 lang::EventObject
aEvent( xCloseable
);
236 ::cppu::OInterfaceIteratorHelper
aIt( *pIC
);
237 while( aIt
.hasMoreElements() )
239 uno::Reference
< util::XCloseListener
> xCloseListener( aIt
.next(), uno::UNO_QUERY
);
240 if(xCloseListener
.is())
241 xCloseListener
->queryClosing( aEvent
, bDeliverOwnership
);
246 catch( const uno::Exception
& )
248 //no mutex is acquired
249 g_close_endTryClose(bDeliverOwnership
, false);
255 void CloseableLifeTimeManager
256 ::g_close_endTryClose(bool bDeliverOwnership
, bool /* bMyVeto */ )
258 //this method is called, if the try to close was not successful
259 osl::Guard
< osl::Mutex
> aGuard( m_aAccessMutex
);
260 impl_setOwnership( bDeliverOwnership
, false );
262 m_bInTryClose
= false;
263 m_aEndTryClosingCondition
.set();
265 //Mutex needs to be acquired exactly ones
266 //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
267 impl_unregisterApiCall(false);
270 bool CloseableLifeTimeManager
271 ::g_close_isNeedToCancelLongLastingCalls( bool bDeliverOwnership
, util::CloseVetoException
& ex
)
272 throw ( util::CloseVetoException
)
274 //this method is called when no closelistener has had a veto during queryclosing
275 //the method returns false, if nothing stands against closing anymore
276 //it returns true, if some longlasting calls are running, which might be cancelled
277 //it throws the given exception, if long calls are running but not cancelable
279 osl::Guard
< osl::Mutex
> aGuard( m_aAccessMutex
);
280 //this count cannot grow after try of close has started, because we wait in all those methods for end of try closing
281 if( !m_nLongLastingCallCount
)
284 if(m_bLongLastingCallsCancelable
)
287 impl_setOwnership( bDeliverOwnership
, true );
289 m_bInTryClose
= false;
290 m_aEndTryClosingCondition
.set();
292 //Mutex needs to be acquired exactly ones
293 //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
294 impl_unregisterApiCall(false);
299 void CloseableLifeTimeManager
300 ::g_close_endTryClose_doClose()
302 //this method is called, if the try to close was successful
303 osl::ResettableGuard
< osl::Mutex
> aGuard( m_aAccessMutex
);
305 m_bInTryClose
= false;
306 m_aEndTryClosingCondition
.set();
308 //Mutex needs to be acquired exactly ones
309 //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
310 impl_unregisterApiCall(false);
314 void CloseableLifeTimeManager::impl_setOwnership( bool bDeliverOwnership
, bool bMyVeto
)
316 m_bOwnership
= bDeliverOwnership
&& bMyVeto
;
319 void CloseableLifeTimeManager
320 ::impl_apiCallCountReachedNull()
322 //Mutex needs to be acquired exactly ones
323 //mutex will be released inbetween in impl_doClose()
324 if( m_pCloseable
&& impl_shouldCloseAtNextChance() )
328 void CloseableLifeTimeManager
331 //Mutex needs to be acquired exactly ones before calling impl_doClose()
334 return; //behave as passive as possible, if disposed or closed already
335 if( m_bDisposed
|| m_bInDispose
)
336 return; //behave as passive as possible, if disposed or closed already
340 NegativeGuard
< osl::Mutex
> aNegativeGuard( m_aAccessMutex
);
341 //mutex is not acquired, mutex will be reacquired at the end of this method automatically
343 uno::Reference
< util::XCloseable
> xCloseable
=NULL
;
346 xCloseable
= uno::Reference
< util::XCloseable
>(m_pCloseable
);;
349 //--call notifyClosing on all registered close listeners
350 ::cppu::OInterfaceContainerHelper
* pIC
= m_aListenerContainer
.getContainer(
351 cppu::UnoType
<util::XCloseListener
>::get());;
354 lang::EventObject
aEvent( xCloseable
);
355 ::cppu::OInterfaceIteratorHelper
aIt( *pIC
);
356 while( aIt
.hasMoreElements() )
358 uno::Reference
< util::XCloseListener
> xListener( aIt
.next(), uno::UNO_QUERY
);
360 xListener
->notifyClosing( aEvent
);
365 catch( const uno::Exception
& ex
)
367 ASSERT_EXCEPTION( ex
);
372 uno::Reference
< lang::XComponent
> xComponent
=
373 uno::Reference
< lang::XComponent
>( xCloseable
, uno::UNO_QUERY
);
376 OSL_ENSURE( m_bClosed
, "a not closed component will be disposed " );
377 xComponent
->dispose();
380 //mutex will be reacquired in destructor of aNegativeGuard
383 bool CloseableLifeTimeManager
384 ::g_addCloseListener( const uno::Reference
< util::XCloseListener
> & xListener
)
385 throw(uno::RuntimeException
)
387 osl::Guard
< osl::Mutex
> aGuard( m_aAccessMutex
);
388 //Mutex needs to be acquired exactly ones; will be released inbetween
389 if( !impl_canStartApiCall() )
393 m_aListenerContainer
.addInterface( cppu::UnoType
<util::XCloseListener
>::get(),xListener
);
394 m_bOwnership
= false;
398 bool CloseableLifeTimeManager
399 ::impl_canStartApiCall()
401 //Mutex needs to be acquired exactly ones before calling this method
402 //the mutex will be released inbetween and reacquired
404 if( impl_isDisposed() )
405 return false; //behave passive if already disposed
407 return false; //behave passive if closing is already done
409 //during try-close most calls need to wait for the decision
410 while( m_bInTryClose
)
412 //if someone tries to close this object at the moment
413 //we need to wait for his end because the result of the preceding call
414 //is relevant for our behaviour here
416 m_aAccessMutex
.release();
417 m_aEndTryClosingCondition
.wait(); //@todo??? this may block??? try closing
418 m_aAccessMutex
.acquire();
419 if( m_bDisposed
|| m_bInDispose
|| m_bClosed
)
420 return false; //return if closed already
427 ::startApiCall(bool bLongLastingCall
)
429 //Mutex needs to be acquired exactly ones; will be released inbetween
430 //mutex is requiered due to constructor of LifeTimeGuard
432 OSL_ENSURE( !m_bCallRegistered
, "this method is only allowed ones" );
433 if(m_bCallRegistered
)
436 //Mutex needs to be acquired exactly ones; will be released inbetween
437 if( !m_rManager
.impl_canStartApiCall() )
441 m_bCallRegistered
= true;
442 m_bLongLastingCallRegistered
= bLongLastingCall
;
443 m_rManager
.impl_registerApiCall(bLongLastingCall
);
447 LifeTimeGuard::~LifeTimeGuard()
451 //do acquire the mutex if it was cleared before
452 osl::MutexGuard
g(m_rManager
.m_aAccessMutex
);
453 if(m_bCallRegistered
)
455 //Mutex needs to be acquired exactly ones
456 //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
457 m_rManager
.impl_unregisterApiCall(m_bLongLastingCallRegistered
);
460 catch( uno::Exception
& ex
)
462 //@todo ? allow a uno::RuntimeException from dispose to travel through??
463 ex
.Context
.is(); //to avoid compilation warnings
467 }//end namespace apphelper
469 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */