| 1 | /* $Header$ */ |
| 2 | |
| 3 | /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */ |
| 4 | /* |
| 5 | Name |
| 6 | vmrefcnt.h - thread-safe reference-counted objects |
| 7 | Function |
| 8 | Defines classes for reference counting, including weak references. |
| 9 | Notes |
| 10 | This header needs to be "interleaved" with the osifcnet.h class |
| 11 | definitions. The classes we define here depend on the OS-specific |
| 12 | thread-safe counter object already being defined in-line, while other |
| 13 | OS-specific classes depend on our classes. This header must thus be |
| 14 | #included within the OS-specific networking header after defining the |
| 15 | counter class but before defining any of the other classes. |
| 16 | Modified |
| 17 | 05/24/10 MJRoberts - Creation |
| 18 | */ |
| 19 | |
| 20 | #ifndef VMREFCNT_H |
| 21 | #define VMREFCNT_H |
| 22 | |
| 23 | |
| 24 | /* ------------------------------------------------------------------------ */ |
| 25 | /* |
| 26 | * Reference-counted object. This is the base class for objects with |
| 27 | * memory management by reference count. Construction sets the initial |
| 28 | * reference count to 1, on behalf of the caller that performed the 'new' |
| 29 | * to create the object. Callers must manually release references as they |
| 30 | * go out of scope. When the reference count reaches zero, the object is |
| 31 | * destroyed. |
| 32 | */ |
| 33 | class CVmRefCntObj |
| 34 | { |
| 35 | public: |
| 36 | /* |
| 37 | * on construction, set the counter to 1, to count the initial |
| 38 | * reference on behalf of the caller that performed the 'new' |
| 39 | */ |
| 40 | CVmRefCntObj() : cnt(1) { } |
| 41 | |
| 42 | /* add a reference */ |
| 43 | void add_ref() { cnt.inc(); } |
| 44 | |
| 45 | /* |
| 46 | * release a reference; automatically deletes the object when the |
| 47 | * reference count reaches zero |
| 48 | */ |
| 49 | void release_ref() |
| 50 | { |
| 51 | if (cnt.dec() == 0) |
| 52 | delete this; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * debugging add/remove ref - print a console message showing where the |
| 57 | * add/remove came from |
| 58 | */ |
| 59 | void add_ref(void *from, const char *msg) |
| 60 | { |
| 61 | add_ref(); |
| 62 | printf("add_ref(this=%llx, cnt=%ld, from=%llx, %s)\n", |
| 63 | (unsigned long long)this, cnt.get(), (unsigned long long)from, msg); |
| 64 | } |
| 65 | void release_ref(void *from, const char *msg) |
| 66 | { |
| 67 | printf("release_ref(this=%llx, cnt=%ld, from=%llx, %s)\n", |
| 68 | (unsigned long long)this, cnt.get()-1, (unsigned long long)from, msg); |
| 69 | release_ref(); |
| 70 | } |
| 71 | |
| 72 | /* get the current reference count */ |
| 73 | long get_ref_count() const { return cnt.get(); } |
| 74 | |
| 75 | protected: |
| 76 | /* |
| 77 | * the destructor must be virtual because this class is designed for |
| 78 | * subclassing, and we invoke the destructor from this base class |
| 79 | */ |
| 80 | virtual ~CVmRefCntObj() { } |
| 81 | |
| 82 | /* reference counter */ |
| 83 | OS_Counter cnt; |
| 84 | }; |
| 85 | |
| 86 | /* ------------------------------------------------------------------------ */ |
| 87 | /* |
| 88 | * Weak reference. This is a reference to a WeakRefable object that |
| 89 | * doesn't count in the reference counting for the referenced object. |
| 90 | */ |
| 91 | class CVmWeakRef: public CVmRefCntObj |
| 92 | { |
| 93 | friend class CVmWeakRefable; |
| 94 | |
| 95 | public: |
| 96 | /* |
| 97 | * Get the referenced object. If the object is still alive, this |
| 98 | * creates a new strong reference to the object and returns the strong |
| 99 | * reference. The caller is responsible for releasing the returned |
| 100 | * strong reference when it's done with the reference. If the object |
| 101 | * has already been deleted, this returns null. |
| 102 | */ |
| 103 | class CVmWeakRefable *ref(); |
| 104 | |
| 105 | /* |
| 106 | * Test the reference to see if the object is still alive. Returns |
| 107 | * true if the object is alive, false if not. |
| 108 | * |
| 109 | * If this returns true, there's of course no guarantee that the object |
| 110 | * won't be deleted at any time. If this returns false, however, you |
| 111 | * can be certain that it will return false (and ref() will return nil) |
| 112 | * from now on, because it means the underlying object has been |
| 113 | * deleted. test() is thus useful for cases where you only need to |
| 114 | * know for sure that an object has been deleted, such as when |
| 115 | * periodically cleaning up a cache containing weak references. When |
| 116 | * you need to use the object after determining if it's alive, you |
| 117 | * should use ref() instead, since that atomically tests to see if the |
| 118 | * object is alive and creates a strong reference to it if it is. |
| 119 | */ |
| 120 | int test() { return ref_ != 0; } |
| 121 | |
| 122 | protected: |
| 123 | CVmWeakRef(class CVmWeakRefable *r); |
| 124 | |
| 125 | virtual ~CVmWeakRef(); |
| 126 | |
| 127 | /* |
| 128 | * Clear our reference. The referenced object calls this when it's |
| 129 | * about to be deleted, to let us know that we can no longer access it. |
| 130 | * |
| 131 | * The referenced object only calls this after locking the mutex. |
| 132 | */ |
| 133 | void clear_ref() { ref_ = 0; } |
| 134 | |
| 135 | /* the object we weakly reference */ |
| 136 | class CVmWeakRefable *ref_; |
| 137 | |
| 138 | /* the mutex we share with the referenced object */ |
| 139 | class OS_Mutex *mu; |
| 140 | }; |
| 141 | |
| 142 | /* |
| 143 | * Weak-referenceable object. This is a specialized reference-counted |
| 144 | * object that can have weak references in addition to regular strong |
| 145 | * references. A weak reference is a reference that doesn't prevent the |
| 146 | * object from being deleted, but still allows access as long as the object |
| 147 | * is alive. |
| 148 | */ |
| 149 | class CVmWeakRefable: public CVmRefCntObj |
| 150 | { |
| 151 | friend class CVmWeakRef; |
| 152 | |
| 153 | public: |
| 154 | CVmWeakRefable(); |
| 155 | |
| 156 | /* release a reference */ |
| 157 | void release_ref(); |
| 158 | |
| 159 | /* |
| 160 | * Get the weak reference to this object |
| 161 | */ |
| 162 | CVmWeakRef *get_weak_ref(); |
| 163 | |
| 164 | /* debugging version */ |
| 165 | void release_ref(void *from, const char *msg) |
| 166 | { |
| 167 | printf("release_ref(this=%llx, cnt=%ld, from=%llx, %s)\n", |
| 168 | (unsigned long long)this, cnt.get()-1, (unsigned long long)from, msg); |
| 169 | release_ref(); |
| 170 | } |
| 171 | |
| 172 | protected: |
| 173 | virtual ~CVmWeakRefable(); |
| 174 | |
| 175 | /* mutex protecting against concurrent access */ |
| 176 | class OS_Mutex *mu; |
| 177 | |
| 178 | /* our weak reference object */ |
| 179 | CVmWeakRef *weakref; |
| 180 | }; |
| 181 | |
| 182 | #endif /* VMREFCNT_H */ |