]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ipc/ReadWriteLock.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / ipc / ReadWriteLock.cc
CommitLineData
44c95fcf 1/*
44c95fcf
AR
2 * DEBUG: section 54 Interprocess Communication
3 */
4
582c2af2 5#include "squid.h"
44c95fcf 6#include "ipc/ReadWriteLock.h"
602d9612 7#include "Store.h"
44c95fcf
AR
8
9bool
10Ipc::ReadWriteLock::lockShared()
11{
12 ++readers; // this locks "new" writers out
13 if (!writers) // there are no old writers
14 return true;
15 --readers;
16 return false;
17}
18
19bool
20Ipc::ReadWriteLock::lockExclusive()
21{
22 if (!writers++) { // we are the first writer + this locks "new" readers out
23 if (!readers) // there are no old readers
24 return true;
25 }
26 --writers;
27 return false;
28}
29
30void
31Ipc::ReadWriteLock::unlockShared()
32{
33 assert(readers-- > 0);
34}
35
36void
37Ipc::ReadWriteLock::unlockExclusive()
38{
39 assert(writers-- > 0);
40}
41
42void
43Ipc::ReadWriteLock::switchExclusiveToShared()
44{
45 ++readers; // must be done before we release exclusive control
46 unlockExclusive();
47}
48
49void
50Ipc::ReadWriteLock::updateStats(ReadWriteLockStats &stats) const
51{
52 if (readers) {
53 ++stats.readable;
54 stats.readers += readers;
55 } else if (writers) {
56 ++stats.writeable;
57 stats.writers += writers;
58 } else {
59 ++stats.idle;
60 }
61 ++stats.count;
62}
63
44c95fcf
AR
64/* Ipc::ReadWriteLockStats */
65
66Ipc::ReadWriteLockStats::ReadWriteLockStats()
67{
68 memset(this, 0, sizeof(*this));
69}
9199139f 70
44c95fcf
AR
71void
72Ipc::ReadWriteLockStats::dump(StoreEntry &e) const
73{
74 storeAppendPrintf(&e, "Available locks: %9d\n", count);
75
76 if (!count)
77 return;
78
79 storeAppendPrintf(&e, "Reading: %9d %6.2f%%\n",
9199139f 80 readable, (100.0 * readable / count));
44c95fcf 81 storeAppendPrintf(&e, "Writing: %9d %6.2f%%\n",
9199139f 82 writeable, (100.0 * writeable / count));
44c95fcf 83 storeAppendPrintf(&e, "Idle: %9d %6.2f%%\n",
9199139f 84 idle, (100.0 * idle / count));
44c95fcf
AR
85
86 if (readers || writers) {
87 const int locked = readers + writers;
88 storeAppendPrintf(&e, "Readers: %9d %6.2f%%\n",
9199139f 89 readers, (100.0 * readers / locked));
44c95fcf 90 storeAppendPrintf(&e, "Writers: %9d %6.2f%%\n",
9199139f 91 writers, (100.0 * writers / locked));
44c95fcf
AR
92 }
93}