LCOV - code coverage report
Current view: top level - libreoffice/chart2/source/tools - LifeTime.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 141 188 75.0 %
Date: 2012-12-27 Functions: 20 25 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "LifeTime.hxx"
      21             : #include "macros.hxx"
      22             : #include <osl/diagnose.h>
      23             : 
      24             : #include <com/sun/star/util/XModifyListener.hpp>
      25             : #include <com/sun/star/util/XCloseListener.hpp>
      26             : 
      27             : using namespace ::com::sun::star;
      28             : 
      29             : namespace apphelper
      30             : {
      31             : //--------------------------
      32             : 
      33          41 : LifeTimeManager::LifeTimeManager( lang::XComponent* pComponent, sal_Bool bLongLastingCallsCancelable )
      34             :     : m_aListenerContainer( m_aAccessMutex )
      35             :     , m_pComponent(pComponent)
      36          41 :     , m_bLongLastingCallsCancelable(bLongLastingCallsCancelable)
      37             : {
      38          41 :     impl_init();
      39          41 : }
      40             : 
      41          41 : void LifeTimeManager::impl_init()
      42             : {
      43          41 :     m_bDisposed = sal_False;
      44          41 :     m_bInDispose = sal_False;
      45          41 :     m_nAccessCount = 0;
      46          41 :     m_nLongLastingCallCount = 0;
      47          41 :     m_aNoAccessCountCondition.set();
      48          41 :     m_aNoLongLastingCallCountCondition.set();
      49          41 : }
      50             : 
      51          41 : LifeTimeManager::~LifeTimeManager()
      52             : {
      53          41 : }
      54             : 
      55        2856 : bool LifeTimeManager::impl_isDisposed( bool bAssert )
      56             : {
      57        2856 :     if( m_bDisposed || m_bInDispose )
      58             :     {
      59             :         if( bAssert )
      60             :         {
      61             :             OSL_FAIL( "This component is already disposed " );
      62             :             (void)(bAssert);
      63             :         }
      64           0 :         return sal_True;
      65             :     }
      66        2856 :     return sal_False;
      67             : }
      68           0 :             sal_Bool LifeTimeManager
      69             : ::impl_canStartApiCall()
      70             : {
      71           0 :     if( impl_isDisposed() )
      72           0 :         return sal_False; //behave passive if already disposed
      73             : 
      74             :     //mutex is acquired
      75           0 :     return sal_True;
      76             : }
      77             : 
      78        2528 :     void LifeTimeManager
      79             : ::impl_registerApiCall(sal_Bool bLongLastingCall)
      80             : {
      81             :     //only allowed if not disposed
      82             :     //do not acquire the mutex here because it will be acquired already
      83        2528 :     m_nAccessCount++;
      84        2528 :     if(m_nAccessCount==1)
      85             :         //@todo? is it ok to wake some threads here while we have acquired the mutex?
      86        2528 :         m_aNoAccessCountCondition.reset();
      87             : 
      88        2528 :     if(bLongLastingCall)
      89           0 :         m_nLongLastingCallCount++;
      90        2528 :     if(m_nLongLastingCallCount==1)
      91           0 :         m_aNoLongLastingCallCountCondition.reset();
      92        2528 : }
      93        2528 :     void LifeTimeManager
      94             : ::impl_unregisterApiCall(sal_Bool bLongLastingCall)
      95             : {
      96             :     //Mutex needs to be acquired exactly ones
      97             :     //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
      98             : 
      99             :     OSL_ENSURE( m_nAccessCount>0, "access count mismatch" );
     100        2528 :     m_nAccessCount--;
     101        2528 :     if(bLongLastingCall)
     102           0 :         m_nLongLastingCallCount--;
     103        2528 :     if( m_nLongLastingCallCount==0 )
     104             :     {
     105        2528 :         m_aNoLongLastingCallCountCondition.set();
     106             :     }
     107        2528 :     if( m_nAccessCount== 0)
     108             :     {
     109        2528 :         m_aNoAccessCountCondition.set();
     110        2528 :         impl_apiCallCountReachedNull();
     111             : 
     112             :     }
     113        2528 : }
     114             : 
     115          82 :         sal_Bool LifeTimeManager
     116             : ::dispose() throw(uno::RuntimeException)
     117             : {
     118             :     //hold no mutex
     119             :     {
     120          82 :         osl::Guard< osl::Mutex > aGuard( m_aAccessMutex );
     121             : 
     122          82 :         if( m_bDisposed || m_bInDispose )
     123             :         {
     124             :             OSL_TRACE( "This component is already disposed " );
     125          41 :             return sal_False; //behave passive if already disposed
     126             :         }
     127             : 
     128          41 :         m_bInDispose = true;
     129             :         //adding any listener is not allowed anymore
     130             :         //new calls will not be accepted
     131             :         //still running calls have the freedom to finish their work without crash
     132             :     }
     133             :     //no mutex is acquired
     134             : 
     135             :     //--do the disposing of listeners after calling this method
     136             :     {
     137             :         uno::Reference< lang::XComponent > xComponent =
     138          41 :             uno::Reference< lang::XComponent >(m_pComponent);;
     139          41 :         if(xComponent.is())
     140             :         {
     141             :             // notify XCLoseListeners
     142          41 :             lang::EventObject aEvent( xComponent );
     143          41 :             m_aListenerContainer.disposeAndClear( aEvent );
     144          41 :         }
     145             :     }
     146             : 
     147             :     //no mutex is acquired
     148             :     {
     149          41 :         osl::ClearableGuard< osl::Mutex > aGuard( m_aAccessMutex );
     150             :         OSL_ENSURE( !m_bDisposed, "dispose was called already" );
     151          41 :         m_bDisposed = sal_True;
     152          41 :         aGuard.clear();
     153             :     }
     154             :     //no mutex is acquired
     155             : 
     156             :     //wait until all still running calls have finished
     157             :     //the accessCount cannot grow anymore, because all calls will return after checking m_bDisposed
     158          41 :     m_aNoAccessCountCondition.wait();
     159             : 
     160             :     //we are the only ones working on our data now
     161             : 
     162          41 :     return sal_True;
     163             :     //--release all resources and references after calling this method successful
     164             : }
     165             : 
     166             : //-----------------------------------------------------------------
     167             : 
     168          41 : CloseableLifeTimeManager::CloseableLifeTimeManager( ::com::sun::star::util::XCloseable* pCloseable
     169             :         , ::com::sun::star::lang::XComponent* pComponent
     170             :         , sal_Bool bLongLastingCallsCancelable )
     171             :         : LifeTimeManager( pComponent, bLongLastingCallsCancelable )
     172          41 :         , m_pCloseable(pCloseable)
     173             : {
     174          41 :     impl_init();
     175          41 : }
     176             : 
     177          41 : CloseableLifeTimeManager::~CloseableLifeTimeManager()
     178             : {
     179          41 : }
     180             : 
     181         287 : bool CloseableLifeTimeManager::impl_isDisposedOrClosed( bool bAssert )
     182             : {
     183         287 :     if( impl_isDisposed( bAssert ) )
     184           0 :         return sal_True;
     185             : 
     186         287 :     if( m_bClosed )
     187             :     {
     188             :         if( bAssert )
     189             :         {
     190             :             OSL_FAIL( "This object is already closed" );
     191             :             (void)(bAssert);//avoid warnings
     192             :         }
     193           0 :         return sal_True;
     194             :     }
     195         287 :     return sal_False;
     196             : }
     197             : 
     198          41 :         sal_Bool CloseableLifeTimeManager
     199             : ::g_close_startTryClose(sal_Bool bDeliverOwnership)
     200             :     throw ( uno::Exception )
     201             : {
     202             :     //no mutex is allowed to be acquired
     203             :     {
     204          41 :         osl::ResettableGuard< osl::Mutex > aGuard( m_aAccessMutex );
     205          41 :         if( impl_isDisposedOrClosed(false) )
     206           0 :             return sal_False;
     207             : 
     208             :         //Mutex needs to be acquired exactly ones; will be released inbetween
     209          41 :         if( !impl_canStartApiCall() )
     210           0 :             return sal_False;
     211             :         //mutex is acquired
     212             : 
     213             :         //not closed already -> we try to close again
     214          41 :         m_bInTryClose = sal_True;
     215          41 :         m_aEndTryClosingCondition.reset();
     216             : 
     217          41 :         impl_registerApiCall(sal_False);
     218             :     }
     219             : 
     220             :     //------------------------------------------------
     221             :     //no mutex is acquired
     222             : 
     223             :     //only remove listener calls will be worked on until end of tryclose
     224             :     //all other new calls will wait till end of try close // @todo? is that really ok
     225             : 
     226             :     //?? still running calls have the freedom to finish their work without crash
     227             : 
     228             :     try
     229             :     {
     230             :         uno::Reference< util::XCloseable > xCloseable =
     231          41 :             uno::Reference< util::XCloseable >(m_pCloseable);;
     232          41 :         if(xCloseable.is())
     233             :         {
     234             :             //--call queryClosing on all registered close listeners
     235             :             ::cppu::OInterfaceContainerHelper* pIC = m_aListenerContainer.getContainer(
     236          41 :                         ::getCppuType((const uno::Reference< util::XCloseListener >*)0) );;
     237          41 :             if( pIC )
     238             :             {
     239          41 :                 lang::EventObject aEvent( xCloseable );
     240          41 :                 ::cppu::OInterfaceIteratorHelper aIt( *pIC );
     241         123 :                 while( aIt.hasMoreElements() )
     242             :                 {
     243          41 :                     uno::Reference< util::XCloseListener > xCloseListener( aIt.next(), uno::UNO_QUERY );
     244          41 :                     if(xCloseListener.is())
     245          41 :                         xCloseListener->queryClosing( aEvent, bDeliverOwnership );
     246          82 :                 }
     247             :             }
     248          41 :         }
     249             :     }
     250           0 :     catch( const uno::Exception& )
     251             :     {
     252             :         //no mutex is acquired
     253           0 :         g_close_endTryClose(bDeliverOwnership, sal_False);
     254           0 :         throw;
     255             :     }
     256          41 :     return sal_True;
     257             : }
     258             : 
     259           0 :     void CloseableLifeTimeManager
     260             : ::g_close_endTryClose(sal_Bool bDeliverOwnership, sal_Bool /* bMyVeto */ )
     261             : {
     262             :     //this method is called, if the try to close was not successfull
     263           0 :     osl::Guard< osl::Mutex > aGuard( m_aAccessMutex );
     264           0 :     impl_setOwnership( bDeliverOwnership, sal_False );
     265             : 
     266           0 :     m_bInTryClose = sal_False;
     267           0 :     m_aEndTryClosingCondition.set();
     268             : 
     269             :     //Mutex needs to be acquired exactly ones
     270             :     //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
     271           0 :     impl_unregisterApiCall(sal_False);
     272           0 : }
     273             : 
     274          41 :     sal_Bool CloseableLifeTimeManager
     275             : ::g_close_isNeedToCancelLongLastingCalls( sal_Bool bDeliverOwnership, util::CloseVetoException& ex )
     276             :     throw ( util::CloseVetoException )
     277             : {
     278             :     //this method is called when no closelistener has had a veto during queryclosing
     279             :     //the method returns false, if nothing stands against closing anymore
     280             :     //it returns true, if some longlasting calls are running, which might be cancelled
     281             :     //it throws the given exception, if long calls are running but not cancelable
     282             : 
     283          41 :     osl::Guard< osl::Mutex > aGuard( m_aAccessMutex );
     284             :     //this count cannot grow after try of close has started, because we wait in all those methods for end of try closing
     285          41 :     if( !m_nLongLastingCallCount )
     286          41 :         return sal_False;
     287             : 
     288           0 :       if(m_bLongLastingCallsCancelable)
     289           0 :         return sal_True;
     290             : 
     291           0 :     impl_setOwnership( bDeliverOwnership, sal_True );
     292             : 
     293           0 :     m_bInTryClose = sal_False;
     294           0 :     m_aEndTryClosingCondition.set();
     295             : 
     296             :     //Mutex needs to be acquired exactly ones
     297             :     //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
     298           0 :     impl_unregisterApiCall(sal_False);
     299             : 
     300           0 :     throw ex;
     301             : }
     302             : 
     303          41 :     void CloseableLifeTimeManager
     304             : ::g_close_endTryClose_doClose()
     305             : {
     306             :     //this method is called, if the try to close was successfull
     307          41 :     osl::ResettableGuard< osl::Mutex > aGuard( m_aAccessMutex );
     308             : 
     309          41 :     m_bInTryClose       = sal_False;
     310          41 :     m_aEndTryClosingCondition.set();
     311             : 
     312             :     //Mutex needs to be acquired exactly ones
     313             :     //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
     314          41 :     impl_unregisterApiCall(sal_False);
     315          41 :     impl_doClose();
     316          41 : }
     317             : 
     318           0 :     void CloseableLifeTimeManager
     319             : ::impl_setOwnership( sal_Bool bDeliverOwnership, sal_Bool bMyVeto )
     320             : {
     321           0 :     m_bOwnership            = bDeliverOwnership && bMyVeto;
     322           0 :     m_bOwnershipIsWellKnown = sal_True;
     323           0 : }
     324        2528 :     sal_Bool CloseableLifeTimeManager
     325             : ::impl_shouldCloseAtNextChance()
     326             : {
     327        2528 :     return m_bOwnership;
     328             : }
     329             : 
     330        2528 :     void CloseableLifeTimeManager
     331             : ::impl_apiCallCountReachedNull()
     332             : {
     333             :     //Mutex needs to be acquired exactly ones
     334             :     //mutex will be released inbetween in impl_doClose()
     335        2528 :     if( m_pCloseable && impl_shouldCloseAtNextChance() )
     336           0 :         impl_doClose();
     337        2528 : }
     338             : 
     339          41 :     void CloseableLifeTimeManager
     340             : ::impl_doClose()
     341             : {
     342             :     //Mutex needs to be acquired exactly ones before calling impl_doClose()
     343             : 
     344          41 :     if(m_bClosed)
     345             :         return; //behave as passive as possible, if disposed or closed already
     346          41 :     if( m_bDisposed || m_bInDispose )
     347             :         return; //behave as passive as possible, if disposed or closed already
     348             : 
     349             :     //--------
     350          41 :     m_bClosed = sal_True;
     351             : 
     352          41 :     NegativeGuard< osl::Mutex > aNegativeGuard( m_aAccessMutex );
     353             :     //mutex is not acquired, mutex will be reacquired at the end of this method automatically
     354             : 
     355          41 :     uno::Reference< util::XCloseable > xCloseable=NULL;
     356             :     try
     357             :     {
     358          41 :         xCloseable = uno::Reference< util::XCloseable >(m_pCloseable);;
     359          41 :         if(xCloseable.is())
     360             :         {
     361             :             //--call notifyClosing on all registered close listeners
     362             :             ::cppu::OInterfaceContainerHelper* pIC = m_aListenerContainer.getContainer(
     363          41 :                         ::getCppuType((const uno::Reference< util::XCloseListener >*)0) );;
     364          41 :             if( pIC )
     365             :             {
     366          41 :                 lang::EventObject aEvent( xCloseable );
     367          41 :                 ::cppu::OInterfaceIteratorHelper aIt( *pIC );
     368         123 :                 while( aIt.hasMoreElements() )
     369             :                 {
     370          41 :                     uno::Reference< util::XCloseListener > xListener( aIt.next(), uno::UNO_QUERY );
     371          41 :                     if( xListener.is() )
     372          41 :                         xListener->notifyClosing( aEvent );
     373          82 :                 }
     374             :             }
     375             :         }
     376             :     }
     377           0 :     catch( const uno::Exception& ex )
     378             :     {
     379             :         ASSERT_EXCEPTION( ex );
     380             :     }
     381             : 
     382          41 :     if(xCloseable.is())
     383             :     {
     384             :         uno::Reference< lang::XComponent > xComponent =
     385          41 :             uno::Reference< lang::XComponent >( xCloseable, uno::UNO_QUERY );
     386          41 :         if(xComponent.is())
     387             :         {
     388             :             OSL_ENSURE( m_bClosed, "a not closed component will be disposed " );
     389          41 :             xComponent->dispose();
     390          41 :         }
     391          41 :     }
     392             :     //mutex will be reacquired in destructor of aNegativeGuard
     393             : }
     394             : 
     395          41 :     sal_Bool CloseableLifeTimeManager
     396             : ::g_addCloseListener( const uno::Reference< util::XCloseListener > & xListener )
     397             :     throw(uno::RuntimeException)
     398             : {
     399          41 :     osl::Guard< osl::Mutex > aGuard( m_aAccessMutex );
     400             :     //Mutex needs to be acquired exactly ones; will be released inbetween
     401          41 :     if( !impl_canStartApiCall() )
     402           0 :         return sal_False;
     403             :     //mutex is acquired
     404             : 
     405          41 :     m_aListenerContainer.addInterface( ::getCppuType((const uno::Reference< util::XCloseListener >*)0),xListener );
     406          41 :     m_bOwnership = sal_False;
     407          41 :     return sal_True;
     408             : }
     409             : 
     410        2569 :     sal_Bool CloseableLifeTimeManager
     411             : ::impl_canStartApiCall()
     412             : {
     413             :     //Mutex needs to be acquired exactly ones before calling this method
     414             :     //the mutex will be released inbetween and reacquired
     415             : 
     416        2569 :     if( impl_isDisposed() )
     417           0 :         return sal_False; //behave passive if already disposed
     418        2569 :     if( m_bClosed )
     419           0 :         return sal_False; //behave passive if closing is already done
     420             : 
     421             :     //during try-close most calls need to wait for the decision
     422        5138 :     while( m_bInTryClose )
     423             :     {
     424             :         //if someone tries to close this object at the moment
     425             :         //we need to wait for his end because the result of the preceding call
     426             :         //is relevant for our behaviour here
     427             : 
     428           0 :         m_aAccessMutex.release();
     429           0 :         m_aEndTryClosingCondition.wait(); //@todo??? this may block??? try closing
     430           0 :         m_aAccessMutex.acquire();
     431           0 :         if( m_bDisposed || m_bInDispose || m_bClosed )
     432           0 :             return sal_False; //return if closed already
     433             :     }
     434             :     //mutex is acquired
     435        2569 :     return sal_True;
     436             : }
     437             : 
     438             : //--------------------------
     439             : 
     440        2487 :     sal_Bool LifeTimeGuard
     441             : ::startApiCall(sal_Bool bLongLastingCall)
     442             : {
     443             :     //Mutex needs to be acquired exactly ones; will be released inbetween
     444             :     //mutex is requiered due to constructor of LifeTimeGuard
     445             : 
     446             :     OSL_ENSURE( !m_bCallRegistered, "this method is only allowed ones" );
     447        2487 :     if(m_bCallRegistered)
     448           0 :         return sal_False;
     449             : 
     450             :     //Mutex needs to be acquired exactly ones; will be released inbetween
     451        2487 :     if( !m_rManager.impl_canStartApiCall() )
     452           0 :         return sal_False;
     453             :     //mutex is acquired
     454             : 
     455        2487 :     m_bCallRegistered = sal_True;
     456        2487 :     m_bLongLastingCallRegistered = bLongLastingCall;
     457        2487 :     m_rManager.impl_registerApiCall(bLongLastingCall);
     458        2487 :     return sal_True;
     459             : }
     460             : 
     461        4974 : LifeTimeGuard::~LifeTimeGuard()
     462             : {
     463             :     try
     464             :     {
     465             :         //do acquire the mutex if it was cleared before
     466        2487 :         osl::MutexGuard g(m_rManager.m_aAccessMutex);
     467        2487 :         if(m_bCallRegistered)
     468             :         {
     469             :             //Mutex needs to be acquired exactly ones
     470             :             //mutex may be released inbetween in special case of impl_apiCallCountReachedNull()
     471        2487 :             m_rManager.impl_unregisterApiCall(m_bLongLastingCallRegistered);
     472        2487 :         }
     473             :     }
     474           0 :     catch( uno::Exception& ex )
     475             :     {
     476             :         //@todo ? allow a uno::RuntimeException from dispose to travel through??
     477           0 :         ex.Context.is(); //to avoid compilation warnings
     478             :     }
     479        2487 : }
     480             : 
     481             : }//end namespace apphelper
     482             : 
     483             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10