2 * Copyright 2013 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "folly/experimental/io/AsyncIO.h"
21 #include <glog/logging.h>
23 #include "folly/Exception.h"
24 #include "folly/Likely.h"
25 #include "folly/String.h"
26 #include "folly/eventfd.h"
30 AsyncIO::AsyncIO(size_t capacity, PollMode pollMode)
35 CHECK_GT(capacity_, 0);
36 completed_.reserve(capacity_);
37 if (pollMode == POLLABLE) {
38 pollFd_ = eventfd(0, EFD_NONBLOCK);
39 checkUnixError(pollFd_, "AsyncIO: eventfd creation failed");
44 CHECK_EQ(pending_, 0);
46 int rc = io_queue_release(ctx_);
47 CHECK_EQ(rc, 0) << "io_queue_release: " << errnoStr(-rc);
50 CHECK_ERR(close(pollFd_));
54 void AsyncIO::pread(Op* op, int fd, void* buf, size_t size, off_t start) {
56 io_prep_pread(&cb, fd, buf, size, start);
60 void AsyncIO::pread(Op* op, int fd, Range<unsigned char*> range,
62 pread(op, fd, range.begin(), range.size(), start);
65 void AsyncIO::preadv(Op* op, int fd, const iovec* iov, int iovcnt,
68 io_prep_preadv(&cb, fd, iov, iovcnt, start);
72 void AsyncIO::pwrite(Op* op, int fd, const void* buf, size_t size,
75 io_prep_pwrite(&cb, fd, const_cast<void*>(buf), size, start);
79 void AsyncIO::pwrite(Op* op, int fd, Range<const unsigned char*> range,
81 pwrite(op, fd, range.begin(), range.size(), start);
84 void AsyncIO::pwritev(Op* op, int fd, const iovec* iov, int iovcnt,
87 io_prep_pwritev(&cb, fd, iov, iovcnt, start);
91 void AsyncIO::initializeContext() {
93 int rc = io_queue_init(capacity_, &ctx_);
94 // returns negative errno
95 checkKernelError(rc, "AsyncIO: io_queue_init failed");
100 void AsyncIO::submit(Op* op, iocb* cb) {
101 CHECK_EQ(op->state(), Op::UNINITIALIZED);
102 CHECK_LT(pending_, capacity_) << "too many pending requests";
103 initializeContext(); // on demand
106 io_set_eventfd(cb, pollFd_);
108 int rc = io_submit(ctx_, 1, &cb);
109 checkKernelError(rc, "AsyncIO: io_submit failed");
115 Range<AsyncIO::Op**> AsyncIO::wait(size_t minRequests) {
117 CHECK_EQ(pollFd_, -1) << "wait() only allowed on non-pollable object";
118 return doWait(minRequests, pending_);
121 Range<AsyncIO::Op**> AsyncIO::pollCompleted() {
123 CHECK_NE(pollFd_, -1) << "pollCompleted() only allowed on pollable object";
125 // This sets the eventFd counter to 0, see
126 // http://www.kernel.org/doc/man-pages/online/pages/man2/eventfd.2.html
129 rc = ::read(pollFd_, &numEvents, 8);
130 } while (rc == -1 && errno == EINTR);
131 if (UNLIKELY(rc == -1 && errno == EAGAIN)) {
132 return Range<Op**>(); // nothing completed
134 checkUnixError(rc, "AsyncIO: read from event fd failed");
137 DCHECK_GT(numEvents, 0);
138 DCHECK_LE(numEvents, pending_);
140 // Don't reap more than numEvents, as we've just reset the counter to 0.
141 return doWait(numEvents, numEvents);
144 Range<AsyncIO::Op**> AsyncIO::doWait(size_t minRequests, size_t maxRequests) {
145 io_event events[pending_];
149 count = io_getevents(ctx_, minRequests, maxRequests, events, nullptr);
150 } while (count == -EINTR);
151 checkKernelError(count, "AsyncIO: io_getevents failed");
152 DCHECK_GE(count, minRequests); // the man page says so
153 DCHECK_LE(count, pending_);
157 return folly::Range<Op**>();
160 for (size_t i = 0; i < count; ++i) {
161 Op* op = static_cast<Op*>(events[i].data);
163 op->complete(events[i].res);
164 completed_.push_back(op);
168 return folly::Range<Op**>(&completed_.front(), count);
172 : state_(UNINITIALIZED),
176 void AsyncIO::Op::reset() {
177 CHECK_NE(state_, PENDING);
178 state_ = UNINITIALIZED;
183 CHECK_NE(state_, PENDING);
186 void AsyncIO::Op::start() {
187 DCHECK_EQ(state_, UNINITIALIZED);
191 void AsyncIO::Op::complete(ssize_t result) {
192 DCHECK_EQ(state_, PENDING);
198 void AsyncIO::Op::onCompleted() { } // default: do nothing
200 ssize_t AsyncIO::Op::result() const {
201 CHECK_EQ(state_, COMPLETED);
205 CallbackOp::CallbackOp(Callback&& callback) : callback_(std::move(callback)) { }
207 CallbackOp::~CallbackOp() { }
209 CallbackOp* CallbackOp::make(Callback&& callback) {
210 // Ensure created on the heap
211 return new CallbackOp(std::move(callback));
214 void CallbackOp::onCompleted() {