| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */ |
| 7 | /* |
| 8 | Name |
| 9 | vmrefcnt.cpp - thread-safe reference-counted objects |
| 10 | Function |
| 11 | |
| 12 | Notes |
| 13 | |
| 14 | Modified |
| 15 | 05/24/10 MJRoberts - Creation |
| 16 | */ |
| 17 | |
| 18 | #include "t3std.h" |
| 19 | #include "osifcnet.h" |
| 20 | |
| 21 | /* ------------------------------------------------------------------------ */ |
| 22 | /* |
| 23 | * Weakly Referenceable Object |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * Construction |
| 28 | */ |
| 29 | CVmWeakRef::CVmWeakRef(CVmWeakRefable *r) |
| 30 | { |
| 31 | /* remember the reference */ |
| 32 | ref_ = r; |
| 33 | |
| 34 | /* |
| 35 | * Remember the object's mutex. We share the mutex with the referenced |
| 36 | * object, because our reference conversion (this->ref()) and its |
| 37 | * reference counting (r->release_ref()) both access the same shared |
| 38 | * data (namely our ref_ member). |
| 39 | */ |
| 40 | mu = r->mu; |
| 41 | mu->add_ref(); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * destruction |
| 46 | */ |
| 47 | CVmWeakRef::~CVmWeakRef() |
| 48 | { |
| 49 | mu->release_ref(); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Get the referenced object. This returns a strong reference to our |
| 54 | * weakly referenced object. The caller is responsible for releasing the |
| 55 | * reference that we return when it's done with it. |
| 56 | * |
| 57 | * The operation of checking our reference and incrementing the object's |
| 58 | * reference count must be atomic. This ensures that we're safe even if |
| 59 | * there's another thread that's about to release the last existing strong |
| 60 | * reference on the object. That other thread's release will either |
| 61 | * complete before we check our reference, in which case our reference will |
| 62 | * be nulled out when we check it; OR we'll begin our atomic check/inc |
| 63 | * operation first, in which case the other thread will be locked out until |
| 64 | * after our inc, meaning we now have our own strong reference on the |
| 65 | * object and it won't be deleted until our caller releases the new strong |
| 66 | * reference. |
| 67 | */ |
| 68 | CVmWeakRefable *CVmWeakRef::ref() |
| 69 | { |
| 70 | /* lock the mutex while we're working */ |
| 71 | mu->lock(); |
| 72 | |
| 73 | /* get the object */ |
| 74 | CVmWeakRefable *r = ref_; |
| 75 | |
| 76 | /* |
| 77 | * If we still have a reference to the object, it means it's still in |
| 78 | * memory and still has at least one other referencer. As long as we |
| 79 | * have the mutex locked, it's impossible for anyone else to release |
| 80 | * their reference, so the object definitely will stay around at least |
| 81 | * until we unlock the mutex. |
| 82 | * |
| 83 | * Add a strong reference on behalf of our caller. This ensures that |
| 84 | * the object lives on for at least as long as the caller needs it, |
| 85 | * because whatever else happens, they have a strong reference on it |
| 86 | * starting now. Even if another thread releases the last pre-existing |
| 87 | * reference on the object the moment we unlock the mutex, it doesn't |
| 88 | * matter: before unlocking the mutex, we add our caller's strong |
| 89 | * reference. |
| 90 | */ |
| 91 | if (r != 0) |
| 92 | r->add_ref(); |
| 93 | |
| 94 | /* done with the mutex */ |
| 95 | mu->unlock(); |
| 96 | |
| 97 | /* return the object */ |
| 98 | return r; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /* ------------------------------------------------------------------------ */ |
| 103 | /* |
| 104 | * Weak-referencable object |
| 105 | */ |
| 106 | |
| 107 | /* |
| 108 | * construction |
| 109 | */ |
| 110 | CVmWeakRefable::CVmWeakRefable() |
| 111 | { |
| 112 | /* we don't have a weak ref yet */ |
| 113 | weakref = 0; |
| 114 | |
| 115 | /* create our mutex */ |
| 116 | mu = new OS_Mutex(); |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * destruction |
| 121 | */ |
| 122 | CVmWeakRefable::~CVmWeakRefable() |
| 123 | { |
| 124 | /* done with our mutex */ |
| 125 | mu->release_ref(); |
| 126 | |
| 127 | /* if we have a weak ref object, release it */ |
| 128 | if (weakref != 0) |
| 129 | weakref->release_ref(); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Get a weak reference to this object. This returns a CVmWeakRef pointer |
| 134 | * that can be used to obtain a strong reference to 'this' on demand, but |
| 135 | * which doesn't by itself keep 'this' alive. If the WeakRef pointer is |
| 136 | * dereferenced after 'this' is deleted, the dereference returns null. |
| 137 | */ |
| 138 | CVmWeakRef *CVmWeakRefable::get_weak_ref() |
| 139 | { |
| 140 | /* lock out other updaters */ |
| 141 | mu->lock(); |
| 142 | |
| 143 | /* if we don't already have a weak reference object, create one */ |
| 144 | if (weakref == 0) |
| 145 | weakref = new CVmWeakRef(this); |
| 146 | |
| 147 | /* done with the lock */ |
| 148 | mu->unlock(); |
| 149 | |
| 150 | /* add a reference on behalf of our caller */ |
| 151 | weakref->add_ref(); |
| 152 | |
| 153 | /* return the weak reference object */ |
| 154 | return weakref; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Release a reference. |
| 159 | * |
| 160 | * The reference count update must be atomic with clearing any weak |
| 161 | * reference pointing to us: this ensures that a weak referencer can't |
| 162 | * convert its weak reference into a strong reference after the last |
| 163 | * existing strong reference has performed a release, which initiates |
| 164 | * deletion. Any weak referencer that gets in just before we begin the |
| 165 | * atomic dec/clear operation will be safe, because it will obtain its |
| 166 | * strong reference, which increments our reference count, BEFORE we do our |
| 167 | * own decrement. Thus the reference count will not reach zero in that |
| 168 | * case until the new strong reference created by the weak reference |
| 169 | * conversion is itself released. |
| 170 | */ |
| 171 | void CVmWeakRefable::release_ref() |
| 172 | { |
| 173 | /* lock the object while we're working */ |
| 174 | mu->lock(); |
| 175 | |
| 176 | /* decrement the reference counter */ |
| 177 | int del = FALSE; |
| 178 | if (cnt.dec() == 0) |
| 179 | { |
| 180 | /* if we have a weak referencer, clear its pointer to me */ |
| 181 | if (weakref != 0) |
| 182 | weakref->clear_ref(); |
| 183 | |
| 184 | /* note that it's time to delete the object */ |
| 185 | del = TRUE; |
| 186 | } |
| 187 | |
| 188 | /* done with our lock */ |
| 189 | mu->unlock(); |
| 190 | |
| 191 | /* if we're unreferenced, delete myself */ |
| 192 | if (del) |
| 193 | delete this; |
| 194 | } |
| 195 |