|
|
@ -26,6 +26,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
|
|
|
|
namespace osg
|
|
|
|
namespace osg
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -53,7 +54,7 @@ class GenericObjectCache : public osg::Referenced
|
|
|
|
void updateTimeStampOfObjectsInCacheWithExternalReferences(double referenceTime)
|
|
|
|
void updateTimeStampOfObjectsInCacheWithExternalReferences(double referenceTime)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// look for objects with external references and update their time stamp.
|
|
|
|
// look for objects with external references and update their time stamp.
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
|
|
std::lock_guard<std::mutex> lock(_objectCacheMutex);
|
|
|
|
for(typename ObjectCacheMap::iterator itr=_objectCache.begin(); itr!=_objectCache.end(); ++itr)
|
|
|
|
for(typename ObjectCacheMap::iterator itr=_objectCache.begin(); itr!=_objectCache.end(); ++itr)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// If ref count is greater than 1, the object has an external reference.
|
|
|
|
// If ref count is greater than 1, the object has an external reference.
|
|
|
@ -71,7 +72,7 @@ class GenericObjectCache : public osg::Referenced
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::vector<osg::ref_ptr<osg::Object> > objectsToRemove;
|
|
|
|
std::vector<osg::ref_ptr<osg::Object> > objectsToRemove;
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
|
|
std::lock_guard<std::mutex> lock(_objectCacheMutex);
|
|
|
|
// Remove expired entries from object cache
|
|
|
|
// Remove expired entries from object cache
|
|
|
|
typename ObjectCacheMap::iterator oitr = _objectCache.begin();
|
|
|
|
typename ObjectCacheMap::iterator oitr = _objectCache.begin();
|
|
|
|
while(oitr != _objectCache.end())
|
|
|
|
while(oitr != _objectCache.end())
|
|
|
@ -92,21 +93,21 @@ class GenericObjectCache : public osg::Referenced
|
|
|
|
/** Remove all objects in the cache regardless of having external references or expiry times.*/
|
|
|
|
/** Remove all objects in the cache regardless of having external references or expiry times.*/
|
|
|
|
void clear()
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
|
|
std::lock_guard<std::mutex> lock(_objectCacheMutex);
|
|
|
|
_objectCache.clear();
|
|
|
|
_objectCache.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Add a key,object,timestamp triple to the Registry::ObjectCache.*/
|
|
|
|
/** Add a key,object,timestamp triple to the Registry::ObjectCache.*/
|
|
|
|
void addEntryToObjectCache(const KeyType& key, osg::Object* object, double timestamp = 0.0)
|
|
|
|
void addEntryToObjectCache(const KeyType& key, osg::Object* object, double timestamp = 0.0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
|
|
std::lock_guard<std::mutex> lock(_objectCacheMutex);
|
|
|
|
_objectCache[key]=ObjectTimeStampPair(object,timestamp);
|
|
|
|
_objectCache[key]=ObjectTimeStampPair(object,timestamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Remove Object from cache.*/
|
|
|
|
/** Remove Object from cache.*/
|
|
|
|
void removeFromObjectCache(const KeyType& key)
|
|
|
|
void removeFromObjectCache(const KeyType& key)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
|
|
std::lock_guard<std::mutex> lock(_objectCacheMutex);
|
|
|
|
typename ObjectCacheMap::iterator itr = _objectCache.find(key);
|
|
|
|
typename ObjectCacheMap::iterator itr = _objectCache.find(key);
|
|
|
|
if (itr!=_objectCache.end()) _objectCache.erase(itr);
|
|
|
|
if (itr!=_objectCache.end()) _objectCache.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -114,7 +115,7 @@ class GenericObjectCache : public osg::Referenced
|
|
|
|
/** Get an ref_ptr<Object> from the object cache*/
|
|
|
|
/** Get an ref_ptr<Object> from the object cache*/
|
|
|
|
osg::ref_ptr<osg::Object> getRefFromObjectCache(const KeyType& key)
|
|
|
|
osg::ref_ptr<osg::Object> getRefFromObjectCache(const KeyType& key)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
|
|
std::lock_guard<std::mutex> lock(_objectCacheMutex);
|
|
|
|
typename ObjectCacheMap::iterator itr = _objectCache.find(key);
|
|
|
|
typename ObjectCacheMap::iterator itr = _objectCache.find(key);
|
|
|
|
if (itr!=_objectCache.end())
|
|
|
|
if (itr!=_objectCache.end())
|
|
|
|
return itr->second.first;
|
|
|
|
return itr->second.first;
|
|
|
@ -124,7 +125,7 @@ class GenericObjectCache : public osg::Referenced
|
|
|
|
/** Check if an object is in the cache, and if it is, update its usage time stamp. */
|
|
|
|
/** Check if an object is in the cache, and if it is, update its usage time stamp. */
|
|
|
|
bool checkInObjectCache(const KeyType& key, double timeStamp)
|
|
|
|
bool checkInObjectCache(const KeyType& key, double timeStamp)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
|
|
std::lock_guard<std::mutex> lock(_objectCacheMutex);
|
|
|
|
typename ObjectCacheMap::iterator itr = _objectCache.find(key);
|
|
|
|
typename ObjectCacheMap::iterator itr = _objectCache.find(key);
|
|
|
|
if (itr!=_objectCache.end())
|
|
|
|
if (itr!=_objectCache.end())
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -137,7 +138,7 @@ class GenericObjectCache : public osg::Referenced
|
|
|
|
/** call releaseGLObjects on all objects attached to the object cache.*/
|
|
|
|
/** call releaseGLObjects on all objects attached to the object cache.*/
|
|
|
|
void releaseGLObjects(osg::State* state)
|
|
|
|
void releaseGLObjects(osg::State* state)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
|
|
std::lock_guard<std::mutex> lock(_objectCacheMutex);
|
|
|
|
for(typename ObjectCacheMap::iterator itr = _objectCache.begin(); itr != _objectCache.end(); ++itr)
|
|
|
|
for(typename ObjectCacheMap::iterator itr = _objectCache.begin(); itr != _objectCache.end(); ++itr)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
osg::Object* object = itr->second.first.get();
|
|
|
|
osg::Object* object = itr->second.first.get();
|
|
|
@ -148,7 +149,7 @@ class GenericObjectCache : public osg::Referenced
|
|
|
|
/** call node->accept(nv); for all nodes in the objectCache. */
|
|
|
|
/** call node->accept(nv); for all nodes in the objectCache. */
|
|
|
|
void accept(osg::NodeVisitor& nv)
|
|
|
|
void accept(osg::NodeVisitor& nv)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
|
|
std::lock_guard<std::mutex> lock(_objectCacheMutex);
|
|
|
|
for(typename ObjectCacheMap::iterator itr = _objectCache.begin(); itr != _objectCache.end(); ++itr)
|
|
|
|
for(typename ObjectCacheMap::iterator itr = _objectCache.begin(); itr != _objectCache.end(); ++itr)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
osg::Object* object = itr->second.first.get();
|
|
|
|
osg::Object* object = itr->second.first.get();
|
|
|
@ -165,7 +166,7 @@ class GenericObjectCache : public osg::Referenced
|
|
|
|
template <class Functor>
|
|
|
|
template <class Functor>
|
|
|
|
void call(Functor& f)
|
|
|
|
void call(Functor& f)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
|
|
std::lock_guard<std::mutex> lock(_objectCacheMutex);
|
|
|
|
for (typename ObjectCacheMap::iterator it = _objectCache.begin(); it != _objectCache.end(); ++it)
|
|
|
|
for (typename ObjectCacheMap::iterator it = _objectCache.begin(); it != _objectCache.end(); ++it)
|
|
|
|
f(it->first, it->second.first.get());
|
|
|
|
f(it->first, it->second.first.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -173,7 +174,7 @@ class GenericObjectCache : public osg::Referenced
|
|
|
|
/** Get the number of objects in the cache. */
|
|
|
|
/** Get the number of objects in the cache. */
|
|
|
|
unsigned int getCacheSize() const
|
|
|
|
unsigned int getCacheSize() const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
|
|
std::lock_guard<std::mutex> lock(_objectCacheMutex);
|
|
|
|
return _objectCache.size();
|
|
|
|
return _objectCache.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -185,7 +186,7 @@ class GenericObjectCache : public osg::Referenced
|
|
|
|
typedef std::map<KeyType, ObjectTimeStampPair > ObjectCacheMap;
|
|
|
|
typedef std::map<KeyType, ObjectTimeStampPair > ObjectCacheMap;
|
|
|
|
|
|
|
|
|
|
|
|
ObjectCacheMap _objectCache;
|
|
|
|
ObjectCacheMap _objectCache;
|
|
|
|
mutable OpenThreads::Mutex _objectCacheMutex;
|
|
|
|
mutable std::mutex _objectCacheMutex;
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|