Skip to content

Commit 6d9ab56

Browse files
joshtriplettgitbot
authored and
gitbot
committed
Improve documentation for file locking
Add notes to each method stating that locks get dropped on close. Clarify the return values of the try methods: they're only defined if the lock is held via a *different* file handle/descriptor. That goes along with the documentation that calling them while holding a lock via the *same* file handle/descriptor may deadlock. Document the behavior of unlock if no lock is held.
1 parent da7c5ba commit 6d9ab56

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

Diff for: std/src/fs.rs

+40-17
Original file line numberDiff line numberDiff line change
@@ -629,16 +629,19 @@ impl File {
629629
/// This acquires an exclusive advisory lock; no other file handle to this file may acquire
630630
/// another lock.
631631
///
632-
/// If this file handle, or a clone of it, already holds an advisory lock the exact behavior is
633-
/// unspecified and platform dependent, including the possibility that it will deadlock.
634-
/// However, if this method returns, then an exclusive lock is held.
632+
/// If this file handle/descriptor, or a clone of it, already holds an advisory lock the exact
633+
/// behavior is unspecified and platform dependent, including the possibility that it will
634+
/// deadlock. However, if this method returns, then an exclusive lock is held.
635635
///
636636
/// If the file not open for writing, it is unspecified whether this function returns an error.
637637
///
638638
/// Note, this is an advisory lock meant to interact with [`lock_shared`], [`try_lock`],
639639
/// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
640640
/// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
641641
///
642+
/// The lock will be released when this file (along with any other file descriptors/handles
643+
/// duplicated or inherited from it) is closed, or if the [`unlock`] method is called.
644+
///
642645
/// # Platform-specific behavior
643646
///
644647
/// This function currently corresponds to the `flock` function on Unix with the `LOCK_EX` flag,
@@ -671,19 +674,22 @@ impl File {
671674
self.inner.lock()
672675
}
673676

674-
/// Acquire a shared advisory lock on the file. Blocks until the lock can be acquired.
677+
/// Acquire a shared (non-exclusive) advisory lock on the file. Blocks until the lock can be acquired.
675678
///
676679
/// This acquires a shared advisory lock; more than one file handle may hold a shared lock, but
677-
/// none may hold an exclusive lock.
680+
/// none may hold an exclusive lock at the same time.
678681
///
679-
/// If this file handle, or a clone of it, already holds an advisory lock, the exact behavior is
680-
/// unspecified and platform dependent, including the possibility that it will deadlock.
681-
/// However, if this method returns, then a shared lock is held.
682+
/// If this file handle/descriptor, or a clone of it, already holds an advisory lock, the exact
683+
/// behavior is unspecified and platform dependent, including the possibility that it will
684+
/// deadlock. However, if this method returns, then a shared lock is held.
682685
///
683686
/// Note, this is an advisory lock meant to interact with [`lock`], [`try_lock`],
684687
/// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
685688
/// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
686689
///
690+
/// The lock will be released when this file (along with any other file descriptors/handles
691+
/// duplicated or inherited from it) is closed, or if the [`unlock`] method is called.
692+
///
687693
/// # Platform-specific behavior
688694
///
689695
/// This function currently corresponds to the `flock` function on Unix with the `LOCK_SH` flag,
@@ -716,21 +722,28 @@ impl File {
716722
self.inner.lock_shared()
717723
}
718724

719-
/// Acquire an exclusive advisory lock on the file. Returns `Ok(false)` if the file is locked.
725+
/// Try to acquire an exclusive advisory lock on the file.
726+
///
727+
/// Returns `Ok(false)` if a different lock is already held on this file (via another
728+
/// handle/descriptor).
720729
///
721730
/// This acquires an exclusive advisory lock; no other file handle to this file may acquire
722731
/// another lock.
723732
///
724-
/// If this file handle, or a clone of it, already holds an advisory lock, the exact behavior is
725-
/// unspecified and platform dependent, including the possibility that it will deadlock.
726-
/// However, if this method returns, then an exclusive lock is held.
733+
/// If this file handle/descriptor, or a clone of it, already holds an advisory lock, the exact
734+
/// behavior is unspecified and platform dependent, including the possibility that it will
735+
/// deadlock. However, if this method returns `Ok(true)`, then it has acquired an exclusive
736+
/// lock.
727737
///
728738
/// If the file not open for writing, it is unspecified whether this function returns an error.
729739
///
730740
/// Note, this is an advisory lock meant to interact with [`lock`], [`lock_shared`],
731741
/// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
732742
/// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
733743
///
744+
/// The lock will be released when this file (along with any other file descriptors/handles
745+
/// duplicated or inherited from it) is closed, or if the [`unlock`] method is called.
746+
///
734747
/// # Platform-specific behavior
735748
///
736749
/// This function currently corresponds to the `flock` function on Unix with the `LOCK_EX` and
@@ -764,20 +777,25 @@ impl File {
764777
self.inner.try_lock()
765778
}
766779

767-
/// Acquire a shared advisory lock on the file.
768-
/// Returns `Ok(false)` if the file is exclusively locked.
780+
/// Try to acquire a shared (non-exclusive) advisory lock on the file.
781+
///
782+
/// Returns `Ok(false)` if an exclusive lock is already held on this file (via another
783+
/// handle/descriptor).
769784
///
770785
/// This acquires a shared advisory lock; more than one file handle may hold a shared lock, but
771-
/// none may hold an exclusive lock.
786+
/// none may hold an exclusive lock at the same time.
772787
///
773788
/// If this file handle, or a clone of it, already holds an advisory lock, the exact behavior is
774789
/// unspecified and platform dependent, including the possibility that it will deadlock.
775-
/// However, if this method returns, then a shared lock is held.
790+
/// However, if this method returns `Ok(true)`, then it has acquired a shared lock.
776791
///
777792
/// Note, this is an advisory lock meant to interact with [`lock`], [`try_lock`],
778793
/// [`try_lock`], and [`unlock`]. Its interactions with other methods, such as [`read`]
779794
/// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
780795
///
796+
/// The lock will be released when this file (along with any other file descriptors/handles
797+
/// duplicated or inherited from it) is closed, or if the [`unlock`] method is called.
798+
///
781799
/// # Platform-specific behavior
782800
///
783801
/// This function currently corresponds to the `flock` function on Unix with the `LOCK_SH` and
@@ -813,7 +831,12 @@ impl File {
813831

814832
/// Release all locks on the file.
815833
///
816-
/// All remaining locks are released when the file handle, and all clones of it, are dropped.
834+
/// All locks are released when the file (along with any other file descriptors/handles
835+
/// duplicated or inherited from it) is closed. This method allows releasing locks without
836+
/// closing the file.
837+
///
838+
/// If no lock is currently held via this file descriptor/handle, this method may return an
839+
/// error, or may return successfully without taking any action.
817840
///
818841
/// # Platform-specific behavior
819842
///

0 commit comments

Comments
 (0)