]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/ReadWriteLock.h
Provide "fake" AtomicWordT implementation for non-SMP configurations.
[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 class ReadWriteLock
15 {
16 public:
17 // default constructor is OK because of shared memory zero-initialization
18
19 bool lockShared(); ///< lock for reading or return false
20 bool lockExclusive(); ///< lock for modification or return false
21 void unlockShared(); ///< undo successful sharedLock()
22 void unlockExclusive(); ///< undo successful exclusiveLock()
23 void switchExclusiveToShared(); ///< stop writing, start reading
24
25 /// adds approximate current stats to the supplied ones
26 void updateStats(ReadWriteLockStats &stats) const;
27
28 public:
29 mutable Atomic::Word readers; ///< number of users trying to read
30 Atomic::Word writers; ///< number of writers trying to modify protected data
31 };
32
33
34 /// approximate stats of a set of ReadWriteLocks
35 class ReadWriteLockStats
36 {
37 public:
38 ReadWriteLockStats();
39
40 void dump(StoreEntry &e) const;
41
42 int count; ///< the total number of locks
43 int readable; ///< number of locks locked for reading
44 int writeable; ///< number of locks locked for writing
45 int idle; ///< number of unlocked locks
46 int readers; ///< sum of lock.readers
47 int writers; ///< sum of lock.writers
48 };
49
50 } // namespace Ipc
51
52 #endif /* SQUID_IPC_READ_WRITE_LOCK_H */