#pragma once
#include <folly/experimental/io/AsyncBase.h>
#if __has_include(<libaio.h>)
#include <libaio.h>
namespace folly {
class AsyncIOOp : public AsyncBaseOp {
friend class AsyncIO;
friend std::ostream& operator<<(std::ostream& os, const AsyncIOOp& o);
public:
explicit AsyncIOOp(NotificationCallback cb = NotificationCallback());
AsyncIOOp(const AsyncIOOp&) = delete;
AsyncIOOp& operator=(const AsyncIOOp&) = delete;
~AsyncIOOp() override;
void pread(int fd, void* buf, size_t size, off_t start) override;
void preadv(int fd, const iovec* iov, int iovcnt, off_t start) override;
void pwrite(int fd, const void* buf, size_t size, off_t start) override;
void pwritev(int fd, const iovec* iov, int iovcnt, off_t start) override;
void reset(NotificationCallback cb = NotificationCallback()) override;
AsyncIOOp* getAsyncIOOp() override { return this; }
IoUringOp* getIoUringOp() override { return nullptr; }
void toStream(std::ostream& os) const override;
const iocb& getIocb() const { return iocb_; }
private:
iocb iocb_;
};
std::ostream& operator<<(std::ostream& os, const AsyncIOOp& op);
class AsyncIO : public AsyncBase {
public:
using Op = AsyncIOOp;
explicit AsyncIO(size_t capacity, PollMode pollMode = NOT_POLLABLE);
AsyncIO(const AsyncIO&) = delete;
AsyncIO& operator=(const AsyncIO&) = delete;
~AsyncIO() override;
void initializeContext() override;
protected:
int drainPollFd() override;
int submitOne(AsyncBase::Op* op) override;
int submitRange(Range<AsyncBase::Op**> ops) override;
private:
Range<AsyncBase::Op**> doWait(
WaitType type,
size_t minRequests,
size_t maxRequests,
std::vector<AsyncBase::Op*>& result) override;
io_context_t ctx_{nullptr};
};
using AsyncIOQueue = AsyncBaseQueue;
}
#endif