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