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

Generated by: LCOV version 1.10