moonpool/moonpool/Moonpool/Task_local_storage/index.html
2024-02-29 20:32:26 +00:00

8 lines
5.1 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>Task_local_storage (moonpool.Moonpool.Task_local_storage)</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; Task_local_storage</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool.Task_local_storage</span></code></h1><p>Task-local storage.</p><p>This storage is associated to the current task, just like thread-local storage is associated with the current thread. The storage is carried along in case the current task is suspended.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> NEXT_RELEASE</li></ul></header><div class="odoc-content"><div class="odoc-spec"><div class="spec type anchored" id="type-storage"><a href="#type-storage" class="anchor"></a><code><span><span class="keyword">type</span> storage</span></code></div><div class="spec-doc"><p>Underlying storage for a task</p></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-key"><a href="#type-key" class="anchor"></a><code><span><span class="keyword">type</span> <span>'a key</span></span></code></div><div class="spec-doc"><p>A key used to access a particular (typed) storage slot on every task.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-new_key"><a href="#val-new_key" class="anchor"></a><code><span><span class="keyword">val</span> new_key : <span><span class="label">init</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>unit <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <a href="#type-key">key</a></span></span></code></div><div class="spec-doc"><p><code>new_key ~init ()</code> makes a new key. Keys are expensive and should never be allocated dynamically or in a loop. The correct pattern is, at toplevel:</p><pre class="language-ocaml"><code> let k_foo : foo Task_ocal_storage.key =
Task_local_storage.new_key ~init:(fun () -&gt; make_foo ()) ()
(* … *)
(* use it: *)
let … = Task_local_storage.get k_foo</code></pre></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-key">key</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p><code>get k</code> gets the value for the current task for key <code>k</code>. Must be run from inside a task running on a runner.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <code>Failure</code> <p>otherwise</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_opt"><a href="#val-get_opt" class="anchor"></a><code><span><span class="keyword">val</span> get_opt : <span><span><span class="type-var">'a</span> <a href="#type-key">key</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> option</span></span></code></div><div class="spec-doc"><p><code>get_opt k</code> gets the current task's value for key <code>k</code>, or <code>None</code> if not run from inside the task.</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-key">key</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><code>set k v</code> sets the storage for <code>k</code> to <code>v</code>. Must be run from inside a task running on a runner.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <code>Failure</code> <p>otherwise</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_value"><a href="#val-with_value" class="anchor"></a><code><span><span class="keyword">val</span> with_value : <span><span><span class="type-var">'a</span> <a href="#type-key">key</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span>unit <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_value k v f</code> sets <code>k</code> to <code>v</code> for the duration of the call to <code>f()</code>. When <code>f()</code> returns (or fails), <code>k</code> is restored to its old value.</p></div></div></div></body></html>