moonpool/thread-local-storage/Thread_local_storage/index.html
2025-04-15 14:16:12 +00:00

2 lines
5.7 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>Thread_local_storage (thread-local-storage.Thread_local_storage)</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">thread-local-storage</a> &#x00BB; Thread_local_storage</nav><header class="odoc-preamble"><h1>Module <code><span>Thread_local_storage</span></code></h1><p>Thread local storage</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> <span>'a t</span></span></code></div><div class="spec-doc"><p>A TLS slot for values of type <code>'a</code>. This allows the storage of a single value of type <code>'a</code> per thread.</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">&#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>Allocate a new TLS slot. The TLS slot starts uninitialised on each thread.</p><p>Note: each TLS slot allocated consumes a word per thread, and it can never be deallocated. <code>create</code> should be called at toplevel to produce constants, do not use it in a loop.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <code>Failure</code> <p>if no more TLS slots can be allocated.</p></li></ul></div></div><div class="odoc-spec"><div class="spec exception anchored" id="exception-Not_set"><a href="#exception-Not_set" class="anchor"></a><code><span><span class="keyword">exception</span> </span><span><span class="exception">Not_set</span></span></code></div><div class="spec-doc"><p>Exception raised when accessing a slot that was not previously set on this thread</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_exn"><a href="#val-get_exn" class="anchor"></a><code><span><span class="keyword">val</span> get_exn : <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><code>get_exn x</code> returns the value previously stored in the TLS slot <code>x</code> for the current thread.</p><p>This function is safe to use from asynchronous callbacks without risks of races.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <a href="#exception-Not_set"><code>Not_set</code></a> <p>if the TLS slot has not been initialised in the current thread. Do note that this uses <code>raise_notrace</code> for performance reasons, so make sure to always catch the exception as it will not carry a backtrace.</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-t">t</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 x</code> returns <code>Some v</code> where v is the value previously stored in the TLS slot <code>x</code> for the current thread. It returns <code>None</code> if the TLS slot has not been initialised in the current thread.</p><p>This function is safe to use from asynchronous callbacks without risks of races.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_default"><a href="#val-get_default" class="anchor"></a><code><span><span class="keyword">val</span> get_default : <span><span class="label">default</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><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><code>get_default x ~default</code> returns the value previously stored in the TLS slot <code>x</code> for the current thread. If the TLS slot has not been initialised, <code>default ()</code> is called, and its result is returned instead, after being stored in the TLS slot <code>x</code>.</p><p>If the call to <code>default ()</code> raises an exception, then the TLS slot remains uninitialised. If <code>default</code> is a function that always raises, then this function is safe to use from asynchronous callbacks without risks of races.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <code>Out_of_memory</code> </li></ul></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><code>set x v</code> stores <code>v</code> into the TLS slot <code>x</code> for the current thread.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <code>Out_of_memory</code> </li></ul></div></div></div></body></html>