]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/Lock.h
merge from SslServerCertValidator r12332
[thirdparty/squid.git] / src / base / Lock.h
1 #ifndef SQUID_SRC_BASE_LOCK_H
2 #define SQUID_SRC_BASE_LOCK_H
3
4 /**
5 * This class provides a tracking counter and presents
6 * lock(), unlock() and LockCount() accessors.
7 *
8 * All locks must be cleared with unlock() before this object
9 * is destroyed.
10 *
11 * Accessors provided by this interface are not private,
12 * to allow class hierarchies.
13 *
14 * Build with -DLOCKCOUNT_DEBUG flag to enable lock debugging.
15 * It is disabled by default due to the cost of debug output.
16 */
17 class Lock
18 {
19 public:
20 Lock():count_(0) {}
21
22 virtual ~Lock() { assert(count_ == 0); }
23
24 /// Register one lock / reference against this object.
25 /// All locks must be cleared before it may be destroyed.
26 void lock() const {
27 #if defined(LOCKCOUNT_DEBUG)
28 old_debug(0,1)("Incrementing this %p from count %u\n",this,count_);
29 #endif
30 ++count_;
31 }
32
33 /// Clear one lock / reference against this object.
34 /// All locks must be cleared before it may be destroyed.
35 unsigned unlock() const {
36 #if defined(LOCKCOUNT_DEBUG)
37 old_debug(0,1)("Decrementing this %p from count %u\n",this,count_);
38 #endif
39 assert(count_ > 0);
40 return --count_;
41 }
42
43 /// Inspect the current count of references.
44 unsigned LockCount() const { return count_; }
45
46 private:
47 mutable unsigned count_; ///< number of references currently being tracked
48 };
49
50 // For clarity we provide some aliases for the tracking mechanisms
51 // using Lock so that we can easily see what type of smart pointers
52 // are to be used for the child object.
53 // NP: CbcPointer<> and RefCount<> pointers should be used consistently
54 // for any given child class type
55
56 /// The locking interface for use on Reference-Counted classes
57 #define RefCountable virtual Lock
58
59 #endif /* SQUID_SRC_BASE_LOCK_H */