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