| 1 | // | 
|---|
| 2 | // ref.cc --- implementation of the reference counting classes | 
|---|
| 3 | // | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc. | 
|---|
| 5 | // | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com> | 
|---|
| 7 | // Maintainer: LPS | 
|---|
| 8 | // | 
|---|
| 9 | // This file is part of the SC Toolkit. | 
|---|
| 10 | // | 
|---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify | 
|---|
| 12 | // it under the terms of the GNU Library General Public License as published by | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 14 | // any later version. | 
|---|
| 15 | // | 
|---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful, | 
|---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 19 | // GNU Library General Public License for more details. | 
|---|
| 20 | // | 
|---|
| 21 | // You should have received a copy of the GNU Library General Public License | 
|---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to | 
|---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 24 | // | 
|---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7. | 
|---|
| 26 | // | 
|---|
| 27 |  | 
|---|
| 28 | #ifdef __GNUC__ | 
|---|
| 29 | #pragma implementation | 
|---|
| 30 | #endif | 
|---|
| 31 |  | 
|---|
| 32 | #include <typeinfo> | 
|---|
| 33 |  | 
|---|
| 34 | #include <util/ref/ref.h> | 
|---|
| 35 | #include <util/misc/exenv.h> | 
|---|
| 36 |  | 
|---|
| 37 | using namespace std; | 
|---|
| 38 |  | 
|---|
| 39 | #if REF_USE_LOCKS | 
|---|
| 40 |  | 
|---|
| 41 | #if HAVE_STHREAD | 
|---|
| 42 |  | 
|---|
| 43 | #include <synch.h> | 
|---|
| 44 | #include <thread.h> | 
|---|
| 45 |  | 
|---|
| 46 | typedef mutex_t sc_lock_t; | 
|---|
| 47 |  | 
|---|
| 48 | static unsigned int __base_nlock__; | 
|---|
| 49 | static int __base_locker__; | 
|---|
| 50 |  | 
|---|
| 51 | static int | 
|---|
| 52 | __init_lock__(sc_lock_t* inLock, int inCount) | 
|---|
| 53 | { | 
|---|
| 54 | for (int i=0; i < inCount; i++) | 
|---|
| 55 | mutex_init(&inLock[i], USYNC_THREAD, 0); | 
|---|
| 56 | return 1; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | #define __LOCK(l) mutex_lock(&l) | 
|---|
| 60 | #define __UNLOCK(l) mutex_unlock(&l) | 
|---|
| 61 |  | 
|---|
| 62 | #elif HAVE_PTHREAD | 
|---|
| 63 |  | 
|---|
| 64 | #include <pthread.h> | 
|---|
| 65 |  | 
|---|
| 66 | typedef pthread_mutex_t sc_lock_t; | 
|---|
| 67 |  | 
|---|
| 68 | static int | 
|---|
| 69 | __init_lock__(sc_lock_t* inLock, int inCount) | 
|---|
| 70 | { | 
|---|
| 71 | for (int i=0; i < inCount; i++) | 
|---|
| 72 | pthread_mutex_init(&inLock[i], 0); | 
|---|
| 73 | return 1; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | #define __LOCK(l) pthread_mutex_lock(&l) | 
|---|
| 77 | #define __UNLOCK(l) pthread_mutex_unlock(&l) | 
|---|
| 78 |  | 
|---|
| 79 | #elif HAVE_CREATETHREAD | 
|---|
| 80 |  | 
|---|
| 81 | #include <windows.h> | 
|---|
| 82 |  | 
|---|
| 83 | typedef HANDLE sc_lock_t; | 
|---|
| 84 |  | 
|---|
| 85 | HANDLE __base_lock__ = 0; | 
|---|
| 86 |  | 
|---|
| 87 | static int | 
|---|
| 88 | __init_lock__(sc_lock_t* inLock, int inCount) | 
|---|
| 89 | { | 
|---|
| 90 | for (int i=0; i < inCount; i++) | 
|---|
| 91 | inLock[i] = CreateMutex(0, FALSE, 0); | 
|---|
| 92 | return 1; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | // windows threads are recursive, so no fanciness is required | 
|---|
| 96 | #define __LOCK(l) WaitForSingleObject(l, INFINITE) | 
|---|
| 97 | #define __UNLOCK(l) ReleaseMutex(l) | 
|---|
| 98 |  | 
|---|
| 99 | #else /* !PTHREAD && !STHREAD  && !CREATETHREAD */ | 
|---|
| 100 |  | 
|---|
| 101 | #define __LOCK(l) 0 | 
|---|
| 102 | #define __UNLOCK(l) 0 | 
|---|
| 103 |  | 
|---|
| 104 | #endif /* HAVE_STHREAD */ | 
|---|
| 105 |  | 
|---|
| 106 | /* | 
|---|
| 107 | * this is the number of locks to use in the round-robin. | 
|---|
| 108 | * since an unsigned char is used for the lock handle, | 
|---|
| 109 | * this cannot be greater than 255. | 
|---|
| 110 | */ | 
|---|
| 111 | #define NLOCKS 128 | 
|---|
| 112 |  | 
|---|
| 113 | static sc_lock_t sRefLocks[NLOCKS]; | 
|---|
| 114 | static int sRefLocksInit = __init_lock__(sRefLocks, NLOCKS); | 
|---|
| 115 | static unsigned char sRefLock = 0; | 
|---|
| 116 |  | 
|---|
| 117 | #else /* !REF_USE_LOCKS */ | 
|---|
| 118 |  | 
|---|
| 119 | #define __LOCK(l) 0 | 
|---|
| 120 | #define __UNLOCK(l) 0 | 
|---|
| 121 |  | 
|---|
| 122 | #endif /* !REF_USE_LOCKS */ | 
|---|
| 123 |  | 
|---|
| 124 | using namespace sc; | 
|---|
| 125 |  | 
|---|
| 126 | int | 
|---|
| 127 | RefCount::lock_ptr() const | 
|---|
| 128 | { | 
|---|
| 129 | #if REF_USE_LOCKS | 
|---|
| 130 | if (ref_lock_ == 0xff) | 
|---|
| 131 | return 1; | 
|---|
| 132 | return __LOCK(sRefLocks[ref_lock_]); | 
|---|
| 133 | #else | 
|---|
| 134 | return 1; | 
|---|
| 135 | #endif | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | int | 
|---|
| 139 | RefCount::unlock_ptr() const | 
|---|
| 140 | { | 
|---|
| 141 | #if REF_USE_LOCKS | 
|---|
| 142 | if (ref_lock_ == 0xff) | 
|---|
| 143 | return 1; | 
|---|
| 144 | return __UNLOCK(sRefLocks[ref_lock_]); | 
|---|
| 145 | #else | 
|---|
| 146 | return 1; | 
|---|
| 147 | #endif | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | void | 
|---|
| 151 | RefCount::use_locks(bool inVal) | 
|---|
| 152 | { | 
|---|
| 153 | #if REF_USE_LOCKS | 
|---|
| 154 | if (inVal) { | 
|---|
| 155 | ref_lock_ = sRefLock; | 
|---|
| 156 | unsigned char tmp_sRefLock = sRefLock+1; | 
|---|
| 157 | if (tmp_sRefLock >= NLOCKS) | 
|---|
| 158 | tmp_sRefLock = 0; | 
|---|
| 159 | sRefLock = tmp_sRefLock; | 
|---|
| 160 | } | 
|---|
| 161 | else | 
|---|
| 162 | ref_lock_ = 0xff; | 
|---|
| 163 | #endif | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 |  | 
|---|
| 167 | void | 
|---|
| 168 | RefCount::error(const char * w) const | 
|---|
| 169 | { | 
|---|
| 170 | ExEnv::errn() << "RefCount: ERROR: " << w << endl; | 
|---|
| 171 | ExEnv::errn() << "The type name is " << typeid(*this).name() | 
|---|
| 172 | << std::endl; | 
|---|
| 173 | abort(); | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | void | 
|---|
| 177 | RefCount::too_many_refs() const | 
|---|
| 178 | { | 
|---|
| 179 | error("Too many refs."); | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | void | 
|---|
| 183 | RefCount::not_enough_refs() const | 
|---|
| 184 | { | 
|---|
| 185 | error("Ref count dropped below zero."); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | RefCount::~RefCount() | 
|---|
| 189 | { | 
|---|
| 190 | #if REF_MANAGE | 
|---|
| 191 | if (managed() && nreference()) { | 
|---|
| 192 | error("Deleting a referenced object."); | 
|---|
| 193 | } | 
|---|
| 194 | #endif | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | /////////////////////////////////////////////////////////////////////// | 
|---|
| 198 |  | 
|---|
| 199 | void | 
|---|
| 200 | RefBase::warn ( const char * msg) const | 
|---|
| 201 | { | 
|---|
| 202 | ExEnv::errn() << "WARNING: " << msg << endl; | 
|---|
| 203 | } | 
|---|
| 204 | void | 
|---|
| 205 | RefBase::warn_ref_to_stack() const | 
|---|
| 206 | { | 
|---|
| 207 | warn("Ref: creating a reference to stack data"); | 
|---|
| 208 | } | 
|---|
| 209 | void | 
|---|
| 210 | RefBase::warn_skip_stack_delete() const | 
|---|
| 211 | { | 
|---|
| 212 | warn("Ref: skipping delete of object on the stack"); | 
|---|
| 213 | } | 
|---|
| 214 | void | 
|---|
| 215 | RefBase::warn_bad_ref_count() const | 
|---|
| 216 | { | 
|---|
| 217 | warn("Ref: bad reference count in referenced object\n"); | 
|---|
| 218 | } | 
|---|
| 219 | void | 
|---|
| 220 | RefBase::ref_info(RefCount*p, ostream& os) const | 
|---|
| 221 | { | 
|---|
| 222 | if (p) | 
|---|
| 223 | os << "nreference() = " << p->nreference() << endl; | 
|---|
| 224 | else | 
|---|
| 225 | os << "reference is null" << endl; | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | void | 
|---|
| 229 | RefBase::require_nonnull() const | 
|---|
| 230 | { | 
|---|
| 231 | if (parentpointer() == 0) { | 
|---|
| 232 | ExEnv::errn() << "RefBase: needed a nonnull pointer but got null" | 
|---|
| 233 | << endl; | 
|---|
| 234 | abort(); | 
|---|
| 235 | } | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | RefBase::~RefBase() | 
|---|
| 239 | { | 
|---|
| 240 | } | 
|---|
| 241 |  | 
|---|
| 242 | void | 
|---|
| 243 | RefBase::check_pointer() const | 
|---|
| 244 | { | 
|---|
| 245 | if (parentpointer() && parentpointer()->nreference() <= 0) { | 
|---|
| 246 | warn_bad_ref_count(); | 
|---|
| 247 | } | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | void | 
|---|
| 251 | RefBase::ref_info(ostream& os) const | 
|---|
| 252 | { | 
|---|
| 253 | RefBase::ref_info(parentpointer(),os); | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | void | 
|---|
| 257 | RefBase::reference(RefCount *p) | 
|---|
| 258 | { | 
|---|
| 259 | if (p) { | 
|---|
| 260 | #if REF_CHECK_STACK | 
|---|
| 261 | if (DO_REF_CHECK_STACK(p)) { | 
|---|
| 262 | DO_REF_UNMANAGE(p); | 
|---|
| 263 | warn_ref_to_stack(); | 
|---|
| 264 | } | 
|---|
| 265 | #endif | 
|---|
| 266 | p->reference(); | 
|---|
| 267 | } | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | int | 
|---|
| 271 | RefBase::dereference(RefCount *p) | 
|---|
| 272 | { | 
|---|
| 273 | if (p) | 
|---|
| 274 | return p->dereference(); | 
|---|
| 275 | else | 
|---|
| 276 | return -1; | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 280 |  | 
|---|
| 281 | // Local Variables: | 
|---|
| 282 | // mode: c++ | 
|---|
| 283 | // c-file-style: "CLJ" | 
|---|
| 284 | // End: | 
|---|