00001 /* Copyright (C) 2008 by Marc Maurer <uwog@uwog.net> 00002 * 00003 * This program is free software; you can redistribute it and/or 00004 * modify it under the terms of the GNU General Public License 00005 * as published by the Free Software Foundation; either version 2 00006 * of the License, or (at your option) any later version. 00007 * 00008 * This program is distributed in the hope that it will be useful, 00009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 * GNU General Public License for more details. 00012 * 00013 * You should have received a copy of the GNU General Public License 00014 * along with this program; if not, write to the Free Software 00015 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00016 * 02111-1307, USA. 00017 */ 00018 00019 #ifndef __SYNCHRONIZED_QUEUE__ 00020 #define __SYNCHRONIZED_QUEUE__ 00021 00022 #include <deque> 00023 #include <boost/bind.hpp> 00024 #include <boost/function.hpp> 00025 #include <sync/xp/lock.h> 00026 #include <sync/xp/Synchronizer.h> 00027 00028 class EmptyQueueException {}; 00029 00030 template <typename T> 00031 class SynchronizedQueue : public Synchronizer, public boost::noncopyable 00032 { 00033 public: 00034 SynchronizedQueue(boost::function<void (SynchronizedQueue&)> sig) 00035 : Synchronizer(boost::bind(&SynchronizedQueue::_signal, this)), 00036 m_mutex(), 00037 m_queue(), 00038 m_sig(sig) 00039 {} 00040 00041 void push(T t) 00042 { 00043 abicollab::scoped_lock lock(m_mutex); 00044 m_queue.push_back( t ); 00045 Synchronizer::signal(); 00046 } 00047 00048 T pop() 00049 { 00050 if (m_queue.size() == 0) 00051 throw EmptyQueueException(); 00052 abicollab::scoped_lock lock(m_mutex); 00053 T t = m_queue.front(); 00054 m_queue.pop_front(); 00055 return t; 00056 } 00057 00058 bool peek() 00059 { 00060 return m_queue.size() > 0; 00061 } 00062 00063 private: 00064 void _signal() 00065 { 00066 m_sig(*this); 00067 } 00068 00069 abicollab::mutex m_mutex; 00070 std::deque< T > m_queue; 00071 boost::function<void (SynchronizedQueue&)> m_sig; 00072 }; 00073 00074 #endif /* __SYNCHRONIZED_QUEUE__ */
1.7.1