1
Fork 0

Rollup merge of #136876 - joshtriplett:locking-might-not-be-advisory, r=Amanieu

Locking documentation updates

- Reword file lock documentation to clarify advisory vs mandatory. Remove the
  word "advisory", and make it more explicit that the lock may be advisory or
  mandatory depending on platform.

- Document that locking a file fails on Windows if the file is opened only for append
This commit is contained in:
Matthias Krüger 2025-02-18 18:40:50 +01:00 committed by GitHub
commit 5a942d67a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -624,21 +624,21 @@ impl File {
self.inner.datasync() self.inner.datasync()
} }
/// Acquire an exclusive advisory lock on the file. Blocks until the lock can be acquired. /// Acquire an exclusive lock on the file. Blocks until the lock can be acquired.
/// ///
/// This acquires an exclusive advisory lock; no other file handle to this file may acquire /// This acquires an exclusive lock; no other file handle to this file may acquire another lock.
/// another lock.
/// ///
/// If this file handle/descriptor, or a clone of it, already holds an advisory lock the exact /// This lock may be advisory or mandatory. This lock is meant to interact with [`lock`],
/// behavior is unspecified and platform dependent, including the possibility that it will /// [`try_lock`], [`lock_shared`], [`try_lock_shared`], and [`unlock`]. Its interactions with
/// deadlock. However, if this method returns, then an exclusive lock is held. /// other methods, such as [`read`] and [`write`] are platform specific, and it may or may not
/// cause non-lockholders to block.
///
/// If this file handle/descriptor, or a clone of it, already holds an lock the exact behavior
/// is unspecified and platform dependent, including the possibility that it will deadlock.
/// However, if this method returns, then an exclusive lock is held.
/// ///
/// If the file not open for writing, it is unspecified whether this function returns an error. /// If the file not open for writing, it is unspecified whether this function returns an error.
/// ///
/// Note, this is an advisory lock meant to interact with [`lock_shared`], [`try_lock`],
/// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
/// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
///
/// The lock will be released when this file (along with any other file descriptors/handles /// The lock will be released when this file (along with any other file descriptors/handles
/// duplicated or inherited from it) is closed, or if the [`unlock`] method is called. /// duplicated or inherited from it) is closed, or if the [`unlock`] method is called.
/// ///
@ -648,8 +648,12 @@ impl File {
/// and the `LockFileEx` function on Windows with the `LOCKFILE_EXCLUSIVE_LOCK` flag. Note that, /// and the `LockFileEx` function on Windows with the `LOCKFILE_EXCLUSIVE_LOCK` flag. Note that,
/// this [may change in the future][changes]. /// this [may change in the future][changes].
/// ///
/// On Windows, locking a file will fail if the file is opened only for append. To lock a file,
/// open it with one of `.read(true)`, `.read(true).append(true)`, or `.write(true)`.
///
/// [changes]: io#platform-specific-behavior /// [changes]: io#platform-specific-behavior
/// ///
/// [`lock`]: File::lock
/// [`lock_shared`]: File::lock_shared /// [`lock_shared`]: File::lock_shared
/// [`try_lock`]: File::try_lock /// [`try_lock`]: File::try_lock
/// [`try_lock_shared`]: File::try_lock_shared /// [`try_lock_shared`]: File::try_lock_shared
@ -674,18 +678,19 @@ impl File {
self.inner.lock() self.inner.lock()
} }
/// Acquire a shared (non-exclusive) advisory lock on the file. Blocks until the lock can be acquired. /// Acquire a shared (non-exclusive) lock on the file. Blocks until the lock can be acquired.
/// ///
/// This acquires a shared advisory lock; more than one file handle may hold a shared lock, but /// This acquires a shared lock; more than one file handle may hold a shared lock, but none may
/// none may hold an exclusive lock at the same time. /// hold an exclusive lock at the same time.
/// ///
/// If this file handle/descriptor, or a clone of it, already holds an advisory lock, the exact /// This lock may be advisory or mandatory. This lock is meant to interact with [`lock`],
/// behavior is unspecified and platform dependent, including the possibility that it will /// [`try_lock`], [`lock_shared`], [`try_lock_shared`], and [`unlock`]. Its interactions with
/// deadlock. However, if this method returns, then a shared lock is held. /// other methods, such as [`read`] and [`write`] are platform specific, and it may or may not
/// cause non-lockholders to block.
/// ///
/// Note, this is an advisory lock meant to interact with [`lock`], [`try_lock`], /// If this file handle/descriptor, or a clone of it, already holds an lock, the exact behavior
/// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`] /// is unspecified and platform dependent, including the possibility that it will deadlock.
/// and [`write`] are platform specific, and it may or may not cause non-lockholders to block. /// However, if this method returns, then a shared lock is held.
/// ///
/// The lock will be released when this file (along with any other file descriptors/handles /// The lock will be released when this file (along with any other file descriptors/handles
/// duplicated or inherited from it) is closed, or if the [`unlock`] method is called. /// duplicated or inherited from it) is closed, or if the [`unlock`] method is called.
@ -696,9 +701,13 @@ impl File {
/// and the `LockFileEx` function on Windows. Note that, this /// and the `LockFileEx` function on Windows. Note that, this
/// [may change in the future][changes]. /// [may change in the future][changes].
/// ///
/// On Windows, locking a file will fail if the file is opened only for append. To lock a file,
/// open it with one of `.read(true)`, `.read(true).append(true)`, or `.write(true)`.
///
/// [changes]: io#platform-specific-behavior /// [changes]: io#platform-specific-behavior
/// ///
/// [`lock`]: File::lock /// [`lock`]: File::lock
/// [`lock_shared`]: File::lock_shared
/// [`try_lock`]: File::try_lock /// [`try_lock`]: File::try_lock
/// [`try_lock_shared`]: File::try_lock_shared /// [`try_lock_shared`]: File::try_lock_shared
/// [`unlock`]: File::unlock /// [`unlock`]: File::unlock
@ -722,25 +731,24 @@ impl File {
self.inner.lock_shared() self.inner.lock_shared()
} }
/// Try to acquire an exclusive advisory lock on the file. /// Try to acquire an exclusive lock on the file.
/// ///
/// Returns `Ok(false)` if a different lock is already held on this file (via another /// Returns `Ok(false)` if a different lock is already held on this file (via another
/// handle/descriptor). /// handle/descriptor).
/// ///
/// This acquires an exclusive advisory lock; no other file handle to this file may acquire /// This acquires an exclusive lock; no other file handle to this file may acquire another lock.
/// another lock.
/// ///
/// If this file handle/descriptor, or a clone of it, already holds an advisory lock, the exact /// This lock may be advisory or mandatory. This lock is meant to interact with [`lock`],
/// behavior is unspecified and platform dependent, including the possibility that it will /// [`try_lock`], [`lock_shared`], [`try_lock_shared`], and [`unlock`]. Its interactions with
/// deadlock. However, if this method returns `Ok(true)`, then it has acquired an exclusive /// other methods, such as [`read`] and [`write`] are platform specific, and it may or may not
/// lock. /// cause non-lockholders to block.
///
/// If this file handle/descriptor, or a clone of it, already holds an lock, the exact behavior
/// is unspecified and platform dependent, including the possibility that it will deadlock.
/// However, if this method returns `Ok(true)`, then it has acquired an exclusive lock.
/// ///
/// If the file not open for writing, it is unspecified whether this function returns an error. /// If the file not open for writing, it is unspecified whether this function returns an error.
/// ///
/// Note, this is an advisory lock meant to interact with [`lock`], [`lock_shared`],
/// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
/// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
///
/// The lock will be released when this file (along with any other file descriptors/handles /// The lock will be released when this file (along with any other file descriptors/handles
/// duplicated or inherited from it) is closed, or if the [`unlock`] method is called. /// duplicated or inherited from it) is closed, or if the [`unlock`] method is called.
/// ///
@ -751,10 +759,14 @@ impl File {
/// and `LOCKFILE_FAIL_IMMEDIATELY` flags. Note that, this /// and `LOCKFILE_FAIL_IMMEDIATELY` flags. Note that, this
/// [may change in the future][changes]. /// [may change in the future][changes].
/// ///
/// On Windows, locking a file will fail if the file is opened only for append. To lock a file,
/// open it with one of `.read(true)`, `.read(true).append(true)`, or `.write(true)`.
///
/// [changes]: io#platform-specific-behavior /// [changes]: io#platform-specific-behavior
/// ///
/// [`lock`]: File::lock /// [`lock`]: File::lock
/// [`lock_shared`]: File::lock_shared /// [`lock_shared`]: File::lock_shared
/// [`try_lock`]: File::try_lock
/// [`try_lock_shared`]: File::try_lock_shared /// [`try_lock_shared`]: File::try_lock_shared
/// [`unlock`]: File::unlock /// [`unlock`]: File::unlock
/// [`read`]: Read::read /// [`read`]: Read::read
@ -777,22 +789,23 @@ impl File {
self.inner.try_lock() self.inner.try_lock()
} }
/// Try to acquire a shared (non-exclusive) advisory lock on the file. /// Try to acquire a shared (non-exclusive) lock on the file.
/// ///
/// Returns `Ok(false)` if an exclusive lock is already held on this file (via another /// Returns `Ok(false)` if an exclusive lock is already held on this file (via another
/// handle/descriptor). /// handle/descriptor).
/// ///
/// This acquires a shared advisory lock; more than one file handle may hold a shared lock, but /// This acquires a shared lock; more than one file handle may hold a shared lock, but none may
/// none may hold an exclusive lock at the same time. /// hold an exclusive lock at the same time.
/// ///
/// If this file handle, or a clone of it, already holds an advisory lock, the exact behavior is /// This lock may be advisory or mandatory. This lock is meant to interact with [`lock`],
/// [`try_lock`], [`lock_shared`], [`try_lock_shared`], and [`unlock`]. Its interactions with
/// other methods, such as [`read`] and [`write`] are platform specific, and it may or may not
/// cause non-lockholders to block.
///
/// If this file handle, or a clone of it, already holds an lock, the exact behavior is
/// unspecified and platform dependent, including the possibility that it will deadlock. /// unspecified and platform dependent, including the possibility that it will deadlock.
/// However, if this method returns `Ok(true)`, then it has acquired a shared lock. /// However, if this method returns `Ok(true)`, then it has acquired a shared lock.
/// ///
/// Note, this is an advisory lock meant to interact with [`lock`], [`try_lock`],
/// [`try_lock`], and [`unlock`]. Its interactions with other methods, such as [`read`]
/// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
///
/// The lock will be released when this file (along with any other file descriptors/handles /// The lock will be released when this file (along with any other file descriptors/handles
/// duplicated or inherited from it) is closed, or if the [`unlock`] method is called. /// duplicated or inherited from it) is closed, or if the [`unlock`] method is called.
/// ///
@ -803,11 +816,15 @@ impl File {
/// `LOCKFILE_FAIL_IMMEDIATELY` flag. Note that, this /// `LOCKFILE_FAIL_IMMEDIATELY` flag. Note that, this
/// [may change in the future][changes]. /// [may change in the future][changes].
/// ///
/// On Windows, locking a file will fail if the file is opened only for append. To lock a file,
/// open it with one of `.read(true)`, `.read(true).append(true)`, or `.write(true)`.
///
/// [changes]: io#platform-specific-behavior /// [changes]: io#platform-specific-behavior
/// ///
/// [`lock`]: File::lock /// [`lock`]: File::lock
/// [`lock_shared`]: File::lock_shared /// [`lock_shared`]: File::lock_shared
/// [`try_lock`]: File::try_lock /// [`try_lock`]: File::try_lock
/// [`try_lock_shared`]: File::try_lock_shared
/// [`unlock`]: File::unlock /// [`unlock`]: File::unlock
/// [`read`]: Read::read /// [`read`]: Read::read
/// [`write`]: Write::write /// [`write`]: Write::write
@ -844,6 +861,9 @@ impl File {
/// and the `UnlockFile` function on Windows. Note that, this /// and the `UnlockFile` function on Windows. Note that, this
/// [may change in the future][changes]. /// [may change in the future][changes].
/// ///
/// On Windows, locking a file will fail if the file is opened only for append. To lock a file,
/// open it with one of `.read(true)`, `.read(true).append(true)`, or `.write(true)`.
///
/// [changes]: io#platform-specific-behavior /// [changes]: io#platform-specific-behavior
/// ///
/// # Examples /// # Examples