LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/framework/source/fwi/threadhelp - transactionmanager.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 57 64 89.1 %
Date: 2013-07-09 Functions: 7 9 77.8 %
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 <threadhelp/transactionmanager.hxx>
      21             : #include <threadhelp/resetableguard.hxx>
      22             : #include <macros/debug.hxx>
      23             : 
      24             : #include <macros/generic.hxx>
      25             : #include <fwidllapi.h>
      26             : 
      27             : #include <com/sun/star/lang/DisposedException.hpp>
      28             : 
      29             : namespace framework{
      30             : 
      31             : /*-************************************************************************************************************//**
      32             :     @short      standard ctor
      33             :     @descr      Initialize instance with right start values for correct working.
      34             : 
      35             :     @seealso    -
      36             : 
      37             :     @param      -
      38             :     @return     -
      39             : 
      40             :     @onerror    -
      41             : *//*-*************************************************************************************************************/
      42        2482 : TransactionManager::TransactionManager()
      43             :     : m_eWorkingMode      ( E_INIT )
      44        2482 :     , m_nTransactionCount ( 0      )
      45             : {
      46        2482 :     m_aBarrier.open();
      47        2482 : }
      48             : 
      49             : /*-************************************************************************************************************//**
      50             :     @short      standard dtor
      51             :     @descr      -
      52             : 
      53             :     @seealso    -
      54             : 
      55             :     @param      -
      56             :     @return     -
      57             : 
      58             :     @onerror    -
      59             : *//*-*************************************************************************************************************/
      60        2434 : TransactionManager::~TransactionManager()
      61             : {
      62        2434 : }
      63             : 
      64             : /*-****************************************************************************************************//**
      65             :     @interface  ITransactionManager
      66             :     @short      set new working mode
      67             :     @descr      These implementation knows for states of working: E_INIT, E_WORK, E_CLOSING, E_CLOSE
      68             :                 You can step during this ones only from the left to the right side and start at left side again!
      69             :                 (This is neccessary e.g. for refcounted objects!)
      70             :                 This call will block till all current existing transactions was finished.
      71             :                 Follow results occure:
      72             :                     E_INIT        :  All requests on this implementation are refused.
      73             :                                         It's your decision to react in a right way.
      74             : 
      75             :                     E_WORK        :  The object can work now. The full functionality is available.
      76             : 
      77             :                     E_BEFORECLOSE :  The object start the closing mechanism ... but sometimes
      78             :                                         e.g. the dispose() method need to call some private methods.
      79             :                                         These some special methods should use E_SOFTEXCEPTIONS or ignore
      80             :                                         E_INCLOSE as returned reason for E_NOEXCEPTIONS to detect this special case!
      81             : 
      82             :                     E_CLOSE       :  Object is already dead! All further requests will be refused.
      83             :                                         It's your decision to react in a right way.
      84             : 
      85             :     @seealso    -
      86             : 
      87             :     @param      "eMode", is the new mode - but we don't accept setting mode in wrong order!
      88             :     @return     -
      89             : 
      90             :     @onerror    We do nothing.
      91             : *//*-*****************************************************************************************************/
      92        3707 : void  TransactionManager::setWorkingMode( EWorkingMode eMode )
      93             : {
      94             :     // Safe member access.
      95        3707 :     ::osl::ClearableMutexGuard  aAccessGuard( m_aAccessLock );
      96        3707 :     sal_Bool                    bWaitFor    = sal_False      ;
      97             :     // Change working mode first!
      98        3707 :     if  (
      99        7414 :             ( m_eWorkingMode == E_INIT        && eMode == E_WORK        ) ||
     100        4932 :             ( m_eWorkingMode == E_WORK        && eMode == E_BEFORECLOSE ) ||
     101        2466 :             ( m_eWorkingMode == E_BEFORECLOSE && eMode == E_CLOSE       ) ||
     102           0 :             ( m_eWorkingMode == E_CLOSE       && eMode == E_INIT        )
     103             :         )
     104             :     {
     105        3707 :         m_eWorkingMode = eMode;
     106        3707 :         if( m_eWorkingMode == E_BEFORECLOSE || m_eWorkingMode == E_CLOSE )
     107             :         {
     108        2466 :             bWaitFor = sal_True;
     109             :         }
     110             :     }
     111             : 
     112             :     // Wait for current existing transactions then!
     113             :     // (Only necessary for changing to E_BEFORECLOSE or E_CLOSE! ...
     114             :     // otherwise; if you wait at setting E_WORK another thrad could finish a acquire-call during our unlock() and wait() call
     115             :     // ... and we will wait forever here!!!)
     116             :     // Don't forget to release access mutex before.
     117        3707 :     aAccessGuard.clear();
     118        3707 :     if( bWaitFor == sal_True )
     119             :     {
     120        2466 :         m_aBarrier.wait();
     121        3707 :     }
     122        3707 : }
     123             : 
     124             : /*-****************************************************************************************************//**
     125             :     @interface  ITransactionManager
     126             :     @short      get current working mode
     127             :     @descr      If you stand in your close() or init() method ... but don't know
     128             :                 if you called more then ones(!) ... you can use this function to get
     129             :                 right information.
     130             :                 e.g:    You have a method init() which is used to change working mode from
     131             :                         E_INIT to E_WORK and should be used to initialize some member too ...
     132             :                         What should you do:
     133             : 
     134             :                             void init( sal_Int32 nValue )
     135             :                             {
     136             :                                 // Reject this call if our transaction manager say: "Object already initialized!"
     137             :                                 // Otherwise initialize your member.
     138             :                                 if( m_aTransactionManager.getWorkingMode() == E_INIT )
     139             :                                 {
     140             :                                     // Object is uninitialized ...
     141             :                                     // Make member access threadsafe!
     142             :                                     ResetableGuard aGuard( m_aMutex );
     143             : 
     144             :                                     // Check working mode again .. because anozï¿œther instance could be faster.
     145             :                                     // (It's possible to set this guard at first of this method too!)
     146             :                                     if( m_aTransactionManager.getWorkingMode() == E_INIT )
     147             :                                     {
     148             :                                         m_aMember = nValue;
     149             : 
     150             :                                         // Object is initialized now ... set working mode to E_WORK!
     151             :                                         m_aTransactionManager.setWorkingMode( E_WORK );
     152             :                                     }
     153             :                                 }
     154             :                             }
     155             : 
     156             :     @seealso    method setWorkingMode()
     157             : 
     158             :     @param      -
     159             :     @return     Current set mode.
     160             : 
     161             :     @onerror    No error should occure.
     162             : *//*-*****************************************************************************************************/
     163           0 : EWorkingMode TransactionManager::getWorkingMode() const
     164             : {
     165             :     // Synchronize access to internal member!
     166           0 :     ::osl::MutexGuard aAccessLock( m_aAccessLock );
     167           0 :     return m_eWorkingMode;
     168             : }
     169             : 
     170             : /*-****************************************************************************************************//**
     171             :     @interface  ITransactionManager
     172             :     @short      start new transaction
     173             :     @descr      A guard should use this method to start a new transaction. He should looks for rejected
     174             :                 calls to by using parameter eMode and eReason.
     175             :                 If call was not rejected your transaction will be non breakable during releasing your transaction
     176             :                 guard! BUT ... your code isn't threadsafe then! It's a transaction manager only ....
     177             : 
     178             :     @seealso    method unregisterTransaction()
     179             : 
     180             :     @param      "eMode"     ,used to enable/disable throwing exceptions automaticly for rejected calls
     181             :     @param      "eReason"   ,reason for rejected calls if eMode=E_NOEXCEPTIONS
     182             :     @return     -
     183             : 
     184             :     @onerror    -
     185             : *//*-*****************************************************************************************************/
     186      423015 : void  TransactionManager::registerTransaction( EExceptionMode eMode, ERejectReason& eReason ) throw( css::uno::RuntimeException, css::lang::DisposedException )
     187             : {
     188             :     // Look for rejected calls first.
     189             :     // If call was refused we throw some exceptions or do nothing!
     190             :     // It depends from given parameter eMode.
     191      423015 :     if( isCallRejected( eReason ) == sal_True )
     192             :     {
     193        7823 :         impl_throwExceptions( eMode, eReason );
     194             :     }
     195             : 
     196             :     // BUT if no exception was thrown ... (may be eMode = E_SOFTEXCEPTIONS!)
     197             :     // we must register this transaction too!
     198             :     // Don't use "else" or a new scope here!!!
     199             : 
     200             :     // Safe access to internal member.
     201      423006 :     ::osl::MutexGuard aAccessGuard( m_aAccessLock );
     202             : 
     203             :     // Register this new transaction.
     204             :     // If it is the first one .. close gate to disable changing of working mode.
     205      423006 :     ++m_nTransactionCount;
     206      423006 :     if( m_nTransactionCount == 1 )
     207             :     {
     208      292768 :         m_aBarrier.close();
     209      423006 :     }
     210      423006 : }
     211             : 
     212             : /*-****************************************************************************************************//**
     213             :     @interface  ITransactionManager
     214             :     @short      finish transaction
     215             :     @descr      A guard should call this method to release current transaction.
     216             : 
     217             :     @seealso    method registerTransaction()
     218             : 
     219             :     @param      -
     220             :     @return     -
     221             : 
     222             :     @onerror    -
     223             : *//*-*****************************************************************************************************/
     224      423006 : void  TransactionManager::unregisterTransaction() throw( css::uno::RuntimeException, css::lang::DisposedException )
     225             : {
     226             :     // This call could not rejected!
     227             :     // Safe access to internal member.
     228      423006 :     ::osl::MutexGuard aAccessGuard( m_aAccessLock );
     229             : 
     230             :     // Deregister this transaction.
     231             :     // If it was the last one ... open gate to enable changing of working mode!
     232             :     // (see setWorkingMode())
     233             : 
     234      423006 :     --m_nTransactionCount;
     235      423006 :     if( m_nTransactionCount == 0 )
     236             :     {
     237      292768 :         m_aBarrier.open();
     238      423006 :     }
     239      423006 : }
     240             : 
     241             : /*-****************************************************************************************************//**
     242             :     @interface  ITransactionManager
     243             :     @short      look for rejected calls
     244             :     @descr      Sometimes user need a possibility to get information about rejected calls
     245             :                 without starting a transaction!
     246             : 
     247             :     @seealso    -
     248             : 
     249             :     @param      "eReason" returns reason of a rejected call
     250             :     @return     true if call was rejected, false otherwise
     251             : 
     252             :     @onerror    We return false.
     253             : *//*-*****************************************************************************************************/
     254      423015 : sal_Bool  TransactionManager::isCallRejected( ERejectReason& eReason ) const
     255             : {
     256             :     // This call must safe access to internal member only.
     257             :     // Set "possible reason" for return and check reject-state then!
     258             :     // User should look for return value first - reason then ...
     259      423015 :     ::osl::MutexGuard aAccessGuard( m_aAccessLock );
     260      423015 :     switch( m_eWorkingMode )
     261             :     {
     262        7742 :         case E_INIT        : eReason = E_UNINITIALIZED ;
     263        7742 :                                 break;
     264      415192 :         case E_WORK        : eReason = E_NOREASON      ;
     265      415192 :                                 break;
     266          72 :         case E_BEFORECLOSE : eReason = E_INCLOSE       ;
     267          72 :                                 break;
     268           9 :         case E_CLOSE       : eReason = E_CLOSED        ;
     269           9 :                                 break;
     270             :     }
     271      423015 :     return( eReason!=E_NOREASON );
     272             : }
     273             : 
     274             : /*-****************************************************************************************************//**
     275             :     @short      throw any exceptions for rejected calls
     276             :     @descr      If user whish to use our automaticly exception mode we use this impl-method.
     277             :                 We check all combinations of eReason and eExceptionMode and throw right exception with some
     278             :                 descriptions for recipient of it.
     279             : 
     280             :     @seealso    method registerTransaction()
     281             :     @seealso    enum ERejectReason
     282             :     @seealso    enum EExceptionMode
     283             : 
     284             :     @param      "eReason" , reason for rejected call
     285             :     @param      "eMode"   , exception mode - set by user
     286             :     @return     -
     287             : 
     288             :     @onerror    -
     289             : *//*-*****************************************************************************************************/
     290        7823 : void TransactionManager::impl_throwExceptions( EExceptionMode eMode, ERejectReason eReason ) const throw( css::uno::RuntimeException, css::lang::DisposedException )
     291             : {
     292        7823 :     if( eMode != E_NOEXCEPTIONS )
     293             :     {
     294        7823 :         switch( eReason )
     295             :         {
     296             :             case E_UNINITIALIZED   :    if( eMode == E_HARDEXCEPTIONS )
     297             :                                         {
     298             :                                             // Help programmer to find out, why this exception is thrown!
     299             :                                             LOG_ERROR( "TransactionManager...", "Owner instance not right initialized yet. Call was rejected! Normaly it's an algorithm error ... wrong usin of class!" )
     300             :                                             //ATTENTION: temp. disabled - till all bad code positions are detected and changed! */
     301             :                                             // throw css::uno::RuntimeException( DECLARE_ASCII("TransactionManager...\nOwner instance not right initialized yet. Call was rejected! Normaly it's an algorithm error ... wrong usin of class!\n" ), css::uno::Reference< css::uno::XInterface >() );
     302             :                                         }
     303        7742 :                                         break;
     304          72 :             case E_INCLOSE         :    if( eMode == E_HARDEXCEPTIONS )
     305             :                                         {
     306             :                                             // Help programmer to find out, why this exception is thrown!
     307             :                                             LOG_ERROR( "TransactionManager...", "Owner instance stand in close method. Call was rejected!" )
     308           0 :                                             throw css::lang::DisposedException( DECLARE_ASCII("TransactionManager...\nOwner instance stand in close method. Call was rejected!\n" ), css::uno::Reference< css::uno::XInterface >() );
     309             :                                         }
     310          72 :                                         break;
     311             :             case E_CLOSED           :   {
     312             :                                             // Help programmer to find out, why this exception is thrown!
     313             :                                             LOG_ERROR( "TransactionManager...", "Owner instance already closed. Call was rejected!" )
     314           9 :                                             throw css::lang::DisposedException( DECLARE_ASCII("TransactionManager...\nOwner instance already closed. Call was rejected!\n" ), css::uno::Reference< css::uno::XInterface >() );
     315             :                                         }
     316             :             case E_NOREASON         :   {
     317             :                                             // Help programmer to find out
     318             :                                             LOG_ERROR( "TransactionManager...", "Impossible case E_NOREASON!" )
     319             :                                         }
     320           0 :                                         break;
     321           0 :             default:                    break; // nothing to do
     322             :         }
     323             :     }
     324        7814 : }
     325             : 
     326             : }   //  namespace framework
     327             : 
     328             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10