Stxxl  1.3.2
read_write_pool.h
1 /***************************************************************************
2  * include/stxxl/bits/mng/read_write_pool.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2009 Andreas Beckmann <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_MNG_READ_WRITE_POOL_H
14 #define STXXL_MNG_READ_WRITE_POOL_H
15 
16 #include <stxxl/bits/mng/write_pool.h>
17 #include <stxxl/bits/mng/prefetch_pool.h>
18 
19 
20 __STXXL_BEGIN_NAMESPACE
21 
24 
25 
27 template <typename BlockType>
28 class read_write_pool : private noncopyable
29 {
30 public:
31  typedef BlockType block_type;
32  typedef typename block_type::bid_type bid_type;
33  typedef unsigned_type size_type;
34 
35 protected:
38 
39  write_pool_type * w_pool;
40  prefetch_pool_type * p_pool;
41  bool delete_pools;
42 
43 public:
47  explicit read_write_pool(size_type init_size_prefetch = 1, size_type init_size_write = 1) :
48  delete_pools(true)
49  {
50  w_pool = new write_pool_type(init_size_write);
51  p_pool = new prefetch_pool_type(init_size_prefetch);
52  }
53 
54  _STXXL_DEPRECATED(read_write_pool(prefetch_pool_type & p_pool, write_pool_type & w_pool)) :
55  w_pool(&w_pool), p_pool(&p_pool), delete_pools(false)
56  { }
57 
58  void swap(read_write_pool & obj)
59  {
60  std::swap(w_pool, obj.w_pool);
61  std::swap(p_pool, obj.p_pool);
62  std::swap(delete_pools, obj.delete_pools);
63  }
64 
66  virtual ~read_write_pool()
67  {
68  if (delete_pools) {
69  delete w_pool;
70  delete p_pool;
71  }
72  }
73 
75  size_type size_write() const { return w_pool->size(); }
76 
78  size_type size_prefetch() const { return p_pool->size(); }
79 
82  void resize_write(size_type new_size)
83  {
84  w_pool->resize(new_size);
85  }
86 
89  void resize_prefetch(size_type new_size)
90  {
91  p_pool->resize(new_size);
92  }
93 
94  // WRITE POOL METHODS
95 
102  request_ptr write(block_type * & block, bid_type bid)
103  {
104  request_ptr result = w_pool->write(block, bid);
105 
106  // if there is a copy of this block in the prefetch pool,
107  // it is now a stale copy, so invalidate it and re-hint the block
108  if (p_pool->invalidate(bid))
109  p_pool->hint(bid, *w_pool);
110 
111  return result;
112  }
113 
116  block_type * steal()
117  {
118  return w_pool->steal();
119  }
120 
121  void add(block_type * & block)
122  {
123  w_pool->add(block);
124  }
125 
126  // PREFETCH POOL METHODS
127 
135  bool hint(bid_type bid)
136  {
137  return p_pool->hint(bid, *w_pool);
138  }
139 
140  bool invalidate(bid_type bid)
141  {
142  return p_pool->invalidate(bid);
143  }
144 
151  request_ptr read(block_type * & block, bid_type bid)
152  {
153  return p_pool->read(block, bid, *w_pool);
154  }
155 };
156 
158 
159 __STXXL_END_NAMESPACE
160 
161 
162 namespace std
163 {
164  template <class BlockType>
165  void swap(stxxl::read_write_pool<BlockType> & a,
166  stxxl::read_write_pool<BlockType> & b)
167  {
168  a.swap(b);
169  }
170 }
171 
172 #endif // !STXXL_MNG_READ_WRITE_POOL_H
173 // vim: et:ts=4:sw=4
bool hint(bid_type bid)
Gives a hint for prefetching a block.
Definition: prefetch_pool.h:121
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 prefetching pool.
Definition: prefetch_pool.h:34
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 resize(unsigned_type new_size)
Resizes size of the pool.
Definition: write_pool.h:171
unsigned_type resize(unsigned_type new_size)
Resizes size of the pool.
Definition: prefetch_pool.h:261
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: prefetch_pool.h:203
bool hint(bid_type bid)
Gives a hint for prefetching a block.
Definition: read_write_pool.h:135
block_type * steal()
Take out a block from the pool.
Definition: write_pool.h:141
void resize_prefetch(size_type new_size)
Resizes size of the pool.
Definition: read_write_pool.h:89
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
unsigned_type size() const
Returns number of owned blocks.
Definition: prefetch_pool.h:112
read_write_pool(size_type init_size_prefetch=1, size_type init_size_write=1)
Constructs pool.
Definition: read_write_pool.h:47
request_ptr write(block_type *&block, bid_type bid)
Passes a block to the pool for writing.
Definition: read_write_pool.h:102
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
virtual ~read_write_pool()
Waits for completion of all ongoing requests and frees memory.
Definition: read_write_pool.h:66
size_type size_prefetch() const
Returns number of blocks owned by the prefetch_pool.
Definition: read_write_pool.h:78
block_type * steal()
Take out a block from the pool.
Definition: read_write_pool.h:116
size_type size_write() const
Returns number of blocks owned by the write_pool.
Definition: read_write_pool.h:75