Repository navigation
Expand file tree
/
Copy pathDeleteHandler.cpp
More file actions
111 lines (92 loc) · 3.34 KB
/
Copy pathDeleteHandler.cpp
File metadata and controls
111 lines (92 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* -*-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):
_numFramesToRetainObjects(numberOfFramesToRetainObjects),
_currentFrameNumber(0)
{
}
DeleteHandler::~DeleteHandler()
{
// flushAll();
}
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);
unsigned 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()
{
unsigned int temp_numFramesToRetainObjects = _numFramesToRetainObjects;
_numFramesToRetainObjects = 0;
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);
}
_numFramesToRetainObjects = temp_numFramesToRetainObjects;
}
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));
}
}
} // end of namespace osg