From 6b7399bf4a3ffd8bce6291d13807d9ad2d978a70 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 14 Jan 2018 22:18:18 -0800 Subject: [PATCH] ircd::fs: Simplify API/AIO by eliminating callback: ctx yield only for now. --- include/ircd/fs/read.h | 13 ------ ircd/aio.h | 93 ++++-------------------------------------- ircd/fs.cc | 45 +------------------- 3 files changed, 10 insertions(+), 141 deletions(-) diff --git a/include/ircd/fs/read.h b/include/ircd/fs/read.h index dc1b8d663..2278e4de9 100644 --- a/include/ircd/fs/read.h +++ b/include/ircd/fs/read.h @@ -23,11 +23,6 @@ namespace ircd::fs { struct read_opts extern const read_opts_default; - using read_callback = std::function; - - // Asynchronous callback-based read into buffer; callback views read portion. - void read(const string_view &path, const mutable_raw_buffer &, const read_opts &, read_callback); - void read(const string_view &path, const mutable_raw_buffer &, read_callback); // Yields ircd::ctx for read into buffer; returns view of read portion. string_view read(const string_view &path, const mutable_raw_buffer &, const read_opts & = read_opts_default); @@ -53,11 +48,3 @@ inline ircd::fs::read_opts::read_opts(const off_t &offset) :offset{offset} {} - -inline void -ircd::fs::read(const string_view &path, - const mutable_raw_buffer &buf, - read_callback callback) -{ - return read(path, buf, read_opts_default, std::move(callback)); -} diff --git a/ircd/aio.h b/ircd/aio.h index 2726aaa84..f7806d6f0 100644 --- a/ircd/aio.h +++ b/ircd/aio.h @@ -52,16 +52,12 @@ struct ircd::fs::aio }; /// Handler to the io context we submit requests to the kernel with - aio_context_t idp - { - 0 - }; + aio_context_t idp{0}; // Callback stack invoked when the sigfd is notified of completed events. void handle_event(const io_event &) noexcept; void handle_events() noexcept; void handle(const error_code &, const size_t) noexcept; - void set_handle(); aio() @@ -87,8 +83,6 @@ struct ircd::fs::aio::request ssize_t retval {0}; ssize_t errcode {0}; ctx::ctx *waiter {nullptr}; - bool close_fd {false}; - bool free_req {false}; /// called if close_fd is true void close_fildes() noexcept; @@ -288,18 +282,6 @@ noexcept try request->retval, request->errcode); */ - const unwind cleanup{[&request] - { - // The user might want us to cleanup their fd after this event - if(request->close_fd) - request->close_fildes(); - - // The user might want us to free a dynamically allocated request struct - // to simplify their callback sequence. The noexcept guarantees h - if(request->free_req) - delete request; - }}; - // virtual dispatch based on the request type. Alternatively, we could // switch() on the iocb lio_opcode and downcast... but that's what vtable // does for us right here. @@ -335,7 +317,6 @@ catch(const std::exception &e) namespace ircd::fs { - void read__aio(const string_view &path, const mutable_raw_buffer &, const read_opts &, read_callback); string_view read__aio(const string_view &path, const mutable_raw_buffer &, const read_opts &); std::string read__aio(const string_view &path, const read_opts &); } @@ -344,19 +325,15 @@ namespace ircd::fs struct ircd::fs::aio::request::read :request { - read_callback callback; - virtual void handle() final override; - read(const int &fd, const mutable_raw_buffer &buf, const read_opts &opts, read_callback callback); + read(const int &fd, const mutable_raw_buffer &buf, const read_opts &opts); }; ircd::fs::aio::request::read::read(const int &fd, const mutable_raw_buffer &buf, - const read_opts &opts, - read_callback callback) + const read_opts &opts) :request{fd} -,callback{std::move(callback)} { aio_data = uintptr_t(this); aio_reqprio = opts.priority; @@ -370,31 +347,11 @@ ircd::fs::aio::request::read::read(const int &fd, void ircd::fs::aio::request::read::handle() { - if(waiter) - { - ircd::ctx::notify(*waiter); - waiter = nullptr; - } + if(!waiter) + return; - if(callback) - { - const size_t bytes - { - retval >= 0? size_t(retval) : 0 - }; - - const string_view view - { - reinterpret_cast(aio_buf), bytes - }; - - std::exception_ptr eptr - { - errcode != 0? make_system_error(errcode) : std::exception_ptr{} - }; - - callback(std::move(eptr), view); - } + ircd::ctx::notify(*waiter); + waiter = nullptr; } // @@ -419,8 +376,6 @@ ircd::fs::read__aio(const string_view &path, syscall(::close, fd); }}; - // This fstat may be defeating; to be sure, don't use this overload - // and progressively buffer chunks into your application. struct stat stat; syscall(::fstat, fd, &stat); const auto &size @@ -436,7 +391,7 @@ ircd::fs::read__aio(const string_view &path, aio::request::read request { - int(fd), buf, opts, nullptr + int(fd), buf, opts }; const size_t bytes @@ -469,7 +424,7 @@ ircd::fs::read__aio(const string_view &path, aio::request::read request { - int(fd), buf, opts, nullptr + int(fd), buf, opts }; const size_t bytes @@ -484,33 +439,3 @@ ircd::fs::read__aio(const string_view &path, return view; } - -void -ircd::fs::read__aio(const string_view &path, - const mutable_raw_buffer &buf, - const read_opts &opts, - read_callback callback) -{ - static thread_local char pathstr[2048]; - strlcpy(pathstr, path, sizeof(pathstr)); - - const auto fd - { - syscall(::open, pathstr, O_CLOEXEC, O_RDONLY) - }; - - const unwind::exceptional cfd{[&fd] - { - syscall(::close, fd); - }}; - - auto request - { - std::make_unique(int(fd), buf, opts, std::move(callback)) - }; - - request->close_fd = true; - request->free_req = true; - (*request)(); - request.release(); -} diff --git a/ircd/fs.cc b/ircd/fs.cc index 1d2182d8d..cb4d009a5 100644 --- a/ircd/fs.cc +++ b/ircd/fs.cc @@ -78,7 +78,6 @@ noexcept namespace ircd::fs { - void read__std(const string_view &path, const mutable_raw_buffer &, const read_opts &, read_callback); string_view read__std(const string_view &path, const mutable_raw_buffer &, const read_opts &); std::string read__std(const string_view &path, const read_opts &); } @@ -116,20 +115,6 @@ ircd::fs::read(const string_view &path, return read__std(path, buf, opts); } -void -ircd::fs::read(const string_view &path, - const mutable_raw_buffer &buf, - const read_opts &opts, - read_callback callback) -{ - #ifdef IRCD_USE_AIO - if(likely(aioctx)) - return read__aio(path, buf, opts, std::move(callback)); - #endif - - return read__std(path, buf, opts, std::move(callback)); -} - // // std read // @@ -149,43 +134,15 @@ ircd::string_view ircd::fs::read__std(const string_view &path, const mutable_raw_buffer &buf, const read_opts &opts) -{ - string_view ret; - std::exception_ptr reptr; - read(path, buf, opts, [&reptr, &ret] - (std::exception_ptr eptr, const string_view &view) - { - reptr = std::move(eptr); - ret = view; - }); - - if(reptr) - std::rethrow_exception(reptr); - - return ret; -} - -void -ircd::fs::read__std(const string_view &path, - const mutable_raw_buffer &buf, - const read_opts &opts, - read_callback callback) -try { std::ifstream file{std::string{path}}; file.exceptions(file.failbit | file.badbit); file.seekg(opts.offset, file.beg); file.read(reinterpret_cast(data(buf)), size(buf)); - const string_view view + return { reinterpret_cast(data(buf)), size_t(file.gcount()) }; - - callback(std::exception_ptr{}, view); -} -catch(...) -{ - callback(std::make_exception_ptr(std::current_exception()), string_view{}); }