Module Mutex.Guard

type 'k t

'k Mutex.Guard.t represents a locked mutex. It morally contains a Capsule.Key.t, but also has a finalizer that poisons the mutex if it is garbage collected without release being called.

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

with_key t ~f runs f, providing it a key for 'k uniquely. If the function raises without returning the key back, the mutex is poisoned.

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

with_password g ~f runs f, providing it a password for 'k, and returns the result of f together with the guard.

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

val access : 'a 'k. 'k t @ unique -> (f: ('k Capsule.Access.t -> 'a @ unique once portable contended) @ local once portable -> 'a * 'k t @ unique once portable contended) @ once

access g ~f runs f, providing it access to the capsule 'k, and returns the result of f together with the guard.

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

val release : 'k t @ unique -> unit

release guard releases the mutex protected by guard, consuming the guard.

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

poison guard poisons the mutex protected by guard, consuming the guard and returning the key to the protected capsule.

Note that poisoning a mutex does not signal waiters on associated condition variables.

val is_poisoning : 'k t @ local -> bool

is_poisoning guard is true if guard will poison if its finalizer runs without release being called.