Repository navigation
Expand file tree
/
Copy pathDeleteQueue.cpp
More file actions
121 lines (87 loc) · 4.14 KB
/
Copy pathDeleteQueue.cpp
File metadata and controls
121 lines (87 loc) · 4.14 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
112
113
114
115
116
117
118
119
120
121
/* <editor-fold desc="MIT License">
Copyright(c) 2024 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/io/Options.h>
#include <vsg/threading/DeleteQueue.h>
#include <vsg/ui/FrameStamp.h>
using namespace vsg;
/////////////////////////////////////////////////////////////////////////
//
// DeleteQueue
//
DeleteQueue::DeleteQueue(ref_ptr<ActivityStatus> status) :
_status(status)
{
}
DeleteQueue::~DeleteQueue()
{
}
void DeleteQueue::advance(ref_ptr<FrameStamp> frameStamp)
{
std::scoped_lock lock(_mutex);
frameCount = frameStamp->frameCount;
if (!_objectsToDelete.empty() && _objectsToDelete.front().frameCount <= frameStamp->frameCount)
{
_cv.notify_one();
}
}
size_t DeleteQueue::wait_then_clear()
{
ObjectsToDelete objectsToDelete;
std::list<ref_ptr<SharedObjects>> sharedObjectsToPrune;
{
std::chrono::duration waitDuration = std::chrono::milliseconds(100);
std::unique_lock lock(_mutex);
uint64_t previous_frameCount = frameCount.load();
// wait until the conditional variable signals that an operation has been added
while ((_objectsToDelete.empty() || (frameCount.load() == previous_frameCount)) && _status->active())
{
_cv.wait_for(lock, waitDuration);
}
auto last_itr = std::find_if(_objectsToDelete.begin(), _objectsToDelete.end(), [&](const ObjectToDelete& otd) { return otd.frameCount > frameCount; });
// use a swap of the container to keep the time the mutex is acquired as short as possible
objectsToDelete.splice(objectsToDelete.end(), _objectsToDelete, _objectsToDelete.begin(), last_itr);
sharedObjectsToPrune.swap(_sharedObjectsToPrune);
}
size_t numObjectsToDelete = objectsToDelete.size();
objectsToDelete.clear();
if (numObjectsToDelete > 0)
{
for (auto& sharedObjects : sharedObjectsToPrune)
{
sharedObjects->prune();
}
sharedObjectsToPrune.clear();
}
deletedCount += numObjectsToDelete;
return numObjectsToDelete;
}
size_t DeleteQueue::clear()
{
ObjectsToDelete objectsToDelete;
std::list<ref_ptr<SharedObjects>> sharedObjectsToPrune;
// use a swap of the container to keep the time the mutex is acquired as short as possible
{
std::scoped_lock lock(_mutex);
auto last_itr = std::find_if(_objectsToDelete.begin(), _objectsToDelete.end(), [&](const ObjectToDelete& otd) { return otd.frameCount > frameCount; });
// use a swap of the container to keep the time the mutex is acquired as short as possible
objectsToDelete.splice(objectsToDelete.end(), _objectsToDelete, _objectsToDelete.begin(), last_itr);
sharedObjectsToPrune.swap(_sharedObjectsToPrune);
}
size_t numObjectsToDelete = objectsToDelete.size();
//vsg::info("DeleteQueue::clear(), releasing ", nodesToRelease.size());
objectsToDelete.clear();
if (numObjectsToDelete > 0)
{
for (auto& sharedObjects : sharedObjectsToPrune)
{
sharedObjects->prune();
}
sharedObjectsToPrune.clear();
}
deletedCount += numObjectsToDelete;
return numObjectsToDelete;
}