LCOV - code coverage report
Current view: top level - framework/inc/threadhelp - writeguard.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 29 33 87.9 %
Date: 2012-08-25 Functions: 5 5 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 6 8 75.0 %

           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                 :            : #ifndef __FRAMEWORK_THREADHELP_WRITEGUARD_HXX_
      21                 :            : #define __FRAMEWORK_THREADHELP_WRITEGUARD_HXX_
      22                 :            : 
      23                 :            : #include <threadhelp/inoncopyable.h>
      24                 :            : #include <threadhelp/irwlock.h>
      25                 :            : 
      26                 :            : 
      27                 :            : namespace framework{
      28                 :            : 
      29                 :            : /*-************************************************************************************************************//**
      30                 :            :     @short          implement a guard to set write locks
      31                 :            :     @descr          This guard should be used to set a lock for reading AND writing object internal member.
      32                 :            :                     We never need a own mutex to safe our internal member access - because
      33                 :            :                     a guard is used as function-local member only. There exist no multithreaded access to it realy ...
      34                 :            : 
      35                 :            :     @attention      a) To prevent us against wrong using, the default ctor, copy ctor and the =operator are maked private!
      36                 :            :                     b) Use interface "IRWLock" of set LockHelper only - because we must support a finer granularity of locking.
      37                 :            :                        Interface "IMutex" should be used by easier guard implementations ... like "ResetableGuard"!
      38                 :            : 
      39                 :            :     @implements     -
      40                 :            :     @base           INonCopyable
      41                 :            : 
      42                 :            :     @devstatus      ready to use
      43                 :            : *//*-*************************************************************************************************************/
      44                 :            : class WriteGuard : private INonCopyable
      45                 :            : {
      46                 :            :     //-------------------------------------------------------------------------------------------------------------
      47                 :            :     //  public methods
      48                 :            :     //-------------------------------------------------------------------------------------------------------------
      49                 :            :     public:
      50                 :            : 
      51                 :            :         /*-****************************************************************************************************//**
      52                 :            :             @short      ctor
      53                 :            :             @descr      These ctors initialize the guard with a reference to used lock member of object to protect.
      54                 :            :                         Null isn't allowed as value!
      55                 :            : 
      56                 :            :             @seealso    -
      57                 :            : 
      58                 :            :             @param      "pLock" ,reference to used lock member of object to protect
      59                 :            :             @param      "rLock" ,reference to used lock member of object to protect
      60                 :            :             @return     -
      61                 :            : 
      62                 :            :             @onerror    -
      63                 :            :         *//*-*****************************************************************************************************/
      64                 :            :         inline WriteGuard( IRWLock* pLock )
      65                 :            :             :   m_pLock ( pLock     )
      66                 :            :             ,   m_eMode ( E_NOLOCK  )
      67                 :            :         {
      68                 :            :             lock();
      69                 :            :         }
      70                 :            : 
      71                 :            :         //*********************************************************************************************************
      72                 :     834088 :         inline WriteGuard( IRWLock& rLock )
      73                 :            :             :   m_pLock ( &rLock    )
      74                 :     834088 :             ,   m_eMode ( E_NOLOCK  )
      75                 :            :         {
      76                 :     834088 :             lock();
      77                 :     834088 :         }
      78                 :            : 
      79                 :            :         /*-****************************************************************************************************//**
      80                 :            :             @short      dtor
      81                 :            :             @descr      We unlock the used lock member automaticly if user forget it.
      82                 :            : 
      83                 :            :             @seealso    -
      84                 :            : 
      85                 :            :             @param      -
      86                 :            :             @return     -
      87                 :            : 
      88                 :            :             @onerror    -
      89                 :            :         *//*-*****************************************************************************************************/
      90                 :     834088 :         inline ~WriteGuard()
      91                 :            :         {
      92                 :     834088 :             unlock();
      93                 :     834088 :         }
      94                 :            : 
      95                 :            :         /*-****************************************************************************************************//**
      96                 :            :             @short      set write lock
      97                 :            :             @descr      Call this method to set the write lock. The call will block till all current threads are synchronized!
      98                 :            : 
      99                 :            :             @seealso    method unlock()
     100                 :            : 
     101                 :            :             @param      -
     102                 :            :             @return     -
     103                 :            : 
     104                 :            :             @onerror    -
     105                 :            :         *//*-*****************************************************************************************************/
     106                 :     975165 :         inline void lock()
     107                 :            :         {
     108      [ +  -  + ]:     975165 :             switch( m_eMode )
     109                 :            :             {
     110                 :            :                 case E_NOLOCK       :   {
     111                 :            :                                             // Acquire write access and set return state.
     112                 :            :                                             // Mode is set later if it was successful!
     113                 :     972984 :                                             m_pLock->acquireWriteAccess();
     114                 :     972984 :                                             m_eMode = E_WRITELOCK;
     115                 :            :                                         }
     116                 :     972984 :                                         break;
     117                 :            :                 case E_READLOCK     :   {
     118                 :            :                                             // User has downgrade to read access before!
     119                 :            :                                             // We must release it before we can set a new write access!
     120                 :          0 :                                             m_pLock->releaseReadAccess();
     121                 :          0 :                                             m_pLock->acquireWriteAccess();
     122                 :          0 :                                             m_eMode = E_WRITELOCK;
     123                 :            :                                         }
     124                 :          0 :                                         break;
     125                 :       2181 :                 default:                break; // nothing to do
     126                 :            :             }
     127                 :     975165 :         }
     128                 :            : 
     129                 :            :         /*-****************************************************************************************************//**
     130                 :            :             @short      unset write lock
     131                 :            :             @descr      Call this method to unlock the rw-lock temp.!
     132                 :            :                         Normaly we do it at dtor automaticly for you ...
     133                 :            : 
     134                 :            :             @seealso    method lock()
     135                 :            : 
     136                 :            :             @param      -
     137                 :            :             @return     -
     138                 :            : 
     139                 :            :             @onerror    -
     140                 :            :         *//*-*****************************************************************************************************/
     141                 :    1462066 :         inline void unlock()
     142                 :            :         {
     143      [ +  +  + ]:    1462066 :             switch( m_eMode )
     144                 :            :             {
     145                 :            :                 case E_READLOCK     :   {
     146                 :            :                                             // User has downgraded to a read lock before!
     147                 :            :                                             // => There isn't realy a write lock ...
     148                 :       1656 :                                             m_pLock->releaseReadAccess();
     149                 :       1656 :                                             m_eMode = E_NOLOCK;
     150                 :            :                                         }
     151                 :       1656 :                                         break;
     152                 :            :                 case E_WRITELOCK    :   {
     153                 :     971328 :                                             m_pLock->releaseWriteAccess();
     154                 :     971328 :                                             m_eMode = E_NOLOCK;
     155                 :            :                                         }
     156                 :     971328 :                                         break;
     157                 :     489082 :                 default:                break; // nothing to do
     158                 :            :             }
     159                 :    1462066 :         }
     160                 :            : 
     161                 :            :         /*-****************************************************************************************************//**
     162                 :            :             @short      downgrade write access to read access without new blocking!
     163                 :            :             @descr      If this write lock is set you can change it to a "read lock".
     164                 :            :                         An "upgrade" is the same like new calling "lock()"!
     165                 :            : 
     166                 :            :             @seealso    -
     167                 :            : 
     168                 :            :             @param      -
     169                 :            :             @return     -
     170                 :            : 
     171                 :            :             @onerror    -
     172                 :            :         *//*-*****************************************************************************************************/
     173                 :       1656 :         inline void downgrade()
     174                 :            :         {
     175         [ +  - ]:       1656 :             if( m_eMode == E_WRITELOCK )
     176                 :            :             {
     177                 :       1656 :                 m_pLock->downgradeWriteAccess();
     178                 :       1656 :                 m_eMode = E_READLOCK;
     179                 :            :             }
     180                 :       1656 :         }
     181                 :            : 
     182                 :            :         /*-****************************************************************************************************//**
     183                 :            :             @short      return internal states
     184                 :            :             @descr      For user they dont know what they are doing ...
     185                 :            : 
     186                 :            :             @seealso    -
     187                 :            : 
     188                 :            :             @param      -
     189                 :            :             @return     Current set lock mode.
     190                 :            : 
     191                 :            :             @onerror    No error should occure.
     192                 :            :         *//*-*****************************************************************************************************/
     193                 :            :         inline ELockMode getMode() const
     194                 :            :         {
     195                 :            :             return m_eMode;
     196                 :            :         }
     197                 :            : 
     198                 :            :     //-------------------------------------------------------------------------------------------------------------
     199                 :            :     //  private methods
     200                 :            :     //-------------------------------------------------------------------------------------------------------------
     201                 :            :     private:
     202                 :            : 
     203                 :            :         /*-****************************************************************************************************//**
     204                 :            :             @short      disable using of these functions!
     205                 :            :             @descr      It's not allowed to use this methods. Different problem can occure otherwise.
     206                 :            :                         Thats why we disable it by make it private.
     207                 :            : 
     208                 :            :             @seealso    other ctor
     209                 :            : 
     210                 :            :             @param      -
     211                 :            :             @return     -
     212                 :            : 
     213                 :            :             @onerror    -
     214                 :            :         *//*-*****************************************************************************************************/
     215                 :            :         WriteGuard();
     216                 :            : 
     217                 :            :     //-------------------------------------------------------------------------------------------------------------
     218                 :            :     //  private member
     219                 :            :     //-------------------------------------------------------------------------------------------------------------
     220                 :            :     private:
     221                 :            : 
     222                 :            :         IRWLock*    m_pLock ;   /// reference to lock-member of protected object
     223                 :            :         ELockMode   m_eMode ;   /// protection against multiple lock calls without unlock and difference between supported lock modi
     224                 :            : 
     225                 :            : };      //  class WriteGuard
     226                 :            : 
     227                 :            : }       //  namespace framework
     228                 :            : 
     229                 :            : #endif  //  #ifndef __FRAMEWORK_THREADHELP_WRITEGUARD_HXX_
     230                 :            : 
     231                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10