Algorithm for Determining If a Range Access Conflicts with Byte-Range Locks

The inputs for this algorithm are:

  • ByteOffset: A 64-bit unsigned integer specifying the offset of the first byte of the range.

  • Length: A 64-bit unsigned integer specifying the number of bytes in the range.

  • IsExclusive: TRUE if the access to the range has exclusive intent, FALSE otherwise.

  • LockIntent: TRUE if the access to the range has locking intent, FALSE if the intent is performing I/O (reads or writes).

  • Open: The open to the file on which to check for range conflicts.

  • Key: A 32-bit unsigned integer containing an identifier for the open by a specific process.

This algorithm outputs a Boolean value:

  • TRUE if the range conflicts with byte-range locks.

  • FALSE if the range does not conflict.

Pseudocode for the algorithm is as follows:

  • If ((ByteOffset == 0) and (Length == 0)):

    • The {0, 0} range doesn't conflict with any byte-range lock.

    • Return FALSE.

  • EndIf

  • For each ByteRangeLock in Open.Stream.ByteRangeLockList:

    • If ((ByteRangeLock.LockOffset == 0) and (ByteRangeLock.LockLength == 0)):

      • The byte-range lock is over the {0, 0} range so there is no overlap by definition.

    • Else:

      • Initialize LastByteOffset1 = ByteOffset + Length - 1.

      • Initialize LastByteOffset2 = ByteRangeLock.LockOffset + ByteRangeLock.LockLength - 1.

      • If ((ByteOffset <= LastByteOffset2) and (LastByteOffset1 >= ByteRangeLock.LockOffset)):

        • ByteRangeLock and the passed range overlap.

        • If (ByteRangeLock.IsExclusive == TRUE):

          • If (ByteRangeLock.OwnerOpen != Open) or (ByteRangeLock.LockKey != Key):

            • Exclusive byte-range locks block all access to other Opens.

            • Return TRUE.

          • Else If ((IsExclusive == TRUE) and (LockIntent == TRUE)):

            • Overlapping exclusive byte-range locks are not allowed even by the same owner.

            • Return TRUE.

          • EndIf

        • Else If (IsExclusive == TRUE):

          • The ByteRangeLock is shared, shared byte-range locks will block all access with exclusive intent.

          • Return TRUE.

        • EndIf

      • EndIf

    • EndIf

  • EndFor

  • Return FALSE.