Nebula
Loading...
Searching...
No Matches
safepriorityqueue.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
14#include "util/queue.h"
16#include "threading/event.h"
17
18//------------------------------------------------------------------------------
19namespace Threading
20{
21template<class PRITYPE, class TYPE> class SafePriorityQueue : protected Util::Queue<Util::KeyValuePair<PRITYPE, TYPE> >
22{
23public:
28
32 SizeT Size() const;
34 bool IsEmpty() const;
36 void Clear();
37
39 void Insert(PRITYPE pri, const TYPE& e);
41 void EraseMatchingElements(const TYPE& e);
43 TYPE Dequeue();
45 TYPE Peek() const;
47 void Wait();
49 void Signal();
50
51protected:
54};
55
56//------------------------------------------------------------------------------
59template<class PRITYPE, class TYPE>
64
65//------------------------------------------------------------------------------
68template<class PRITYPE, class TYPE>
70{
71 this->criticalSection.Enter();
72 this->queueArray = rhs.queueArray;
73 this->criticalSection.Leave();
74}
75
76//------------------------------------------------------------------------------
79template<class PRITYPE, class TYPE> void
81{
82 this->criticalSection.Enter();
83 this->queueArray = rhs.queueArray;
84 this->criticalSection.Leave();
85}
86
87//------------------------------------------------------------------------------
90template<class PRITYPE, class TYPE> void
92{
93 this->criticalSection.Enter();
94 this->queueArray.Clear();
95 this->criticalSection.Leave();
96}
97
98//------------------------------------------------------------------------------
101template<class PRITYPE, class TYPE> SizeT
103{
104 return this->queueArray.Size();
105}
106
107//------------------------------------------------------------------------------
110template<class PRITYPE, class TYPE> bool
112{
113 return this->queueArray.IsEmpty();
114}
115
116//------------------------------------------------------------------------------
119template<class PRITYPE, class TYPE> void
121{
123 this->criticalSection.Enter();
124 this->queueArray.InsertSorted(kvp);
125 this->criticalSection.Leave();
126 this->enqueueEvent.Signal();
127}
128
129//------------------------------------------------------------------------------
132template<class PRITYPE, class TYPE> void
134{
135 this->criticalSection.Enter();
136 //Util::Array<Util::KeyValuePair<PRITYPE,TYPE> >::Iterator iter;
137 for (auto iter = this->queueArray.Begin(); iter != this->queueArray.End();)
138 {
139 if ((*iter).Value() == e)
140 {
141 iter = this->queueArray.Erase(iter);
142 }
143 else
144 {
145 iter++;
146 }
147 }
148 this->criticalSection.Leave();
149}
150
151//------------------------------------------------------------------------------
154template<class PRITYPE, class TYPE> TYPE
156{
157 this->criticalSection.Enter();
158 TYPE value = this->queueArray.Front().Value();
159 this->queueArray.EraseIndex(0);
160 this->criticalSection.Leave();
161 return value;
162}
163
164//------------------------------------------------------------------------------
167template<class PRITYPE, class TYPE> TYPE
169{
170 this->criticalSection.Enter();
171 TYPE value = this->queueArray.Front().Value();
172 this->criticalSection.Leave();
173 return value;
174}
175
176//------------------------------------------------------------------------------
179template<class PRITYPE, class TYPE> void
181{
182 if (this->queueArray.Size() == 0)
183 {
184 this->enqueueEvent.Wait();
185 }
186}
187
188//------------------------------------------------------------------------------
194template<class PRITYPE, class TYPE> void
196{
197 this->enqueueEvent.Signal();
198}
199
200} // namespace Threading
201//------------------------------------------------------------------------------
Critical section objects are used to protect a portion of code from parallel execution.
A thread-safe priority-sorted queue which protects itself with critical sections.
Definition safepriorityqueue.h:22
void Signal()
signal the internal event, so that Wait() will return
Definition safepriorityqueue.h:195
bool IsEmpty() const
return true if queue is empty
Definition safepriorityqueue.h:111
SafePriorityQueue()
constructor
Definition safepriorityqueue.h:60
SizeT Size() const
returns number of elements in the queue
Definition safepriorityqueue.h:102
CriticalSection criticalSection
Definition safepriorityqueue.h:52
void EraseMatchingElements(const TYPE &e)
erase all matching elements
Definition safepriorityqueue.h:133
TYPE Dequeue()
remove the element from the front of the queue
Definition safepriorityqueue.h:155
TYPE Peek() const
get copy of element at front of queue without removing it
Definition safepriorityqueue.h:168
Event enqueueEvent
Definition safepriorityqueue.h:53
void Clear()
remove all elements from the queue
Definition safepriorityqueue.h:91
void operator=(const SafePriorityQueue< PRITYPE, TYPE > &rhs)
assignment operator
Definition safepriorityqueue.h:80
void Insert(PRITYPE pri, const TYPE &e)
add element to the back of the queue
Definition safepriorityqueue.h:120
void Wait()
wait until queue contains at least one element
Definition safepriorityqueue.h:180
Key/Value pair objects are used by most assiociative container classes, like Dictionary or HashTable.
Definition keyvaluepair.h:19
Nebula's queue class (a FIFO container).
Definition queue.h:28
The Jobs2 system provides a set of threads and a pool of jobs from which threads can pickup work.
Definition jobs2.h:16
int SizeT
Definition types.h:49