Stxxl  1.3.2
request.h
1 /***************************************************************************
2  * include/stxxl/bits/io/request.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_IO__REQUEST_H_
15 #define STXXL_IO__REQUEST_H_
16 
17 #include <cassert>
18 
19 #include <stxxl/bits/namespace.h>
20 #include <stxxl/bits/io/request_interface.h>
21 #include <stxxl/bits/common/mutex.h>
22 #include <stxxl/bits/common/exceptions.h>
23 #include <stxxl/bits/io/completion_handler.h>
24 #include <stxxl/bits/compat_unique_ptr.h>
25 #include <stxxl/bits/verbose.h>
26 
27 
28 __STXXL_BEGIN_NAMESPACE
29 
32 
33 #define BLOCK_ALIGN 4096
34 
35 class file;
36 class request_ptr;
37 
39 class request : virtual public request_interface
40 {
41  friend class request_ptr;
42 
43 protected:
44  completion_handler on_complete;
45  int ref_cnt;
46  compat_unique_ptr<stxxl::io_error>::result error;
47 
48  mutex ref_cnt_mutex;
49 
50 protected:
51  file * file_;
52  void * buffer;
53  offset_type offset;
54  size_type bytes;
55  request_type type;
56 
57  void completed();
58 
59 public:
60  // returns number of references
61  int nref()
62  {
63  scoped_mutex_lock Lock(ref_cnt_mutex);
64  return ref_cnt;
65  }
66 
67  request(const completion_handler & on_compl,
68  file * file__,
69  void * buffer_,
70  offset_type offset_,
71  size_type bytes_,
72  request_type type_);
73 
74  virtual ~request();
75 
76  file * get_file() const { return file_; }
77  void * get_buffer() const { return buffer; }
78  offset_type get_offset() const { return offset; }
79  size_type get_size() const { return bytes; }
80  request_type get_type() const { return type; }
81 
82  void check_alignment() const;
83 
84  std::ostream & print(std::ostream & out) const;
85 
88  void error_occured(const char * msg)
89  {
90  error.reset(new stxxl::io_error(msg));
91  }
92 
95  void error_occured(const std::string & msg)
96  {
97  error.reset(new stxxl::io_error(msg));
98  }
99 
101  void check_errors() throw (stxxl::io_error)
102  {
103  if (error.get())
104  throw *(error.get());
105  }
106 
107 private:
108  void add_ref()
109  {
110  scoped_mutex_lock Lock(ref_cnt_mutex);
111  ref_cnt++;
112  STXXL_VERBOSE3("[" << static_cast<void *>(this) << "] request::add_ref(): added reference, ref_cnt=" << ref_cnt);
113  }
114 
115  bool sub_ref()
116  {
117  int val;
118  {
119  scoped_mutex_lock Lock(ref_cnt_mutex);
120  val = --ref_cnt;
121  STXXL_VERBOSE3("[" << static_cast<void *>(this) << "] request::sub_ref(): subtracted reference, ref_cnt=" << ref_cnt);
122  }
123  assert(val >= 0);
124  return (val == 0);
125  }
126 
127 protected:
128  void check_nref(bool after = false)
129  {
130  if (nref() < 2)
131  check_nref_failed(after);
132  }
133 
134 private:
135  void check_nref_failed(bool after);
136 };
137 
138 inline std::ostream & operator << (std::ostream & out, const request & req)
139 {
140  return req.print(out);
141 }
142 
144 
145 __STXXL_END_NAMESPACE
146 
147 #endif // !STXXL_IO__REQUEST_H_
148 // vim: et:ts=4:sw=4
Defines interface of file.
Definition: file.h:90
Request with basic properties like file and offset.
Definition: request.h:39
Completion handler class (Loki-style)
Definition: completion_handler.h:63
std::ostream & print(std::ostream &out) const
Dumps properties of a request.
Definition: request.cpp:83
void error_occured(const std::string &msg)
Inform the request object that an error occurred during the I/O execution.
Definition: request.h:95
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:82
Implemented as reference counting smart pointer.
Definition: request_ptr.h:34
Functional interface of a request.
Definition: request_interface.h:37
void error_occured(const char *msg)
Inform the request object that an error occurred during the I/O execution.
Definition: request.h:88
void check_errors()
Rises an exception if there were error with the I/O.
Definition: request.h:101