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>
21 #include <osl/diagnose.h>
23 #include <com/sun/star/lang/XComponent.hpp>
24 #include <com/sun/star/util/CloseVetoException.hpp>
25 #include <com/sun/star/util/XCloseListener.hpp>
26 #include <tools/diagnose_ex.h>
27 #include <sal/log.hxx>
29 using namespace ::com::sun::star
;
34 LifeTimeManager::LifeTimeManager( lang::XComponent
* pComponent
)
35 : m_aListenerContainer( m_aAccessMutex
)
36 , m_pComponent(pComponent
)
41 m_nLongLastingCallCount
= 0;
42 m_aNoAccessCountCondition
.set();
43 m_aNoLongLastingCallCountCondition
.set();
46 LifeTimeManager::~LifeTimeManager()
50 bool LifeTimeManager::impl_isDisposed( bool bAssert
)
52 if( m_bDisposed
|| m_bInDispose
)
56 OSL_FAIL( "This component is already disposed " );
63 bool LifeTimeManager::impl_canStartApiCall()
65 if( impl_isDisposed() )
66 return false; //behave passive if already disposed
72 void LifeTimeManager::impl_registerApiCall(bool bLongLastingCall
)
74 //only allowed if not disposed
75 //do not acquire the mutex here because it will be acquired already
78 //@todo? is it ok to wake some threads here while we have acquired the mutex?
79 m_aNoAccessCountCondition
.reset();
82 m_nLongLastingCallCount
++;
83 if(m_nLongLastingCallCount
==1)
84 m_aNoLongLastingCallCountCondition
.reset();
87 void LifeTimeManager::impl_unregisterApiCall(bool bLongLastingCall
)
89 //Mutex needs to be acquired exactly once
90 //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
92 OSL_ENSURE( m_nAccessCount
>0, "access count mismatch" );
95 m_nLongLastingCallCount
--;
96 if( m_nLongLastingCallCount
==0 )
98 m_aNoLongLastingCallCountCondition
.set();
100 if( m_nAccessCount
== 0)
102 m_aNoAccessCountCondition
.set();
103 impl_apiCallCountReachedNull();
108 bool LifeTimeManager::dispose()
112 osl::MutexGuard
aGuard( m_aAccessMutex
);
114 if( m_bDisposed
|| m_bInDispose
)
116 SAL_WARN("chart2", "This component is already disposed " );
117 return false; //behave passive if already disposed
121 //adding any listener is not allowed anymore
122 //new calls will not be accepted
123 //still running calls have the freedom to finish their work without crash
125 //no mutex is acquired
127 //--do the disposing of listeners after calling this method
129 uno::Reference
< lang::XComponent
> xComponent(m_pComponent
);
132 // notify XCLoseListeners
133 lang::EventObject
aEvent( xComponent
);
134 m_aListenerContainer
.disposeAndClear( aEvent
);
138 //no mutex is acquired
140 osl::MutexGuard
aGuard( m_aAccessMutex
);
141 OSL_ENSURE( !m_bDisposed
, "dispose was called already" );
144 //no mutex is acquired
146 //wait until all still running calls have finished
147 //the accessCount cannot grow anymore, because all calls will return after checking m_bDisposed
148 m_aNoAccessCountCondition
.wait();
150 //we are the only ones working on our data now
153 //--release all resources and references after calling this method successful
156 CloseableLifeTimeManager::CloseableLifeTimeManager( css::util::XCloseable
* pCloseable
157 , css::lang::XComponent
* pComponent
)
158 : LifeTimeManager( pComponent
)
159 , m_pCloseable(pCloseable
)
162 m_bInTryClose
= false;
163 m_bOwnership
= false;
164 m_aEndTryClosingCondition
.set();
167 CloseableLifeTimeManager::~CloseableLifeTimeManager()
171 bool CloseableLifeTimeManager::impl_isDisposedOrClosed( bool bAssert
)
173 if( impl_isDisposed( bAssert
) )
180 OSL_FAIL( "This object is already closed" );
187 bool CloseableLifeTimeManager::g_close_startTryClose(bool bDeliverOwnership
)
189 //no mutex is allowed to be acquired
191 osl::MutexGuard
aGuard( m_aAccessMutex
);
192 if( impl_isDisposedOrClosed(false) )
195 //Mutex needs to be acquired exactly once; will be released inbetween
196 if( !impl_canStartApiCall() )
200 //not closed already -> we try to close again
201 m_bInTryClose
= true;
202 m_aEndTryClosingCondition
.reset();
204 impl_registerApiCall(false);
207 //no mutex is acquired
209 //only remove listener calls will be worked on until end of tryclose
210 //all other new calls will wait till end of try close // @todo? is that really ok
212 //?? still running calls have the freedom to finish their work without crash
216 uno::Reference
< util::XCloseable
> xCloseable(m_pCloseable
);
219 //--call queryClosing on all registered close listeners
220 ::cppu::OInterfaceContainerHelper
* pIC
= m_aListenerContainer
.getContainer(
221 cppu::UnoType
<util::XCloseListener
>::get());
224 lang::EventObject
aEvent( xCloseable
);
225 ::cppu::OInterfaceIteratorHelper
aIt( *pIC
);
226 while( aIt
.hasMoreElements() )
228 uno::Reference
< util::XCloseListener
> xCloseListener( aIt
.next(), uno::UNO_QUERY
);
229 if(xCloseListener
.is())
230 xCloseListener
->queryClosing( aEvent
, bDeliverOwnership
);
235 catch( const uno::Exception
& )
237 //no mutex is acquired
238 g_close_endTryClose(bDeliverOwnership
);
244 void CloseableLifeTimeManager::g_close_endTryClose(bool bDeliverOwnership
)
246 //this method is called, if the try to close was not successful
247 osl::MutexGuard
aGuard( m_aAccessMutex
);
248 impl_setOwnership( bDeliverOwnership
, false );
250 m_bInTryClose
= false;
251 m_aEndTryClosingCondition
.set();
253 //Mutex needs to be acquired exactly once
254 //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
255 impl_unregisterApiCall(false);
258 void CloseableLifeTimeManager::g_close_isNeedToCancelLongLastingCalls( bool bDeliverOwnership
, util::CloseVetoException
const & ex
)
260 //this method is called when no closelistener has had a veto during queryclosing
261 //the method returns false, if nothing stands against closing anymore
262 //it returns true, if some longlasting calls are running, which might be cancelled
263 //it throws the given exception, if long calls are running but not cancelable
265 osl::MutexGuard
aGuard( m_aAccessMutex
);
266 //this count cannot grow after try of close has started, because we wait in all those methods for end of try closing
267 if( !m_nLongLastingCallCount
)
270 impl_setOwnership( bDeliverOwnership
, true );
272 m_bInTryClose
= false;
273 m_aEndTryClosingCondition
.set();
275 //Mutex needs to be acquired exactly once
276 //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
277 impl_unregisterApiCall(false);
282 void CloseableLifeTimeManager::g_close_endTryClose_doClose()
284 //this method is called, if the try to close was successful
285 osl::MutexGuard
aGuard( m_aAccessMutex
);
287 m_bInTryClose
= false;
288 m_aEndTryClosingCondition
.set();
290 //Mutex needs to be acquired exactly once
291 //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
292 impl_unregisterApiCall(false);
296 void CloseableLifeTimeManager::impl_setOwnership( bool bDeliverOwnership
, bool bMyVeto
)
298 m_bOwnership
= bDeliverOwnership
&& bMyVeto
;
301 void CloseableLifeTimeManager::impl_apiCallCountReachedNull()
303 //Mutex needs to be acquired exactly once
304 //mutex will be released inbetween in impl_doClose()
305 if( m_pCloseable
&& m_bOwnership
)
309 void CloseableLifeTimeManager::impl_doClose()
311 //Mutex needs to be acquired exactly once before calling impl_doClose()
314 return; //behave as passive as possible, if disposed or closed already
315 if( m_bDisposed
|| m_bInDispose
)
316 return; //behave as passive as possible, if disposed or closed already
320 NegativeGuard
< osl::Mutex
> aNegativeGuard( m_aAccessMutex
);
321 //mutex is not acquired, mutex will be reacquired at the end of this method automatically
323 uno::Reference
< util::XCloseable
> xCloseable
;
326 xCloseable
.set(m_pCloseable
);
329 //--call notifyClosing on all registered close listeners
330 ::cppu::OInterfaceContainerHelper
* pIC
= m_aListenerContainer
.getContainer(
331 cppu::UnoType
<util::XCloseListener
>::get());
334 lang::EventObject
aEvent( xCloseable
);
335 ::cppu::OInterfaceIteratorHelper
aIt( *pIC
);
336 while( aIt
.hasMoreElements() )
338 uno::Reference
< util::XCloseListener
> xListener( aIt
.next(), uno::UNO_QUERY
);
340 xListener
->notifyClosing( aEvent
);
345 catch( const uno::Exception
& )
347 DBG_UNHANDLED_EXCEPTION("chart2");
352 uno::Reference
< lang::XComponent
> xComponent( xCloseable
, uno::UNO_QUERY
);
355 OSL_ENSURE( m_bClosed
, "a not closed component will be disposed " );
356 xComponent
->dispose();
359 //mutex will be reacquired in destructor of aNegativeGuard
362 void CloseableLifeTimeManager::g_addCloseListener( const uno::Reference
< util::XCloseListener
> & xListener
)
364 osl::MutexGuard
aGuard( m_aAccessMutex
);
365 //Mutex needs to be acquired exactly once; will be released inbetween
366 if( !impl_canStartApiCall() )
370 m_aListenerContainer
.addInterface( cppu::UnoType
<util::XCloseListener
>::get(),xListener
);
371 m_bOwnership
= false;
374 bool CloseableLifeTimeManager::impl_canStartApiCall()
376 //Mutex needs to be acquired exactly once before calling this method
377 //the mutex will be released inbetween and reacquired
379 if( impl_isDisposed() )
380 return false; //behave passive if already disposed
382 return false; //behave passive if closing is already done
384 //during try-close most calls need to wait for the decision
385 while( m_bInTryClose
)
387 //if someone tries to close this object at the moment
388 //we need to wait for his end because the result of the preceding call
389 //is relevant for our behaviour here
391 m_aAccessMutex
.release();
392 m_aEndTryClosingCondition
.wait(); //@todo??? this may block??? try closing
393 m_aAccessMutex
.acquire();
394 if( m_bDisposed
|| m_bInDispose
|| m_bClosed
)
395 return false; //return if closed already
401 bool LifeTimeGuard::startApiCall(bool bLongLastingCall
)
403 //Mutex needs to be acquired exactly once; will be released inbetween
404 //mutex is required due to constructor of LifeTimeGuard
406 OSL_ENSURE( !m_bCallRegistered
, "this method is only allowed ones" );
407 if(m_bCallRegistered
)
410 //Mutex needs to be acquired exactly once; will be released inbetween
411 if( !m_rManager
.impl_canStartApiCall() )
415 m_bCallRegistered
= true;
416 m_bLongLastingCallRegistered
= bLongLastingCall
;
417 m_rManager
.impl_registerApiCall(bLongLastingCall
);
421 LifeTimeGuard::~LifeTimeGuard()
425 //do acquire the mutex if it was cleared before
426 osl::MutexGuard
g(m_rManager
.m_aAccessMutex
);
427 if(m_bCallRegistered
)
429 //Mutex needs to be acquired exactly once
430 //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
431 m_rManager
.impl_unregisterApiCall(m_bLongLastingCallRegistered
);
434 catch( uno::Exception
& ex
)
436 //@todo ? allow a uno::RuntimeException from dispose to travel through??
437 ex
.Context
.is(); //to avoid compilation warnings
441 }//end namespace apphelper
443 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */