3 /// FIFO queue of Data objects
7 Copyright (C) 2007-2013, 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__
37 /// This class provides a thread aware fifo queue for Data objects,
38 /// providing memory management for all Data object pointers it contains.
40 /// It uses similar member names as std::queue<>, for consistency.
42 class BXEXPORT DataQueue
44 // always use the raw_push() and raw_pop() functions
45 typedef std::list
<Data
*> queue_type
;
47 pthread_mutex_t m_waitMutex
;
48 pthread_cond_t m_waitCond
;
50 mutable pthread_mutex_t m_accessMutex
; // locked for each access of m_queue
55 void raw_push(Data
*data
);
60 ~DataQueue(); // frees all data in the queue
62 // Pushes data into the end of the queue.
63 // The queue owns this pointer as soon as the function is
64 // called. In the case of an exception, it will be freed.
65 // Performs a thread broadcast once new data has been added.
66 void push(Data
*data
);
68 // Pops the next element off the front of the queue.
69 // Returns 0 if empty.
70 // The queue no longer owns this pointer upon return.
73 // Pops the next element off the front of the queue, and
74 // waits until one exists if empty. If still no data
75 // on timeout, returns null.
76 // Timeout specified in milliseconds. Default is wait forever.
77 Data
* wait_pop(int timeout
= -1);
79 // Pops all data from other and appends it to this.
80 // After calling this function, other will be empty, and
81 // this will contain all its data.
82 // In the case of an exception, any uncopied data will
84 void append_from(DataQueue
&other
);
86 bool empty() const; // return true if empty
89 void DumpAll(std::ostream
&os
) const;
92 inline std::ostream
& operator<< (std::ostream
&os
, const DataQueue
&dq
)