Module Core.Lock_file

Mutual exclusion between processes using flock and lockf. A file is considered locked only if both of these mechanisms work.

These locks are OS-level and as such are local to the machine and will not work across computers even if they mount the same directory.

val create : ?⁠message:string ‑> ?⁠close_on_exec:bool ‑> ?⁠unlink_on_exit:bool ‑> string ‑> bool

create ?close_on_exec ?message path tries to create a file at path containing the text message, which defaults to the pid of the locking process. It returns true on success, false on failure.

Note: there is no way to release the lock or the fd created inside! It will only be released when the process dies. If close_on_exec is false, then the lock will not be released until children created via fork and exec also terminate. If not specified, close_on_exec=true.

Note that by default, the lock file is not cleaned up for you when the process exits. If you pass unlink_on_exit:true, an at_exit handler will be set up to remove the lock file on program termination.

The lock file is created with mode 664, so will not be world-writable even with umask 0.

val create_exn : ?⁠message:string ‑> ?⁠close_on_exec:bool ‑> ?⁠unlink_on_exit:bool ‑> string ‑> unit

create_exn ?message path is like create except that it throws an exception on failure instead of returning a boolean value.

val blocking_create : ?⁠timeout:Core__.Import_time.Time.Span.t ‑> ?⁠message:string ‑> ?⁠close_on_exec:bool ‑> ?⁠unlink_on_exit:bool ‑> string ‑> unit

blocking_create t tries to create the lock. If another process holds the lock this function will wait until it is released or until timeout expires.

val is_locked : string ‑> bool

is_locked path returns true when the file at path exists and is locked, false otherwise. Requires write permission for the lock file.

val get_pid : string ‑> Core__.Import.Pid.t option

get_pid path reads the lock file at path and returns the pid in the file. Returns None if the file cannot be read, or if the file contains a message that is not an int.

module Nfs : sig ... end

An implementation-neutral NFS lock file scheme that relies on the atomicity of link over NFS. Rather than relying on a working traditional advisory lock system over NFS, we create a hard link between the file given to the create call and a new file <filename>.nfs_lock. This link call is atomic (in that it succeeds or fails) across all systems that have the same filesystem mounted. The link file must be cleaned up on program exit (normally accomplished by an at_exit handler, but see caveats below).