Stxxl  1.4.0
include/stxxl/bits/mng/read_write_pool.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  include/stxxl/bits/mng/read_write_pool.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 #ifndef STXXL_MNG_READ_WRITE_POOL_H
00014 #define STXXL_MNG_READ_WRITE_POOL_H
00015 
00016 #include <stxxl/bits/mng/write_pool.h>
00017 #include <stxxl/bits/mng/prefetch_pool.h>
00018 
00019 
00020 __STXXL_BEGIN_NAMESPACE
00021 
00022 //! \addtogroup schedlayer
00023 //! \{
00024 
00025 
00026 //! \brief Implements dynamically resizable buffered writing and prefetched reading pool
00027 template <typename BlockType>
00028 class read_write_pool : private noncopyable
00029 {
00030 public:
00031     typedef BlockType block_type;
00032     typedef typename block_type::bid_type bid_type;
00033     typedef unsigned_type size_type;
00034 
00035 protected:
00036     typedef write_pool<block_type> write_pool_type;
00037     typedef prefetch_pool<block_type> prefetch_pool_type;
00038 
00039     write_pool_type * w_pool;
00040     prefetch_pool_type * p_pool;
00041     bool delete_pools;
00042 
00043 public:
00044     //! \brief Constructs pool
00045     //! \param init_size_prefetch initial number of blocks in the prefetch pool
00046     //! \param init_size_write initial number of blocks in the write pool
00047     explicit read_write_pool(size_type init_size_prefetch = 1, size_type init_size_write = 1) :
00048         delete_pools(true)
00049     {
00050         w_pool = new write_pool_type(init_size_write);
00051         p_pool = new prefetch_pool_type(init_size_prefetch);
00052     }
00053 
00054     _STXXL_DEPRECATED(read_write_pool(prefetch_pool_type & p_pool, write_pool_type & w_pool)) :
00055         w_pool(&w_pool), p_pool(&p_pool), delete_pools(false)
00056     { }
00057 
00058     void swap(read_write_pool & obj)
00059     {
00060         std::swap(w_pool, obj.w_pool);
00061         std::swap(p_pool, obj.p_pool);
00062         std::swap(delete_pools, obj.delete_pools);
00063     }
00064 
00065     //! \brief Waits for completion of all ongoing requests and frees memory
00066     virtual ~read_write_pool()
00067     {
00068         if (delete_pools) {
00069             delete w_pool;
00070             delete p_pool;
00071         }
00072     }
00073 
00074     //! \brief Returns number of blocks owned by the write_pool
00075     size_type size_write() const { return w_pool->size(); }
00076 
00077     //! \brief Returns number of blocks owned by the prefetch_pool
00078     size_type size_prefetch() const { return p_pool->size(); }
00079 
00080     //! \brief Resizes size of the pool
00081     //! \param new_size new size of the pool after the call
00082     void resize_write(size_type new_size)
00083     {
00084         w_pool->resize(new_size);
00085     }
00086 
00087     //! \brief Resizes size of the pool
00088     //! \param new_size new size of the pool after the call
00089     void resize_prefetch(size_type new_size)
00090     {
00091         p_pool->resize(new_size);
00092     }
00093 
00094     // WRITE POOL METHODS
00095 
00096     //! \brief Passes a block to the pool for writing
00097     //! \param block block to write. Ownership of the block goes to the pool.
00098     //! \c block must be allocated dynamically with using \c new .
00099     //! \param bid location, where to write
00100     //! \warning \c block must be allocated dynamically with using \c new .
00101     //! \return request object of the write operation
00102     request_ptr write(block_type * & block, bid_type bid)
00103     {
00104         request_ptr result = w_pool->write(block, bid);
00105 
00106         // if there is a copy of this block in the prefetch pool,
00107         // it is now a stale copy, so invalidate it and re-hint the block
00108         if (p_pool->invalidate(bid))
00109             p_pool->hint(bid, *w_pool);
00110 
00111         return result;
00112     }
00113 
00114     //! \brief Take out a block from the pool
00115     //! \return pointer to the block. Ownership of the block goes to the caller.
00116     block_type * steal()
00117     {
00118         return w_pool->steal();
00119     }
00120 
00121     void add(block_type * & block)
00122     {
00123         w_pool->add(block);
00124     }
00125 
00126     // PREFETCH POOL METHODS
00127 
00128     //! \brief Gives a hint for prefetching a block
00129     //! \param bid address of a block to be prefetched
00130     //! \return \c true if there was a free block to do prefetch and prefetching
00131     //! was scheduled, \c false otherwise
00132     //! \note If there are no free blocks available (all blocks
00133     //! are already in reading or read but not retrieved by user calling \c read
00134     //! method) calling \c hint function has no effect
00135     bool hint(bid_type bid)
00136     {
00137         return p_pool->hint(bid, *w_pool);
00138     }
00139 
00140     bool invalidate(bid_type bid)
00141     {
00142         return p_pool->invalidate(bid);
00143     }
00144 
00145     //! \brief Reads block. If this block is cached block is not read but passed from the cache
00146     //! \param block block object, where data to be read to. If block was cached \c block 's
00147     //! ownership goes to the pool and block from cache is returned in \c block value.
00148     //! \param bid address of the block
00149     //! \warning \c block parameter must be allocated dynamically using \c new .
00150     //! \return request pointer object of read operation
00151     request_ptr read(block_type * & block, bid_type bid)
00152     {
00153         return p_pool->read(block, bid, *w_pool);
00154     }
00155 };
00156 
00157 //! \}
00158 
00159 __STXXL_END_NAMESPACE
00160 
00161 
00162 namespace std
00163 {
00164     template <class BlockType>
00165     void swap(stxxl::read_write_pool<BlockType> & a,
00166               stxxl::read_write_pool<BlockType> & b)
00167     {
00168         a.swap(b);
00169     }
00170 }
00171 
00172 #endif // !STXXL_MNG_READ_WRITE_POOL_H
00173 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines