LCOV - code coverage report
Current view: top level - unotools/source/i18n - readwritemutexguard.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 47 47 100.0 %
Date: 2014-11-03 Functions: 3 3 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             : #include <unotools/readwritemutexguard.hxx>
      21             : #include <tools/debug.hxx>
      22             : 
      23             : namespace utl {
      24             : 
      25     7724453 : ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP,
      26             :             sal_Int32 nRequestMode )
      27     7724453 :         : rMutex( rMutexP )
      28             : {
      29             :     // don't do anything until a pending write completed (or another
      30             :     // ReadWriteGuard leaves the ctor phase)
      31     7724453 :     ::osl::MutexGuard aGuard( rMutex.pWriteMutex );
      32     7724453 :     nMode = nRequestMode;
      33     7724453 :     if ( nMode & ReadWriteGuardMode::nWrite )
      34             :     {
      35       17091 :         rMutex.pWriteMutex->acquire();
      36             :         // wait for any read to complete
      37             : // TODO: set up a waiting thread instead of a loop
      38       17091 :         bool bWait = true;
      39       17091 :         do
      40             :         {
      41       17091 :             rMutex.pMutex->acquire();
      42       17091 :             bWait = (rMutex.nReadCount != 0);
      43       17091 :             if ( nMode & ReadWriteGuardMode::nCriticalChange )
      44       17091 :                 bWait |= (rMutex.nBlockCriticalCount != 0);
      45       17091 :             rMutex.pMutex->release();
      46             :         } while ( bWait );
      47             :     }
      48     7707362 :     else if ( nMode & ReadWriteGuardMode::nBlockCritical )
      49             :     {
      50      136052 :         rMutex.pMutex->acquire();
      51      136052 :         ++rMutex.nBlockCriticalCount;
      52      136052 :         rMutex.pMutex->release();
      53             :     }
      54             :     else
      55             :     {
      56     7571310 :         rMutex.pMutex->acquire();
      57     7571310 :         ++rMutex.nReadCount;
      58     7571310 :         rMutex.pMutex->release();
      59     7724453 :     }
      60     7724453 : }
      61             : 
      62     7724453 : ReadWriteGuard::~ReadWriteGuard()
      63             : {
      64     7724453 :     if ( nMode & ReadWriteGuardMode::nWrite )
      65       81963 :         rMutex.pWriteMutex->release();
      66     7642490 :     else if ( nMode & ReadWriteGuardMode::nBlockCritical )
      67             :     {
      68      136052 :         rMutex.pMutex->acquire();
      69      136052 :         --rMutex.nBlockCriticalCount;
      70      136052 :         rMutex.pMutex->release();
      71             :     }
      72             :     else
      73             :     {
      74     7506438 :         rMutex.pMutex->acquire();
      75     7506438 :         --rMutex.nReadCount;
      76     7506438 :         rMutex.pMutex->release();
      77             :     }
      78     7724453 : }
      79             : 
      80       64872 : void ReadWriteGuard::changeReadToWrite()
      81             : {
      82       64872 :     bool bOk = !(nMode & (ReadWriteGuardMode::nWrite | ReadWriteGuardMode::nBlockCritical));
      83             :     DBG_ASSERT( bOk, "ReadWriteGuard::changeReadToWrite: can't" );
      84       64872 :     if ( bOk )
      85             :     {
      86             :         // MUST release read before acquiring write mutex or dead lock would
      87             :         // occur if there was a write in another thread waiting for this read
      88             :         // to complete.
      89       64872 :         rMutex.pMutex->acquire();
      90       64872 :         --rMutex.nReadCount;
      91       64872 :         rMutex.pMutex->release();
      92             : 
      93       64872 :         rMutex.pWriteMutex->acquire();
      94       64872 :         nMode |= ReadWriteGuardMode::nWrite;
      95             :         // wait for any other read to complete
      96             : // TODO: set up a waiting thread instead of a loop
      97       64872 :         bool bWait = true;
      98       64872 :         do
      99             :         {
     100       64872 :             rMutex.pMutex->acquire();
     101       64872 :             bWait = (rMutex.nReadCount != 0);
     102       64872 :             rMutex.pMutex->release();
     103             :         } while ( bWait );
     104             :     }
     105       64872 : }
     106             : 
     107             : }   // namespace utl
     108             : 
     109             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10