Bumped copyright dates for 2013
[barry.git] / src / dataqueue.h
blob58a03c783fa38127d2ac85675f2658dc250b2360
1 ///
2 /// \file dataqueue.h
3 /// FIFO queue of Data objects
4 ///
6 /*
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__
25 #include "dll.h"
26 #include <list>
27 #include <pthread.h>
28 #include <iosfwd>
30 namespace Barry {
32 class Data;
35 // DataQueue class
37 /// This class provides a thread aware fifo queue for Data objects,
38 /// providing memory management for all Data object pointers it contains.
39 ///
40 /// It uses similar member names as std::queue<>, for consistency.
41 ///
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
52 queue_type m_queue;
54 protected:
55 void raw_push(Data *data);
56 Data* raw_pop();
58 public:
59 DataQueue();
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.
71 Data* pop();
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
83 // remain in other.
84 void append_from(DataQueue &other);
86 bool empty() const; // return true if empty
87 size_t size() const;
89 void DumpAll(std::ostream &os) const;
92 inline std::ostream& operator<< (std::ostream &os, const DataQueue &dq)
94 dq.DumpAll(os);
95 return os;
98 } // namespace Barry
100 #endif