blob: eb323b067e04f90161827b251a0da2cdc4c723a7 (
plain) (
blame)
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
|
#pragma once
#include <list>
#include "../OSSupport/Promise.h"
//this empty struct allows function inlining
template<class T>
struct cQueueFuncs
{
public:
static void Delete(T) {};
static void Combine(T&, const T) {};
};
template<class ItemType, class Funcs = cQueueFuncs<ItemType> >
class cQueue
{
typedef typename std::list<ItemType> ListType;
//magic typedef to persuade clang that the iterator is a type
typedef typename ListType::iterator iterator;
public:
cQueue() {}
~cQueue() {}
void EnqueueItem(ItemType a_item)
{
cCSLock Lock(m_CS);
m_contents.push_back(a_item);
m_evtAdded.Set();
}
void EnqueueItemIfNotPresent(ItemType a_item)
{
cCSLock Lock(m_CS);
for (iterator itr = m_contents.begin(); itr != m_contents.end(); ++itr)
{
if((*itr) == a_item) {
Funcs funcTable;
funcTable.Combine(*itr,a_item);
return;
}
}
m_contents.push_back(a_item);
m_evtAdded.Set();
}
bool TryDequeueItem(ItemType& item)
{
cCSLock Lock(m_CS);
if (m_contents.size() == 0) return false;
item = m_contents.front();
m_contents.pop_front();
return true;
}
ItemType DequeueItem()
{
cCSLock Lock(m_CS);
while (m_contents.size() == 0)
{
cCSUnlock Unlock(m_CS);
m_evtAdded.Wait();
}
return m_contents.pop_front();
}
cPromise* BlockTillEmpty() {
return new cEmptyQueuePromise(this);
}
//can all be inlined when delete is a noop
void Clear()
{
cCSLock Lock(m_CS);
Funcs funcTable;
while (!m_contents.empty())
{
funcTable.Delete(m_contents.front());
m_contents.pop_front();
}
}
size_t Size()
{
cCSLock Lock(m_CS);
return m_contents.size();
}
bool Remove(ItemType item)
{
cCSLock Lock(m_CS);
m_contents.remove(item);
}
private:
ListType m_contents;
cCriticalSection m_CS;
cEvent m_evtAdded;
class cEmptyQueuePromise : public cPromise {
public:
cEmptyQueuePromise(cQueue* a_Queue) : cPromise(), m_Queue(a_Queue) {}
virtual bool IsCompleted() {return m_Queue->Size() != 0;}
private:
cQueue* m_Queue;
};
};
|