Customize ObjectCache for more efficient locking in removeExpiredObjectsInCache

coverity_scan
scrawl 9 years ago
parent d855a13b44
commit c155680d3c

@ -11,13 +11,10 @@
* OpenSceneGraph Public License for more details.
*/
#include <osg/Version>
#if OSG_VERSION_LESS_THAN(3,3,3)
#include "objectcache.hpp"
using namespace osgDB;
namespace Resource
{
////////////////////////////////////////////////////////////////////////////////////////////
//
@ -95,21 +92,29 @@ void ObjectCache::updateTimeStampOfObjectsInCacheWithExternalReferences(double r
void ObjectCache::removeExpiredObjectsInCache(double expiryTime)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
std::vector<osg::ref_ptr<osg::Object> > objectsToRemove;
// Remove expired entries from object cache
ObjectCacheMap::iterator oitr = _objectCache.begin();
while(oitr != _objectCache.end())
{
if (oitr->second.second<=expiryTime)
{
_objectCache.erase(oitr++);
}
else
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
// Remove expired entries from object cache
ObjectCacheMap::iterator oitr = _objectCache.begin();
while(oitr != _objectCache.end())
{
++oitr;
if (oitr->second.second<=expiryTime)
{
objectsToRemove.push_back(oitr->second.first);
_objectCache.erase(oitr++);
}
else
{
++oitr;
}
}
}
// note, actual unref happens outside of the lock
objectsToRemove.clear();
}
void ObjectCache::removeFromObjectCache(const std::string& fileName)
@ -138,4 +143,4 @@ void ObjectCache::releaseGLObjects(osg::State* state)
}
}
#endif
}

@ -1,3 +1,6 @@
// Resource ObjectCache for OpenMW, forked from osgDB ObjectCache by Robert Osfield, see copyright notice below.
// The main change from the upstream version is that removeExpiredObjectsInCache no longer keeps a lock while the unref happens.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
@ -11,17 +14,8 @@
* OpenSceneGraph Public License for more details.
*/
// Wrapper for osgDB/ObjectCache. Works around ObjectCache not being available in old OSG 3.2.
// Use "#include objectcache.hpp" in place of "#include <osgDB/ObjectCache".
#ifndef OSGDB_OBJECTCACHE_WRAPPER
#define OSGDB_OBJECTCACHE_WRAPPER 1
#include <osg/Version>
#if OSG_VERSION_GREATER_OR_EQUAL(3,3,3)
#include <osgDB/ObjectCache>
#else
#ifndef OPENMW_COMPONENTS_RESOURCE_OBJECTCACHE
#define OPENMW_COMPONENTS_RESOURCE_OBJECTCACHE
#include <osg/Node>
@ -30,9 +24,9 @@
#include <map>
namespace osgDB {
namespace Resource {
class /*OSGDB_EXPORT*/ ObjectCache : public osg::Referenced
class ObjectCache : public osg::Referenced
{
public:
@ -70,7 +64,7 @@ class /*OSGDB_EXPORT*/ ObjectCache : public osg::Referenced
/** Get an ref_ptr<Object> from the object cache*/
osg::ref_ptr<osg::Object> getRefFromObjectCache(const std::string& fileName);
/** call rleaseGLObjects on all objects attached to the object cache.*/
/** call releaseGLObjects on all objects attached to the object cache.*/
void releaseGLObjects(osg::State* state);
protected:
@ -88,5 +82,3 @@ class /*OSGDB_EXPORT*/ ObjectCache : public osg::Referenced
}
#endif
#endif

@ -7,7 +7,7 @@ namespace Resource
ResourceManager::ResourceManager(const VFS::Manager *vfs, const double expiryDelay)
: mVFS(vfs)
, mCache(new osgDB::ObjectCache)
, mCache(new Resource::ObjectCache)
, mExpiryDelay(expiryDelay)
{

@ -8,13 +8,9 @@ namespace VFS
class Manager;
}
namespace osgDB
{
class ObjectCache;
}
namespace Resource
{
class ObjectCache;
/// @brief Base class for managers that require a virtual file system and object cache.
/// @par This base class implements clearing of the cache, but populating it and what it's used for is up to the individual sub classes.
@ -34,7 +30,7 @@ namespace Resource
protected:
const VFS::Manager* mVFS;
osg::ref_ptr<osgDB::ObjectCache> mCache;
osg::ref_ptr<Resource::ObjectCache> mCache;
double mExpiryDelay;
};

@ -54,7 +54,6 @@ namespace Resource
void ResourceSystem::updateCache(double referenceTime)
{
// TODO: change ObjectCache to not hold lock while the unref happens
for (std::vector<ResourceManager*>::iterator it = mResourceManagers.begin(); it != mResourceManagers.end(); ++it)
(*it)->updateCache(referenceTime);
}

Loading…
Cancel
Save