moonpool/picos_std/Picos_std_sync/Mutex/index.html
2025-04-15 14:16:12 +00:00

2 lines
6.1 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>Mutex (picos_std.Picos_std_sync.Mutex)</title><meta charset="utf-8"/><link rel="stylesheet" href="../../../_odoc-theme/odoc.css"/><meta name="generator" content="odoc 3.0.0"/><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">Index</a> &#x00BB; <a href="../../index.html">picos_std</a> &#x00BB; <a href="../index.html">Picos_std_sync</a> &#x00BB; Mutex</nav><header class="odoc-preamble"><h1>Module <code><span>Picos_std_sync.Mutex</span></code></h1><p>A mutual-exclusion lock or mutex.</p><p> This intentionally mimics the interface of <a href="../../../ocaml/Stdlib/Mutex/index.html"><code>Stdlib.Mutex</code></a>. Unlike with the standard library mutex, blocking on this mutex potentially allows an effects based scheduler to run other fibers on the thread.</p><p>🏎️ The optional <code>checked</code> argument taken by most of the operations defaults to <code>true</code>. When explicitly specified as <code>~checked:false</code> the mutex implementation may avoid having to obtain the <a href="../../../picos/Picos/Fiber/index.html#val-current" title="Picos.Fiber.current">current fiber</a>, which can be expensive relative to locking or unlocking an uncontested mutex. Note that specifying <code>~checked:false</code> on an operation may prevent error checking also on a subsequent operation.</p></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> t</span></code></div><div class="spec-doc"><p>Represents a mutual-exclusion lock or 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="optlabel">?padded</span>:bool <span class="arrow">&#45;&gt;</span></span> <span>unit <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>create ()</code> returns a new mutex that is initially unlocked.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-lock"><a href="#val-lock" class="anchor"></a><code><span><span class="keyword">val</span> lock : <span><span class="optlabel">?checked</span>:bool <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>lock mutex</code> locks the <code>mutex</code>.</p><p> If the fiber has been canceled and propagation of cancelation is allowed, this may raise the cancelation exception before locking the mutex. If <code>~checked:false</code> was specified, the cancelation exception may or may not be raised.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <code>Sys_error</code> <p>if the mutex is already locked by the fiber. If <code>~checked:false</code> was specified for some previous operation on the mutex the exception may or may not be raised.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-try_lock"><a href="#val-try_lock" class="anchor"></a><code><span><span class="keyword">val</span> try_lock : <span><span class="optlabel">?checked</span>:bool <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> bool</span></code></div><div class="spec-doc"><p><code>try_lock mutex</code> locks the mutex in case the mutex is unlocked. Returns <code>true</code> on success and <code>false</code> in case the mutex was locked.</p><p> If the fiber has been canceled and propagation of cancelation is allowed, this may raise the cancelation exception before locking the mutex. If <code>~checked:false</code> was specified, the cancelation exception may or may not be raised.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-unlock"><a href="#val-unlock" class="anchor"></a><code><span><span class="keyword">val</span> unlock : <span><span class="optlabel">?checked</span>:bool <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>unlock mutex</code> unlocks the mutex.</p><p> This operation is not cancelable.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <code>Sys_error</code> <p>if the mutex was locked by another fiber. If <code>~checked:false</code> was specified for some previous operation on the mutex the exception may or may not be raised.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-protect"><a href="#val-protect" class="anchor"></a><code><span><span class="keyword">val</span> protect : <span><span class="optlabel">?checked</span>:bool <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span>unit <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span>)</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p><code>protect mutex thunk</code> locks the <code>mutex</code>, runs <code>thunk ()</code>, and unlocks the <code>mutex</code> after <code>thunk ()</code> returns or raises.</p><p> If the fiber has been canceled and propagation of cancelation is allowed, this may raise the cancelation exception before locking the mutex. If <code>~checked:false</code> was specified, the cancelation exception may or may not be raised.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <code>Sys_error</code> <p>for the same reasons as <a href="#val-lock"><code>lock</code></a> and <a href="#val-unlock"><code>unlock</code></a>.</p></li></ul></div></div></div></body></html>