dataqueue.h

Go to the documentation of this file.
00001 ///
00002 /// \file       dataqueue.h
00003 ///             FIFO queue of Data objects
00004 ///
00005 
00006 /*
00007     Copyright (C) 2007-2012, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #ifndef __BARRY_DATAQUEUE_H__
00023 #define __BARRY_DATAQUEUE_H__
00024 
00025 #include <list>
00026 #include <pthread.h>
00027 #include <iosfwd>
00028 
00029 namespace Barry {
00030 
00031 class Data;
00032 
00033 //
00034 // DataQueue class
00035 //
00036 /// This class provides a thread aware fifo queue for Data objects,
00037 /// providing memory management for all Data object pointers it contains.
00038 ///
00039 /// It uses similar member names as std::queue<>, for consistency.
00040 ///
00041 class DataQueue
00042 {
00043         // always use the raw_push() and raw_pop() functions
00044         typedef std::list<Data*>                        queue_type;
00045 
00046         pthread_mutex_t m_waitMutex;
00047         pthread_cond_t m_waitCond;
00048 
00049         mutable pthread_mutex_t m_accessMutex;  // locked for each access of m_queue
00050 
00051         queue_type m_queue;
00052 
00053 protected:
00054         void raw_push(Data *data);
00055         Data* raw_pop();
00056 
00057 public:
00058         DataQueue();
00059         ~DataQueue();           // frees all data in the queue
00060 
00061         // Pushes data into the end of the queue.
00062         // The queue owns this pointer as soon as the function is
00063         // called.  In the case of an exception, it will be freed.
00064         // Performs a thread broadcast once new data has been added.
00065         void push(Data *data);
00066 
00067         // Pops the next element off the front of the queue.
00068         // Returns 0 if empty.
00069         // The queue no longer owns this pointer upon return.
00070         Data* pop();
00071 
00072         // Pops the next element off the front of the queue, and
00073         // waits until one exists if empty.  If still no data
00074         // on timeout, returns null.
00075         // Timeout specified in milliseconds.  Default is wait forever.
00076         Data* wait_pop(int timeout = -1);
00077 
00078         // Pops all data from other and appends it to this.
00079         // After calling this function, other will be empty, and
00080         // this will contain all its data.
00081         // In the case of an exception, any uncopied data will
00082         // remain in other.
00083         void append_from(DataQueue &other);
00084 
00085         bool empty() const;     // return true if empty
00086         size_t size() const;
00087 
00088         void DumpAll(std::ostream &os) const;
00089 };
00090 
00091 inline std::ostream& operator<< (std::ostream &os, const DataQueue &dq)
00092 {
00093         dq.DumpAll(os);
00094         return os;
00095 }
00096 
00097 } // namespace Barry
00098 
00099 #endif
00100