Module Picos_std_sync.Lock

A poisonable mutual exclusion lock.

🏎️ This uses a low overhead, optimistic, and unfair implementation that also does not perform runtime ownership error checking. In most cases this should be the mutual exclusion lock you will want to use. Consider using Rwlock in case most operations are reads.

See also Mutex, and Rwlock.

type t

Represents a poisonable mutual exclusion lock.

Basic API

val create : ?padded:bool -> unit -> t

create () returns a new mutual exclusion lock that is initially unlocked.

exception Poisoned

Exception raised in case the lock has been poisoned.

val holding : t -> (unit -> 'a) -> 'a

holding lock thunk acquires the lock and calls thunk (). In case thunk () returns a value, the lock is released and the value is returned. Otherwise the lock is poisoned and the exception is reraised.

The implementation of holding in terms of the low level operations is equivalent to:

let holding t thunk =
  Lock.acquire t;
  match thunk () with
  | value ->
      Lock.release t;
      value
  | exception exn ->
      let bt = Printexc.get_raw_backtrace () in
      Lock.poison t;
      Printexc.raise_with_backtrace exn bt
val protect : t -> (unit -> 'a) -> 'a

protect lock thunk acquires the lock, runs thunk (), and releases the lock after thunk () returns or raises.

module Condition : sig ... end

A condition variable.

State query API

val is_locked : t -> bool

is_locked lock determines whether the lock is currently held exclusively.

val is_poisoned : t -> bool

is_poisoned lock determines whether the lock has been poisoned.

Expert API

⚠️ The calls in this section must be matched correctly or the state of the lock may become corrupted.

val acquire : t -> unit

acquire lock acquires the lock exlusively.

val try_acquire : t -> bool

try_acquire lock attempts to acquire the lock exclusively. Returns true in case of success and false in case of failure.

val release : t -> unit

release lock releases the lock or does nothing in case the lock has been poisoned.

val poison : t -> unit

poison lock marks an exclusively held lock as poisoned.

  • raises Invalid_argument

    in case the lock is not currently held exclusively.