Skip to content

Commit

Permalink
improve construct
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jul 3, 2024
1 parent d25041e commit 95a3eaf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
60 changes: 56 additions & 4 deletions include/cinatra/ylt/coro_io/coro_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ inline bool open_native_async_file(File &file, Executor &executor,
}
#endif

enum class execution_type { native_async, thread_pool };
enum class execution_type { none, native_async, thread_pool };

template <execution_type execute_type = execution_type::native_async>
class seq_coro_file {
Expand All @@ -174,6 +174,19 @@ class seq_coro_file {
seq_coro_file(asio::io_context::executor_type executor)
: executor_wrapper_(executor) {}

seq_coro_file(std::string_view filepath,
std::ios::ios_base::openmode open_flags,
coro_io::ExecutorWrapper<> *executor =
coro_io::get_global_block_executor())
: seq_coro_file(filepath, open_flags, executor->get_asio_executor()) {}

seq_coro_file(std::string_view filepath,
std::ios::ios_base::openmode open_flags,
asio::io_context::executor_type executor)
: executor_wrapper_(executor) {
open(filepath, open_flags);
}

bool open(std::string_view filepath,
std::ios::ios_base::openmode open_flags) {
if constexpr (execute_type == execution_type::thread_pool) {
Expand Down Expand Up @@ -278,12 +291,23 @@ class seq_coro_file {
async_seq_file_.close(ec);
}
#endif
if (frw_seq_file_) {
if (frw_seq_file_.is_open()) {
frw_seq_file_.close();
}
}

bool is_in_thread_pool() { return frw_seq_file_.is_open(); }
execution_type get_execution_type() {
#if defined(ENABLE_FILE_IO_URING) || defined(ASIO_WINDOWS)
if (frw_seq_file_.is_open()) {
return execution_type::native_async;
}
#endif
if (frw_seq_file_.is_open()) {
return execution_type::thread_pool;
}

return execution_type::none;
}

private:
bool open_stream_file_in_pool(std::string_view filepath,
Expand Down Expand Up @@ -314,6 +338,8 @@ class seq_coro_file {
bool eof_ = false;
};

using coro_file0 = seq_coro_file<>;

template <execution_type execute_type = execution_type::native_async>
class random_coro_file {
public:
Expand All @@ -324,6 +350,19 @@ class random_coro_file {
random_coro_file(asio::io_context::executor_type executor)
: executor_wrapper_(executor) {}

random_coro_file(std::string_view filepath,
std::ios::ios_base::openmode open_flags,
coro_io::ExecutorWrapper<> *executor =
coro_io::get_global_block_executor())
: random_coro_file(filepath, open_flags, executor->get_asio_executor()) {}

random_coro_file(std::string_view filepath,
std::ios::ios_base::openmode open_flags,
asio::io_context::executor_type executor)
: executor_wrapper_(executor) {
open(filepath, open_flags);
}

bool open(std::string_view filepath,
std::ios::ios_base::openmode open_flags) {
if constexpr (execute_type == execution_type::thread_pool) {
Expand Down Expand Up @@ -398,7 +437,18 @@ class random_coro_file {

bool eof() { return eof_; }

bool is_in_thread_pool() { return prw_random_file_ != nullptr; }
execution_type get_execution_type() {
#if defined(ENABLE_FILE_IO_URING) || defined(ASIO_WINDOWS)
if (async_seq_file_.is_open()) {
return execution_type::native_async;
}
#endif
if (prw_random_file_ != nullptr) {
return execution_type::thread_pool;
}

return execution_type::none;
}

void close() {
#if defined(ENABLE_FILE_IO_URING) || defined(ASIO_WINDOWS)
Expand Down Expand Up @@ -516,6 +566,8 @@ class random_coro_file {
bool eof_ = false;
};

using random_coro_file0 = random_coro_file<>;

class coro_file {
public:
#if defined(ENABLE_FILE_IO_URING)
Expand Down
12 changes: 7 additions & 5 deletions tests/test_corofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ void create_files(const std::vector<std::string> &files, size_t file_size) {
template <coro_io::execution_type execute_type>
void test_random_read_write(std::string_view filename) {
create_files({std::string(filename)}, 190);
coro_io::random_coro_file<execute_type> file{};
file.open(filename, std::ios::binary | std::ios::in | std::ios::out);
coro_io::random_coro_file<execute_type> file(
filename, std::ios::binary | std::ios::in | std::ios::out);
CHECK(file.is_open());
CHECK(file.get_execution_type() == coro_io::execution_type::thread_pool);

char buf[100];
auto pair = async_simple::coro::syncAwait(file.async_read_at(0, buf, 10));
Expand Down Expand Up @@ -110,9 +111,10 @@ void test_random_read_write(std::string_view filename) {
template <coro_io::execution_type execute_type>
void test_seq_read_write(std::string_view filename) {
create_files({std::string(filename)}, 190);
coro_io::seq_coro_file<execute_type> file;
file.open(filename, std::ios::binary | std::ios::in | std::ios::out);
CHECK(file.get_stream_file().is_open());
coro_io::seq_coro_file<execute_type> file(
filename, std::ios::binary | std::ios::in | std::ios::out);
CHECK(file.is_open());
CHECK(file.get_execution_type() == coro_io::execution_type::thread_pool);
char buf[100];
std::error_code ec;
size_t size;
Expand Down

0 comments on commit 95a3eaf

Please sign in to comment.