2007-02-02 20:41:13 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
#include <osg/DeleteHandler>
|
|
|
|
#include <osg/Notify>
|
|
|
|
|
|
|
|
namespace osg
|
|
|
|
{
|
|
|
|
|
|
|
|
DeleteHandler::DeleteHandler(int numberOfFramesToRetainObjects):
|
2007-07-09 23:02:03 +08:00
|
|
|
_numFramesToRetainObjects(numberOfFramesToRetainObjects),
|
|
|
|
_currentFrameNumber(0)
|
2007-02-02 20:41:13 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
DeleteHandler::~DeleteHandler()
|
|
|
|
{
|
|
|
|
// flushAll();
|
|
|
|
}
|
|
|
|
|
2007-02-02 20:41:13 +08:00
|
|
|
void DeleteHandler::flush()
|
|
|
|
{
|
|
|
|
typedef std::list<const osg::Referenced*> DeletionList;
|
|
|
|
DeletionList deletionList;
|
|
|
|
|
|
|
|
{
|
|
|
|
// gather all the objects to delete whilst holding the mutex to the _objectsToDelete
|
|
|
|
// list, but delete the objects outside this scoped lock so that if any objects deleted
|
|
|
|
// unref their children then no deadlock happens.
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
|
|
|
int frameNumberToClearTo = _currentFrameNumber - _numFramesToRetainObjects;
|
|
|
|
|
|
|
|
ObjectsToDeleteList::iterator itr;
|
|
|
|
for(itr = _objectsToDelete.begin();
|
|
|
|
itr != _objectsToDelete.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if (itr->first > frameNumberToClearTo) break;
|
|
|
|
|
|
|
|
deletionList.push_back(itr->second);
|
|
|
|
|
|
|
|
itr->second = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_objectsToDelete.erase( _objectsToDelete.begin(), itr);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(DeletionList::iterator ditr = deletionList.begin();
|
|
|
|
ditr != deletionList.end();
|
|
|
|
++ditr)
|
|
|
|
{
|
|
|
|
doDelete(*ditr);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteHandler::flushAll()
|
|
|
|
{
|
2007-07-06 02:33:20 +08:00
|
|
|
int temp_numFramesToRetainObjects = _numFramesToRetainObjects;
|
2007-02-02 20:41:13 +08:00
|
|
|
_numFramesToRetainObjects = 0;
|
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
typedef std::list<const osg::Referenced*> DeletionList;
|
|
|
|
DeletionList deletionList;
|
|
|
|
|
|
|
|
{
|
|
|
|
// gather all the objects to delete whilst holding the mutex to the _objectsToDelete
|
|
|
|
// list, but delete the objects outside this scoped lock so that if any objects deleted
|
|
|
|
// unref their children then no deadlock happens.
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
|
|
|
ObjectsToDeleteList::iterator itr;
|
|
|
|
for(itr = _objectsToDelete.begin();
|
|
|
|
itr != _objectsToDelete.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
deletionList.push_back(itr->second);
|
|
|
|
itr->second = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_objectsToDelete.erase( _objectsToDelete.begin(), _objectsToDelete.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
for(DeletionList::iterator ditr = deletionList.begin();
|
|
|
|
ditr != deletionList.end();
|
|
|
|
++ditr)
|
|
|
|
{
|
|
|
|
doDelete(*ditr);
|
|
|
|
}
|
2007-02-02 20:41:13 +08:00
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
_numFramesToRetainObjects = temp_numFramesToRetainObjects;
|
2007-02-02 20:41:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteHandler::requestDelete(const osg::Referenced* object)
|
|
|
|
{
|
|
|
|
if (_numFramesToRetainObjects==0) doDelete(object);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
|
|
|
_objectsToDelete.push_back(FrameNumberObjectPair(_currentFrameNumber,object));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-06 23:13:02 +08:00
|
|
|
} // end of namespace osg
|