Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * include/stxxl/bits/io/request.h 00003 * 00004 * Part of the STXXL. See http://stxxl.sourceforge.net 00005 * 00006 * Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de> 00007 * Copyright (C) 2008 Andreas Beckmann <beckmann@cs.uni-frankfurt.de> 00008 * 00009 * Distributed under the Boost Software License, Version 1.0. 00010 * (See accompanying file LICENSE_1_0.txt or copy at 00011 * http://www.boost.org/LICENSE_1_0.txt) 00012 **************************************************************************/ 00013 00014 #ifndef STXXL_IO__REQUEST_H_ 00015 #define STXXL_IO__REQUEST_H_ 00016 00017 #include <cassert> 00018 00019 #include <stxxl/bits/namespace.h> 00020 #include <stxxl/bits/io/request_interface.h> 00021 #include <stxxl/bits/common/mutex.h> 00022 #include <stxxl/bits/common/exceptions.h> 00023 #include <stxxl/bits/io/completion_handler.h> 00024 #include <stxxl/bits/compat_unique_ptr.h> 00025 #include <stxxl/bits/verbose.h> 00026 00027 00028 __STXXL_BEGIN_NAMESPACE 00029 00030 //! \addtogroup iolayer 00031 //! \{ 00032 00033 #define BLOCK_ALIGN 4096 00034 00035 class file; 00036 class request_ptr; 00037 00038 //! \brief Request with basic properties like file and offset. 00039 class request : virtual public request_interface 00040 { 00041 friend class request_ptr; 00042 00043 protected: 00044 completion_handler on_complete; 00045 int ref_cnt; 00046 compat_unique_ptr<stxxl::io_error>::result error; 00047 00048 mutex ref_cnt_mutex; 00049 00050 protected: 00051 file * file_; 00052 void * buffer; 00053 offset_type offset; 00054 size_type bytes; 00055 request_type type; 00056 00057 void completed(); 00058 00059 public: 00060 // returns number of references 00061 int nref() 00062 { 00063 scoped_mutex_lock Lock(ref_cnt_mutex); 00064 return ref_cnt; 00065 } 00066 00067 request(const completion_handler & on_compl, 00068 file * file__, 00069 void * buffer_, 00070 offset_type offset_, 00071 size_type bytes_, 00072 request_type type_); 00073 00074 virtual ~request(); 00075 00076 file * get_file() const { return file_; } 00077 void * get_buffer() const { return buffer; } 00078 offset_type get_offset() const { return offset; } 00079 size_type get_size() const { return bytes; } 00080 request_type get_type() const { return type; } 00081 00082 void check_alignment() const; 00083 00084 std::ostream & print(std::ostream & out) const; 00085 00086 //! \brief Inform the request object that an error occurred 00087 //! during the I/O execution 00088 void error_occured(const char * msg) 00089 { 00090 error.reset(new stxxl::io_error(msg)); 00091 } 00092 00093 //! \brief Inform the request object that an error occurred 00094 //! during the I/O execution 00095 void error_occured(const std::string & msg) 00096 { 00097 error.reset(new stxxl::io_error(msg)); 00098 } 00099 00100 //! \brief Rises an exception if there were error with the I/O 00101 void check_errors() throw (stxxl::io_error) 00102 { 00103 if (error.get()) 00104 throw *(error.get()); 00105 } 00106 00107 private: 00108 void add_ref() 00109 { 00110 scoped_mutex_lock Lock(ref_cnt_mutex); 00111 ref_cnt++; 00112 STXXL_VERBOSE3("[" << static_cast<void *>(this) << "] request::add_ref(): added reference, ref_cnt=" << ref_cnt); 00113 } 00114 00115 bool sub_ref() 00116 { 00117 int val; 00118 { 00119 scoped_mutex_lock Lock(ref_cnt_mutex); 00120 val = --ref_cnt; 00121 STXXL_VERBOSE3("[" << static_cast<void *>(this) << "] request::sub_ref(): subtracted reference, ref_cnt=" << ref_cnt); 00122 } 00123 assert(val >= 0); 00124 return (val == 0); 00125 } 00126 00127 protected: 00128 void check_nref(bool after = false) 00129 { 00130 if (nref() < 2) 00131 check_nref_failed(after); 00132 } 00133 00134 private: 00135 void check_nref_failed(bool after); 00136 }; 00137 00138 inline std::ostream & operator << (std::ostream & out, const request & req) 00139 { 00140 return req.print(out); 00141 } 00142 00143 //! \} 00144 00145 __STXXL_END_NAMESPACE 00146 00147 #endif // !STXXL_IO__REQUEST_H_ 00148 // vim: et:ts=4:sw=4