chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.85k stars 1.2k forks source link

copy file using io_uring + coroutine failed #1244

Closed ysbarney closed 1 year ago

ysbarney commented 1 year ago

OS: ubuntu22.04

I'm write copy_file using io_uring and coroutine.

coro_copy_file.cc

include "asio.hpp"

include

asio::awaitable copy_file(char from_file, char to_file) { auto ex = co_await asio::this_coro::executor; asio::stream_file from_stream(ex, from_file, asio::stream_file::read_only); asio::stream_file to_stream(ex, to_file, asio::stream_file::write_only | asio::stream_file::create | asio::stream_file::truncate); char data[4096]; for(;;) { auto [e, n] = co_await from_stream.async_read_some(asio::buffer(data), asio::as_tuple(asio::use_awaitable)); std::cout << "read size: " << n << std::endl; if(!e) { co_await async_write(to_stream, asio::buffer(data, n), asio::use_awaitable); } else if (e != asio::error::eof) { std::cerr << "Error copying file: " << e.message() << "\n"; break; } else break; } }

int main(int argc, char** argv) { try { if (argc != 3) { std::cerr << "Usage: coro_file_copy \n"; return 1; }

asio::io_context io_ctx;
co_spawn(io_ctx, copy_file(argv[1], argv[2]), asio::detached);
io_ctx.run();

} catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; return 1; }

return 0; }

run coro_copy_file and get failed like this: Error copying file: No buffer space available

in asio-1.25 io_uring can't support coroutine(c++20) ?

ysbarney commented 1 year ago

I fixed this problem, add -DASIO_DISABLE_EPOLL in building coro_copy_file