Added a Refrenced::getGlobalReferencedMutex, and OpenThreads::ScopedPointerLock() and use of this in add/removeParent() codes

to avoid threading problems when using atomic ref counting.
This commit is contained in:
Robert Osfield 2008-10-14 14:27:41 +00:00
parent af13e84093
commit ac975bf79a
7 changed files with 53 additions and 61 deletions

View File

@ -43,5 +43,28 @@ template <class M> class ReverseScopedLock
~ReverseScopedLock(){m_lock.lock();}
};
template <class M> class ScopedPointerLock
{
private:
M* m_lock;
ScopedPointerLock(const ScopedPointerLock&); // prevent copy
ScopedPointerLock& operator=(const ScopedPointerLock&); // prevent assign
public:
explicit ScopedPointerLock(M* m):m_lock(m) { if (m_lock) m_lock->lock();}
~ScopedPointerLock(){ if (m_lock) m_lock->unlock();}
};
template <class M> class ReverseScopedPointerLock
{
private:
M* m_lock;
ReverseScopedPointerLock(const ReverseScopedPointerLock&); // prevent copy
ReverseScopedPointerLock& operator=(const ReverseScopedPointerLock&); // prevent assign
public:
explicit ReverseScopedPointerLock(M* m):m_lock(m) { if (m_lock) m_lock->unlock();}
~ReverseScopedPointerLock(){ if (m_lock) m_lock->lock();}
};
}
#endif

View File

@ -70,11 +70,14 @@ class OSG_EXPORT Referenced
/** Get the mutex used to ensure thread safety of ref()/unref(). */
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
OpenThreads::Mutex* getRefMutex() const { return 0; }
OpenThreads::Mutex* getRefMutex() const { return getGlobalReferencedMutex(); }
#else
OpenThreads::Mutex* getRefMutex() const { return _refMutex; }
#endif
/** Get the optional global Referenced mutex, this can be shared between all osg::Referenced.*/
static OpenThreads::Mutex* getGlobalReferencedMutex();
/** Increment the reference count by one, indicating that
this object has another pointer which is referencing it.*/
inline void ref() const;

View File

@ -281,32 +281,17 @@ void Drawable::computeDataVariance()
void Drawable::addParent(osg::Node* node)
{
if (getRefMutex())
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*getRefMutex());
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> lock(getRefMutex());
_parents.push_back(node);
}
else
{
_parents.push_back(node);
}
_parents.push_back(node);
}
void Drawable::removeParent(osg::Node* node)
{
if (getRefMutex())
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*getRefMutex());
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> 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);
}
ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),node);
if (pitr!=_parents.end()) _parents.erase(pitr);
}

View File

@ -95,32 +95,17 @@ Node::~Node()
void Node::addParent(osg::Group* node)
{
if (getRefMutex())
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*getRefMutex());
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> lock(getRefMutex());
_parents.push_back(node);
}
else
{
_parents.push_back(node);
}
_parents.push_back(node);
}
void Node::removeParent(osg::Group* node)
{
if (getRefMutex())
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*getRefMutex());
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> 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);
}
ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),node);
if (pitr!=_parents.end()) _parents.erase(pitr);
}
void Node::accept(NodeVisitor& nv)

View File

@ -79,6 +79,12 @@ struct DeleteHandlerPointer
DeleteHandler* _ptr;
};
OpenThreads::Mutex* Referenced::getGlobalReferencedMutex()
{
return 0;
// static OpenThreads::Mutex s_ReferencedGlobalMutext;
// return &s_ReferencedGlobalMutext;
}
typedef std::set<Observer*> ObserverSet;

View File

@ -27,11 +27,16 @@ StateAttribute::StateAttribute()
void StateAttribute::addParent(osg::StateSet* object)
{
osg::notify(osg::INFO)<<"Adding parent"<<getRefMutex()<<std::endl;
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> lock(getRefMutex());
_parents.push_back(object);
}
void StateAttribute::removeParent(osg::StateSet* object)
{
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> lock(getRefMutex());
ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),object);
if (pitr!=_parents.end()) _parents.erase(pitr);
}

View File

@ -252,32 +252,17 @@ void StateSet::computeDataVariance()
void StateSet::addParent(osg::Object* object)
{
// osg::notify(osg::INFO)<<"Adding parent"<<std::endl;
if (getRefMutex())
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*getRefMutex());
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> lock(getRefMutex());
_parents.push_back(object);
}
else
{
_parents.push_back(object);
}
_parents.push_back(object);
}
void StateSet::removeParent(osg::Object* object)
{
if (getRefMutex())
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*getRefMutex());
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> 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);
}
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