jon.recoil.org

Module Capsule_blocking_sync.MutexSource

Sourcetype mutex : value mod contended portable

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

Sourcetype 'k t : value mod contended portable = 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.

Sourcetype packed : value mod contended portable =
  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.

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

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

Sourceexception Poisoned

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

Sourceval with_lock : ('a : value_or_null) '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.

Sourceval with_key : ('a : value_or_null) '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.

Sourceval 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.