]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/ReadWriteLock.h
1 #ifndef SQUID_IPC_READ_WRITE_LOCK_H
2 #define SQUID_IPC_READ_WRITE_LOCK_H
4 #include "ipc/AtomicWord.h"
11 class ReadWriteLockStats
;
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.
20 // default constructor is OK because of shared memory zero-initialization
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
28 void startAppending(); ///< writer keeps its lock but also allows reading
30 /// adds approximate current stats to the supplied ones
31 void updateStats(ReadWriteLockStats
&stats
) const;
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
39 mutable Atomic::Word readLevel
; ///< number of users reading (or trying to)
40 Atomic::Word writeLevel
; ///< number of users writing (or trying to write)
43 /// approximate stats of a set of ReadWriteLocks
44 class ReadWriteLockStats
49 void dump(StoreEntry
&e
) const;
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
62 #endif /* SQUID_IPC_READ_WRITE_LOCK_H */