3 /// FIFO queue of Data objects
7 Copyright (C) 2007-2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_DATAQUEUE_H__
23 #define __BARRY_DATAQUEUE_H__
35 /// This class provides a thread aware fifo queue for Data objects,
36 /// providing memory management for all Data object pointers it contains.
38 /// It uses similar member names as std::queue<>, for consistency.
42 typedef std::queue
<Data
*> queue_type
;
44 pthread_mutex_t m_waitMutex
;
45 pthread_cond_t m_waitCond
;
47 mutable pthread_mutex_t m_accessMutex
; // locked for each access of m_queue
53 ~DataQueue(); // frees all data in the queue
55 // Pushes data into the end of the queue.
56 // The queue owns this pointer as soon as the function is
57 // called. In the case of an exception, it will be freed.
58 // Performs a thread broadcast once new data has been added.
59 void push(Data
*data
);
61 // Pops the next element off the front of the queue.
62 // Returns 0 if empty.
63 // The queue no longer owns this pointer upon return.
66 // Pops the next element off the front of the queue, and
67 // waits until one exists if empty. If still no data
68 // on timeout, returns null.
69 // Timeout specified in milliseconds. Default is wait forever.
70 Data
* wait_pop(int timeout
= -1);
72 // Pops all data from other and appends it to this.
73 // After calling this function, other will be empty, and
74 // this will contain all its data.
75 // In the case of an exception, any uncopied data will
77 void append_from(DataQueue
&other
);
79 bool empty() const; // return true if empty