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

Generated by: LCOV version 1.10