]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/ReadWriteLock.h
paranoid_hit_validation directive (#559)
[thirdparty/squid.git] / src / ipc / ReadWriteLock.h
1 /*
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_IPC_READ_WRITE_LOCK_H
10 #define SQUID_IPC_READ_WRITE_LOCK_H
11
12 #include <atomic>
13 #include <iosfwd>
14
15 class StoreEntry;
16
17 namespace Ipc
18 {
19
20 class ReadWriteLockStats;
21
22 /// an atomic readers-writer or shared-exclusive lock suitable for maps/tables
23 /// Also supports reading-while-appending mode when readers and writer are
24 /// allowed to access the same locked object because the writer promisses
25 /// to only append new data and all size-related object properties are atomic.
26 class ReadWriteLock
27 {
28 public:
29 ReadWriteLock() : readers(0), writing(false), appending(false), readLevel(0), writeLevel(0)
30 {}
31
32 bool lockShared(); ///< lock for reading or return false
33 bool lockExclusive(); ///< lock for modification or return false
34 bool lockHeaders(); ///< lock for [readable] metadata update or return false
35 void unlockShared(); ///< undo successful sharedLock()
36 void unlockExclusive(); ///< undo successful exclusiveLock()
37 void unlockHeaders(); ///< undo successful lockHeaders()
38 void switchExclusiveToShared(); ///< stop writing, start reading
39 /// same as unlockShared() but also attempts to get a writer lock beforehand
40 /// \returns whether the writer lock was acquired
41 bool unlockSharedAndSwitchToExclusive();
42
43 void startAppending(); ///< writer keeps its lock but also allows reading
44
45 /// adds approximate current stats to the supplied ones
46 void updateStats(ReadWriteLockStats &stats) const;
47
48 public:
49 mutable std::atomic<uint32_t> readers; ///< number of reading users
50 std::atomic<bool> writing; ///< there is a writing user (there can be at most 1)
51 std::atomic<bool> appending; ///< the writer has promised to only append
52 std::atomic_flag updating; ///< a reader is updating metadata/headers
53
54 private:
55 mutable std::atomic<uint32_t> readLevel; ///< number of users reading (or trying to)
56 std::atomic<uint32_t> writeLevel; ///< number of users writing (or trying to write)
57 };
58
59 /// dumps approximate lock state (for debugging)
60 std::ostream &operator <<(std::ostream &os, const Ipc::ReadWriteLock &);
61
62 /// approximate stats of a set of ReadWriteLocks
63 class ReadWriteLockStats
64 {
65 public:
66 ReadWriteLockStats();
67
68 void dump(StoreEntry &e) const;
69
70 int count; ///< the total number of locks
71 int readable; ///< number of locks locked for reading
72 int writeable; ///< number of locks locked for writing
73 int idle; ///< number of unlocked locks
74 int readers; ///< sum of lock.readers
75 int writers; ///< sum of lock.writers
76 int appenders; ///< number of appending writers
77 };
78
79 /// Same as assert(flag is set): The process assert()s if flag is not set.
80 /// Side effect: The unset flag becomes set just before we assert().
81 /// Needed because atomic_flag cannot be compared with a boolean.
82 void AssertFlagIsSet(std::atomic_flag &flag);
83
84 } // namespace Ipc
85
86 #endif /* SQUID_IPC_READ_WRITE_LOCK_H */
87