LCOV - code coverage report
Current view: top level - framework/source/fwi/threadhelp - transactionmanager.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 55 60 91.7 %
Date: 2012-08-25 Functions: 7 9 77.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 52 94 55.3 %

           Branch data     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                 :       4634 : TransactionManager::TransactionManager()
      43                 :            :     : m_eWorkingMode      ( E_INIT )
      44 [ +  - ][ +  - ]:       4634 :     , m_nTransactionCount ( 0      )
      45                 :            : {
      46         [ +  - ]:       4634 :     m_aBarrier.open();
      47                 :       4634 : }
      48                 :            : 
      49                 :            : /*-************************************************************************************************************//**
      50                 :            :     @short      standard dtor
      51                 :            :     @descr      -
      52                 :            : 
      53                 :            :     @seealso    -
      54                 :            : 
      55                 :            :     @param      -
      56                 :            :     @return     -
      57                 :            : 
      58                 :            :     @onerror    -
      59                 :            : *//*-*************************************************************************************************************/
      60 [ +  - ][ +  - ]:       4252 : TransactionManager::~TransactionManager()
      61                 :            : {
      62         [ -  + ]:       4252 : }
      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                 :       6653 : void  TransactionManager::setWorkingMode( EWorkingMode eMode )
      93                 :            : {
      94                 :            :     // Safe member access.
      95         [ +  - ]:       6653 :     ::osl::ClearableMutexGuard  aAccessGuard( m_aAccessLock );
      96                 :       6653 :     sal_Bool                    bWaitFor    = sal_False      ;
      97                 :            :     // Change working mode first!
      98 [ +  + ][ -  + ]:       6653 :     if  (
         [ +  + ][ -  + ]
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
      99                 :            :             ( m_eWorkingMode == E_INIT        && eMode == E_WORK        ) ||
     100                 :            :             ( m_eWorkingMode == E_WORK        && eMode == E_BEFORECLOSE ) ||
     101                 :            :             ( m_eWorkingMode == E_BEFORECLOSE && eMode == E_CLOSE       ) ||
     102                 :            :             ( m_eWorkingMode == E_CLOSE       && eMode == E_INIT        )
     103                 :            :         )
     104                 :            :     {
     105                 :       6653 :         m_eWorkingMode = eMode;
     106 [ +  + ][ +  + ]:       6653 :         if( m_eWorkingMode == E_BEFORECLOSE || m_eWorkingMode == E_CLOSE )
     107                 :            :         {
     108                 :       4336 :             bWaitFor = sal_True;
     109                 :            :         }
     110                 :            :     }
     111                 :            : 
     112                 :            :     // Wait for current existing transactions then!
     113                 :            :     // (Only neccessary 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         [ +  - ]:       6653 :     aAccessGuard.clear();
     118         [ +  + ]:       6653 :     if( bWaitFor == sal_True )
     119                 :            :     {
     120         [ +  - ]:       4336 :         m_aBarrier.wait();
     121         [ +  - ]:       6653 :     }
     122                 :       6653 : }
     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                 :     782975 : 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 [ +  - ][ +  + ]:     782975 :     if( isCallRejected( eReason ) == sal_True )
     192                 :            :     {
     193         [ +  + ]:      14537 :         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         [ +  - ]:     782958 :     ::osl::MutexGuard aAccessGuard( m_aAccessLock );
     202                 :            : 
     203                 :            :     #ifdef ENABLE_MUTEXDEBUG
     204                 :            :     LOG_ASSERT2( m_nTransactionCount<0, "TransactionManager::acquire()", "Wrong ref count detected!" )
     205                 :            :     #endif
     206                 :            : 
     207                 :            :     // Register this new transaction.
     208                 :            :     // If it is the first one .. close gate to disable changing of working mode.
     209                 :     782958 :     ++m_nTransactionCount;
     210         [ +  + ]:     782958 :     if( m_nTransactionCount == 1 )
     211                 :            :     {
     212         [ +  - ]:     550769 :         m_aBarrier.close();
     213         [ +  - ]:     782975 :     }
     214                 :     782958 : }
     215                 :            : 
     216                 :            : /*-****************************************************************************************************//**
     217                 :            :     @interface  ITransactionManager
     218                 :            :     @short      finish transaction
     219                 :            :     @descr      A guard should call this method to release current transaction.
     220                 :            : 
     221                 :            :     @seealso    method registerTransaction()
     222                 :            : 
     223                 :            :     @param      -
     224                 :            :     @return     -
     225                 :            : 
     226                 :            :     @onerror    -
     227                 :            : *//*-*****************************************************************************************************/
     228                 :     782958 : void  TransactionManager::unregisterTransaction() throw( css::uno::RuntimeException, css::lang::DisposedException )
     229                 :            : {
     230                 :            :     // This call could not rejected!
     231                 :            :     // Safe access to internal member.
     232         [ +  - ]:     782958 :     ::osl::MutexGuard aAccessGuard( m_aAccessLock );
     233                 :            : 
     234                 :            :     #ifdef ENABLE_MUTEXDEBUG
     235                 :            :     LOG_ASSERT2( m_nTransactionCount<=0, "TransactionManager::release()", "Wrong ref count detected!" )
     236                 :            :     #endif
     237                 :            : 
     238                 :            :     // Deregister this transaction.
     239                 :            :     // If it was the last one ... open gate to enable changing of working mode!
     240                 :            :     // (see setWorkingMode())
     241                 :            : 
     242                 :     782958 :     --m_nTransactionCount;
     243         [ +  + ]:     782958 :     if( m_nTransactionCount == 0 )
     244                 :            :     {
     245         [ +  - ]:     550769 :         m_aBarrier.open();
     246         [ +  - ]:     782958 :     }
     247                 :     782958 : }
     248                 :            : 
     249                 :            : /*-****************************************************************************************************//**
     250                 :            :     @interface  ITransactionManager
     251                 :            :     @short      look for rejected calls
     252                 :            :     @descr      Sometimes user need a possibility to get information about rejected calls
     253                 :            :                 without starting a transaction!
     254                 :            : 
     255                 :            :     @seealso    -
     256                 :            : 
     257                 :            :     @param      "eReason" returns reason of a rejected call
     258                 :            :     @return     true if call was rejected, false otherwise
     259                 :            : 
     260                 :            :     @onerror    We return false.
     261                 :            : *//*-*****************************************************************************************************/
     262                 :     782975 : sal_Bool  TransactionManager::isCallRejected( ERejectReason& eReason ) const
     263                 :            : {
     264                 :            :     // This call must safe access to internal member only.
     265                 :            :     // Set "possible reason" for return and check reject-state then!
     266                 :            :     // User should look for return value first - reason then ...
     267         [ +  - ]:     782975 :     ::osl::MutexGuard aAccessGuard( m_aAccessLock );
     268   [ +  +  +  +  :     782975 :     switch( m_eWorkingMode )
                      - ]
     269                 :            :     {
     270                 :      14315 :         case E_INIT        : eReason = E_UNINITIALIZED ;
     271                 :      14315 :                                 break;
     272                 :     768438 :         case E_WORK        : eReason = E_NOREASON      ;
     273                 :     768438 :                                 break;
     274                 :        205 :         case E_BEFORECLOSE : eReason = E_INCLOSE       ;
     275                 :        205 :                                 break;
     276                 :         17 :         case E_CLOSE       : eReason = E_CLOSED        ;
     277                 :         17 :                                 break;
     278                 :            :     }
     279         [ +  - ]:     782975 :     return( eReason!=E_NOREASON );
     280                 :            : }
     281                 :            : 
     282                 :            : /*-****************************************************************************************************//**
     283                 :            :     @short      throw any exceptions for rejected calls
     284                 :            :     @descr      If user whish to use our automaticly exception mode we use this impl-method.
     285                 :            :                 We check all combinations of eReason and eExceptionMode and throw right exception with some
     286                 :            :                 descriptions for recipient of it.
     287                 :            : 
     288                 :            :     @seealso    method registerTransaction()
     289                 :            :     @seealso    enum ERejectReason
     290                 :            :     @seealso    enum EExceptionMode
     291                 :            : 
     292                 :            :     @param      "eReason" , reason for rejected call
     293                 :            :     @param      "eMode"   , exception mode - set by user
     294                 :            :     @return     -
     295                 :            : 
     296                 :            :     @onerror    -
     297                 :            : *//*-*****************************************************************************************************/
     298                 :      14537 : void TransactionManager::impl_throwExceptions( EExceptionMode eMode, ERejectReason eReason ) const throw( css::uno::RuntimeException, css::lang::DisposedException )
     299                 :            : {
     300         [ +  - ]:      14537 :     if( eMode != E_NOEXCEPTIONS )
     301                 :            :     {
     302   [ +  +  +  -  :      14537 :         switch( eReason )
                      - ]
     303                 :            :         {
     304                 :            :             case E_UNINITIALIZED   :    if( eMode == E_HARDEXCEPTIONS )
     305                 :            :                                         {
     306                 :            :                                             // Help programmer to find out, why this exception is thrown!
     307                 :            :                                             LOG_ERROR( "TransactionManager...", "Owner instance not right initialized yet. Call was rejected! Normaly it's an algorithm error ... wrong usin of class!" )
     308                 :            :                                             //ATTENTION: temp. disabled - till all bad code positions are detected and changed! */
     309                 :            :                                             // 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 >() );
     310                 :            :                                         }
     311                 :      14315 :                                         break;
     312         [ -  + ]:        205 :             case E_INCLOSE         :    if( eMode == E_HARDEXCEPTIONS )
     313                 :            :                                         {
     314                 :            :                                             // Help programmer to find out, why this exception is thrown!
     315                 :            :                                             LOG_ERROR( "TransactionManager...", "Owner instance stand in close method. Call was rejected!" )
     316 [ #  # ][ #  # ]:          0 :                                             throw css::lang::DisposedException( DECLARE_ASCII("TransactionManager...\nOwner instance stand in close method. Call was rejected!\n" ), css::uno::Reference< css::uno::XInterface >() );
     317                 :            :                                         }
     318                 :        205 :                                         break;
     319                 :            :             case E_CLOSED           :   {
     320                 :            :                                             // Help programmer to find out, why this exception is thrown!
     321                 :            :                                             LOG_ERROR( "TransactionManager...", "Owner instance already closed. Call was rejected!" )
     322 [ +  - ][ +  - ]:         17 :                                             throw css::lang::DisposedException( DECLARE_ASCII("TransactionManager...\nOwner instance already closed. Call was rejected!\n" ), css::uno::Reference< css::uno::XInterface >() );
     323                 :            :                                         }
     324                 :            :             case E_NOREASON         :   {
     325                 :            :                                             // Help programmer to find out
     326                 :            :                                             LOG_ERROR( "TransactionManager...", "Impossible case E_NOREASON!" )
     327                 :            :                                         }
     328                 :          0 :                                         break;
     329                 :      14520 :             default:                    break; // nothing to do
     330                 :            :         }
     331                 :            :     }
     332                 :      14520 : }
     333                 :            : 
     334                 :            : }   //  namespace framework
     335                 :            : 
     336                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10