STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
read_write_pool.h
Go to the documentation of this file.
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_HEADER
14 #define STXXL_MNG_READ_WRITE_POOL_HEADER
15 
18 
20 
21 //! \addtogroup schedlayer
22 //! \{
23 
24 //! Implements dynamically resizable buffered writing and prefetched reading pool.
25 template <typename BlockType>
27 {
28 public:
29  typedef BlockType block_type;
30  typedef typename block_type::bid_type bid_type;
32 
33 protected:
36 
40 
41 public:
42  //! Constructs pool.
43  //! \param init_size_prefetch initial number of blocks in the prefetch pool
44  //! \param init_size_write initial number of blocks in the write pool
45  explicit read_write_pool(size_type init_size_prefetch = 1, size_type init_size_write = 1)
46  : delete_pools(true)
47  {
48  w_pool = new write_pool_type(init_size_write);
49  p_pool = new prefetch_pool_type(init_size_prefetch);
50  }
51 
53  : w_pool(&w_pool), p_pool(&p_pool), delete_pools(false)
54  { }
55 
56  void swap(read_write_pool& obj)
57  {
58  std::swap(w_pool, obj.w_pool);
59  std::swap(p_pool, obj.p_pool);
60  std::swap(delete_pools, obj.delete_pools);
61  }
62 
63  //! Waits for completion of all ongoing requests and frees memory.
65  {
66  if (delete_pools) {
67  delete w_pool;
68  delete p_pool;
69  }
70  }
71 
72  //! Returns number of blocks owned by the write_pool.
73  size_type size_write() const { return w_pool->size(); }
74 
75  //! Returns number of blocks owned by the prefetch_pool.
76  size_type size_prefetch() const { return p_pool->size(); }
77 
78  //! Resizes size of the pool.
79  //! \param new_size new size of the pool after the call
80  void resize_write(size_type new_size)
81  {
82  w_pool->resize(new_size);
83  }
84 
85  //! Resizes size of the pool.
86  //! \param new_size new size of the pool after the call
87  void resize_prefetch(size_type new_size)
88  {
89  p_pool->resize(new_size);
90  }
91 
92  // WRITE POOL METHODS
93 
94  //! Passes a block to the pool for writing.
95  //! \param block block to write. Ownership of the block goes to the pool.
96  //! \c block must be allocated dynamically with using \c new .
97  //! \param bid location, where to write
98  //! \warning \c block must be allocated dynamically with using \c new .
99  //! \return request object of the write operation
101  {
102  request_ptr result = w_pool->write(block, bid);
103 
104  // if there is a copy of this block in the prefetch pool,
105  // it is now a stale copy, so invalidate it and re-hint the block
106  if (p_pool->invalidate(bid))
107  p_pool->hint(bid, *w_pool);
108 
109  return result;
110  }
111 
112  //! Take out a block from the pool.
113  //! \return pointer to the block. Ownership of the block goes to the caller.
115  {
116  return w_pool->steal();
117  }
118 
119  //! Add block to write pool
120  void add(block_type*& block)
121  {
122  w_pool->add(block);
123  }
124 
125  // PREFETCH POOL METHODS
126 
127  //! Gives a hint for prefetching a block.
128  //! \param bid address of a block to be prefetched
129  //! \return \c true if there was a free block to do prefetch and prefetching
130  //! was scheduled, \c false otherwise
131  //! \note If there are no free blocks available (all blocks
132  //! are already in reading or read but not retrieved by user calling \c read
133  //! method) calling \c hint function has no effect
134  bool hint(bid_type bid)
135  {
136  return p_pool->hint(bid, *w_pool);
137  }
138 
139  //! Cancel a hint request in case the block is no longer desired.
141  {
142  return p_pool->invalidate(bid);
143  }
144 
145  /*!
146  * Reads block. If this block is cached block is not read but passed from
147  * the cache.
148  *
149  * \param block block object, where data to be read to. If block was cached
150  * \c block 's ownership goes to the pool and block from cache is returned
151  * in \c block value.
152  *
153  * \param bid address of the block
154  * \warning \c block parameter must be allocated dynamically using \c new .
155  * \return request pointer object of read operation
156  */
158  {
159  return p_pool->read(block, bid, *w_pool);
160  }
161 
162  //! Returns the request pointer for a hinted block, or an invalid NULL
163  //! request in case it was not requested due to lack of prefetch buffers.
165  {
166  return p_pool->find(bid);
167  }
168 
169  //! Returns true if the blocks was hinted and the request is finished.
170  bool poll_hint(bid_type bid)
171  {
172  return p_pool->poll(bid);
173  }
174 
175  //! Add block to prefetch pool
176  void add_prefetch(block_type*& block)
177  {
178  p_pool->add(block);
179  }
180 
181  //! Take out a block from the prefetch pool, one unhinted free block must
182  //! be available.
183  //! \return pointer to the block. Ownership of the block goes to the caller.
185  {
186  return p_pool->steal();
187  }
188 
189  //! Checks if a block is in the hinted block set.
191  {
192  return p_pool->in_prefetching(bid);
193  }
194 
195  //! Returns the number of free prefetching blocks.
197  {
198  return p_pool->free_size();
199  }
200 
201  //! Returns the number of busy prefetching blocks.
203  {
204  return p_pool->busy_size();
205  }
206 };
207 
208 //! \}
209 
211 
212 namespace std {
213 
214 template <class BlockType>
217 {
218  a.swap(b);
219 }
220 
221 } // namespace std
222 
223 #endif // !STXXL_MNG_READ_WRITE_POOL_HEADER
224 // vim: et:ts=4:sw=4
bool invalidate(bid_type bid)
Cancel a hint request in case the block is no longer desired.
read_write_pool(size_type init_size_prefetch=1, size_type init_size_write=1)
Constructs pool.
void add_prefetch(block_type *&block)
Add block to prefetch pool.
bool hint(bid_type bid)
Gives a hint for prefetching a block.
unsigned_type free_size_prefetch() const
Returns the number of free prefetching blocks.
prefetch_pool< block_type > prefetch_pool_type
void swap(read_write_pool &obj)
request_ptr read(block_type *&block, bid_type bid)
Reads block.
unsigned_type busy_size_prefetch() const
Returns the number of busy prefetching blocks.
#define STXXL_DEPRECATED(x)
Definition: deprecated.h:30
~read_write_pool()
Waits for completion of all ongoing requests and frees memory.
bool in_prefetching(bid_type bid)
Checks if a block is in the hinted block set.
Implements dynamically resizable buffered writing pool.
Definition: write_pool.h:32
Implements dynamically resizable prefetching pool.
Definition: prefetch_pool.h:29
void add(block_type *&block)
Add block to write pool.
Implements dynamically resizable buffered writing and prefetched reading pool.
request_ptr find_hint(bid_type bid)
Returns the request pointer for a hinted block, or an invalid NULL request in case it was not request...
write_pool< block_type > write_pool_type
read_write_pool(prefetch_pool_type &p_pool, write_pool_type &w_pool)
request_ptr write(block_type *&block, bid_type bid)
Passes a block to the pool for writing.
void resize_prefetch(size_type new_size)
Resizes size of the pool.
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
block_type * steal_prefetch()
Take out a block from the prefetch pool, one unhinted free block must be available.
block_type * steal()
Take out a block from the pool.
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:64
size_type size_prefetch() const
Returns number of blocks owned by the prefetch_pool.
block_type::bid_type bid_type
bool poll_hint(bid_type bid)
Returns true if the blocks was hinted and the request is finished.
prefetch_pool_type * p_pool
void resize_write(size_type new_size)
Resizes size of the pool.
size_type size_write() const
Returns number of blocks owned by the write_pool.
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
write_pool_type * w_pool