proxygen
folly::AsyncIOOp Class Reference

#include <AsyncIO.h>

Inheritance diagram for folly::AsyncIOOp:

Public Types

enum  State {
  State::UNINITIALIZED, State::INITIALIZED, State::PENDING, State::COMPLETED,
  State::CANCELED
}
 
typedef std::function< void(AsyncIOOp *)> NotificationCallback
 

Public Member Functions

 AsyncIOOp (NotificationCallback cb=NotificationCallback())
 
 ~AsyncIOOp ()
 
void pread (int fd, void *buf, size_t size, off_t start)
 
void pread (int fd, Range< unsigned char * > range, off_t start)
 
void preadv (int fd, const iovec *iov, int iovcnt, off_t start)
 
void pwrite (int fd, const void *buf, size_t size, off_t start)
 
void pwrite (int fd, Range< const unsigned char * > range, off_t start)
 
void pwritev (int fd, const iovec *iov, int iovcnt, off_t start)
 
State state () const
 
void reset (NotificationCallback cb=NotificationCallback())
 
void setNotificationCallback (NotificationCallback cb)
 
const NotificationCallbacknotificationCallback () const
 
ssize_t result () const
 

Private Member Functions

void init ()
 
void start ()
 
void complete (ssize_t result)
 
void cancel ()
 

Private Attributes

NotificationCallback cb_
 
iocb iocb_
 
State state_
 
ssize_t result_
 

Friends

class AsyncIO
 
std::ostream & operator<< (std::ostream &stream, const AsyncIOOp &o)
 

Detailed Description

An AsyncIOOp represents a pending operation. You may set a notification callback or you may use this class's methods directly.

The op must remain allocated until it is completed or canceled.

Definition at line 45 of file AsyncIO.h.

Member Typedef Documentation

typedef std::function<void(AsyncIOOp*)> folly::AsyncIOOp::NotificationCallback

Definition at line 50 of file AsyncIO.h.

Member Enumeration Documentation

Enumerator
UNINITIALIZED 
INITIALIZED 
PENDING 
COMPLETED 
CANCELED 

Definition at line 55 of file AsyncIO.h.

55  {
57  INITIALIZED,
58  PENDING,
59  COMPLETED,
60  CANCELED,
61  };

Constructor & Destructor Documentation

folly::AsyncIOOp::AsyncIOOp ( NotificationCallback  cb = NotificationCallback())
explicit

Definition at line 36 of file AsyncIO.cpp.

References iocb_, and folly::UNINITIALIZED.

