# 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-2011, 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 <queue>
    00026 #include <pthread.h>
    00027
    00028 namespace Barry {
    00029
    00030 class Data;
    00031
    00032 //
    00033 // DataQueue class
    00034 //
    00035 /// This class provides a thread aware fifo queue for Data objects,
    00036 /// providing memory management for all Data object pointers it contains.
    00037 ///
    00038 /// It uses similar member names as std::queue<>, for consistency.
    00039 ///
    00040 class DataQueue
    00041 {
    00042         typedef std::queue<Data*>                       queue_type;
    00043
    00044         pthread_mutex_t m_waitMutex;
    00045         pthread_cond_t m_waitCond;
    00046
    00047         mutable pthread_mutex_t m_accessMutex;  // locked for each access of m_queue
    00048
    00049         queue_type m_queue;
    00050
    00051 public:
    00052         DataQueue();
    00053         ~DataQueue();           // frees all data in the queue
    00054
    00055         // Pushes data into the end of the queue.
    00056         // The queue owns this pointer as soon as the function is
    00057         // called.  In the case of an exception, it will be freed.
    00058         // Performs a thread broadcast once new data has been added.
    00059         void push(Data *data);
    00060
    00061         // Pops the next element off the front of the queue.
    00062         // Returns 0 if empty.
    00063         // The queue no longer owns this pointer upon return.
    00064         Data* pop();
    00065
    00066         // Pops the next element off the front of the queue, and
    00067         // waits until one exists if empty.  If still no data
    00068         // on timeout, returns null.
    00069         // Timeout specified in milliseconds.  Default is wait forever.
    00070         Data* wait_pop(int timeout = -1);
    00071
    00072         // Pops all data from other and appends it to this.
    00073         // After calling this function, other will be empty, and
    00074         // this will contain all its data.
    00075         // In the case of an exception, any uncopied data will
    00076         // remain in other.
    00077         void append_from(DataQueue &other);
    00078
    00079         bool empty() const;     // return true if empty
    00080         size_t size() const;
    00081 };
    00082
    00083 } // namespace Barry
    00084
    00085 #endif
    00086