aboutsummaryrefslogtreecommitdiff
path: root/src/stable/synchroqueue.h
blob: 26f483cae7a07954da2e1bf82d8897a62f686f89 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright (C) 2007-2023 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_SYNCHRO_QUEUE_H
#define BU_SYNCHRO_QUEUE_H

#include <pthread.h>

#include "bu/mutex.h"
#include "bu/condition.h"

namespace Bu
{
    /**
     * A thread-safe queue class.  This class is a very simple queue with some
     * cool extra functionality for use with the Synchro 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 SynchroQueue
    {
    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.
         */
        SynchroQueue() :
            pStart( NULL ),
            pEnd( NULL ),
            nSize( 0 ),
            bRunning( true )
        {
        }
        
        /**
         * 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!
         */
        ~SynchroQueue()
        {
            cBlock.lock();
            Item *pCur = pStart;
            while( pCur )
            {
                Item *pTmp = pCur->pNext;
                delete pCur;
                pCur = pTmp;
            }
            cBlock.unlock();
        }

        /**
         * 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 )
        {
            mRunning.lock();
            if( !bRunning )
            {
                mRunning.unlock();
                throw Bu::ExceptionBase("SynchoQueue is stopped.");
            }
            mRunning.unlock();

            cBlock.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();

            cBlock.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 )
        {
            mRunning.lock();
            if( !bRunning )
            {
                mRunning.unlock();
                return T();
            }
            mRunning.unlock();

            cBlock.lock();
            if( pStart == NULL )
            {
                if( bBlock )
                {
                    cBlock.wait();

                    if( pStart == NULL )
                    {
                        cBlock.unlock();
                        return T();
                    }
                
                    mRunning.lock();
                    if( !bRunning )
                    {
                        mRunning.unlock();
                        cBlock.unlock();
                        return T();
                    }
                    mRunning.unlock();

                    T pTmp = pStart->pData;
                    Item *pDel = pStart;
                    pStart = pStart->pNext;
                    delete pDel;
                    nSize--;
                    
                    cBlock.unlock();
                    return pTmp;

                }

                cBlock.unlock();
                return T();
            }
            else
            {
                T pTmp = pStart->pData;
                Item *pDel = pStart;
                pStart = pStart->pNext;
                delete pDel;
                nSize--;
            
                cBlock.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 )
        {
            mRunning.lock();
            if( !bRunning )
            {
                mRunning.unlock();
                return T();
            }
            mRunning.unlock();

            cBlock.lock();
            if( pStart == NULL )
            {
                cBlock.wait( nSec, nUSec );

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

            cBlock.lock();
            if( pStart == NULL )
            {
                cBlock.unlock();
                return T();
            }
            else
            {
                T pTmp = pStart->pData;
                Item *pDel = pStart;
                pStart = pStart->pNext;
                delete pDel;
                nSize--;
            
                cBlock.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()
        {
            cBlock.lock();
            bool bEmpty = (pStart == NULL );
            cBlock.unlock();

            return bEmpty;
        }

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

            return nRet;
        }

        void unblockAll()
        {
            cBlock.lock();
            cBlock.broadcast();
            cBlock.unlock();
        }

        void stop()
        {
            mRunning.lock();
            bRunning = false;
            mRunning.unlock();
            unblockAll();
        }

        bool isRunning() const
        {
            bool bRet;
            mRunning.lock();
            bRet = bRunning;
            mRunning.unlock();
            return bRet;
        }

    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. */

        Condition cBlock;   /**< The condition for blocking dequeues. */
        mutable Mutex mRunning;
        bool bRunning;
    };
}

#endif