37  : cb_(std::move(cb)), state_(State::UNINITIALIZED), result_(-EINVAL) {
38  memset(&iocb_, 0, sizeof(iocb_));
39 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
NotificationCallback cb_
Definition: AsyncIO.h:113
ssize_t result_
Definition: AsyncIO.h:116
folly::AsyncIOOp::~AsyncIOOp ( )

Definition at line 49 of file AsyncIO.cpp.

References PENDING, and state_.

Member Function Documentation

void folly::AsyncIOOp::cancel ( )
private
void folly::AsyncIOOp::complete ( ssize_t  result)
private

Definition at line 58 of file AsyncIO.cpp.

References cb_, COMPLETED, PENDING, result(), result_, and state_.

Referenced by folly::AsyncIO::doWait(), and notificationCallback().

58  {
59  DCHECK_EQ(state_, State::PENDING);
61  result_ = result;
62  if (cb_) {
63  cb_(this);
64  }
65 }
ssize_t result() const
Definition: AsyncIO.cpp:72
NotificationCallback cb_
Definition: AsyncIO.h:113
ssize_t result_
Definition: AsyncIO.h:116
void folly::AsyncIOOp::init ( )
private
const NotificationCallback& folly::AsyncIOOp::notificationCallback ( ) const
inline

Definition at line 93 of file AsyncIO.h.

References cancel(), cb_, complete(), init(), result(), and start().

93  {
94  return cb_;
95  }
NotificationCallback cb_
Definition: AsyncIO.h:113
void folly::AsyncIOOp::pread ( int  fd,
void *  buf,
size_t  size,
off_t  start 
)

Initiate a read request.

Definition at line 77 of file AsyncIO.cpp.

References init(), and iocb_.

Referenced by pread().

77  {
78  init();
79  io_prep_pread(&iocb_, fd, buf, size, start);
80 }
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
void folly::AsyncIOOp::pread ( int  fd,
Range< unsigned char * >  range,
off_t  start 
)

Definition at line 82 of file AsyncIO.cpp.

References folly::Range< Iter >::begin(), pread(), folly::Range< Iter >::size(), and start().

82  {
83  pread(fd, range.begin(), range.size(), start);
84 }
void pread(int fd, void *buf, size_t size, off_t start)
Definition: AsyncIO.cpp:77
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
void folly::AsyncIOOp::preadv ( int  fd,
const iovec *  iov,
int  iovcnt,
off_t  start 
)

Definition at line 86 of file AsyncIO.cpp.

References init(), and iocb_.

86  {
87  init();
88  io_prep_preadv(&iocb_, fd, iov, iovcnt, start);
89 }
void folly::AsyncIOOp::pwrite ( int  fd,
const void *  buf,
size_t  size,
off_t  start 
)

Initiate a write request.

Definition at line 91 of file AsyncIO.cpp.

References init(), and iocb_.

Referenced by pwrite().

91  {
92  init();
93  io_prep_pwrite(&iocb_, fd, const_cast<void*>(buf), size, start);
94 }
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
void folly::AsyncIOOp::pwrite ( int  fd,
Range< const unsigned char * >  range,
off_t  start 
)

Definition at line 96 of file AsyncIO.cpp.

References folly::Range< Iter >::begin(), pwrite(), folly::Range< Iter >::size(), and start().

96  {
97  pwrite(fd, range.begin(), range.size(), start);
98 }
void pwrite(int fd, const void *buf, size_t size, off_t start)
Definition: AsyncIO.cpp:91
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
void folly::AsyncIOOp::pwritev ( int  fd,
const iovec *  iov,
int  iovcnt,
off_t  start 
)

Definition at line 100 of file AsyncIO.cpp.

References init(), and iocb_.

100  {
101  init();
102  io_prep_pwritev(&iocb_, fd, iov, iovcnt, start);
103 }
void folly::AsyncIOOp::reset ( NotificationCallback  cb = NotificationCallback())

Reset the operation for reuse. It is an error to call reset() on an Op that is still pending.

Definition at line 41 of file AsyncIO.cpp.

References cb_, iocb_, folly::gen::move, PENDING, result_, state_, and UNINITIALIZED.

Referenced by state().

41  {
42  CHECK_NE(state_, State::PENDING);
43  cb_ = std::move(cb);
45  result_ = -EINVAL;
46  memset(&iocb_, 0, sizeof(iocb_));
47 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
NotificationCallback cb_
Definition: AsyncIO.h:113
ssize_t result_
Definition: AsyncIO.h:116
ssize_t folly::AsyncIOOp::result ( ) const

Retrieve the result of this operation. Returns >=0 on success, -errno on failure (that is, using the Linux kernel error reporting conventions). Use checkKernelError (folly/Exception.h) on the result to throw a std::system_error in case of error instead.

It is an error to call this if the Op hasn't completed.

Definition at line 72 of file AsyncIO.cpp.

References COMPLETED, result_, and state_.

Referenced by complete(), and notificationCallback().

72  {
73  CHECK_EQ(state_, State::COMPLETED);
74  return result_;
75 }
ssize_t result_
Definition: AsyncIO.h:116
void folly::AsyncIOOp::setNotificationCallback ( NotificationCallback  cb)
inline

Definition at line 90 of file AsyncIO.h.

References cb_, and folly::gen::move.

90  {
91  cb_ = std::move(cb);
92  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
NotificationCallback cb_
Definition: AsyncIO.h:113
void folly::AsyncIOOp::start ( )
private
State folly::AsyncIOOp::state ( ) const
inline

Return the current operation state.

Definition at line 80 of file AsyncIO.h.

References reset(), and state_.

Referenced by folly::AsyncIO::submit().

80  {
81  return state_;
82  }

Friends And Related Function Documentation

friend class AsyncIO
friend

Definition at line 46 of file AsyncIO.h.

std::ostream& operator<< ( std::ostream &  stream,
const AsyncIOOp o 
)
friend

Definition at line 388 of file AsyncIO.cpp.

388  {
389  os << "{" << op.state_ << ", ";
390 
391  if (op.state_ != AsyncIOOp::State::UNINITIALIZED) {
392  os << op.iocb_;
393  }
394 
395  if (op.state_ == AsyncIOOp::State::COMPLETED) {
396  os << "result=" << op.result_;
397  if (op.result_ < 0) {
398  os << " (" << errnoStr(-op.result_) << ')';
399  }
400  os << ", ";
401  }
402 
403  return os << "}";
404 }
fbstring errnoStr(int err)
Definition: String.cpp:463

Member Data Documentation

NotificationCallback folly::AsyncIOOp::cb_
private

Definition at line 113 of file AsyncIO.h.

Referenced by complete(), notificationCallback(), reset(), and setNotificationCallback().

iocb folly::AsyncIOOp::iocb_
private
ssize_t folly::AsyncIOOp::result_
private

Definition at line 116 of file AsyncIO.h.

Referenced by complete(), folly::operator<<(), reset(), and result().

State folly::AsyncIOOp::state_
private

Definition at line 115 of file AsyncIO.h.

Referenced by cancel(), complete(), init(), folly::operator<<(), reset(), result(), start(), state(), and ~AsyncIOOp().


The documentation for this class was generated from the following files: