LCOV - code coverage report
Current view: top level - chart2/source/tools - LifeTime.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 190 0.0 %
Date: 2014-04-14 Functions: 0 25 0.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           0 : LifeTimeManager::LifeTimeManager( lang::XComponent* pComponent, sal_Bool bLongLastingCallsCancelable )
      33             :     : m_aListenerContainer( m_aAccessMutex )
      34             :     , m_pComponent(pComponent)
      35           0 :     , m_bLongLastingCallsCancelable(bLongLastingCallsCancelable)
      36             : {
      37           0 :     impl_init();
      38           0 : }
      39             : 
      40           0 : void LifeTimeManager::impl_init()
      41             : {
      42           0 :     m_bDisposed = sal_False;
      43           0 :     m_bInDispose = sal_False;
      44           0 :     m_nAccessCount = 0;
      45           0 :     m_nLongLastingCallCount = 0;
      46           0 :     m_aNoAccessCountCondition.set();
      47           0 :     m_aNoLongLastingCallCountCondition.set();
      48           0 : }
      49             : 
      50           0 : LifeTimeManager::~LifeTimeManager()
      51             : {
      52           0 : }
      53             : 
      54           0 : bool LifeTimeManager::impl_isDisposed( bool bAssert )
      55             : {
      56           0 :     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           0 :     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           0 :     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           0 :     m_nAccessCount++;
      83           0 :     if(m_nAccessCount==1)
      84             :         //@todo? is it ok to wake some threads here while we have acquired the mutex?
      85           0 :         m_aNoAccessCountCondition.reset();
      86             : 
      87           0 :     if(bLongLastingCall)
      88           0 :         m_nLongLastingCallCount++;
      89           0 :     if(m_nLongLastingCallCount==1)
      90           0 :         m_aNoLongLastingCallCountCondition.reset();
      91           0 : }
      92           0 :     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           0 :     m_nAccessCount--;
     100           0 :     if(bLongLastingCall)
     101           0 :         m_nLongLastingCallCount--;
     102           0 :     if( m_nLongLastingCallCount==0 )
     103             :     {
     104           0 :         m_aNoLongLastingCallCountCondition.set();
     105             :     }
     106           0 :     if( m_nAccessCount== 0)
     107             :     {
     108           0 :         m_aNoAccessCountCondition.set();
     109           0 :         impl_apiCallCountReachedNull();
     110             : 
     111             :     }
     112           0 : }
     113             : 
     114           0 :         sal_Bool LifeTimeManager
     115             : ::dispose() throw(uno::RuntimeException)
     116             : {
     117             :     //hold no mutex
     118             :     {
     119           0 :         osl::Guard< osl::Mutex > aGuard( m_aAccessMutex );
     120             : 
     121           0 :         if( m_bDisposed || m_bInDispose )
     122             :         {
     123             :             OSL_TRACE( "This component is already disposed " );
     124           0 :             return sal_False; //behave passive if already disposed
     125             :         }
     126             : 
     127           0 :         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           0 :             uno::Reference< lang::XComponent >(m_pComponent);;
     138           0 :         if(xComponent.is())
     139             :         {
     140             :             // notify XCLoseListeners
     141           0 :             lang::EventObject aEvent( xComponent );
     142           0 :             m_aListenerContainer.disposeAndClear( aEvent );
     143           0 :         }
     144             :     }
     145             : 
     146             :     //no mutex is acquired
     147             :     {
     148           0 :         osl::ClearableGuard< osl::Mutex > aGuard( m_aAccessMutex );
     149             :         OSL_ENSURE( !m_bDisposed, "dispose was called already" );
     150           0 :         m_bDisposed = sal_True;
     151           0 :         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           0 :     m_aNoAccessCountCondition.wait();
     158             : 
     159             :     //we are the only ones working on our data now
     160             : 
     161           0 :     return sal_True;
     162             :     //--release all resources and references after calling this method successful
     163             : }
     164             : 
     165           0 : CloseableLifeTimeManager::CloseableLifeTimeManager( ::com::sun::star::util::XCloseable* pCloseable
     166             :         , ::com::sun::star::lang::XComponent* pComponent
     167             :         , sal_Bool bLongLastingCallsCancelable )
     168             :         : LifeTimeManager( pComponent, bLongLastingCallsCancelable )
     169           0 :         , m_pCloseable(pCloseable)
     170             : {
     171           0 :     impl_init();
     172           0 : }
     173             : 
     174           0 : CloseableLifeTimeManager::~CloseableLifeTimeManager()
     175             : {
     176           0 : }
     177             : 
     178           0 : bool CloseableLifeTimeManager::impl_isDisposedOrClosed( bool bAssert )
     179             : {
     180           0 :     if( impl_isDisposed( bAssert ) )
     181           0 :         return true;
     182             : 
     183           0 :     if( m_bClosed )
     184             :     {
     185             :         if( bAssert )
     186             :         {
     187             :             OSL_FAIL( "This object is already closed" );
     188             :             (void)(bAssert);//avoid warnings
     189             :         }
     190           0 :         return true;
     191             :     }
     192           0 :     return false;
     193             : }
     194             : 
     195           0 :         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           0 :         osl::ResettableGuard< osl::Mutex > aGuard( m_aAccessMutex );
     202           0 :         if( impl_isDisposedOrClosed(false) )
     203           0 :             return sal_False;
     204             : 
     205             :         //Mutex needs to be acquired exactly ones; will be released inbetween
     206           0 :         if( !impl_canStartApiCall() )
     207           0 :             return sal_False;
     208             :         //mutex is acquired
     209             : 
     210             :         //not closed already -> we try to close again
     211           0 :         m_bInTryClose = sal_True;
     212           0 :         m_aEndTryClosingCondition.reset();
     213             : 
     214           0 :         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           0 :             uno::Reference< util::XCloseable >(m_pCloseable);;
     228           0 :         if(xCloseable.is())
     229             :         {
     230             :             //--call queryClosing on all registered close listeners
     231             :             ::cppu::OInterfaceContainerHelper* pIC = m_aListenerContainer.getContainer(
     232           0 :                         ::getCppuType((const uno::Reference< util::XCloseListener >*)0) );;
     233           0 :             if( pIC )
     234             :             {
     235           0 :                 lang::EventObject aEvent( xCloseable );
     236           0 :                 ::cppu::OInterfaceIteratorHelper aIt( *pIC );
     237           0 :                 while( aIt.hasMoreElements() )
     238             :                 {
     239           0 :                     uno::Reference< util::XCloseListener > xCloseListener( aIt.next(), uno::UNO_QUERY );
     240           0 :                     if(xCloseListener.is())
     241           0 :                         xCloseListener->queryClosing( aEvent, bDeliverOwnership );
     242           0 :                 }
     243             :             }
     244           0 :         }
     245             :     }
     246           0 :     catch( const uno::Exception& )
     247             :     {
     248             :         //no mutex is acquired
     249           0 :         g_close_endTryClose(bDeliverOwnership, sal_False);
     250           0 :         throw;
     251             :     }
     252           0 :     return sal_True;
     253             : }
     254             : 
     255           0 :     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           0 :     osl::Guard< osl::Mutex > aGuard( m_aAccessMutex );
     260           0 :     impl_setOwnership( bDeliverOwnership, sal_False );
     261             : 
     262           0 :     m_bInTryClose = sal_False;
     263           0 :     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           0 :     impl_unregisterApiCall(sal_False);
     268           0 : }
     269             : 
     270           0 :     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           0 :     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           0 :     if( !m_nLongLastingCallCount )
     282           0 :         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           0 :     void CloseableLifeTimeManager
     300             : ::g_close_endTryClose_doClose()
     301             : {
     302             :     //this method is called, if the try to close was successful
     303           0 :     osl::ResettableGuard< osl::Mutex > aGuard( m_aAccessMutex );
     304             : 
     305           0 :     m_bInTryClose       = sal_False;
     306           0 :     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           0 :     impl_unregisterApiCall(sal_False);
     311           0 :     impl_doClose();
     312           0 : }
     313             : 
     314           0 :     void CloseableLifeTimeManager
     315             : ::impl_setOwnership( sal_Bool bDeliverOwnership, sal_Bool bMyVeto )
     316             : {
     317           0 :     m_bOwnership            = bDeliverOwnership && bMyVeto;
     318           0 :     m_bOwnershipIsWellKnown = sal_True;
     319           0 : }
     320           0 :     sal_Bool CloseableLifeTimeManager
     321             : ::impl_shouldCloseAtNextChance()
     322             : {
     323           0 :     return m_bOwnership;
     324             : }
     325             : 
     326           0 :     void CloseableLifeTimeManager
     327             : ::impl_apiCallCountReachedNull()
     328             : {
     329             :     //Mutex needs to be acquired exactly ones
     330             :     //mutex will be released inbetween in impl_doClose()
     331           0 :     if( m_pCloseable && impl_shouldCloseAtNextChance() )
     332           0 :         impl_doClose();
     333           0 : }
     334             : 
     335           0 :     void CloseableLifeTimeManager
     336             : ::impl_doClose()
     337             : {
     338             :     //Mutex needs to be acquired exactly ones before calling impl_doClose()
     339             : 
     340           0 :     if(m_bClosed)
     341           0 :         return; //behave as passive as possible, if disposed or closed already
     342           0 :     if( m_bDisposed || m_bInDispose )
     343           0 :         return; //behave as passive as possible, if disposed or closed already
     344             : 
     345             : 
     346           0 :     m_bClosed = sal_True;
     347             : 
     348           0 :     NegativeGuard< osl::Mutex > aNegativeGuard( m_aAccessMutex );
     349             :     //mutex is not acquired, mutex will be reacquired at the end of this method automatically
     350             : 
     351           0 :     uno::Reference< util::XCloseable > xCloseable=NULL;
     352             :     try
     353             :     {
     354           0 :         xCloseable = uno::Reference< util::XCloseable >(m_pCloseable);;
     355           0 :         if(xCloseable.is())
     356             :         {
     357             :             //--call notifyClosing on all registered close listeners
     358             :             ::cppu::OInterfaceContainerHelper* pIC = m_aListenerContainer.getContainer(
     359           0 :                         ::getCppuType((const uno::Reference< util::XCloseListener >*)0) );;
     360           0 :             if( pIC )
     361             :             {
     362           0 :                 lang::EventObject aEvent( xCloseable );
     363           0 :                 ::cppu::OInterfaceIteratorHelper aIt( *pIC );
     364           0 :                 while( aIt.hasMoreElements() )
     365             :                 {
     366           0 :                     uno::Reference< util::XCloseListener > xListener( aIt.next(), uno::UNO_QUERY );
     367           0 :                     if( xListener.is() )
     368           0 :                         xListener->notifyClosing( aEvent );
     369           0 :                 }
     370             :             }
     371             :         }
     372             :     }
     373           0 :     catch( const uno::Exception& ex )
     374             :     {
     375             :         ASSERT_EXCEPTION( ex );
     376             :     }
     377             : 
     378           0 :     if(xCloseable.is())
     379             :     {
     380             :         uno::Reference< lang::XComponent > xComponent =
     381           0 :             uno::Reference< lang::XComponent >( xCloseable, uno::UNO_QUERY );
     382           0 :         if(xComponent.is())
     383             :         {
     384             :             OSL_ENSURE( m_bClosed, "a not closed component will be disposed " );
     385           0 :             xComponent->dispose();
     386           0 :         }
     387           0 :     }
     388             :     //mutex will be reacquired in destructor of aNegativeGuard
     389             : }
     390             : 
     391           0 :     sal_Bool CloseableLifeTimeManager
     392             : ::g_addCloseListener( const uno::Reference< util::XCloseListener > & xListener )
     393             :     throw(uno::RuntimeException)
     394             : {
     395           0 :     osl::Guard< osl::Mutex > aGuard( m_aAccessMutex );
     396             :     //Mutex needs to be acquired exactly ones; will be released inbetween
     397           0 :     if( !impl_canStartApiCall() )
     398           0 :         return sal_False;
     399             :     //mutex is acquired
     400             : 
     401           0 :     m_aListenerContainer.addInterface( ::getCppuType((const uno::Reference< util::XCloseListener >*)0),xListener );
     402           0 :     m_bOwnership = sal_False;
     403           0 :     return sal_True;
     404             : }
     405             : 
     406           0 :     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           0 :     if( impl_isDisposed() )
     413           0 :         return sal_False; //behave passive if already disposed
     414           0 :     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           0 :     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           0 :     return sal_True;
     432             : }
     433             : 
     434           0 :     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           0 :     if(m_bCallRegistered)
     442           0 :         return sal_False;
     443             : 
     444             :     //Mutex needs to be acquired exactly ones; will be released inbetween
     445           0 :     if( !m_rManager.impl_canStartApiCall() )
     446           0 :         return sal_False;
     447             :     //mutex is acquired
     448             : 
     449           0 :     m_bCallRegistered = sal_True;
     450           0 :     m_bLongLastingCallRegistered = bLongLastingCall;
     451           0 :     m_rManager.impl_registerApiCall(bLongLastingCall);
     452           0 :     return sal_True;
     453             : }
     454             : 
     455           0 : LifeTimeGuard::~LifeTimeGuard()
     456             : {
     457             :     try
     458             :     {
     459             :         //do acquire the mutex if it was cleared before
     460           0 :         osl::MutexGuard g(m_rManager.m_aAccessMutex);
     461           0 :         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           0 :             m_rManager.impl_unregisterApiCall(m_bLongLastingCallRegistered);
     466           0 :         }
     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           0 : }
     474             : 
     475             : }//end namespace apphelper
     476             : 
     477             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10