Skip to content

Commit

Permalink
Mark libc's `fs::Dir as Sync (#1232)
Browse files Browse the repository at this point in the history
This is safe since all methods that mutate the internal state require
a &mut self. It also makes behavior consistent with the linux_raw
backend (whose `fs::Dir` is Sync+Send).

Fixes: #1230.
  • Loading branch information
nrath-js authored and sunfishcode committed Dec 8, 2024
1 parent a7ba1b8 commit 4046ed7
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/backend/libc/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,12 @@ impl Dir {
}
}

/// `Dir` implements `Send` but not `Sync`, because we use `readdir` which is
/// not guaranteed to be thread-safe. Users can wrap this in a `Mutex` if they
/// need `Sync`, which is effectively what'd need to do to implement `Sync`
/// ourselves.
/// `Dir` is `Send` and `Sync`, because even though it contains internal
/// state, all methods that modify the state require a `mut &self` and
/// can therefore not be called concurrently. Calling them from different
/// threads sequentially is fine.
unsafe impl Send for Dir {}
unsafe impl Sync for Dir {}

impl Drop for Dir {
#[inline]
Expand Down

0 comments on commit 4046ed7

Please sign in to comment.