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

Generated by: LCOV version 1.10