moonpool/dev/moonpool/Moonpool/Lock/index.html
2024-02-16 02:14:10 +00:00

13 lines
5.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Lock (moonpool.Moonpool.Lock)</title><meta charset="utf-8"/><link rel="stylesheet" href="../../../_odoc-theme/odoc.css"/><meta name="generator" content="odoc 2.4.1"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> <a href="../../index.html">moonpool</a> &#x00BB; <a href="../index.html">Moonpool</a> &#x00BB; Lock</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool.Lock</span></code></h1><p>Mutex-protected resource.</p><p>This lock is a synchronous concurrency primitive, as a thin wrapper around <code>Mutex</code> that encourages proper management of the critical section in RAII style:</p><pre class="language-ocaml"><code>let (let@) = (@@)
let compute_foo =
(* enter critical section *)
let@ x = Lock.with_ protected_resource in
use_x;
return_foo ()
(* exit critical section *)
in
</code></pre><p>This lock does not work well with <a href="../Fut/index.html#await"><code>await</code></a>. A critical section that contains a call to <code>await</code> might cause deadlocks, or lock starvation, because it will hold onto the lock while it goes to sleep.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.3</li></ul></header><div class="odoc-content"><div class="odoc-spec"><div class="spec type anchored" id="type-t"><a href="#type-t" class="anchor"></a><code><span><span class="keyword">type</span> <span>'a t</span></span></code></div><div class="spec-doc"><p>A value protected by a mutex</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-create"><a href="#val-create" class="anchor"></a><code><span><span class="keyword">val</span> create : <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-doc"><p>Create a new protected value.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_"><a href="#val-with_" class="anchor"></a><code><span><span class="keyword">val</span> with_ : <span><span><span class="type-var">'a</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'b</span>)</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'b</span></span></code></div><div class="spec-doc"><p><code>with_ l f</code> runs <code>f x</code> where <code>x</code> is the value protected with the lock <code>l</code>, in a critical section. If <code>f x</code> fails, <code>with_lock l f</code> fails too but the lock is released.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-update"><a href="#val-update" class="anchor"></a><code><span><span class="keyword">val</span> update : <span><span><span class="type-var">'a</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span>)</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>update l f</code> replaces the content <code>x</code> of <code>l</code> with <code>f x</code>, while protected by the mutex.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-update_map"><a href="#val-update_map" class="anchor"></a><code><span><span class="keyword">val</span> update_map : <span><span><span class="type-var">'a</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span> * <span class="type-var">'b</span>)</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'b</span></span></code></div><div class="spec-doc"><p><code>update_map l f</code> computes <code>x', y = f (get l)</code>, then puts <code>x'</code> in <code>l</code> and returns <code>y</code>, while protected by the mutex.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-mutex"><a href="#val-mutex" class="anchor"></a><code><span><span class="keyword">val</span> mutex : <span><span><span class="type-var">_</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <a href="../../../ocaml/Stdlib/Mutex/index.html#type-t">Stdlib.Mutex.t</a></span></code></div><div class="spec-doc"><p>Underlying mutex.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get"><a href="#val-get" class="anchor"></a><code><span><span class="keyword">val</span> get : <span><span><span class="type-var">'a</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Atomically get the value in the lock. The value that is returned isn't protected!</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set"><a href="#val-set" class="anchor"></a><code><span><span class="keyword">val</span> set : <span><span><span class="type-var">'a</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Atomically set the value.</p><p><b>NOTE</b> caution: using <a href="#val-get"><code>get</code></a> and <a href="#val-set"><code>set</code></a> as if this were a <code>ref</code> is an anti pattern and will not protect data against some race conditions.</p></div></div></div></body></html>