nss: upgrade to release 3.73
[LibreOffice.git] / unotools / source / i18n / readwritemutexguard.cxx
blob48da011b75377504931273cc0a4f9c135fc48c83
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <unotools/readwritemutexguard.hxx>
21 #include <tools/debug.hxx>
23 namespace utl {
25 ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP,
26 ReadWriteGuardMode nRequestMode )
27 : rMutex( rMutexP )
29 // don't do anything until a pending write completed (or another
30 // ReadWriteGuard leaves the ctor phase)
31 ::osl::MutexGuard aGuard( rMutex.maWriteMutex );
32 nMode = nRequestMode;
33 if ( nMode & ReadWriteGuardMode::Write )
35 rMutex.maWriteMutex.acquire();
36 // wait for any read to complete
37 // TODO: set up a waiting thread instead of a loop
38 bool bWait = true;
41 rMutex.maMutex.acquire();
42 bWait = (rMutex.nReadCount != 0);
43 if ( nMode & ReadWriteGuardMode::CriticalChange )
44 bWait |= (rMutex.nBlockCriticalCount != 0);
45 rMutex.maMutex.release();
46 } while ( bWait );
48 else if ( nMode & ReadWriteGuardMode::BlockCritical )
50 rMutex.maMutex.acquire();
51 ++rMutex.nBlockCriticalCount;
52 rMutex.maMutex.release();
54 else
56 rMutex.maMutex.acquire();
57 ++rMutex.nReadCount;
58 rMutex.maMutex.release();
62 ReadWriteGuard::~ReadWriteGuard()
64 if ( nMode & ReadWriteGuardMode::Write )
65 rMutex.maWriteMutex.release();
66 else if ( nMode & ReadWriteGuardMode::BlockCritical )
68 rMutex.maMutex.acquire();
69 --rMutex.nBlockCriticalCount;
70 rMutex.maMutex.release();
72 else
74 rMutex.maMutex.acquire();
75 --rMutex.nReadCount;
76 rMutex.maMutex.release();
80 void ReadWriteGuard::changeReadToWrite()
82 bool bOk = !(nMode & (ReadWriteGuardMode::Write | ReadWriteGuardMode::BlockCritical));
83 DBG_ASSERT( bOk, "ReadWriteGuard::changeReadToWrite: can't" );
84 if ( !bOk )
85 return;
87 // MUST release read before acquiring write mutex or dead lock would
88 // occur if there was a write in another thread waiting for this read
89 // to complete.
90 rMutex.maMutex.acquire();
91 --rMutex.nReadCount;
92 rMutex.maMutex.release();
94 rMutex.maWriteMutex.acquire();
95 nMode |= ReadWriteGuardMode::Write;
96 // wait for any other read to complete
97 // TODO: set up a waiting thread instead of a loop
98 bool bWait = true;
101 rMutex.maMutex.acquire();
102 bWait = (rMutex.nReadCount != 0);
103 rMutex.maMutex.release();
104 } while ( bWait );
107 } // namespace utl
109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */