| 0.0.3 |
|
|---|---|
| 0.0.2 |
|
| 0.0.1 |
|
#66 在 #fs-file
320KB
5.5K SLoC
霜冻
具有 io_uring 的每个核心一个线程的运行时。
lib.rs:
Tokio-uring 为 Tokio 运行时提供了一个安全的 io-uring 接口。该库需要 Linux 内核 5.10 或更高版本。
入门
使用 tokio-uring 需要启动一个 tokio-uring 运行时。这个运行时内部管理主要的 Tokio 运行时和一个 io-uring 驱动。
use frosty::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
frosty::start(async {
// Open a file
let file = File::open("hello.txt").await?;
let buf = vec![0; 4096];
// Read some data, the buffer is passed by ownership and
// submitted to the kernel. When the operation completes,
// we get the buffer back.
let (res, buf) = file.read_at(buf, 0).await;
let n = res?;
// Display the contents
println!("{:?}", &buf[..n]);
Ok(())
})
}
在底层,frosty::start 启动一个 current-thread 运行时。为了并发,启动多个线程,每个线程都有一个 tokio-uring 运行时。`tokio-uring` 资源类型针对单线程使用进行了优化,大多数都是 !Sync。
基于提交的操作
与 Tokio 正式版本不同,io-uring 是基于提交操作的。资源所有权传递给内核,内核然后执行操作。操作完成后,所有权传递回调用者。由于这种差异,`tokio-uring` API 也发生了变化。
例如,在上面的例子中,从 File 读取需要传递缓冲区的所有权。
关闭资源
使用 io-uring,关闭资源(例如文件)是一个异步操作。由于 Rust 还不支持异步丢弃,资源类型提供了一个显式的 close() 函数。如果没有调用 close() 函数,资源在丢弃时仍然会被关闭,但操作将在后台发生。无法保证隐式关闭操作何时发生,因此建议显式调用 close()。
依赖项
~1.5–2.7MB
~50K SLoC