Stxxl  1.4.0
mng/test_read_write_pool.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  mng/test_read_write_pool.cpp
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 //! \example mng/test_read_write_pool.cpp
00014 
00015 #include <iostream>
00016 #include <stxxl/mng>
00017 #include <stxxl/bits/mng/read_write_pool.h>
00018 
00019 #define BLOCK_SIZE (1024 * 512)
00020 
00021 struct MyType
00022 {
00023     int integer;
00024     char chars[5];
00025 };
00026 
00027 typedef stxxl::typed_block<BLOCK_SIZE, MyType> block_type;
00028 
00029 int main()
00030 {
00031     stxxl::block_manager * bm = stxxl::block_manager::get_instance();
00032     STXXL_DEFAULT_ALLOC_STRATEGY alloc;
00033 
00034     {
00035         STXXL_MSG("Write-After-Write coherence test");
00036         stxxl::read_write_pool<block_type> pool(2, 10);
00037         block_type * blk;
00038         block_type::bid_type bid;
00039 
00040         bm->new_block(alloc, bid);
00041 
00042         // write the block for the first time
00043         blk = pool.steal();
00044         (*blk)[0].integer = 42;
00045         pool.write(blk, bid);
00046 
00047         // read the block
00048         blk = pool.steal();
00049         pool.read(blk, bid)->wait();
00050         delete blk;
00051 
00052         // write the block for the second time
00053         blk = pool.steal();
00054         (*blk)[0].integer = 23;
00055         pool.write(blk, bid);
00056 
00057         // hint the block
00058         pool.hint(bid); // flush w_pool
00059 
00060         // get the hinted block
00061         blk = pool.steal();
00062         pool.read(blk, bid)->wait();
00063 
00064         if ((*blk)[0].integer != 23) {
00065             STXXL_ERRMSG("WRITE-AFTER-WRITE COHERENCE FAILURE");
00066         }
00067 
00068         pool.add(blk);
00069         bm->delete_block(bid);
00070     }
00071 
00072     {
00073         STXXL_MSG("Write-After-Hint coherence test #1");
00074         stxxl::read_write_pool<block_type> pool(1, 1);
00075         block_type * blk;
00076         block_type::bid_type bid;
00077 
00078         bm->new_block(alloc, bid);
00079         blk = pool.steal();
00080         (*blk)[0].integer = 42;
00081         pool.write(blk, bid);
00082         blk = pool.steal(); // flush w_pool
00083 
00084         // hint the block
00085         pool.hint(bid);
00086 
00087         // update the hinted block
00088         (*blk)[0].integer = 23;
00089         pool.write(blk, bid);
00090         blk = pool.steal(); // flush w_pool
00091 
00092         // get the hinted block
00093         pool.read(blk, bid)->wait();
00094 
00095         if ((*blk)[0].integer != 23) {
00096             STXXL_ERRMSG("WRITE-AFTER-HINT COHERENCE FAILURE");
00097         }
00098 
00099         pool.add(blk);
00100         bm->delete_block(bid);
00101     }
00102 
00103     {
00104         STXXL_MSG("Write-After-Hint coherence test #2");
00105         stxxl::read_write_pool<block_type> pool(1, 1);
00106         block_type * blk;
00107         block_type::bid_type bid;
00108 
00109         bm->new_block(alloc, bid);
00110         blk = pool.steal();
00111         (*blk)[0].integer = 42;
00112         pool.write(blk, bid);
00113 
00114         // hint the block
00115         pool.hint(bid);
00116 
00117         // update the hinted block
00118         blk = pool.steal();
00119         (*blk)[0].integer = 23;
00120         pool.write(blk, bid);
00121         blk = pool.steal(); // flush w_pool
00122 
00123         // get the hinted block
00124         pool.read(blk, bid)->wait();
00125 
00126         if ((*blk)[0].integer != 23) {
00127             STXXL_ERRMSG("WRITE-AFTER-HINT COHERENCE FAILURE");
00128         }
00129 
00130         pool.add(blk);
00131         bm->delete_block(bid);
00132     }
00133 }
00134 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines