14 #ifndef STXXL_WRITE_POOL_HEADER
15 #define STXXL_WRITE_POOL_HEADER
19 #ifdef STXXL_BOOST_CONFIG
20 #include <boost/config.hpp>
23 #include <stxxl/bits/noncopyable.h>
24 #include <stxxl/bits/deprecated.h>
25 #include <stxxl/bits/io/request_operations.h>
28 __STXXL_BEGIN_NAMESPACE
35 template <
class BlockType>
39 typedef BlockType block_type;
40 typedef typename block_type::bid_type bid_type;
49 busy_entry() : block(NULL) { }
50 busy_entry(
const busy_entry & a) : block(a.block), req(a.req), bid(a.bid) { }
51 busy_entry(block_type * & bl,
request_ptr & r, bid_type & bi) :
52 block(bl), req(r), bid(bi) { }
56 typedef typename std::list<block_type *>::iterator free_blocks_iterator;
57 typedef typename std::list<busy_entry>::iterator busy_blocks_iterator;
61 std::list<block_type *> free_blocks;
63 std::list<busy_entry> busy_blocks;
65 unsigned_type free_blocks_size, busy_blocks_size;
70 explicit write_pool(unsigned_type init_size = 1) : free_blocks_size(init_size), busy_blocks_size(0)
73 for ( ; i < init_size; ++i)
74 free_blocks.push_back(
new block_type);
79 std::swap(free_blocks, obj.free_blocks);
80 std::swap(busy_blocks, obj.busy_blocks);
81 std::swap(free_blocks_size, obj.free_blocks_size);
82 std::swap(busy_blocks_size, busy_blocks_size);
88 STXXL_VERBOSE2(
"write_pool::~write_pool free_blocks_size: " <<
89 free_blocks_size <<
" busy_blocks_size: " << busy_blocks_size);
90 while (!free_blocks.empty())
92 delete free_blocks.back();
93 free_blocks.pop_back();
98 for (busy_blocks_iterator i2 = busy_blocks.begin(); i2 != busy_blocks.end(); ++i2)
109 unsigned_type
size()
const {
return free_blocks_size + busy_blocks_size; }
119 STXXL_VERBOSE1(
"write_pool::write: " << block <<
" @ " << bid);
120 for (busy_blocks_iterator i2 = busy_blocks.begin(); i2 != busy_blocks.end(); ++i2)
122 if (i2->bid == bid) {
123 assert(i2->block != block);
124 STXXL_VERBOSE1(
"WAW dependency");
134 busy_blocks.push_back(busy_entry(block, result, bid));
144 if (free_blocks_size)
146 STXXL_VERBOSE1(
"write_pool::steal : " << free_blocks_size <<
" free blocks available");
148 block_type * p = free_blocks.back();
149 free_blocks.pop_back();
152 STXXL_VERBOSE1(
"write_pool::steal : all " << busy_blocks_size <<
" are busy");
153 busy_blocks_iterator completed =
wait_any(busy_blocks.begin(), busy_blocks.end());
154 assert(completed != busy_blocks.end());
155 assert(completed->req->poll());
156 block_type * p = completed->block;
157 busy_blocks.erase(completed);
164 _STXXL_DEPRECATED(block_type *
get())
173 int_type diff = int_type(new_size) - int_type(
size());
176 free_blocks_size += diff;
178 free_blocks.push_back(
new block_type);
187 _STXXL_DEPRECATED(
request_ptr get_request(bid_type bid))
189 busy_blocks_iterator i2 = busy_blocks.begin();
190 for ( ; i2 != busy_blocks.end(); ++i2)
198 bool has_request(bid_type bid)
200 for (busy_blocks_iterator i2 = busy_blocks.begin(); i2 != busy_blocks.end(); ++i2)
208 _STXXL_DEPRECATED(block_type *
steal(bid_type bid))
210 busy_blocks_iterator i2 = busy_blocks.begin();
211 for ( ; i2 != busy_blocks.end(); ++i2)
215 block_type * p = i2->block;
217 busy_blocks.erase(i2);
226 std::pair<block_type *, request_ptr> steal_request(bid_type bid)
228 for (busy_blocks_iterator i2 = busy_blocks.begin(); i2 != busy_blocks.end(); ++i2)
233 block_type * blk = i2->block;
235 busy_blocks.erase(i2);
239 return std::pair<block_type *, request_ptr>(blk, req);
243 return std::pair<block_type *, request_ptr>((block_type *)NULL,
request_ptr());
246 void add(block_type * & block)
248 free_blocks.push_back(block);
254 void check_all_busy()
256 busy_blocks_iterator cur = busy_blocks.begin();
258 while (cur != busy_blocks.end())
260 if (cur->req->poll())
262 free_blocks.push_back(cur->block);
263 cur = busy_blocks.erase(cur);
271 STXXL_VERBOSE1(
"write_pool::check_all_busy : " << cnt <<
272 " are completed out of " << busy_blocks_size + cnt <<
" busy blocks");
278 __STXXL_END_NAMESPACE
283 template <
class BlockType>
284 void swap(stxxl::write_pool<BlockType> & a,
285 stxxl::write_pool<BlockType> & b)
291 #endif // !STXXL_WRITE_POOL_HEADER
request_ptr write(block_type *&block, bid_type bid)
Passes a block to the pool for writing.
Definition: write_pool.h:117
Implements dynamically resizable buffered writing pool.
Definition: write_pool.h:36
void resize(unsigned_type new_size)
Resizes size of the pool.
Definition: write_pool.h:171
virtual ~write_pool()
Waits for completion of all ongoing write requests and frees memory.
Definition: write_pool.h:86
block_type * steal()
Take out a block from the pool.
Definition: write_pool.h:141
unsigned_type size() const
Returns number of owned blocks.
Definition: write_pool.h:109
Implemented as reference counting smart pointer.
Definition: request_ptr.h:34
request_iterator_ wait_any(request_iterator_ reqs_begin, request_iterator_ reqs_end)
Suspends calling thread until any of requests is completed.
Definition: request_operations.h:108
write_pool(unsigned_type init_size=1)
Constructs pool.
Definition: write_pool.h:70