Squid Web Cache master
Loading...
Searching...
No Matches
ReadWriteLock.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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_SRC_IPC_READWRITELOCK_H
10#define SQUID_SRC_IPC_READWRITELOCK_H
11
12#include <atomic>
13#include <iosfwd>
14
15class StoreEntry;
16
17namespace Ipc
18{
19
20class ReadWriteLockStats;
21
27{
28public:
30 {}
31
32 bool lockShared();
33 bool lockExclusive();
34 bool lockHeaders();
35 void unlockShared();
36 void unlockExclusive();
37 void unlockHeaders();
42
43 void startAppending();
44
49
51 void updateStats(ReadWriteLockStats &stats) const;
52
53public:
54 mutable std::atomic<uint32_t> readers;
55 std::atomic<bool> writing;
56 std::atomic<bool> appending;
57 std::atomic_flag updating;
58
59private:
60 bool finalizeExclusive();
61
62 mutable std::atomic<uint32_t> readLevel;
63 std::atomic<uint32_t> writeLevel;
64};
65
67std::ostream &operator <<(std::ostream &, const ReadWriteLock &);
68
71{
72public:
74
75 void dump(StoreEntry &e) const;
76
77 int count;
80 int idle;
81 int readers;
82 int writers;
84};
85
89void AssertFlagIsSet(std::atomic_flag &flag);
90
91} // namespace Ipc
92
93#endif /* SQUID_SRC_IPC_READWRITELOCK_H */
94
approximate stats of a set of ReadWriteLocks
int readable
number of locks locked for reading
int idle
number of unlocked locks
int writers
sum of lock.writers
int count
the total number of locks
void dump(StoreEntry &e) const
int writeable
number of locks locked for writing
int readers
sum of lock.readers
int appenders
number of appending writers
void unlockHeaders()
undo successful lockHeaders()
std::atomic< uint32_t > readLevel
number of users reading (or trying to)
bool lockHeaders()
lock for [readable] metadata update or return false
std::atomic< uint32_t > readers
number of reading users
void unlockExclusive()
undo successful exclusiveLock()
bool lockExclusive()
lock for modification or return false
bool stopAppendingAndRestoreExclusive()
std::atomic_flag updating
a reader is updating metadata/headers
std::atomic< uint32_t > writeLevel
number of users writing (or trying to write)
std::atomic< bool > writing
there is a writing user (there can be at most 1)
bool unlockSharedAndSwitchToExclusive()
std::atomic< bool > appending
the writer has promised to only append
void unlockShared()
undo successful sharedLock()
bool lockShared()
lock for reading or return false
void updateStats(ReadWriteLockStats &stats) const
adds approximate current stats to the supplied ones
void startAppending()
writer keeps its lock but also allows reading
void AssertFlagIsSet(std::atomic_flag &flag)
std::ostream & operator<<(std::ostream &os, const QuestionerId &qid)