vsg 1.1.13
VulkanSceneGraph library
Loading...
Searching...
No Matches
DeleteQueue.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2024 Robert Osfield
6
7Permission 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:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE 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.
12
13</editor-fold> */
14
15#include <vsg/threading/ActivityStatus.h>
16#include <vsg/utils/SharedObjects.h>
17
18#include <condition_variable>
19#include <list>
20
21namespace vsg
22{
23
25 class VSG_DECLSPEC DeleteQueue : public Inherit<Object, DeleteQueue>
26 {
27 public:
28 explicit DeleteQueue(ref_ptr<ActivityStatus> status);
29
31 {
32 uint64_t frameCount = 0;
33 ref_ptr<Object> object;
34 };
35
36 using ObjectsToDelete = std::list<ObjectToDelete>;
37
38 std::atomic_uint64_t frameCount = 0;
39 uint64_t retainForFrameCount = 3;
40
41 ActivityStatus* getStatus() { return _status; }
42 const ActivityStatus* getStatus() const { return _status; }
43
44 void advance(ref_ptr<FrameStamp> frameStamp);
45
46 void add(ref_ptr<Object> object)
47 {
48 std::scoped_lock lock(_mutex);
49 _objectsToDelete.push_back(ObjectToDelete{frameCount + retainForFrameCount, object});
50 _cv.notify_one();
51 }
52
53 template<typename T>
54 void add(T& objects)
55 {
56 std::scoped_lock lock(_mutex);
57
58 // register the Objects to delete
59 for (auto& object : objects)
60 {
61 _objectsToDelete.emplace_back(ObjectToDelete{frameCount + retainForFrameCount, object});
62 }
63
64 _cv.notify_one();
65 }
66
67 void prune(ref_ptr<SharedObjects> sharedObjects)
68 {
69 std::scoped_lock lock(_mutex);
70
71 // register the SharedObjects to call prune on
72 if (std::find(_sharedObjectsToPrune.begin(), _sharedObjectsToPrune.end(), sharedObjects) == _sharedObjectsToPrune.end())
73 {
74 _sharedObjectsToPrune.push_back(sharedObjects);
75 }
76
77 _cv.notify_one();
78 }
79
80 template<typename T>
81 void prune(T& sharedObjectsList)
82 {
83 std::scoped_lock lock(_mutex);
84
85 // register the Objects to delete
86 for (auto& sharedObjects : sharedObjectsList)
87 {
88 if (std::find(_sharedObjectsToPrune.begin(), _sharedObjectsToPrune.end(), sharedObjects) == _sharedObjectsToPrune.end())
89 _sharedObjectsToPrune.push_back(sharedObjects);
90 }
91
92 _cv.notify_one();
93 }
94
95 template<typename T, typename R>
96 void add_prune(T& objects, R& sharedObjectsList)
97 {
98 std::scoped_lock lock(_mutex);
99
100 // register the Objects to delete
101 for (auto& object : objects)
102 {
103 _objectsToDelete.emplace_back(ObjectToDelete{frameCount + retainForFrameCount, object});
104 }
105
106 // register the SharedObjects to call prune on
107 for (auto& sharedObjects : sharedObjectsList)
108 {
109 if (std::find(_sharedObjectsToPrune.begin(), _sharedObjectsToPrune.end(), sharedObjects) == _sharedObjectsToPrune.end())
110 _sharedObjectsToPrune.push_back(sharedObjects);
111 }
112 _cv.notify_one();
113 }
114
115 void wait_then_clear();
116
117 void clear();
118
119 protected:
120 virtual ~DeleteQueue();
121
122 std::mutex _mutex;
123 std::condition_variable _cv;
124 ObjectsToDelete _objectsToDelete;
125 std::list<ref_ptr<SharedObjects>> _sharedObjectsToPrune;
126
127 ref_ptr<ActivityStatus> _status;
128 };
129 VSG_type_name(vsg::DeleteQueue);
130
131} // namespace vsg
ActivityStatus provides atomic management of whether threads watching this ActivityStatus object shou...
Definition ActivityStatus.h:22
Definition ref_ptr.h:22
Definition DeleteQueue.h:31