val create : ?message:string -> string -> unit Core.Or_error.t
create ?message path
tries to create and lock the file at path
by creating a hard link to path
.nfs_lock. The contents of path
will be replaced with a sexp containing the caller's hostname and pid, and the optional message
.
Efforts will be made to release this lock when the calling program exits. But there is no guarantee that this will occur under some types of program crash. If the program crashes without removing the lock file an attempt will be made to clean up on restart by checking the hostname and pid stored in the lockfile.
create_exn ?message path
is like create
, but throws an exception when it fails to obtain the lock.
val blocking_create : ?timeout:Core.Time.Span.t -> ?message:string -> string -> unit
blocking_create ?message path
is like create
, but sleeps for a short while between lock attempts and does not return until it succeeds or timeout
expires. Timeout defaults to wait indefinitely.
val critical_section : ?message:string -> string -> timeout:Core.Time.Span.t -> f:(unit -> 'a) -> 'a
critical_section ?message ~timeout path ~f
wraps function f
(including exceptions escaping it) by first locking (using blocking_create
) and then unlocking the given lock file.
val get_hostname_and_pid : string -> (string * Core.Pid.t) option
get_hostname_and_pid path
reads the lock file at path
and returns the hostname and path in the file. Returns None
if the file cannot be read.
get_message path
reads the lock file at path
and returns the message in the file. Returns None
if the file cannot be read.
unlock_exn path
unlocks path
if path
was locked from the same host and the pid in the file is either the current pid or not the pid of a running process.
It will raise if for some reason the lock at the given path cannot be unlocked, for example if the lock is taken by somebody else that is still alive on the same box, or taken by a process on a different host, or if there are Unix permissions issues, etc.
This function should be used only by programs that need to release their lock before exiting. If releasing the lock can or should wait till the end of the running process, do not call this function -- this library already takes care of releasing at exit all the locks taken.
val unlock : string -> unit Core.Or_error.t