STXXL  1.4.0
 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 
19 
21 
22 //! \addtogroup schedlayer
23 //! \{
24 
25 //! Implements dynamically resizable buffered writing and prefetched reading pool.
26 template <typename BlockType>
28 {
29 public:
30  typedef BlockType block_type;
31  typedef typename block_type::bid_type bid_type;
33 
34 protected:
37 
41 
42 public:
43  //! Constructs pool.
44  //! \param init_size_prefetch initial number of blocks in the prefetch pool
45  //! \param init_size_write initial number of blocks in the write pool
46  explicit read_write_pool(size_type init_size_prefetch = 1, size_type init_size_write = 1) :
47  delete_pools(true)
48  {
49  w_pool = new write_pool_type(init_size_write);
50  p_pool = new prefetch_pool_type(init_size_prefetch);
51  }
52 
54  w_pool(&w_pool), p_pool(&p_pool), delete_pools(false)
55  { }
56 
57  void swap(read_write_pool& obj)
58  {
59  std::swap(w_pool, obj.w_pool);
60  std::swap(p_pool, obj.p_pool);
61  std::swap(delete_pools, obj.delete_pools);
62  }
63 
64  //! Waits for completion of all ongoing requests and frees memory.
65  virtual ~read_write_pool()
66  {
67  if (delete_pools) {
68  delete w_pool;
69  delete p_pool;
70  }
71  }
72 
73  //! Returns number of blocks owned by the write_pool.
74  size_type size_write() const { return w_pool->size(); }
75 
76  //! Returns number of blocks owned by the prefetch_pool.
77  size_type size_prefetch() const { return p_pool->size(); }
78 
79  //! Resizes size of the pool.
80  //! \param new_size new size of the pool after the call
81  void resize_write(size_type new_size)
82  {
83  w_pool->resize(new_size);
84  }
85 
86  //! Resizes size of the pool.
87  //! \param new_size new size of the pool after the call
88  void resize_prefetch(size_type new_size)
89  {
90  p_pool->resize(new_size);
91  }
92 
93  // WRITE POOL METHODS
94 
95  //! Passes a block to the pool for writing.
96  //! \param block block to write. Ownership of the block goes to the pool.
97  //! \c block must be allocated dynamically with using \c new .
98  //! \param bid location, where to write
99  //! \warning \c block must be allocated dynamically with using \c new .
100  //! \return request object of the write operation
102  {
103  request_ptr result = w_pool->write(block, bid);
104 
105  // if there is a copy of this block in the prefetch pool,
106  // it is now a stale copy, so invalidate it and re-hint the block
107  if (p_pool->invalidate(bid))
108  p_pool->hint(bid, *w_pool);
109 
110  return result;
111  }
112 
113  //! Take out a block from the pool.
114  //! \return pointer to the block. Ownership of the block goes to the caller.
116  {
117  return w_pool->steal();
118  }
119 
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 
140  {
141  return p_pool->invalidate(bid);
142  }
143 
144  //! Reads block. If this block is cached block is not read but passed from the cache.
145  //! \param block block object, where data to be read to. If block was cached \c block 's
146  //! ownership goes to the pool and block from cache is returned in \c block value.
147  //! \param bid address of the block
148  //! \warning \c block parameter must be allocated dynamically using \c new .
149  //! \return request pointer object of read operation
151  {
152  return p_pool->read(block, bid, *w_pool);
153  }
154 };
155 
156 //! \}
157 
159 
160 
161 namespace std {
162 
163 template <class BlockType>
166 {
167  a.swap(b);
168 }
169 
170 } // namespace std
171 
172 #endif // !STXXL_MNG_READ_WRITE_POOL_HEADER
173 // vim: et:ts=4:sw=4
bool invalidate(bid_type bid)
read_write_pool(size_type init_size_prefetch=1, size_type init_size_write=1)
Constructs pool.
bool hint(bid_type bid)
Gives a hint for prefetching a block.
prefetch_pool< block_type > prefetch_pool_type
void swap(read_write_pool &obj)
virtual ~read_write_pool()
Waits for completion of all ongoing requests and frees memory.
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. ...
#define STXXL_DEPRECATED(x)
Definition: deprecated.h:30
Implements dynamically resizable buffered writing pool.
Definition: write_pool.h:34
Implements dynamically resizable prefetching pool.
Definition: prefetch_pool.h:30
void add(block_type *&block)
Implements dynamically resizable buffered writing and prefetched reading pool.
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()
Take out a block from the pool.
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
size_type size_prefetch() const
Returns number of blocks owned by the prefetch_pool.
block_type::bid_type bid_type
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