Lunchbox  1.8.0
mtQueue.h
1 
2 /* Copyright (c) 2005-2012, 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 {
41  template< typename T, size_t S = ULONG_MAX > class MTQueue
42  // S = std::numeric_limits< size_t >::max() does not work:
43  // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=6424
44  {
45  public:
46  class Group;
47  typedef T value_type;
48 
50  MTQueue( size_t maxSize = S ) : _maxSize( maxSize ) {}
51 
53  MTQueue( const MTQueue< T, S >& from ) { *this = from; }
54 
56  ~MTQueue() {}
57 
60 
65  const T& operator[]( const size_t index ) const;
66 
68  bool isEmpty() const { return _queue.empty(); }
69 
71  size_t getSize() const { return _queue.size(); }
72 
81  void setMaxSize( const size_t maxSize );
82 
84  size_t getMaxSize() const { return _maxSize; }
85 
92  size_t waitSize( const size_t minSize ) const;
93 
95  void clear();
96 
101  T pop();
102 
111  bool timedPop( const unsigned timeout, T& element );
112 
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 
159  void tryPop( const size_t num, std::vector< T >& result );
160 
174  bool popBarrier( T& result, Group& barrier );
175 
182  bool getFront( T& result ) const;
183 
190  bool getBack( T& result ) const;
191 
193  void push( const T& element );
194 
196  void push( const std::vector< T >& elements );
197 
199  void pushFront( const T& element );
200 
202  void pushFront( const std::vector< T >& elements );
203 
206  void push_back( const T& element ) { push( element ); }
207  bool empty() const { return isEmpty(); }
209 
210  private:
211  std::deque< T > _queue;
212  mutable Condition _cond;
213  size_t _maxSize;
214  };
215 }
216 
217 #include "mtQueue.ipp" // template implementation
218 
219 #endif //LUNCHBOX_MTQUEUE_H