vsg: include/vsg/threading/DeleteQueue.h Source File

vsg 1.1.10 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
17#include <condition_variable>
18#include <list>
19
20namespace vsg
21{
22
24 class VSG_DECLSPEC DeleteQueue : public Inherit<Object, DeleteQueue>
25 {
26 public:
27 explicit DeleteQueue(ref_ptr<ActivityStatus> status);
28
29 struct ObectToDelete
30 {
31 uint64_t frameCount = 0;
32 ref_ptr<Object> object;
33 };
34
35 using ObjectsToDelete = std::list<ObectToDelete>;
36
37 std::atomic_uint64_t frameCount = 0;
38 uint64_t retainForFrameCount = 3;
39
40 ActivityStatus* getStatus() { return _status; }
41 const ActivityStatus* getStatus() const { return _status; }
42
43 void advance(ref_ptr<FrameStamp> frameStamp);
44
45 void add(ref_ptr<Object> object)
46 {
47 std::scoped_lock lock(_mutex);
48 _objectsToDelete.push_back(ObectToDelete{frameCount + retainForFrameCount, object});
49 _cv.notify_one();
50 }
51
52 template<typename T>
53 void add(T& objects)
54 {
55 std::scoped_lock lock(_mutex);
56 for (auto& object : objects)
57 {
58 _objectsToDelete.emplace_back(ObectToDelete{frameCount + retainForFrameCount, object});
59 }
60 _cv.notify_one();
61 }
62
63 void wait_then_clear();
64
65 void clear();
66
67 protected:
68 virtual ~DeleteQueue();
69
70 std::mutex _mutex;
71 std::condition_variable _cv;
72 ObjectsToDelete _objectsToDelete;
73 ref_ptr<ActivityStatus> _status;
74 };
75 VSG_type_name(vsg::DeleteQueue);
76
77} // namespace vsg
vsg::ActivityStatus
ActivityStatus provides atomic management of whether threads watching this ActivityStatus object shou...
Definition ActivityStatus.h:22
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::DeleteQueue::ObectToDelete
Definition DeleteQueue.h:30