summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/Queue.h
blob: da0c88ec06cb47355e516d65a080125c1214263b (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

// Queue.h

// Implements the cQueue class representing a thread safe queue

#pragma once

/*
Items can be added multiple times to a queue, there are two functions for
adding, EnqueueItem() and EnqueueItemIfNotPresent(). The first one always
enqueues the specified item, the second one checks if the item is already
present and only queues it if it isn't.

Usage:
To create a queue of type T, instantiate a cQueue<T> object. You can also
modify the behavior of the queue when deleting items and when adding items
that are already in the queue by providing a second parameter, a class that
implements the functions Delete() and Combine(). An example is given in
cQueueFuncs and is used as the default behavior. */

/** This empty struct allows for the callback functions to be inlined */
template <class T> struct cQueueFuncs
{
  public:
	/** Called when an Item is deleted from the queue without being returned */
	static void Delete(T) {}

	/** Called when an Item is inserted with EnqueueItemIfNotPresent and there is another equal value already inserted
	 */
	static void Combine(T & a_existing, const T & a_new)
	{
		UNUSED(a_existing);
		UNUSED(a_new);
	}
};





template <class ItemType, class Funcs = cQueueFuncs<ItemType>> class cQueue
{
	// The actual storage type for the queue
	typedef typename std::list<ItemType> QueueType;

	// Make iterator an alias for the QueueType's iterator
	typedef typename QueueType::iterator iterator;

  public:
	cQueue() {}
	~cQueue() {}


	/** Enqueues an item to the queue, may block if other threads are accessing the queue. */
	void EnqueueItem(ItemType a_Item)
	{
		cCSLock Lock(m_CS);
		m_Contents.push_back(a_Item);
		m_evtAdded.Set();
	}


	/** Enqueues an item in the queue if not already present (as determined by operator ==). Blocks other threads from
	 * accessing the queue. */
	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::Combine(*itr, a_Item);
				return;
			}
		}
		m_Contents.push_back(a_Item);
		m_evtAdded.Set();
	}


	/** Dequeues an item from the queue if any are present.
	Returns true if successful. Value of item is undefined if dequeuing was unsuccessful. */
	bool TryDequeueItem(ItemType & item)
	{
		cCSLock Lock(m_CS);
		if (m_Contents.empty())
		{
			return false;
		}
		item = m_Contents.front();
		m_Contents.pop_front();
		m_evtRemoved.Set();
		return true;
	}


	/** Dequeues an item from the queue, blocking until an item is available. */
	ItemType DequeueItem(void)
	{
		cCSLock Lock(m_CS);
		while (m_Contents.empty())
		{
			cCSUnlock Unlock(Lock);
			m_evtAdded.Wait();
		}
		ItemType item = m_Contents.front();
		m_Contents.pop_front();
		m_evtRemoved.Set();
		return item;
	}


	/** Blocks until the queue is empty. */
	void BlockTillEmpty(void)
	{
		cCSLock Lock(m_CS);
		while (!m_Contents.empty())
		{
			cCSUnlock Unlock(Lock);
			m_evtRemoved.Wait();
		}
	}


	/** Removes all Items from the Queue, calling Delete on each of them. */
	void Clear(void)
	{
		cCSLock Lock(m_CS);
		while (!m_Contents.empty())
		{
			Funcs::Delete(m_Contents.front());
			m_Contents.pop_front();
		}
	}


	/** Returns the size at time of being called.
	Do not use to determine whether to call DequeueItem(), use TryDequeueItem() instead */
	size_t Size(void)
	{
		cCSLock Lock(m_CS);
		return m_Contents.size();
	}


	/** Removes the item from the queue. If there are multiple such items, only the first one is removed.
	Returns true if the item has been removed, false if no such item found. */
	bool Remove(ItemType a_Item)
	{
		cCSLock Lock(m_CS);
		for (iterator itr = m_Contents.begin(); itr != m_Contents.end(); ++itr)
		{
			if ((*itr) == a_Item)
			{
				m_Contents.erase(itr);
				m_evtRemoved.Set();
				return true;
			}
		}
		return false;
	}


	/** Removes all items for which the predicate returns true. */
	template <class Predicate> void RemoveIf(Predicate a_Predicate)
	{
		cCSLock Lock(m_CS);
		for (auto itr = m_Contents.begin(); itr != m_Contents.end();)
		{
			if (a_Predicate(*itr))
			{
				auto itr2 = itr;
				++itr2;
				m_Contents.erase(itr);
				m_evtRemoved.Set();
				itr = itr2;
			}
			else
			{
				++itr;
			}
		}  // for itr - m_Contents[]
	}

  private:
	/** The contents of the queue */
	QueueType m_Contents;

	/** Mutex that protects access to the queue contents */
	cCriticalSection m_CS;

	/** Event that is signalled when an item is added */
	cEvent m_evtAdded;

	/** Event that is signalled when an item is removed (both dequeued or erased) */
	cEvent m_evtRemoved;
};