Added mutex usage to protect access to plugin and readerwriter containers

This commit is contained in:
Robert Osfield 2008-09-23 17:29:28 +00:00
parent edc966c4c2
commit 5c264c7c74
2 changed files with 32 additions and 15 deletions

View File

@ -14,6 +14,8 @@
#ifndef OSGDB_REGISTRY #ifndef OSGDB_REGISTRY
#define OSGDB_REGISTRY 1 #define OSGDB_REGISTRY 1
#include <OpenThreads/ReentrantMutex>
#include <osg/ref_ptr> #include <osg/ref_ptr>
#include <osg/ArgumentParser> #include <osg/ArgumentParser>
#include <osg/KdTree> #include <osg/KdTree>
@ -562,6 +564,7 @@ class OSGDB_EXPORT Registry : public osg::Referenced
DotOsgWrapperMap _classNameWrapperMap; DotOsgWrapperMap _classNameWrapperMap;
OpenThreads::ReentrantMutex _pluginMutex;
ReaderWriterList _rwList; ReaderWriterList _rwList;
DynamicLibraryList _dlList; DynamicLibraryList _dlList;

View File

@ -62,8 +62,9 @@ static osg::ApplicationUsageProxy Registry_e2(osg::ApplicationUsage::ENVIRONMENT
class Registry::AvailableReaderWriterIterator class Registry::AvailableReaderWriterIterator
{ {
public: public:
AvailableReaderWriterIterator(Registry::ReaderWriterList& rwList): AvailableReaderWriterIterator(Registry::ReaderWriterList& rwList, OpenThreads::ReentrantMutex& pluginMutex):
_rwList(rwList) {} _rwList(rwList),
_pluginMutex(pluginMutex) {}
ReaderWriter& operator * () { return *get(); } ReaderWriter& operator * () { return *get(); }
@ -80,10 +81,13 @@ public:
protected: protected:
Registry::ReaderWriterList& _rwList; Registry::ReaderWriterList& _rwList;
OpenThreads::ReentrantMutex& _pluginMutex;
std::set<ReaderWriter*> _rwUsed; std::set<ReaderWriter*> _rwUsed;
ReaderWriter* get() ReaderWriter* get()
{ {
OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
Registry::ReaderWriterList::iterator itr=_rwList.begin(); Registry::ReaderWriterList::iterator itr=_rwList.begin();
for(;itr!=_rwList.end();++itr) for(;itr!=_rwList.end();++itr)
{ {
@ -517,6 +521,8 @@ void Registry::addReaderWriter(ReaderWriter* rw)
// notify(INFO) << "osg::Registry::addReaderWriter("<<rw->className()<<")"<< std::endl; // notify(INFO) << "osg::Registry::addReaderWriter("<<rw->className()<<")"<< std::endl;
OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
_rwList.push_back(rw); _rwList.push_back(rw);
} }
@ -528,6 +534,8 @@ void Registry::removeReaderWriter(ReaderWriter* rw)
// notify(INFO) << "osg::Registry::removeReaderWriter();"<< std::endl; // notify(INFO) << "osg::Registry::removeReaderWriter();"<< std::endl;
OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
ReaderWriterList::iterator rwitr = std::find(_rwList.begin(),_rwList.end(),rw); ReaderWriterList::iterator rwitr = std::find(_rwList.begin(),_rwList.end(),rw);
if (rwitr!=_rwList.end()) if (rwitr!=_rwList.end())
{ {
@ -681,12 +689,14 @@ std::string Registry::createLibraryNameForNodeKit(const std::string& name)
bool Registry::loadLibrary(const std::string& fileName) bool Registry::loadLibrary(const std::string& fileName)
{ {
DynamicLibrary* dl = getLibrary(fileName); OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
if (dl) return false;
DynamicLibraryList::iterator ditr = getLibraryItr(fileName);
if (ditr!=_dlList.end()) return true;
_openingLibrary=true; _openingLibrary=true;
dl = DynamicLibrary::loadLibrary(fileName); DynamicLibrary* dl = DynamicLibrary::loadLibrary(fileName);
_openingLibrary=false; _openingLibrary=false;
if (dl) if (dl)
@ -700,6 +710,7 @@ bool Registry::loadLibrary(const std::string& fileName)
bool Registry::closeLibrary(const std::string& fileName) bool Registry::closeLibrary(const std::string& fileName)
{ {
OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
DynamicLibraryList::iterator ditr = getLibraryItr(fileName); DynamicLibraryList::iterator ditr = getLibraryItr(fileName);
if (ditr!=_dlList.end()) if (ditr!=_dlList.end())
{ {
@ -712,7 +723,7 @@ bool Registry::closeLibrary(const std::string& fileName)
void Registry::closeAllLibraries() void Registry::closeAllLibraries()
{ {
// osg::notify(osg::NOTICE)<<"Registry::closeAllLibraries()"<<std::endl; // osg::notify(osg::NOTICE)<<"Registry::closeAllLibraries()"<<std::endl;
OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
_dlList.clear(); _dlList.clear();
} }
@ -728,6 +739,7 @@ Registry::DynamicLibraryList::iterator Registry::getLibraryItr(const std::string
DynamicLibrary* Registry::getLibrary(const std::string& fileName) DynamicLibrary* Registry::getLibrary(const std::string& fileName)
{ {
OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
DynamicLibraryList::iterator ditr = getLibraryItr(fileName); DynamicLibraryList::iterator ditr = getLibraryItr(fileName);
if (ditr!=_dlList.end()) return ditr->get(); if (ditr!=_dlList.end()) return ditr->get();
else return NULL; else return NULL;
@ -738,6 +750,8 @@ ReaderWriter* Registry::getReaderWriterForExtension(const std::string& ext)
// record the existing reader writer. // record the existing reader writer.
std::set<ReaderWriter*> rwOriginal; std::set<ReaderWriter*> rwOriginal;
OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
// first attemt one of the installed loaders // first attemt one of the installed loaders
for(ReaderWriterList::iterator itr=_rwList.begin(); for(ReaderWriterList::iterator itr=_rwList.begin();
itr!=_rwList.end(); itr!=_rwList.end();
@ -1471,7 +1485,7 @@ ReaderWriter::ReadResult Registry::read(const ReadFunctor& readFunctor)
Results results; Results results;
// first attempt to load the file from existing ReaderWriter's // first attempt to load the file from existing ReaderWriter's
AvailableReaderWriterIterator itr(_rwList); AvailableReaderWriterIterator itr(_rwList, _pluginMutex);
for(;itr.valid();++itr) for(;itr.valid();++itr)
{ {
ReaderWriter::ReadResult rr = readFunctor.doRead(*itr); ReaderWriter::ReadResult rr = readFunctor.doRead(*itr);
@ -1661,7 +1675,7 @@ ReaderWriter::WriteResult Registry::writeObjectImplementation(const Object& obj,
Results results; Results results;
// first attempt to load the file from existing ReaderWriter's // first attempt to load the file from existing ReaderWriter's
AvailableReaderWriterIterator itr(_rwList); AvailableReaderWriterIterator itr(_rwList, _pluginMutex);
for(;itr.valid();++itr) for(;itr.valid();++itr)
{ {
ReaderWriter::WriteResult rr = itr->writeObject(obj,fileName,options); ReaderWriter::WriteResult rr = itr->writeObject(obj,fileName,options);
@ -1714,7 +1728,7 @@ ReaderWriter::WriteResult Registry::writeImageImplementation(const Image& image,
Results results; Results results;
// first attempt to load the file from existing ReaderWriter's // first attempt to load the file from existing ReaderWriter's
AvailableReaderWriterIterator itr(_rwList); AvailableReaderWriterIterator itr(_rwList, _pluginMutex);
for(;itr.valid();++itr) for(;itr.valid();++itr)
{ {
ReaderWriter::WriteResult rr = itr->writeImage(image,fileName,options); ReaderWriter::WriteResult rr = itr->writeImage(image,fileName,options);
@ -1768,7 +1782,7 @@ ReaderWriter::WriteResult Registry::writeHeightFieldImplementation(const HeightF
Results results; Results results;
// first attempt to load the file from existing ReaderWriter's // first attempt to load the file from existing ReaderWriter's
AvailableReaderWriterIterator itr(_rwList); AvailableReaderWriterIterator itr(_rwList, _pluginMutex);
for(;itr.valid();++itr) for(;itr.valid();++itr)
{ {
ReaderWriter::WriteResult rr = itr->writeHeightField(HeightField,fileName,options); ReaderWriter::WriteResult rr = itr->writeHeightField(HeightField,fileName,options);
@ -1822,7 +1836,7 @@ ReaderWriter::WriteResult Registry::writeNodeImplementation(const Node& node,con
Results results; Results results;
// first attempt to write the file from existing ReaderWriter's // first attempt to write the file from existing ReaderWriter's
AvailableReaderWriterIterator itr(_rwList); AvailableReaderWriterIterator itr(_rwList, _pluginMutex);
for(;itr.valid();++itr) for(;itr.valid();++itr)
{ {
ReaderWriter::WriteResult rr = itr->writeNode(node,fileName,options); ReaderWriter::WriteResult rr = itr->writeNode(node,fileName,options);
@ -1878,7 +1892,7 @@ ReaderWriter::WriteResult Registry::writeShaderImplementation(const Shader& shad
Results results; Results results;
// first attempt to load the file from existing ReaderWriter's // first attempt to load the file from existing ReaderWriter's
AvailableReaderWriterIterator itr(_rwList); AvailableReaderWriterIterator itr(_rwList, _pluginMutex);
for(;itr.valid();++itr) for(;itr.valid();++itr)
{ {
ReaderWriter::WriteResult rr = itr->writeShader(shader,fileName,options); ReaderWriter::WriteResult rr = itr->writeShader(shader,fileName,options);