Module Capsule_blocking_sync.Mutex

type mutex

Private implementation type exposed to allow packing mutexes into @@unboxed records/variants.

type 'k t = private mutex

'k t is the type of the mutex that controls access to the capsule 'k. A mutex can be created from a 'k Key @ unique.

type packed =
  1. | P : 'k t -> packed

packed is the type of a mutex for some unknown capsule. Unpacking one provides a 'k Mutex.t together with a fresh existential type brand for 'k.

val create : 'k Capsule.Key.t @ unique -> 'k t

create k creates a new mutex for the capsule 'k, consuming its key.

exception Poisoned

Raising an uncaught exception while holding the lock poisons the mutex. All operations on a poisoned mutex raise the Poisoned exception.

val with_lock : 'a 'k. 'k t -> f:('k Capsule.Password.t @ local -> 'a @ unique once) @ local once -> 'a @ unique once

with_lock m ~f tries to acquire the mutex m. If m is already locked, blocks the current thread until it's unlocked. If successful, provides f a password for the capsule 'k associated with m.

If f raises an exception, the mutex is marked as poisoned and the exception is reraised.

If m is already locked by the current thread, raises Sys_error.

val with_key : 'a 'k. 'k t -> f: ('k Capsule.Key.t @ unique -> #('a * 'k Capsule.Key.t) @ unique once) @ local once -> 'a @ unique once

with_key m ~f tries to acquire the mutex m. If m is already locked, blocks the current thread until it's unlocked. If successful, provides f the key for the capsule 'k associated with m.

If f raises an exception, the mutex is marked as poisoned and the exception is reraised.

If m is already locked by the current thread, raises Sys_error.

val destroy : 'k t -> 'k Capsule.Key.t @ unique

destroy m acquires the mutex m and returns the key representing ownership of 'k. The mutex is marked as poisoned.