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

vsg 1.1.10 VulkanSceneGraph library |

Loading...
Searching...
No Matches
OperationQueue.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 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/io/Logger.h>
16#include <vsg/threading/ActivityStatus.h>
17#include <vsg/threading/Latch.h>
18
19#include <list>
20
21namespace vsg
22{
23
25 template<class T>
26 class ThreadSafeQueue : public Inherit<Object, ThreadSafeQueue<T>>
27 {
28 public:
29 using value_type = T;
30 using container_type = std::list<value_type>;
31
32 explicit ThreadSafeQueue(ref_ptr<ActivityStatus> status) :
33 _status(status)
34 {
35 }
36
37 ActivityStatus* getStatus() { return _status; }
38 const ActivityStatus* getStatus() const { return _status; }
39
41 void add(value_type operation)
42 {
43 std::scoped_lock lock(_mutex);
44 _queue.emplace_back(operation);
45 _cv.notify_one();
46 }
47
49 template<typename Iterator>
50 void add(Iterator begin, Iterator end)
51 {
52 size_t numAdditions = 0;
53 std::scoped_lock lock(_mutex);
54 for (auto itr = begin; itr != end; ++itr)
55 {
56 _queue.emplace_back(*itr);
57 ++numAdditions;
58 }
59
60 if (numAdditions == 1)
61 _cv.notify_one();
62 else if (numAdditions > 1)
63 _cv.notify_all();
64 }
65
67 bool empty() const
68 {
69 std::unique_lock lock(_mutex);
70 return _queue.empty();
71 }
72
74 container_type take_all()
75 {
76 std::unique_lock lock(_mutex);
77
78 container_type objects;
79 objects.swap(_queue);
80
81 return objects;
82 }
83
85 value_type take()
86 {
87 std::unique_lock lock(_mutex);
88
89 if (_queue.empty()) return {};
90
91 auto operation = _queue.front();
92
93 _queue.erase(_queue.begin());
94
95 return operation;
96 }
97
99 value_type take_when_available()
100 {
101 std::chrono::duration waitDuration = std::chrono::milliseconds(100);
102
103 std::unique_lock lock(_mutex);
104
105 // wait until the conditional variable signals that an operation has been added
106 while (_queue.empty() && _status->active())
107 {
108 // debug("Waiting on condition variable");
109 _cv.wait_for(lock, waitDuration);
110 }
111
112 // if the threads we are associated with should no longer be running go for a quick exit and return nothing.
113 if (_status->cancel())
114 {
115 return {};
116 }
117
118 // remove and return the head of the queue
119 auto operation = _queue.front();
120 _queue.erase(_queue.begin());
121 return operation;
122 }
123
124 protected:
125 mutable std::mutex _mutex;
126 std::condition_variable _cv;
127 container_type _queue;
128 ref_ptr<ActivityStatus> _status;
129 };
130
131 // clang-format screws up handling of VSG_type_name macro so have to switch it off.
132 // clang-format off
133
135 struct Operation : public Object
136 {
137 virtual void run() = 0;
138 };
139 VSG_type_name(vsg::Operation)
140
141
142 using OperationQueue = ThreadSafeQueue<ref_ptr<Operation>>;
143 VSG_type_name(vsg::OperationQueue)
144
145} // namespace vsg
vsg::ActivityStatus
ActivityStatus provides atomic management of whether threads watching this ActivityStatus object shou...
Definition ActivityStatus.h:22
vsg::ThreadSafeQueue
Template thread safe queue.
Definition OperationQueue.h:27
vsg::ThreadSafeQueue::take
value_type take()
take the head from the queue of objects, return null pointer if none are available
Definition OperationQueue.h:85
vsg::ThreadSafeQueue::take_all
container_type take_all()
take all available objects from the queue
Definition OperationQueue.h:74
vsg::ThreadSafeQueue::take_when_available
value_type take_when_available()
take the head of the queue, waiting till one is made available if initially empty
Definition OperationQueue.h:99
vsg::ThreadSafeQueue::add
void add(Iterator begin, Iterator end)
add multiple objects to the back of the queue
Definition OperationQueue.h:50
vsg::ThreadSafeQueue::empty
bool empty() const
return true if the queue is empty.
Definition OperationQueue.h:67
vsg::ThreadSafeQueue::add
void add(value_type operation)
add a single object to the back of the queue
Definition OperationQueue.h:41
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::Operation
Operation base class.
Definition OperationQueue.h:136