Skip to content

Commit 9b46fc0

Browse files
committed
Reduce syscalls in Async::new
1 parent f9ba3f7 commit 9b46fc0

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ harness = false
2020

2121
[dependencies]
2222
async-lock = "2.6"
23+
cfg-if = "1"
2324
concurrent-queue = "2"
2425
futures-lite = "1.11.0"
2526
log = "0.4.11"

src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,23 @@ impl<T: AsRawFd> Async<T> {
636636
// depend on Rust >= 1.63, where `AsFd` is stabilized, and when
637637
// `TimerFd` implements it, we can remove this unsafe and simplify this.
638638
let fd = unsafe { rustix::fd::BorrowedFd::borrow_raw(raw) };
639-
let flags = rustix::fs::fcntl_getfl(fd)?;
640-
rustix::fs::fcntl_setfl(fd, flags | rustix::fs::OFlags::NONBLOCK)?;
639+
cfg_if::cfg_if! {
640+
// ioctl(FIONBIO) sets the flag atomically, but we use this only on Linux
641+
// for now, as with the standard library, because it seems to behave
642+
// differently depending on the platform.
643+
// https://github.com/rust-lang/rust/commit/efeb42be2837842d1beb47b51bb693c7474aba3d
644+
// https://github.com/libuv/libuv/blob/e9d91fccfc3e5ff772d5da90e1c4a24061198ca0/src/unix/poll.c#L78-L80
645+
// https://github.com/tokio-rs/mio/commit/0db49f6d5caf54b12176821363d154384357e70a
646+
if #[cfg(target_os = "linux")] {
647+
rustix::io::ioctl_fionbio(fd, true)?;
648+
} else {
649+
let previous = rustix::fs::fcntl_getfl(fd)?;
650+
let new = previous | rustix::fs::OFlags::NONBLOCK;
651+
if new != previous {
652+
rustix::fs::fcntl_setfl(fd, new)?;
653+
}
654+
}
655+
}
641656

642657
Ok(Async {
643658
source: Reactor::get().insert_io(raw)?,

0 commit comments

Comments
 (0)