moonpool/lwt/Lwt_preemptive/index.html
2025-09-26 19:08:06 +00:00

2 lines
8.3 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>Lwt_preemptive (lwt.Lwt_preemptive)</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> &#x00BB; <a href="../index.html">lwt</a> &#x00BB; Lwt_preemptive</nav><header class="odoc-preamble"><h1>Module <code><span>Lwt_preemptive</span></code></h1><p>This module allows to mix preemptive threads with <code>Lwt</code> cooperative threads. It maintains an extensible pool of preemptive threads to which you can detach computations.</p><p>See <a href="https://github.com/hcarty/mwt">Mwt</a> for a more modern implementation.</p></header><div class="odoc-content"><div class="odoc-spec"><div class="spec value anchored" id="val-detach"><a href="#val-detach" class="anchor"></a><code><span><span class="keyword">val</span> detach : <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><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'b</span> <a href="../Lwt/index.html#type-t">Lwt.t</a></span></span></code></div><div class="spec-doc"><p><code>detach f x</code> runs the computation <code>f x</code> in a separate preemptive thread. <code>detach</code> evaluates to an Lwt promise, which is pending until the preemptive thread completes.</p><p><code>detach</code> calls <a href="#val-simple_init"><code>simple_init</code></a> internally, which means that the number of preemptive threads is capped by default at four. If you would like a higher limit, call <a href="#val-init"><code>init</code></a> or <a href="#val-set_bounds"><code>set_bounds</code></a> directly.</p><p>Note that Lwt thread-local storage (i.e., <a href="../Lwt/index.html#val-with_value"><code>Lwt.with_value</code></a>) cannot be safely used from within <code>f</code>. The same goes for most of the rest of Lwt. If you need to run an Lwt thread in <code>f</code>, use <a href="#val-run_in_main"><code>run_in_main</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-run_in_main"><a href="#val-run_in_main" class="anchor"></a><code><span><span class="keyword">val</span> run_in_main : <span><span>(<span>unit <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <a href="../Lwt/index.html#type-t">Lwt.t</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>run_in_main f</code> can be called from a detached computation to execute <code>f ()</code> in the main preemptive thread, i.e. the one executing <a href="../Lwt_main/index.html#val-run"><code>Lwt_main.run</code></a>. <code>run_in_main f</code> blocks until <code>f ()</code> completes, then returns its result. If <code>f ()</code> raises an exception, <code>run_in_main f</code> raises the same exception.</p><p><a href="../Lwt/index.html#val-with_value"><code>Lwt.with_value</code></a> may be used inside <code>f ()</code>. <a href="../Lwt/index.html#val-get"><code>Lwt.get</code></a> can correctly retrieve values set this way inside <code>f ()</code>, but not values set using <a href="../Lwt/index.html#val-with_value"><code>Lwt.with_value</code></a> outside <code>f ()</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-run_in_main_dont_wait"><a href="#val-run_in_main_dont_wait" class="anchor"></a><code><span><span class="keyword">val</span> run_in_main_dont_wait : <span><span>(<span>unit <span class="arrow">&#45;&gt;</span></span> <span>unit <a href="../Lwt/index.html#type-t">Lwt.t</a></span>)</span> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span>exn <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>run_in_main_dont_wait f h</code> does the same as <code>run_in_main f</code> but a bit faster and lighter as it does not wait for the result of <code>f</code>.</p><p>If <code>f</code>'s promise is rejected (or if it raises), then the function <code>h</code> is called with the rejection exception.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 5.7.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-init"><a href="#val-init" class="anchor"></a><code><span><span class="keyword">val</span> init : <span>int <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span><span>(<span>string <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>init min max log</code> initialises this module. i.e. it launches the minimum number of preemptive threads and starts the <b>dispatcher</b>.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">min</span> <p>is the minimum number of threads</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">max</span> <p>is the maximum number of threads</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">log</span> <p>is used to log error messages</p></li></ul><p>If <a href="#"><code>Lwt_preemptive</code></a> has already been initialised, this call only modify bounds and the log function.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-simple_init"><a href="#val-simple_init" class="anchor"></a><code><span><span class="keyword">val</span> simple_init : <span>unit <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>simple_init ()</code> checks if the library is not yet initialized, and if not, does a <i>simple initialization</i>. The minimum number of threads is set to zero, maximum to four, and the log function is left unchanged, i.e. the default built-in logging function is used. See <a href="#val-init"><code>Lwt_preemptive.init</code></a>.</p><p>Note: this function is automatically called by <a href="#val-detach"><code>detach</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_bounds"><a href="#val-get_bounds" class="anchor"></a><code><span><span class="keyword">val</span> get_bounds : <span>unit <span class="arrow">&#45;&gt;</span></span> int * int</span></code></div><div class="spec-doc"><p><code>get_bounds ()</code> returns the minimum and the maximum number of preemptive threads.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_bounds"><a href="#val-set_bounds" class="anchor"></a><code><span><span class="keyword">val</span> set_bounds : <span><span>(int * int)</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>set_bounds (min, max)</code> set the minimum and the maximum number of preemptive threads.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_max_number_of_threads_queued"><a href="#val-set_max_number_of_threads_queued" class="anchor"></a><code><span><span class="keyword">val</span> set_max_number_of_threads_queued : <span>int <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Sets the size of the waiting queue, if no more preemptive threads are available. When the queue is full, <a href="#val-detach"><code>detach</code></a> will sleep until a thread is available.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_max_number_of_threads_queued"><a href="#val-get_max_number_of_threads_queued" class="anchor"></a><code><span><span class="keyword">val</span> get_max_number_of_threads_queued : <span>unit <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p>Returns the size of the waiting queue, if no more threads are available</p></div></div></div></body></html>