LCOV - code coverage report
Current view: top level - libreoffice/framework/inc/threadhelp - writeguard.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 29 33 87.9 %
Date: 2012-12-27 Functions: 5 5 100.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             : #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       72959 :         inline WriteGuard( IRWLock& rLock )
      73             :             :   m_pLock ( &rLock    )
      74       72959 :             ,   m_eMode ( E_NOLOCK  )
      75             :         {
      76       72959 :             lock();
      77       72959 :         }
      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       72959 :         inline ~WriteGuard()
      91             :         {
      92       72959 :             unlock();
      93       72959 :         }
      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       92299 :         inline void lock()
     107             :         {
     108       92299 :             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       91908 :                                             m_pLock->acquireWriteAccess();
     114       91908 :                                             m_eMode = E_WRITELOCK;
     115             :                                         }
     116       91908 :                                         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         391 :                 default:                break; // nothing to do
     126             :             }
     127       92299 :         }
     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      147966 :         inline void unlock()
     142             :         {
     143      147966 :             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          63 :                                             m_pLock->releaseReadAccess();
     149          63 :                                             m_eMode = E_NOLOCK;
     150             :                                         }
     151          63 :                                         break;
     152             :                 case E_WRITELOCK    :   {
     153       91845 :                                             m_pLock->releaseWriteAccess();
     154       91845 :                                             m_eMode = E_NOLOCK;
     155             :                                         }
     156       91845 :                                         break;
     157       56058 :                 default:                break; // nothing to do
     158             :             }
     159      147966 :         }
     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          63 :         inline void downgrade()
     174             :         {
     175          63 :             if( m_eMode == E_WRITELOCK )
     176             :             {
     177          63 :                 m_pLock->downgradeWriteAccess();
     178          63 :                 m_eMode = E_READLOCK;
     179             :             }
     180          63 :         }
     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