Expand description
Cross-process byte-range file locking.
M6 issue #44. POSIX uses OFD fcntl locks (F_OFD_SETLK /
F_OFD_SETLKW) — kernel-tracked per-fd, fork-safe, automatically
released on process exit. Windows uses LockFileEx /
UnlockFileEx. The lock byte offsets are pinned in
docs/format.md § File locking:
WRITER_LOCK_OFFSET(byte 96): exclusive 1-byte writer lock.READER_LOCK_RANGE_OFFSET(byte 97..128): 31-byte shared reader-lock range; readers pick any byte in the range.
The lock state lives in the OS kernel’s per-fd lock table — the
bytes on disk are never read or written by obj. See
docs/format.md § File locking and § Reader snapshots (MVCC)
for the user-visible protocol.
§unsafe policy
rustix::fs::fcntl_lock does whole-file locking with F_SETLK*,
not OFD locks. We therefore call libc::fcntl directly with the
OFD command IDs. On Windows we call LockFileEx /
UnlockFileEx via windows-sys. Every unsafe block carries a
// SAFETY: comment per power-of-ten Rule 8.
Structs§
- Reader
Lock - RAII guard for a held reader-lock byte. Dropping the guard releases the OS-side lock.
- Writer
Lock - RAII guard for a held
WRITER_LOCKbyte. Dropping the guard releases the OS-side lock. The guard is!Sendonly by virtue of the file handle it does NOT own — the underlying lock is per-fd, so as long as the fd survives, releasing from any thread is sound.
Constants§
- READER_
LOCK_ RANGE_ LEN - Length of the reader-lock byte range. 31 slots.
- READER_
LOCK_ RANGE_ OFFSET - Byte offset of the first reader-lock slot.
- WRITER_
LOCK_ OFFSET - Byte offset of the
WRITER_LOCK(1 byte, exclusive).