From 4046ed7678cd168b171bce273036bfacebdd153e Mon Sep 17 00:00:00 2001 From: nrath-js Date: Sun, 8 Dec 2024 00:20:26 +0000 Subject: [PATCH] Mark libc's ``fs::Dir` as `Sync` (#1232) 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. --- src/backend/libc/fs/dir.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/backend/libc/fs/dir.rs b/src/backend/libc/fs/dir.rs index 4b4676872..9865e13bb 100644 --- a/src/backend/libc/fs/dir.rs +++ b/src/backend/libc/fs/dir.rs @@ -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]