mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-06 03:05:30 -05:00
16 lines
6.9 KiB
HTML
16 lines
6.9 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Lwt_switch (lwt.Lwt_switch)</title><meta charset="utf-8"/><link rel="stylesheet" href="../../_odoc-theme/odoc.css"/><meta name="generator" content="odoc 3.1.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> » <a href="../index.html">lwt</a> » Lwt_switch</nav><header class="odoc-preamble"><h1>Module <code><span>Lwt_switch</span></code></h1><p>Lwt switches</p></header><div class="odoc-content"><p>Switch has two goals:</p><ul><li>being able to free multiple resources at the same time,</li><li>offer a better alternative than always returning an id to free some resource.</li></ul><p>For example, consider the following interface:</p><pre class="language-ocaml"><code> type id
|
||
|
||
val free : id -> unit Lwt.t
|
||
|
||
val f : unit -> id Lwt.t
|
||
val g : unit -> id Lwt.t
|
||
val h : unit -> id Lwt.t</code></pre><p>Now you want to call <code>f</code>, <code>g</code> and <code>h</code> in parallel. You can simply do:</p><pre class="language-ocaml"><code> lwt idf = f () and idg = g () and idh = h () in
|
||
...</code></pre><p>However, one may want to handle possible failures of <code>f ()</code>, <code>g ()</code> and <code>h ()</code>, and disable all allocated resources if one of these three threads fails. This may be hard since you have to remember which one failed and which one returned correctly.</p><p>Now if we change the interface a little bit:</p><pre class="language-ocaml"><code> val f : ?switch : Lwt_switch.t -> unit -> id Lwt.t
|
||
val g : ?switch : Lwt_switch.t -> unit -> id Lwt.t
|
||
val h : ?switch : Lwt_switch.t -> unit -> id Lwt.t</code></pre><p>the code becomes:</p><pre class="language-ocaml"><code> Lwt_switch.with_switch (fun switch ->
|
||
lwt idf = f ~switch ()
|
||
and idg = g ~switch ()
|
||
and idh = h ~switch () in
|
||
...
|
||
)</code></pre><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>Type of switches.</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>unit <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>create ()</code> creates a new switch.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_switch"><a href="#val-with_switch" class="anchor"></a><code><span><span class="keyword">val</span> with_switch : <span><span>(<span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="../Lwt/index.html#type-t">Lwt.t</a></span>)</span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="../Lwt/index.html#type-t">Lwt.t</a></span></span></code></div><div class="spec-doc"><p><code>with_switch fn</code> is <code>fn switch</code>, where <code>switch</code> is a fresh switch that is turned off when the callback thread finishes (whether it succeeds or fails).</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 2.6.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-is_on"><a href="#val-is_on" class="anchor"></a><code><span><span class="keyword">val</span> is_on : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> bool</span></code></div><div class="spec-doc"><p><code>is_on switch</code> returns <code>true</code> if the switch is currently on, and <code>false</code> otherwise.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-turn_off"><a href="#val-turn_off" class="anchor"></a><code><span><span class="keyword">val</span> turn_off : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span>unit <a href="../Lwt/index.html#type-t">Lwt.t</a></span></span></code></div><div class="spec-doc"><p><code>turn_off switch</code> turns off the switch. It calls all registered hooks, waits for all of them to terminate, then returns. If one of the hooks failed, it will fail with the exception raised by the hook. If the switch is already off, it does nothing.</p></div></div><div class="odoc-spec"><div class="spec exception anchored" id="exception-Off"><a href="#exception-Off" class="anchor"></a><code><span><span class="keyword">exception</span> </span><span><span class="exception">Off</span></span></code></div><div class="spec-doc"><p>Exception raised when trying to add a hook to a switch that is already off.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-check"><a href="#val-check" class="anchor"></a><code><span><span class="keyword">val</span> check : <span><span><a href="#type-t">t</a> option</span> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>check switch</code> does nothing if <code>switch</code> is <code>None</code> or contains an switch that is currently on, and raises <a href="#exception-Off"><code>Off</code></a> otherwise.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-add_hook"><a href="#val-add_hook" class="anchor"></a><code><span><span class="keyword">val</span> add_hook : <span><span><a href="#type-t">t</a> option</span> <span class="arrow">-></span></span> <span><span>(<span>unit <span class="arrow">-></span></span> <span>unit <a href="../Lwt/index.html#type-t">Lwt.t</a></span>)</span> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>add_hook switch f</code> registers <code>f</code> so it will be called when <a href="#val-turn_off"><code>turn_off</code></a> is invoked. It does nothing if <code>switch</code> is <code>None</code>. If <code>switch</code> contains an switch that is already off then <a href="#exception-Off"><code>Off</code></a> is raised.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-add_hook_or_exec"><a href="#val-add_hook_or_exec" class="anchor"></a><code><span><span class="keyword">val</span> add_hook_or_exec : <span><span><a href="#type-t">t</a> option</span> <span class="arrow">-></span></span> <span><span>(<span>unit <span class="arrow">-></span></span> <span>unit <a href="../Lwt/index.html#type-t">Lwt.t</a></span>)</span> <span class="arrow">-></span></span> <span>unit <a href="../Lwt/index.html#type-t">Lwt.t</a></span></span></code></div><div class="spec-doc"><p><code>add_hook_or_exec switch f</code> is the same as <a href="#val-add_hook"><code>add_hook</code></a> except that if the switch is already off, <code>f</code> is called immediately.</p></div></div></div></body></html>
|