Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
request_with_state.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/io/request_with_state.cpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
5  *
6  * Copyright (C) 2002, 2005, 2008 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2009 Johannes Singler <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #include <cassert>
16 
19 #include <foxxll/io/file.hpp>
20 #include <foxxll/io/iostats.hpp>
21 #include <foxxll/io/request.hpp>
23 #include <foxxll/singleton.hpp>
24 
25 namespace foxxll {
26 
28 {
29  LOG << "request_with_state[" << static_cast<void*>(this) << "]::~(), ref_cnt: " << reference_count();
30 
31  assert(state_() == DONE || state_() == READY2DIE);
32 }
33 
34 void request_with_state::wait(bool measure_time)
35 {
36  LOG << "request_with_state[" << static_cast<void*>(this) << "]::wait()";
37 
38  stats::scoped_wait_timer wait_timer(
39  op_ == READ ? stats::WAIT_OP_READ : stats::WAIT_OP_WRITE, measure_time);
40 
42 
43  check_errors();
44 }
45 
47 {
48  LOG << "request_with_state[" << static_cast<void*>(this) << "]::cancel() "
49  << file_ << " " << buffer_ << " " << offset_;
50 
51  if (!file_) return false;
52 
53  // TODO(tb): remove
54  request_ptr rp(this);
55  if (disk_queues::get_instance()->cancel_request(rp, file_->get_queue_id()))
56  {
58  if (on_complete_)
59  on_complete_(this, /* success */ false);
62  file_ = nullptr;
64  return true;
65  }
66  return false;
67 }
68 
70 {
71  const request_state s = state_();
72 
73  check_errors();
74 
75  return s == DONE || s == READY2DIE;
76 }
77 
78 void request_with_state::completed(bool canceled)
79 {
80  LOG << "request_with_state[" << static_cast<void*>(this) << "]::completed()";
81  // change state
83  // user callback
84  if (on_complete_)
85  on_complete_(this, !canceled);
87  // delete request reference in file
90 }
91 
92 } // namespace foxxll
93 
94 /**************************************************************************/
offset_type offset_
offset within file
Definition: request.hpp:67
void set_to(const value_type &new_state)
file * file_
file implementation to perform I/O with
Definition: request.hpp:63
bool cancel() override
Cancel a request.
void * buffer_
data buffer to transfer
Definition: request.hpp:65
void delete_request_ref()
decrement referenced requests
Definition: file.hpp:229
virtual int get_queue_id() const =0
void completed(bool canceled) override
void check_errors()
Rises an exception if there were error with the I/O.
Definition: request.hpp:114
size_t reference_count() const noexcept
Return the number of references to this object (for debugging)
static instance_pointer get_instance()
Definition: singleton.hpp:44
High-performance smart pointer used as a wrapping reference counting pointer.
completion_handler on_complete_
Definition: request.hpp:55
void release_file_reference()
Definition: request.cpp:95
void wait_for(const value_type &needed_state)
shared_state< request_state > state_
#define LOG
Default logging method: output if the local debug variable is true.
Definition: logger.hpp:141
void wait(bool measure_time=true) final
Suspends calling thread until completion of the request.
read_or_write op_
READ or WRITE.
Definition: request.hpp:71