Bump version to 6.4-15
[LibreOffice.git] / include / unotools / readwritemutexguard.hxx
bloba7c6d5799ae4a675168c3f9961e16867be9c1bb5
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 #ifndef INCLUDED_UNOTOOLS_READWRITEMUTEXGUARD_HXX
21 #define INCLUDED_UNOTOOLS_READWRITEMUTEXGUARD_HXX
23 #include <osl/mutex.hxx>
24 #include <o3tl/typed_flags_set.hxx>
26 enum class ReadWriteGuardMode {
27 ReadOnly = 0x00,
28 Write = 0x01,
29 CriticalChange = 0x02 | Write,
30 BlockCritical = 0x04, // only a block, not a read, exclusive flag!
32 namespace o3tl {
33 template<> struct typed_flags<ReadWriteGuardMode> : is_typed_flags<ReadWriteGuardMode, 0x7> {};
36 namespace utl {
38 class ReadWriteMutex
40 friend class ReadWriteGuard;
42 sal_uInt32 nReadCount;
43 sal_uInt32 nBlockCriticalCount;
44 ::osl::Mutex maMutex;
45 ::osl::Mutex maWriteMutex;
47 public:
48 ReadWriteMutex()
49 : nReadCount(0)
50 , nBlockCriticalCount(0)
54 /** Enable multiple threads to read simultaneously, but a write blocks all
55 other reads and writes, and a read blocks any write.
56 Used in I18N wrappers to be able to maintain a single instance of a wrapper
57 for the standard Office locale.
58 NEVER construct a writing guard if there is already a reading guard in the
59 same context, the following will dead lock EVEN IN THE SAME THREAD!
60 void foo()
62 ReadWriteGuard aGuard1( aMutex );
63 bar();
65 void bar()
67 // waits forever for aGuard1
68 ReadWriteGuard aGuard2( aMutex, ReadWriteGuardMode::nWrite );
71 class ReadWriteGuard
73 ReadWriteMutex& rMutex;
74 ReadWriteGuardMode nMode;
75 public:
76 ReadWriteGuard(
77 ReadWriteMutex& rMutex,
78 ReadWriteGuardMode nRequestMode = ReadWriteGuardMode::ReadOnly // read only
80 ~ReadWriteGuard();
82 /** Be careful with this, it does wait for ANY read to complete.
83 The following will dead lock EVEN IN THE SAME THREAD!
84 void foo()
86 ReadWriteGuard aGuard1( aMutex );
87 bar();
89 void bar()
91 ReadWriteGuard aGuard2( aMutex );
92 aGuard2.changeReadToWrite(); // waits forever for aGuard1
95 void changeReadToWrite();
98 } // namespace utl
100 #endif // INCLUDED_UNOTOOLS_READWRITEMUTEXGUARD_HXX
102 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */