Menu

SynchronizedBoundedBuffer.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2008-2010 Michael Schulze <mschulze@ivs.cs.uni-magdeburg.de>
00004  * All rights reserved.
00005  *
00006  *    Redistribution and use in source and binary forms, with or without
00007  *    modification, are permitted provided that the following conditions
00008  *    are met:
00009  *
00010  *    * Redistributions of source code must retain the above copyright
00011  *      notice, this list of conditions and the following disclaimer.
00012  *
00013  *    * Redistributions in binary form must reproduce the above copyright
00014  *      notice, this list of conditions and the following disclaimer in
00015  *      the documentation and/or other materials provided with the
00016  *      distribution.
00017  *
00018  *    * Neither the name of the copyright holders nor the names of
00019  *      contributors may be used to endorse or promote products derived
00020  *      from this software without specific prior written permission.
00021  *
00022  *
00023  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00024  *    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00025  *    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00026  *    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00027  *    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00028  *    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029  *    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00030  *    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00031  *    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  *
00036  * $Id$
00037  *
00038  ******************************************************************************/
00039 
00040 #ifndef __SynchronizedBoundedBuffer_h__
00041 #define __SynchronizedBoundedBuffer_h__
00042 
00043 #include <boost/thread/condition.hpp>
00044 #include <boost/thread/mutex.hpp>
00045 #include <boost/thread.hpp>
00046 #include <boost/bind.hpp>
00047 #include <boost/function.hpp>
00048 #include <boost/circular_buffer.hpp>
00049 #include <deque>
00050 #include "debug.h"
00051 
00052 namespace object {
00053 
00054     template <class T>
00055     class SynchronizedBoundedBuffer {
00056         public:
00057 
00058             typedef std::deque<T> container_type;
00059             typedef typename container_type::size_type size_type;
00060             typedef typename container_type::value_type value_type;
00061 
00062             explicit SynchronizedBoundedBuffer(size_type capacity) : m_capacity(capacity) {}
00063 
00064             void push_front(const value_type& item) {
00065                 boost::mutex::scoped_lock lock(m_mutex);
00066                 m_not_full.wait(lock, boost::bind(&SynchronizedBoundedBuffer<value_type>::is_not_full, this));
00067                 m_container.push_front(item);
00068                 lock.unlock();
00069                 m_not_empty.notify_one();
00070             }
00071 
00072             void pop_back(value_type* pItem) {
00073                 boost::mutex::scoped_lock lock(m_mutex);
00074                 m_not_empty.wait(lock, boost::bind(&SynchronizedBoundedBuffer<value_type>::is_not_empty, this));
00075                 *pItem = m_container.back();
00076                 m_container.pop_back();
00077                 lock.unlock();
00078                 m_not_full.notify_one();
00079             }
00080 
00081             bool is_not_empty() const {
00082                 return m_container.size() > 0;
00083             }
00084             bool is_not_full() const {
00085                 return m_container.size() < m_capacity;
00086             }
00087 
00088         private:
00089 
00090             SynchronizedBoundedBuffer(const SynchronizedBoundedBuffer&);              // Disabled copy constructor
00091             SynchronizedBoundedBuffer& operator = (const SynchronizedBoundedBuffer&); // Disabled assign operator
00092 
00093             const size_type m_capacity;
00094             container_type m_container;
00095             boost::mutex m_mutex;
00096             boost::condition m_not_empty;
00097             boost::condition m_not_full;
00098     };
00099 
00100 } /* namespace object */
00101 
00102 #endif
00103