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