summaryrefslogtreecommitdiff
path: root/src/itoqueue.h
blob: dc3cadb9535b551e3877120fc2dafc551e2a930d (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_ITO_QUEUE_H
#define BU_ITO_QUEUE_H

#include <pthread.h>

#include "itomutex.h"
#include "itocondition.h"

namespace Bu
{
	/**
	 * A thread-safe queue class.  This class is a very simple queue with some
	 * cool extra functionality for use with the Ito system.  The main extra
	 * that it provides is the option to either dequeue without blocking, with
	 * infinite blocking, or with timed blocking, which will return a value if
	 * something is enqueued within the specified time limit, or NULL if the
	 * time limit is exceded.
	 *@ingroup Threading Containers
	 */
	template <class T>
	class ItoQueue
	{
	private:
		/**
		 * Helper struct.  Keeps track of linked-list items for the queue data.
		 */
		typedef struct Item
		{
			T pData;
			Item *pNext;
		} Item;

	public:
		/**
		 * Construct an empty queue.
		 */
		ItoQueue() :
			pStart( NULL ),
			pEnd( NULL ),
			nSize( 0 )
		{
		}
		
		/**
		 * Destroy the queue.  This function will simply free all contained
		 * structures.  If you stored pointers in the queue, this will lose the
		 * pointers without cleaning up the memory they pointed to.  Make sure
		 * you're queue is empty before allowing it to be destroyed!
		 */
		~ItoQueue()
		{
			Item *pCur = pStart;
			while( pCur )
			{
				Item *pTmp = pCur->pNext;
				delete pCur;
				pCur = pTmp;
			}
		}

		/**
		 * Enqueue a pieces of data.  The new data will go at the end of the
		 * queue, and unless another piece of data is enqueued, will be the
		 * last piece of data to be dequeued.
		 *@param pData The data to enqueue.  If this is not a primitive data
		 * type it's probably best to use a pointer type.
		 */
		void enqueue( T pData )
		{
			mOperate.lock();

			if( pStart == NULL )
			{
				pStart = pEnd = new Item;
				pStart->pData = pData;
				pStart->pNext = NULL;
				nSize++;
			}
			else
			{
				pEnd->pNext = new Item;
				pEnd = pEnd->pNext;
				pEnd->pData = pData;
				pEnd->pNext = NULL;
				nSize++;
			}

			cBlock.signal();
		
			mOperate.unlock();
		}

		/**
		 * Dequeue the first item from the queue.  This function can operate in
		 * two different modes, blocking and non-blocking.  In non-blocking
		 * mode it will return immediately weather there was data in the queue
		 * or not.  If there was data it will remove it from the queue and
		 * return it to the caller.
		 *
		 * In blocking mode it will block forever wating for data to be
		 * enqueued.  When data finally is enqueued this function will return
		 * immediately with the new data.  The only way this function should
		 * ever return a null in blocking mode is if the calling thread was
		 * cancelled.  It's probably a good idea to check for NULL return
		 * values even if you use blocking, just to be on the safe side.
		 *@param bBlock Set to true to enable blocking, leave as false to work
		 * in non-blocking mode.
		 *@returns The next piece of data in the queue, or NULL if no data was
		 * in the queue.
		 */
		T dequeue( bool bBlock=false )
		{
			mOperate.lock();
			if( pStart == NULL )
			{
				mOperate.unlock();

				if( bBlock )
				{
					cBlock.lock();

					while( pStart == NULL )
						cBlock.wait();
				
					T tmp = dequeue( false );
					
					cBlock.unlock();
					return tmp;

				}

				return NULL;
			}
			else
			{
				T pTmp = pStart->pData;
				Item *pDel = pStart;
				pStart = pStart->pNext;
				delete pDel;
				nSize--;
			
				mOperate.unlock();
				return pTmp;
			}
		}

		/**
		 * Operates just like the other dequeue function in blocking mode with
		 * one twist.  This function will block for at most nSec seconds and
		 * nUSec micro-seconds.  If the timer is up and no data is available,
		 * this will just return NULL.  If data is enqueued before the timeout
		 * expires, it will dequeue and exit immediately.
		 *@param nSec The number of seconds to wait, max.
		 *@param nUSec The number of micro-seconds to wait, max.
		 *@returns The next piece of data in the queue, or NULL if the timeout
		 * was exceeded.
		 */
		T dequeue( int nSec, int nUSec )
		{
			mOperate.lock();
			if( pStart == NULL )
			{
				mOperate.unlock();

				cBlock.lock();
				
				cBlock.wait( nSec, nUSec );

				if( pStart == NULL )
				{
					cBlock.unlock();
					return NULL;
				}
			
				mOperate.lock();
				T pTmp = pStart->pData;
				Item *pDel = pStart;
				pStart = pStart->pNext;
				delete pDel;
				nSize--;
				mOperate.unlock();
					
				cBlock.unlock();
				return pTmp;
			}
			else
			{
				T pTmp = pStart->pData;
				Item *pDel = pStart;
				pStart = pStart->pNext;
				delete pDel;
				nSize--;
			
				mOperate.unlock();
				return pTmp;
			}
		}

		/**
		 * Checks to see if the queue has data in it or not.  Note that there
		 * is no function to determine the length of the queue.  This data
		 * isn't kept track of.  If you really need to know, fix this.
		 *@returns True if the queue is empty, false if it has data in it.
		 */
		bool isEmpty()
		{
			mOperate.lock();
			bool bEmpty = (pStart == NULL );
			mOperate.unlock();

			return bEmpty;
		}

		long getSize()
		{
			mOperate.lock();
			long nRet = nSize;
			mOperate.unlock();

			return nRet;
		}

	private:
		Item *pStart;	/**< The start of the queue, the next element to dequeue. */
		Item *pEnd;		/**< The end of the queue, the last element to dequeue. */
		long nSize;		/**< The number of items in the queue. */

		ItoMutex mOperate;	/**< The master mutex, used on all operations. */
		ItoCondition cBlock;	/**< The condition for blocking dequeues. */
	};
}

#endif