LCOV - code coverage report
Current view: top level - include/unotools - readwritemutexguard.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 8 8 100.0 %
Date: 2015-06-13 12:38:46 Functions: 2 2 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 INCLUDED_UNOTOOLS_READWRITEMUTEXGUARD_HXX
      21             : #define INCLUDED_UNOTOOLS_READWRITEMUTEXGUARD_HXX
      22             : 
      23             : #include <osl/mutex.hxx>
      24             : 
      25             : namespace utl {
      26             : 
      27             : class ReadWriteGuard;
      28             : class ReadWriteMutex
      29             : {
      30             :     friend class ReadWriteGuard;
      31             : 
      32             :             sal_uInt32          nReadCount;
      33             :             sal_uInt32          nBlockCriticalCount;
      34             :             ::osl::Mutex*       pMutex;
      35             :             ::osl::Mutex*       pWriteMutex;
      36             : 
      37             : public:
      38       10037 :                                 ReadWriteMutex()
      39             :                                     : nReadCount(0)
      40             :                                     , nBlockCriticalCount(0)
      41       10037 :                                     , pMutex( new ::osl::Mutex )
      42       20074 :                                     , pWriteMutex( new ::osl::Mutex )
      43       10037 :                                     {}
      44        6405 :                                 ~ReadWriteMutex()
      45             :                                     {
      46        6405 :                                         delete pMutex;
      47        6405 :                                         delete pWriteMutex;
      48        6405 :                                     }
      49             : };
      50             : 
      51             : namespace ReadWriteGuardMode {
      52             : const sal_Int32 nWrite          = 0x01;
      53             : const sal_Int32 nCriticalChange = 0x02 | nWrite;
      54             : const sal_Int32 nBlockCritical  = 0x04;     // only a block, not a read, exclusive flag!
      55             : }
      56             : 
      57             : /** Enable multiple threads to read simultaneously, but a write blocks all
      58             :     other reads and writes, and a read blocks any write.
      59             :     Used in I18N wrappers to be able to maintain a single instance of a wrapper
      60             :     for the standard Office locale.
      61             :     NEVER construct a writing guard if there is already a reading guard in the
      62             :     same context, the following will dead lock EVEN IN THE SAME THREAD!
      63             :     void foo()
      64             :     {
      65             :         ReadWriteGuard aGuard1( aMutex );
      66             :         bar();
      67             :     }
      68             :     void bar()
      69             :     {
      70             :         // waits forever for aGuard1
      71             :         ReadWriteGuard aGuard2( aMutex, ReadWriteGuardMode::nWrite );
      72             :     }
      73             :  */
      74             : class ReadWriteGuard
      75             : {
      76             :             ReadWriteMutex&     rMutex;
      77             :             sal_Int32           nMode;
      78             : public:
      79             :                                 ReadWriteGuard(
      80             :                                     ReadWriteMutex& rMutex,
      81             :                                     sal_Int32 nRequestMode = 0  // read only
      82             :                                     );
      83             :                                 ~ReadWriteGuard();
      84             : 
      85             :     /** Be careful with this, it does wait for ANY read to complete.
      86             :         The following will dead lock EVEN IN THE SAME THREAD!
      87             :         void foo()
      88             :         {
      89             :             ReadWriteGuard aGuard1( aMutex );
      90             :             bar();
      91             :         }
      92             :         void bar()
      93             :         {
      94             :             ReadWriteGuard aGuard2( aMutex );
      95             :             aGuard2.changeReadToWrite();    // waits forever for aGuard1
      96             :         }
      97             :      */
      98             :             void                changeReadToWrite();
      99             : };
     100             : 
     101             : }   // namespace utl
     102             : 
     103             : #endif // INCLUDED_UNOTOOLS_READWRITEMUTEXGUARD_HXX
     104             : 
     105             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11