Skip to content

Commit 7e44578

Browse files
committed
Add docs for futex-based wake and wait
1 parent dd10306 commit 7e44578

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/use_file.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ fn open_fd() -> Result<libc::c_int, Error> {
8787

8888
#[cfg(not(any(target_os = "android", target_os = "linux")))]
8989
mod sync {
90-
// On non-Linux targets the critical section only opens file,
91-
// which should not block, so in the unlikely contended case,
92-
// we can sleep-wait for the opening operation to finish.
90+
/// Sleep 1 ms before checking `FD` again.
91+
///
92+
/// On non-Linux targets the critical section only opens file,
93+
/// which should not block, so in the unlikely contended case,
94+
/// we can sleep-wait for the opening operation to finish.
9395
pub(super) fn wait() {
9496
let rqtp = libc::timespec {
9597
tv_sec: 0,
@@ -114,6 +116,12 @@ mod sync {
114116
use super::{Error, FD, FD_ONGOING_INIT};
115117
use crate::util_libc::{last_os_error, open_readonly};
116118

119+
/// Wait for atomic `FD` to change value from `FD_ONGOING_INIT` to something else.
120+
///
121+
/// Futex syscall with `FUTEX_WAIT` op puts the current thread to sleep
122+
/// until futex syscall with `FUTEX_WAKE` op gets executed for `FD`.
123+
///
124+
/// For more information read: https://www.man7.org/linux/man-pages/man2/futex.2.html
117125
pub(super) fn wait() {
118126
let op = libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG;
119127
let timeout_ptr = core::ptr::null::<libc::timespec>();
@@ -128,6 +136,7 @@ mod sync {
128136
});
129137
}
130138

139+
/// Wake up all threads which wait for value of atomic `FD` to change.
131140
pub(super) fn wake() {
132141
let op = libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG;
133142
let ret = unsafe { libc::syscall(libc::SYS_futex, &FD, op, libc::INT_MAX) };

0 commit comments

Comments
 (0)