1 /* Copyright (C) 2008 by Marc Maurer <uwog@uwog.net>
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License
5  * as published by the Free Software Foundation; either version 2
6  * of the License, or (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301 USA.
17  */
18 
19 #ifndef __SYNCHRONIZED_QUEUE__
20 #define __SYNCHRONIZED_QUEUE__
21 
22 #include <deque>
23 #include <boost/bind.hpp>
24 #include <boost/function.hpp>
25 #include <boost/noncopyable.hpp>
26 #include <sync/xp/lock.h>
27 #include <sync/xp/Synchronizer.h>
28 
29 class EmptyQueueException {};
30 
31 template <typename T>
32 class SynchronizedQueue : public Synchronizer, public asio::noncopyable
33 {
34 public:
SynchronizedQueue(boost::function<void (SynchronizedQueue &)> sig)35 	SynchronizedQueue(boost::function<void (SynchronizedQueue&)> sig)
36 		: Synchronizer(boost::bind(&SynchronizedQueue::_signal, this)),
37 		m_mutex(),
38 		m_queue(),
39 		m_sig(sig)
40 	{}
41 
push(T t)42 	void push(T t)
43 	{
44 		abicollab::scoped_lock lock(m_mutex);
45 		m_queue.push_back( t );
46 		Synchronizer::signal();
47 	}
48 
pop()49 	T pop()
50 	{
51 		if (m_queue.size() == 0)
52 			throw EmptyQueueException();
53 		abicollab::scoped_lock lock(m_mutex);
54 		T t = m_queue.front();
55 		m_queue.pop_front();
56 		return t;
57 	}
58 
peek()59 	bool peek()
60 	{
61 		return m_queue.size() > 0;
62 	}
63 
64 private:
_signal()65 	void _signal()
66 	{
67 		m_sig(*this);
68 	}
69 
70 	abicollab::mutex m_mutex;
71 	std::deque< T > m_queue;
72 	boost::function<void (SynchronizedQueue&)> m_sig;
73 };
74 
75 #endif /* __SYNCHRONIZED_QUEUE__ */
76