]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/ReadWriteLock.h
Merged from trunk 13199.
[thirdparty/squid.git] / src / ipc / ReadWriteLock.h
1 #ifndef SQUID_IPC_READ_WRITE_LOCK_H
2 #define SQUID_IPC_READ_WRITE_LOCK_H
3
4 #include "ipc/AtomicWord.h"
5
6 class StoreEntry;
7
8 namespace Ipc
9 {
10
11 class ReadWriteLockStats;
12
13 /// an atomic readers-writer or shared-exclusive lock suitable for maps/tables
14 /// Also supports reading-while-appending mode when readers and writer are
15 /// allowed to access the same locked object because the writer promisses
16 /// to only append new data and all size-related object properties are atomic.
17 class ReadWriteLock
18 {
19 public:
20 // default constructor is OK because of shared memory zero-initialization
21
22 bool lockShared(); ///< lock for reading or return false
23 bool lockExclusive(); ///< lock for modification or return false
24 void unlockShared(); ///< undo successful sharedLock()
25 void unlockExclusive(); ///< undo successful exclusiveLock()
26 void switchExclusiveToShared(); ///< stop writing, start reading
27
28 void startAppending(); ///< writer keeps its lock but also allows reading
29
30 /// adds approximate current stats to the supplied ones
31 void updateStats(ReadWriteLockStats &stats) const;
32
33 public:
34 mutable Atomic::Word readers; ///< number of reading users
35 Atomic::Word writing; ///< there is a writing user (there can be at most 1)
36 Atomic::Word appending; ///< the writer has promissed to only append
37
38 private:
39 mutable Atomic::Word readLevel; ///< number of users reading (or trying to)
40 Atomic::Word writeLevel; ///< number of users writing (or trying to write)
41 };
42
43 /// approximate stats of a set of ReadWriteLocks
44 class ReadWriteLockStats
45 {
46 public:
47 ReadWriteLockStats();
48
49 void dump(StoreEntry &e) const;
50
51 int count; ///< the total number of locks
52 int readable; ///< number of locks locked for reading
53 int writeable; ///< number of locks locked for writing
54 int idle; ///< number of unlocked locks
55 int readers; ///< sum of lock.readers
56 int writers; ///< sum of lock.writers
57 int appenders; ///< number of appending writers
58 };
59
60 } // namespace Ipc
61
62 #endif /* SQUID_IPC_READ_WRITE_LOCK_H */