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 
37 namespace Pt {
38 
39 namespace System {
40 
49 template <typename T>
50 class Queue
51 {
52  public:
53  typedef T value_type;
54  typedef typename std::deque<T>::size_type size_type;
55  typedef typename std::deque<T>::const_reference const_reference;
56 
57  private:
58  mutable Mutex _mutex;
59  Condition _notEmpty;
60  Condition _notFull;
61  std::deque<value_type> _queue;
62  size_type _maxSize;
63  size_type _numWaiting;
64 
65  public:
68  : _maxSize(0)
69  , _numWaiting(0)
70  { }
71 
77  value_type get();
78 
85  void put(const_reference element);
86 
88  bool empty() const
89  {
90  MutexLock lock(_mutex);
91  return _queue.empty();
92  }
93 
95  size_type size() const
96  {
97  MutexLock lock(_mutex);
98  return _queue.size();
99  }
100 
107  void maxSize(size_type m);
108 
110  size_type maxSize() const
111  {
112  MutexLock lock(_mutex);
113  return _maxSize;
114  }
115 
117  size_type numWaiting() const
118  {
119  MutexLock lock(_mutex);
120  return _numWaiting;
121  }
122 };
123 
124 template <typename T>
125 typename Queue<T>::value_type Queue<T>::get()
126 {
127  MutexLock lock(_mutex);
128 
129  ++_numWaiting;
130  while (_queue.empty())
131  _notEmpty.wait(lock);
132  --_numWaiting;
133 
134  value_type element = _queue.front();
135  _queue.pop_front();
136 
137  if (!_queue.empty())
138  _notEmpty.signal();
139 
140  _notFull.signal();
141 
142  return element;
143 }
144 
145 template <typename T>
146 void Queue<T>::put(typename Queue<T>::const_reference element)
147 {
148  MutexLock lock(_mutex);
149 
150  while (_maxSize > 0 && _queue.size() >= _maxSize)
151  _notFull.wait(lock);
152 
153  _queue.push_back(element);
154  _notEmpty.signal();
155 
156  if (_maxSize > 0 && _queue.size() < _maxSize)
157  _notFull.signal();
158 }
159 
160 template <typename T>
161 void Queue<T>::maxSize(size_type m)
162 {
163  _maxSize = m;
164  MutexLock lock(_mutex);
165  if (_queue.size() < _maxSize)
166  _notFull.signal();
167 }
168 
169 } // namespace System
170 
171 } // namespace Pt
172 
173 #endif // PT_SYSTEM_QUEUE_H
174 
size_type numWaiting() const
returns the number of threads blocked in the get method.
Definition: Queue.h:117
Signal and wait synchronisation promitive.
Definition: Condition.h:43
void put(const_reference element)
Adds a element to the queue.
Definition: Queue.h:146
value_type get()
Returns the next element.
Definition: Queue.h:125
bool empty() const
Returns true, if the queue is empty.
Definition: Queue.h:88
Mutual exclusion device.
Definition: Mutex.h:48
This class implements a thread safe queue.
Definition: Queue.h:50
size_type size() const
Returns the number of elements currently in queue.
Definition: Queue.h:95
Queue()
Default Constructor.
Definition: Queue.h:67
size_type maxSize() const
returns the maximum size of the queue.
Definition: Queue.h:110
MutexLock class for Mutex.
Definition: Mutex.h:126