Skip to main content

Module lock

Module lock 

Source
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:

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§

ReaderLock
RAII guard for a held reader-lock byte. Dropping the guard releases the OS-side lock.
WriterLock
RAII guard for a held WRITER_LOCK byte. Dropping the guard releases the OS-side lock. The guard is !Send only 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).