std.fs.flock

std.fs

flock(fd, how) -> ok, err

Take, convert or drop an advisory lock on an open fd

Wraps flock(2), and never blocks. how is "ex" for an exclusive lock, "sh" for a shared one, or "un" to drop it. Another word raises.

Answers true, or nil and "locked" when another holder is in the way. A failure answers nil and the errno text, which is never "locked".

The lock belongs to the open file description, not to the fd number or the process. A dup of the fd shares it, and so does a child of fork. Another open of the same file is another taker, even in the same process. The lock drops when the last fd on that description closes, so a killed process frees it.

Converting between "sh" and "ex" is not atomic. The kernel drops the old lock first, so a refused conversion leaves the fd holding none.