14 #ifndef STXXL_QUEUE_HEADER
15 #define STXXL_QUEUE_HEADER
21 #include <stxxl/bits/deprecated.h>
22 #include <stxxl/bits/mng/mng.h>
23 #include <stxxl/bits/mng/typed_block.h>
24 #include <stxxl/bits/common/simple_vector.h>
25 #include <stxxl/bits/common/tmeta.h>
26 #include <stxxl/bits/mng/read_write_pool.h>
27 #include <stxxl/bits/mng/write_pool.h>
28 #include <stxxl/bits/mng/prefetch_pool.h>
31 __STXXL_BEGIN_NAMESPACE
33 #ifndef STXXL_VERBOSE_QUEUE
34 #define STXXL_VERBOSE_QUEUE STXXL_VERBOSE2
47 template <
class ValTp,
48 unsigned BlkSz = STXXL_DEFAULT_BLOCK_SIZE(ValTp),
49 class AllocStr = STXXL_DEFAULT_ALLOC_STRATEGY,
50 class SzTp = stxxl::uint64>
51 class queue :
private noncopyable
54 typedef ValTp value_type;
55 typedef AllocStr alloc_strategy_type;
56 typedef SzTp size_type;
72 value_type * front_element;
73 value_type * back_element;
74 alloc_strategy_type alloc_strategy;
75 unsigned_type alloc_count;
76 std::deque<bid_type> bids;
78 unsigned_type blocks2prefetch;
87 explicit queue(unsigned_type w_pool_size = 3, unsigned_type p_pool_size = 1,
int blocks2prefetch_ = -1) :
93 STXXL_VERBOSE_QUEUE(
"queue[" <<
this <<
"]::queue(sizes)");
94 pool =
new pool_type(p_pool_size, w_pool_size);
95 init(blocks2prefetch_);
113 STXXL_VERBOSE_QUEUE(
"queue[" <<
this <<
"]::queue(pools)");
115 init(blocks2prefetch_);
132 STXXL_VERBOSE_QUEUE(
"queue[" <<
this <<
"]::queue(pool)");
133 init(blocks2prefetch_);
137 void init(
int blocks2prefetch_)
140 STXXL_ERRMSG(
"queue: invalid configuration, not enough blocks (" << pool->
size_write() <<
141 ") in write pool, at least 2 are needed, resizing to 3");
146 STXXL_MSG(
"queue: inefficient configuration, no blocks for buffered writing available");
150 STXXL_MSG(
"queue: inefficient configuration, no blocks for prefetching available");
153 front_block = back_block = pool->
steal();
154 back_element = back_block->
begin() - 1;
155 front_element = back_block->
begin();
166 if (blocks2prefetch_ < 0)
169 blocks2prefetch = blocks2prefetch_;
175 return blocks2prefetch;
179 void push(
const value_type & val)
184 if (front_block == back_block)
187 STXXL_VERBOSE1(
"queue::push Case 1");
191 STXXL_VERBOSE1(
"queue::push Case 2");
196 bm->
new_block(alloc_strategy, newbid, alloc_count++);
198 STXXL_VERBOSE_QUEUE(
"queue[" <<
this <<
"]: push block " << back_block <<
" @ " << FMT_BID(newbid));
199 bids.push_back(newbid);
200 pool->
write(back_block, newbid);
201 if (bids.size() <= blocks2prefetch) {
202 STXXL_VERBOSE1(
"queue::push Case Hints");
206 back_block = pool->
steal();
208 back_element = back_block->
begin();
226 if (back_block == front_block)
228 STXXL_VERBOSE1(
"queue::pop Case 3");
230 assert(back_element == front_element);
231 assert(bids.empty());
233 back_element = back_block->
begin() - 1;
234 front_element = back_block->
begin();
242 STXXL_VERBOSE1(
"queue::pop Case 4");
243 assert(bids.empty());
245 pool->add(front_block);
246 front_block = back_block;
247 front_element = back_block->
begin();
250 STXXL_VERBOSE1(
"queue::pop Case 5");
252 assert(!bids.empty());
254 STXXL_VERBOSE_QUEUE(
"queue[" <<
this <<
"]: pop block " << front_block <<
" @ " << FMT_BID(bids.front()));
257 for (unsigned_type i = 0; i < blocks2prefetch && i < bids.size() - 1; ++i)
259 STXXL_VERBOSE1(
"queue::pop Case Hints");
260 pool->
hint(bids[i + 1]);
263 front_element = front_block->
begin();
291 return *back_element;
295 const value_type &
back()
const
298 return *back_element;
305 return *front_element;
312 return *front_element;
317 pool->add(front_block);
318 if (front_block != back_block)
319 pool->add(back_block);
334 __STXXL_END_NAMESPACE
336 #endif // !STXXL_QUEUE_HEADER
queue(pool_type &pool_, int blocks2prefetch_=-1)
Constructs empty queue.
Definition: queue.h:125
Block containing elements of fixed length.
Definition: typed_block.h:223
number of elements in block
Definition: typed_block.h:240
value_type & back()
Returns a mutable reference at the back of the queue.
Definition: queue.h:288
value_type & front()
Returns a mutable reference at the front of the queue.
Definition: queue.h:302
bool empty() const
Returns true if queue is empty.
Definition: queue.h:282
Implements dynamically resizable prefetching pool.
Definition: prefetch_pool.h:34
iterator begin()
Returns iterator pointing to the first element.
Definition: typed_block.h:81
External FIFO queue container.
Definition: queue.h:51
const value_type & front() const
Returns a const reference at the front of the queue.
Definition: queue.h:309
Implements dynamically resizable buffered writing pool.
Definition: write_pool.h:36
Implements dynamically resizable buffered writing and prefetched reading pool.
Definition: read_write_pool.h:28
void resize_write(size_type new_size)
Resizes size of the pool.
Definition: read_write_pool.h:82
void delete_blocks(const BIDIteratorClass &bidbegin, const BIDIteratorClass &bidend)
Deallocates blocks.
Definition: mng.h:218
bool hint(bid_type bid)
Gives a hint for prefetching a block.
Definition: read_write_pool.h:135
size_type size() const
Returns the size of the queue.
Definition: queue.h:276
void push(const value_type &val)
Adds an element in the queue.
Definition: queue.h:179
unsigned_type get_prefetch_aggr() const
Returns the number of blocks prefetched from the front side.
Definition: queue.h:173
Implemented as reference counting smart pointer.
Definition: request_ptr.h:34
void new_block(const DiskAssignFunctor &functor, BID< BLK_SIZE > &bid, unsigned_type offset=0)
Definition: mng.h:131
request_ptr write(block_type *&block, bid_type bid)
Passes a block to the pool for writing.
Definition: read_write_pool.h:102
void delete_block(const BID< BLK_SIZE > &bid)
Deallocates a block.
Definition: mng.h:204
const value_type & back() const
Returns a const reference at the back of the queue.
Definition: queue.h:295
void set_prefetch_aggr(int_type blocks2prefetch_)
Defines the number of blocks to prefetch (front side) This method should be called whenever the prefe...
Definition: queue.h:164
request_ptr read(block_type *&block, bid_type bid)
Reads block. If this block is cached block is not read but passed from the cache. ...
Definition: read_write_pool.h:151
Block manager class.
Definition: mng.h:59
size_type size_prefetch() const
Returns number of blocks owned by the prefetch_pool.
Definition: read_write_pool.h:78
queue(unsigned_type w_pool_size=3, unsigned_type p_pool_size=1, int blocks2prefetch_=-1)
Constructs empty queue with own write and prefetch block pool.
Definition: queue.h:87
block_type * steal()
Take out a block from the pool.
Definition: read_write_pool.h:116
_STXXL_DEPRECATED(queue(write_pool< block_type > &w_pool, prefetch_pool< block_type > &p_pool, int blocks2prefetch_=-1))
Constructs empty queue.
Definition: queue.h:106
size_type size_write() const
Returns number of blocks owned by the write_pool.
Definition: read_write_pool.h:75
void pop()
Removes element from the queue.
Definition: queue.h:219
Block identifier class.
Definition: bid.h:40