diff --git a/include/osg/Referenced b/include/osg/Referenced index ae09f58bf..c04af59e1 100644 --- a/include/osg/Referenced +++ b/include/osg/Referenced @@ -59,6 +59,9 @@ class OSG_EXPORT Referenced /** Get whether a mutex is used to ensure ref() and unref() are thread safe.*/ bool getThreadSafeRefUnref() const { return _refMutex!=0; } + /** Get the mutex used to ensure thread safety of ref()/unref(). */ + OpenThreads::Mutex* getRefMutex() const { return _refMutex; } + /** Increment the reference count by one, indicating that this object has another pointer which is referencing it.*/ inline void ref() const; diff --git a/src/osg/Drawable.cpp b/src/osg/Drawable.cpp index 761491d5f..15af2fe85 100644 --- a/src/osg/Drawable.cpp +++ b/src/osg/Drawable.cpp @@ -313,13 +313,32 @@ void Drawable::computeDataVariance() void Drawable::addParent(osg::Node* node) { - _parents.push_back(node); + if (getRefMutex()) + { + OpenThreads::ScopedLock lock(*getRefMutex()); + + _parents.push_back(node); + } + else + { + _parents.push_back(node); + } } void Drawable::removeParent(osg::Node* node) { - ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),node); - if (pitr!=_parents.end()) _parents.erase(pitr); + if (getRefMutex()) + { + OpenThreads::ScopedLock lock(*getRefMutex()); + + ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),node); + if (pitr!=_parents.end()) _parents.erase(pitr); + } + else + { + ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),node); + if (pitr!=_parents.end()) _parents.erase(pitr); + } } diff --git a/src/osg/Node.cpp b/src/osg/Node.cpp index 67e041202..d60571f5d 100644 --- a/src/osg/Node.cpp +++ b/src/osg/Node.cpp @@ -95,13 +95,32 @@ Node::~Node() void Node::addParent(osg::Group* node) { - _parents.push_back(node); + if (getRefMutex()) + { + OpenThreads::ScopedLock lock(*getRefMutex()); + + _parents.push_back(node); + } + else + { + _parents.push_back(node); + } } void Node::removeParent(osg::Group* node) { - ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),node); - if (pitr!=_parents.end()) _parents.erase(pitr); + if (getRefMutex()) + { + OpenThreads::ScopedLock lock(*getRefMutex()); + + ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),node); + if (pitr!=_parents.end()) _parents.erase(pitr); + } + else + { + ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),node); + if (pitr!=_parents.end()) _parents.erase(pitr); + } } void Node::accept(NodeVisitor& nv) diff --git a/src/osg/StateSet.cpp b/src/osg/StateSet.cpp index c7137b8e8..faafedd01 100644 --- a/src/osg/StateSet.cpp +++ b/src/osg/StateSet.cpp @@ -78,7 +78,8 @@ bool osg::isTextureMode(StateAttribute::GLMode mode) return getTextureGLModeSet().isTextureMode(mode); } -StateSet::StateSet() +StateSet::StateSet(): + Object(true) { _renderingHint = DEFAULT_BIN; @@ -247,14 +248,32 @@ void StateSet::computeDataVariance() void StateSet::addParent(osg::Object* object) { // osg::notify(osg::INFO)<<"Adding parent"< lock(*getRefMutex()); - _parents.push_back(object); + _parents.push_back(object); + } + else + { + _parents.push_back(object); + } } void StateSet::removeParent(osg::Object* object) { - ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),object); - if (pitr!=_parents.end()) _parents.erase(pitr); + if (getRefMutex()) + { + OpenThreads::ScopedLock lock(*getRefMutex()); + + ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),object); + if (pitr!=_parents.end()) _parents.erase(pitr); + } + else + { + ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),object); + if (pitr!=_parents.end()) _parents.erase(pitr); + } } int StateSet::compare(const StateSet& rhs,bool compareAttributeContents) const