Module Lock_file_blocking
Mutual exclusion between processes using flock and lockf. A file is considered locked only if both of these mechanisms work.
These locks are advisory, meaning that they will not work with systems that don't also try to acquire the matching locks. Although lockf can work across systems (and, in our environment, does work across Linux systems), it is not guaranteed to do so across all implementations.
val create : ?message:string -> ?close_on_exec:bool -> ?unlink_on_exit:bool -> string -> boolcreate ?close_on_exec ?message pathtries to create a file atpathcontaining the textmessage, 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_execisfalse, 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, anat_exithandler 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 -> unitcreate_exn ?message pathis likecreateexcept that it throws an exception on failure instead of returning a boolean value.
val blocking_create : ?timeout:Core.Time.Span.t -> ?message:string -> ?close_on_exec:bool -> ?unlink_on_exit:bool -> string -> unitblocking_create ttries to create the lock. If another process holds the lock this function will wait until it is released or untiltimeoutexpires.
val is_locked : string -> boolis_locked pathreturnstruewhen the file atpathexists and is locked,falseotherwise. Requires write permission for the lock file.
val get_pid : string -> Core.Pid.t optionget_pid pathreads the lock file atpathand returns the pid in the file. ReturnsNoneif the file cannot be read, or if the file contains a message that is not an int.
module Nfs : sig ... endAn 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
createcall 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 anat_exithandler, but see caveats below).
module Mkdir : sig ... endThis is the dumbest lock imaginable: we
mkdirto lock andrmdirto unlock. This gives you pretty good mutual exclusion, but it makes you vulnerable to stale locks.
module Symlink : sig ... endThis is a bit better than
Mkdirand is very likely to be compatible: it lets you atomically write the owner of the lock into the symlink, it's used both by emacs and hg, and it's supposed to work on nfs.
module Flock : sig ... endThis just uses
flock. The main reason this module exists is thatcreatewon't let you release locks, so we need a new interface.