]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/ReadWriteLock.h
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_IPC_READ_WRITE_LOCK_H
10 #define SQUID_IPC_READ_WRITE_LOCK_H
20 class ReadWriteLockStats
;
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.
29 ReadWriteLock() : readers(0), writing(false), appending(false), readLevel(0), writeLevel(0)
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();
43 void startAppending(); ///< writer keeps its lock but also allows reading
45 /// adds approximate current stats to the supplied ones
46 void updateStats(ReadWriteLockStats
&stats
) const;
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
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)
59 /// dumps approximate lock state (for debugging)
60 std::ostream
&operator <<(std::ostream
&os
, const Ipc::ReadWriteLock
&);
62 /// approximate stats of a set of ReadWriteLocks
63 class ReadWriteLockStats
68 void dump(StoreEntry
&e
) const;
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
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
);
86 #endif /* SQUID_IPC_READ_WRITE_LOCK_H */