Queue.h
1/*
2 * Copyright (C) 2010 Tommi Maekitalo
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef PT_SYSTEM_QUEUE_H
30#define PT_SYSTEM_QUEUE_H
31
32#include <Pt/System/Api.h>
33#include <Pt/System/Mutex.h>
34#include <Pt/System/Condition.h>
35#include <deque>
36
37namespace Pt {
38
39namespace System {
40
50template <typename T>
51class Queue
52{
53 public:
54 typedef T value_type;
55 typedef typename std::deque<T>::size_type size_type;
56 typedef typename std::deque<T>::const_reference const_reference;
57
58 private:
59 mutable Mutex _mutex;
60 Condition _notEmpty;
61 Condition _notFull;
62 std::deque<value_type> _queue;
63 size_type _maxSize;
64 size_type _numWaiting;
65
66 public:
69 : _maxSize(0)
70 , _numWaiting(0)
71 { }
72
78 value_type get();
79
86 void put(const_reference element);
87
89 bool empty() const
90 {
91 MutexLock lock(_mutex);
92 return _queue.empty();
93 }
94
96 size_type size() const
97 {
98 MutexLock lock(_mutex);
99 return _queue.size();
100 }
101
108 void maxSize(size_type m);
109
111 size_type maxSize() const
112 {
113 MutexLock lock(_mutex);
114 return _maxSize;
115 }
116
118 size_type numWaiting() const
119 {
120 MutexLock lock(_mutex);
121 return _numWaiting;
122 }
123};
124
125template <typename T>
126typename Queue<T>::value_type Queue<T>::get()
127{
128 MutexLock lock(_mutex);
129
130 ++_numWaiting;
131 while (_queue.empty())
132 _notEmpty.wait(lock);
133 --_numWaiting;
134
135 value_type element = _queue.front();
136 _queue.pop_front();
137
138 if (!_queue.empty())
139 _notEmpty.signal();
140
141 _notFull.signal();
142
143 return element;
144}
145
146template <typename T>
147void Queue<T>::put(typename Queue<T>::const_reference element)
148{
149 MutexLock lock(_mutex);
150
151 while (_maxSize > 0 && _queue.size() >= _maxSize)
152 _notFull.wait(lock);
153
154 _queue.push_back(element);
155 _notEmpty.signal();
156
157 if (_maxSize > 0 && _queue.size() < _maxSize)
158 _notFull.signal();
159}
160
161template <typename T>
162void Queue<T>::maxSize(size_type m)
163{
164 _maxSize = m;
165 MutexLock lock(_mutex);
166 if (_queue.size() < _maxSize)
167 _notFull.signal();
168}
169
170} // namespace System
171
172} // namespace Pt
173
174#endif // PT_SYSTEM_QUEUE_H
175
Signal and wait synchronisation primitive.
Definition Condition.h:51
Scoped lock for a Mutex.
Definition Mutex.h:127
Mutual exclusion device.
Definition Mutex.h:73
void put(const_reference element)
Adds a element to the queue.
Definition Queue.h:147
size_type size() const
Returns the number of elements currently in queue.
Definition Queue.h:96
bool empty() const
Returns true, if the queue is empty.
Definition Queue.h:89
size_type numWaiting() const
returns the number of threads blocked in the get method.
Definition Queue.h:118
Queue()
Default Constructor.
Definition Queue.h:68
value_type get()
Returns the next element.
Definition Queue.h:126
size_type maxSize() const
returns the maximum size of the queue.
Definition Queue.h:111
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33