| [5328] | 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| [1529] | 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| [51] | 13 | |
|---|
| [2] | 14 | #ifndef OSG_REFERENCED |
|---|
| 15 | #define OSG_REFERENCED 1 |
|---|
| 16 | |
|---|
| [3565] | 17 | // When building OSG with Java need to derive from Noodle::CBridgable class, |
|---|
| [2] | 18 | #include <osg/Export> |
|---|
| [4021] | 19 | |
|---|
| [3857] | 20 | #include <OpenThreads/ScopedLock> |
|---|
| [3436] | 21 | #include <OpenThreads/Mutex> |
|---|
| [3157] | 22 | |
|---|
| [8465] | 23 | #include <OpenThreads/Atomic> |
|---|
| 24 | #if !defined(_OPENTHREADS_ATOMIC_USE_MUTEX) |
|---|
| 25 | # define _OSG_REFERENCED_USE_ATOMIC_OPERATIONS |
|---|
| 26 | #endif |
|---|
| [6086] | 27 | |
|---|
| [2] | 28 | namespace osg { |
|---|
| 29 | |
|---|
| [5014] | 30 | // forward declare, declared after Referenced below. |
|---|
| [1414] | 31 | class DeleteHandler; |
|---|
| [5014] | 32 | class Observer; |
|---|
| [1414] | 33 | |
|---|
| [2] | 34 | /** Base class from providing referencing counted objects.*/ |
|---|
| [4021] | 35 | class OSG_EXPORT Referenced |
|---|
| [2] | 36 | { |
|---|
| 37 | |
|---|
| 38 | public: |
|---|
| [3436] | 39 | |
|---|
| 40 | |
|---|
| [3855] | 41 | Referenced(); |
|---|
| 42 | |
|---|
| [5125] | 43 | explicit Referenced(bool threadSafeRefUnref); |
|---|
| 44 | |
|---|
| [3855] | 45 | Referenced(const Referenced&); |
|---|
| [2] | 46 | |
|---|
| [2383] | 47 | inline Referenced& operator = (const Referenced&) { return *this; } |
|---|
| [2] | 48 | |
|---|
| [3855] | 49 | /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/ |
|---|
| [5885] | 50 | virtual void setThreadSafeRefUnref(bool threadSafe); |
|---|
| [1414] | 51 | |
|---|
| [3855] | 52 | /** Get whether a mutex is used to ensure ref() and unref() are thread safe.*/ |
|---|
| [8465] | 53 | |
|---|
| 54 | #if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS) |
|---|
| 55 | bool getThreadSafeRefUnref() const { return true; } |
|---|
| 56 | #else |
|---|
| [3855] | 57 | bool getThreadSafeRefUnref() const { return _refMutex!=0; } |
|---|
| [8465] | 58 | #endif |
|---|
| [1414] | 59 | |
|---|
| [7332] | 60 | /** Get the mutex used to ensure thread safety of ref()/unref(). */ |
|---|
| [8465] | 61 | #if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS) |
|---|
| [9023] | 62 | OpenThreads::Mutex* getRefMutex() const { return getGlobalReferencedMutex(); } |
|---|
| [8465] | 63 | #else |
|---|
| [7332] | 64 | OpenThreads::Mutex* getRefMutex() const { return _refMutex; } |
|---|
| [8465] | 65 | #endif |
|---|
| [7332] | 66 | |
|---|
| [9023] | 67 | /** Get the optional global Referenced mutex, this can be shared between all osg::Referenced.*/ |
|---|
| 68 | static OpenThreads::Mutex* getGlobalReferencedMutex(); |
|---|
| 69 | |
|---|
| [3855] | 70 | /** Increment the reference count by one, indicating that |
|---|
| [2] | 71 | this object has another pointer which is referencing it.*/ |
|---|
| [3857] | 72 | inline void ref() const; |
|---|
| [460] | 73 | |
|---|
| [3855] | 74 | /** Decrement the reference count by one, indicating that |
|---|
| [2] | 75 | a pointer to this object is referencing it. If the |
|---|
| [39] | 76 | reference count goes to zero, it is assumed that this object |
|---|
| 77 | is no longer referenced and is automatically deleted.*/ |
|---|
| [3857] | 78 | inline void unref() const; |
|---|
| [460] | 79 | |
|---|
| [3855] | 80 | /** Decrement the reference count by one, indicating that |
|---|
| [460] | 81 | a pointer to this object is referencing it. However, do |
|---|
| 82 | not delete it, even if ref count goes to 0. Warning, unref_nodelete() |
|---|
| 83 | should only be called if the user knows exactly who will |
|---|
| [7648] | 84 | be responsible for, one should prefer unref() over unref_nodelete() |
|---|
| [460] | 85 | as the later can lead to memory leaks.*/ |
|---|
| [3436] | 86 | void unref_nodelete() const; |
|---|
| [460] | 87 | |
|---|
| [3855] | 88 | /** Return the number pointers currently referencing this object. */ |
|---|
| [1133] | 89 | inline int referenceCount() const { return _refCount; } |
|---|
| [2] | 90 | |
|---|
| [7648] | 91 | /** Add a Observer that is observing this object, notify the Observer when this object gets deleted.*/ |
|---|
| [9549] | 92 | void addObserver(Observer* observer) const; |
|---|
| [3436] | 93 | |
|---|
| [7648] | 94 | /** Add a Observer that is observing this object, notify the Observer when this object gets deleted.*/ |
|---|
| [9549] | 95 | void removeObserver(Observer* observer) const; |
|---|
| [5014] | 96 | |
|---|
| [3436] | 97 | public: |
|---|
| 98 | |
|---|
| 99 | /** Set whether reference counting should be use a mutex to create thread reference counting.*/ |
|---|
| 100 | static void setThreadSafeReferenceCounting(bool enableThreadSafeReferenceCounting); |
|---|
| 101 | |
|---|
| 102 | /** Get whether reference counting is active.*/ |
|---|
| 103 | static bool getThreadSafeReferenceCounting(); |
|---|
| 104 | |
|---|
| 105 | friend class DeleteHandler; |
|---|
| 106 | |
|---|
| 107 | /** Set a DeleteHandler to which deletion of all referenced counted objects |
|---|
| 108 | * will be delegated to.*/ |
|---|
| 109 | static void setDeleteHandler(DeleteHandler* handler); |
|---|
| 110 | |
|---|
| 111 | /** Get a DeleteHandler.*/ |
|---|
| 112 | static DeleteHandler* getDeleteHandler(); |
|---|
| 113 | |
|---|
| [1497] | 114 | |
|---|
| [2] | 115 | protected: |
|---|
| [6086] | 116 | |
|---|
| [359] | 117 | virtual ~Referenced(); |
|---|
| [1414] | 118 | |
|---|
| [7038] | 119 | void deleteUsingDeleteHandler() const; |
|---|
| [8465] | 120 | |
|---|
| 121 | #if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS) |
|---|
| 122 | struct ObserverSetData; |
|---|
| 123 | |
|---|
| [9549] | 124 | mutable OpenThreads::AtomicPtr _observerSetDataPtr; |
|---|
| [8465] | 125 | |
|---|
| 126 | mutable OpenThreads::Atomic _refCount; |
|---|
| 127 | #else |
|---|
| [6086] | 128 | |
|---|
| [5014] | 129 | mutable OpenThreads::Mutex* _refMutex; |
|---|
| [3436] | 130 | |
|---|
| [5014] | 131 | mutable int _refCount; |
|---|
| [3157] | 132 | |
|---|
| [9549] | 133 | mutable void* _observers; |
|---|
| [8465] | 134 | #endif |
|---|
| [2] | 135 | }; |
|---|
| 136 | |
|---|
| [3857] | 137 | inline void Referenced::ref() const |
|---|
| 138 | { |
|---|
| [8465] | 139 | #if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS) |
|---|
| 140 | ++_refCount; |
|---|
| 141 | #else |
|---|
| [3857] | 142 | if (_refMutex) |
|---|
| 143 | { |
|---|
| 144 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex); |
|---|
| 145 | ++_refCount; |
|---|
| 146 | } |
|---|
| 147 | else |
|---|
| 148 | { |
|---|
| 149 | ++_refCount; |
|---|
| 150 | } |
|---|
| [8465] | 151 | #endif |
|---|
| [3857] | 152 | } |
|---|
| 153 | |
|---|
| 154 | inline void Referenced::unref() const |
|---|
| 155 | { |
|---|
| [8465] | 156 | #if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS) |
|---|
| 157 | bool needDelete = (--_refCount == 0); |
|---|
| 158 | #else |
|---|
| [3857] | 159 | bool needDelete = false; |
|---|
| 160 | if (_refMutex) |
|---|
| 161 | { |
|---|
| 162 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex); |
|---|
| 163 | --_refCount; |
|---|
| 164 | needDelete = _refCount<=0; |
|---|
| 165 | } |
|---|
| 166 | else |
|---|
| 167 | { |
|---|
| 168 | --_refCount; |
|---|
| 169 | needDelete = _refCount<=0; |
|---|
| 170 | } |
|---|
| [8465] | 171 | #endif |
|---|
| [3857] | 172 | |
|---|
| 173 | if (needDelete) |
|---|
| 174 | { |
|---|
| [7038] | 175 | if (getDeleteHandler()) deleteUsingDeleteHandler(); |
|---|
| [3857] | 176 | else delete this; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| [8976] | 180 | // intrusive_ptr_add_ref and intrusive_ptr_release allow |
|---|
| 181 | // use of osg Referenced classes with boost::intrusive_ptr |
|---|
| 182 | inline void intrusive_ptr_add_ref(Referenced* p) { p->ref(); } |
|---|
| 183 | inline void intrusive_ptr_release(Referenced* p) { p->unref(); } |
|---|
| [3857] | 184 | |
|---|
| [1414] | 185 | } |
|---|
| 186 | |
|---|
| [2] | 187 | #endif |
|---|