Lunchbox  1.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mtQueue.h
1 
2 /* Copyright (c) 2005-2013, Stefan Eilemann <eile@equalizergraphics.com>
3  * 2012, Daniel Nachbaur <danielnachbaur@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 2.1 as published
7  * by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef LUNCHBOX_MTQUEUE_H
20 #define LUNCHBOX_MTQUEUE_H
21 
22 #include <lunchbox/condition.h>
23 #include <lunchbox/debug.h>
24 
25 #include <algorithm>
26 #include <limits.h>
27 #include <queue>
28 #include <string.h>
29 
30 namespace lunchbox
31 {
43 template< typename T, size_t S = ULONG_MAX > class MTQueue
44 // S = std::numeric_limits< size_t >::max() does not work:
45 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=6424
46 {
47 public:
48  class Group;
49  typedef T value_type;
50 
52  explicit MTQueue( const size_t maxSize = S ) : _maxSize( maxSize ) {}
53 
55  MTQueue( const MTQueue< T, S >& from ) { *this = from; }
56 
58  ~MTQueue() {}
59 
62 
67  const T& operator[]( const size_t index ) const;
68 
70  bool isEmpty() const { return _queue.empty(); }
71 
73  size_t getSize() const { return _queue.size(); }
74 
83  void setMaxSize( const size_t maxSize );
84 
86  size_t getMaxSize() const { return _maxSize; }
87 
94  size_t waitSize( const size_t minSize ) const;
95 
97  void clear();
98 
103  T pop();
104 
113  bool timedPop( const unsigned timeout, T& element );
114 
133  std::vector< T > timedPopRange( const unsigned timeout,
134  const size_t minimum = 1,
135  const size_t maximum = S );
136 
145  bool tryPop( T& result );
146 
158  void tryPop( const size_t num, std::vector< T >& result );
159 
173  bool popBarrier( T& result, Group& barrier );
174 
181  bool getFront( T& result ) const;
182 
189  bool getBack( T& result ) const;
190 
192  void push( const T& element );
193 
195  void push( const std::vector< T >& elements );
196 
198  void pushFront( const T& element );
199 
201  void pushFront( const std::vector< T >& elements );
202 
205  void push_back( const T& element ) { push( element ); }
206  bool empty() const { return isEmpty(); }
208 
209 private:
210  std::deque< T > _queue;
211  mutable Condition _cond;
212  size_t _maxSize;
213 };
214 }
215 
216 #include "mtQueue.ipp" // template implementation
217 
218 #endif //LUNCHBOX_MTQUEUE_H
std::vector< T > timedPopRange(const unsigned timeout, const size_t minimum=1, const size_t maximum=S)
Retrieve a number of items from the front of the queue.
Definition: mtQueue.ipp:121
bool tryPop(T &result)
Retrieve and pop the front element from the queue if it is not empty.
Definition: mtQueue.ipp:147
MTQueue< T, S > & operator=(const MTQueue< T, S > &from)
Assign the values of another queue.
Definition: mtQueue.ipp:22
void pushFront(const T &element)
Push a new element to the front of the queue.
Definition: mtQueue.ipp:280
size_t waitSize(const size_t minSize) const
Wait for the size to be at least the number of given elements.
Definition: mtQueue.ipp:65
void clear()
Reset (empty) the queue.
Definition: mtQueue.ipp:77
A thread-safe queue with a blocking read access.
Definition: mtQueue.h:43
const T & operator[](const size_t index) const
Retrieve the requested element from the queue, may block.
Definition: mtQueue.ipp:41
~MTQueue()
Destruct this Queue.
Definition: mtQueue.h:58
bool getFront(T &result) const
Definition: mtQueue.ipp:227
bool popBarrier(T &result, Group &barrier)
Retrieve the front element, or abort if the barrier is reached.
Definition: mtQueue.ipp:200
bool isEmpty() const
Definition: mtQueue.h:70
void setMaxSize(const size_t maxSize)
Set the new maximum size of the queue.
Definition: mtQueue.ipp:54
bool getBack(T &result) const
Definition: mtQueue.ipp:242
size_t getSize() const
Definition: mtQueue.h:73
bool timedPop(const unsigned timeout, T &element)
Retrieve and pop the front element from the queue.
Definition: mtQueue.ipp:101
MTQueue(const MTQueue< T, S > &from)
Construct a copy of a queue.
Definition: mtQueue.h:55
Group descriptor for popBarrier().
Definition: mtQueue.ipp:182
T pop()
Retrieve and pop the front element from the queue, may block.
Definition: mtQueue.ipp:86
MTQueue(const size_t maxSize=S)
Construct a new queue.
Definition: mtQueue.h:52
size_t getMaxSize() const
Definition: mtQueue.h:86
void push(const T &element)
Push a new element to the back of the queue.
Definition: mtQueue.ipp:257