Compare commits

...

3 commits

Author SHA1 Message Date
c-cube
338ed1fdfc deploy: 4de33f0121 2025-11-14 01:30:27 +00:00
c-cube
564fe8399e deploy: 58a0f891f7 2025-11-14 01:01:32 +00:00
c-cube
72fd2a8ad9 deploy: c3f235f7e9 2025-11-14 00:46:45 +00:00
28 changed files with 44 additions and 133 deletions

View file

@ -1,4 +1,16 @@
# 0.10
- breaking: remove `around_task` from schedulers
- breaking: remove `moonpool.fib` entirely. Please use `picos_std.structured`
if you really need structured concurrency.
- remove deprecated moonpool-io and moonpool.sync
- feat core: add `Main`, salvaged from moonpool.fib
- block signals in background threads
- refactor `chan`; fix bug in `Chan.try_push`
- fix: make `Moonpool_lwt.fut_of_lwt` idempotent
# 0.9
- breaking: require OCaml 5

View file

@ -173,49 +173,9 @@ val expected_sum : int = 5050
We have a `Exn_bt.t` type that comes in handy in many places. It bundles together
an exception and the backtrace associated with the place the exception was caught.
### Fibers
### Local storage
On OCaml 5, Moonpool comes with a library `moonpool.fib` (module `Moonpool_fib`)
which provides _lightweight fibers_
that can run on any Moonpool runner.
These fibers are a sort of lightweight thread, dispatched on the runner's
background thread(s).
Fibers rely on effects to implement `Fiber.await`, suspending themselves until the `await`-ed fiber
is done.
```ocaml
# #require "moonpool.fib";;
...
# (* convenient alias *)
module F = Moonpool_fib;;
module F = Moonpool_fib
# F.main (fun _runner ->
let f1 = F.spawn (fun () -> fib 10) in
let f2 = F.spawn (fun () -> fib 15) in
F.await f1 + F.await f2);;
- : int = 1076
```
Fibers form a _tree_, where a fiber calling `Fiber.spawn` to start a sub-fiber is
the sub-fiber's _parent_.
When a parent fails, all its children are cancelled (forced to fail).
This is a simple form of [Structured Concurrency](https://en.wikipedia.org/wiki/Structured_concurrency).
Like a future, a fiber eventually _resolves_ into a value (or an `Exn_bt.t`) that it's possible
to `await`. With `Fiber.res : 'a Fiber.t -> 'a Fut.t` it's possible to access that result
as a regular future, too.
However, this resolution is only done after all the children of the fiber have
resolved — the lifetime of fibers forms a well-nested tree in that sense.
When a fiber is suspended because it `await`s another fiber (or future), the scheduler's
thread on which it was running becomes available again and can go on process another task.
When the fiber resumes, it will automatically be re-scheduled on the same runner it started on.
This means fibers on pool P1 can await fibers from pool P2 and still be resumed on P1.
In addition to all that, fibers provide _fiber local storage_ (like thread-local storage, but per fiber).
This storage is inherited in `spawn` (as a shallow copy only — it's advisable to only
put persistent data in storage to avoid confusing aliasing).
Moonpool, via picos, provides _task local storage_ (like thread-local storage, but per task).
The storage is convenient for carrying around context for cross-cutting concerns such
as logging or tracing (e.g. a log tag for the current user or request ID, or a tracing
scope).

View file

@ -3,6 +3,5 @@
<span><span class="optlabel">?on_init_thread</span>:<span>(<span><span class="label">dom_id</span>:int <span class="arrow">&#45;&gt;</span></span> <span><span class="label">t_id</span>:int <span class="arrow">&#45;&gt;</span></span> <span>unit <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?on_exit_thread</span>:<span>(<span><span class="label">dom_id</span>:int <span class="arrow">&#45;&gt;</span></span> <span><span class="label">t_id</span>:int <span class="arrow">&#45;&gt;</span></span> <span>unit <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?on_exn</span>:<span>(<span>exn <span class="arrow">&#45;&gt;</span></span> <span><a href="../../../ocaml/Stdlib/Printexc/index.html#type-raw_backtrace">Stdlib.Printexc.raw_backtrace</a> <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?around_task</span>:<span>(<span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'b</span>)</span> * <span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'b</span> <span class="arrow">&#45;&gt;</span></span> unit)</span>)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?name</span>:string <span class="arrow">&#45;&gt;</span></span>
<span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Arguments used in <a href="#val-create"><code>create</code></a>. See <a href="#val-create"><code>create</code></a> for explanations.</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>(<span>unit <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a>, <span class="type-var">_</span>)</span> <a href="#type-create_args">create_args</a></span></span></code></div><div class="spec-doc"><p>Create the background runner</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_"><a href="#val-with_" class="anchor"></a><code><span><span class="keyword">val</span> with_ : <span><span>(<span>unit <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><a href="#type-t">t</a> <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 class="type-var">_</span>)</span> <a href="#type-create_args">create_args</a></span></span></code></div></div></div></body></html>

View file

@ -3,7 +3,6 @@
<span><span class="optlabel">?on_init_thread</span>:<span>(<span><span class="label">dom_id</span>:int <span class="arrow">&#45;&gt;</span></span> <span><span class="label">t_id</span>:int <span class="arrow">&#45;&gt;</span></span> <span>unit <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?on_exit_thread</span>:<span>(<span><span class="label">dom_id</span>:int <span class="arrow">&#45;&gt;</span></span> <span><span class="label">t_id</span>:int <span class="arrow">&#45;&gt;</span></span> <span>unit <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?on_exn</span>:<span>(<span>exn <span class="arrow">&#45;&gt;</span></span> <span><a href="../../../ocaml/Stdlib/Printexc/index.html#type-raw_backtrace">Stdlib.Printexc.raw_backtrace</a> <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?around_task</span>:<span>(<span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'b</span>)</span> * <span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'b</span> <span class="arrow">&#45;&gt;</span></span> unit)</span>)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?num_threads</span>:int <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?name</span>:string <span class="arrow">&#45;&gt;</span></span>
<span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Arguments used in <a href="#val-create"><code>create</code></a>. See <a href="#val-create"><code>create</code></a> for explanations.</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>(<span>unit <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a>, <span class="type-var">_</span>)</span> <a href="#type-create_args">create_args</a></span></span></code></div><div class="spec-doc"><p><code>create ()</code> makes a new thread pool.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">on_init_thread</span> <p>called at the beginning of each new thread in the pool.</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">min</span> <p>minimum size of the pool. See <code>Pool.create_args</code>. The default is <code>Domain.recommended_domain_count()</code>, ie one worker per CPU core. On OCaml 4 the default is <code>4</code> (since there is only one domain).</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">on_exit_thread</span> <p>called at the end of each worker thread in the pool.</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">around_task</span> <p>a pair of <code>before, after</code> functions ran around each task. See <code>Pool.create_args</code>.</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">name</span> <p>name for the pool, used in tracing (since 0.6)</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_"><a href="#val-with_" class="anchor"></a><code><span><span class="keyword">val</span> with_ : <span><span>(<span>unit <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><a href="#type-t">t</a> <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 class="type-var">_</span>)</span> <a href="#type-create_args">create_args</a></span></span></code></div><div class="spec-doc"><p><code>with_ () f</code> calls <code>f pool</code>, where <code>pool</code> is obtained via <a href="#val-create"><code>create</code></a>. When <code>f pool</code> returns or fails, <code>pool</code> is shutdown and its resources are released. Most parameters are the same as in <a href="#val-create"><code>create</code></a>.</p></div></div></div></body></html>
<span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Arguments used in <a href="#val-create"><code>create</code></a>. See <a href="#val-create"><code>create</code></a> for explanations.</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>(<span>unit <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a>, <span class="type-var">_</span>)</span> <a href="#type-create_args">create_args</a></span></span></code></div><div class="spec-doc"><p><code>create ()</code> makes a new thread pool.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">on_init_thread</span> <p>called at the beginning of each new thread in the pool.</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">min</span> <p>minimum size of the pool. See <code>Pool.create_args</code>. The default is <code>Domain.recommended_domain_count()</code>, ie one worker per CPU core. On OCaml 4 the default is <code>4</code> (since there is only one domain).</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">on_exit_thread</span> <p>called at the end of each worker thread in the pool.</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">name</span> <p>name for the pool, used in tracing (since 0.6)</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_"><a href="#val-with_" class="anchor"></a><code><span><span class="keyword">val</span> with_ : <span><span>(<span>unit <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><a href="#type-t">t</a> <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 class="type-var">_</span>)</span> <a href="#type-create_args">create_args</a></span></span></code></div><div class="spec-doc"><p><code>with_ () f</code> calls <code>f pool</code>, where <code>pool</code> is obtained via <a href="#val-create"><code>create</code></a>. When <code>f pool</code> returns or fails, <code>pool</code> is shutdown and its resources are released. Most parameters are the same as in <a href="#val-create"><code>create</code></a>.</p></div></div></div></body></html>

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Advanced (moonpool.Moonpool.Fut.Advanced)</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">moonpool</a> &#x00BB; <a href="../../index.html">Moonpool</a> &#x00BB; <a href="../index.html">Fut</a> &#x00BB; Advanced</nav><header class="odoc-preamble"><h1>Module <code><span>Fut.Advanced</span></code></h1></header><div class="odoc-content"><div class="odoc-spec"><div class="spec value anchored" id="val-barrier_on_abstract_container_of_futures"><a href="#val-barrier_on_abstract_container_of_futures" class="anchor"></a><code><span><span class="keyword">val</span> barrier_on_abstract_container_of_futures :
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Advanced (moonpool.Moonpool.Fut.Advanced)</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">moonpool</a> &#x00BB; <a href="../../index.html">Moonpool</a> &#x00BB; <a href="../index.html">Fut</a> &#x00BB; Advanced</nav><header class="odoc-preamble"><h1>Module <code><span>Fut.Advanced</span></code></h1><p>Advanced primitives for synchronization</p></header><div class="odoc-content"><div class="odoc-spec"><div class="spec value anchored" id="val-barrier_on_abstract_container_of_futures"><a href="#val-barrier_on_abstract_container_of_futures" class="anchor"></a><code><span><span class="keyword">val</span> barrier_on_abstract_container_of_futures :
<span><span class="label">iter</span>:<span>(<span><span>(<span><span><span class="type-var">'a</span> <a href="../index.html#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'cont</span> <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="label">len</span>:<span>(<span><span class="type-var">'cont</span> <span class="arrow">&#45;&gt;</span></span> int)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="label">aggregate_results</span>:<span>(<span><span>(<span><span><span class="type-var">'a</span> <a href="../index.html#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span>)</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'cont</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'res</span>)</span> <span class="arrow">&#45;&gt;</span></span>

File diff suppressed because one or more lines are too long

View file

@ -1,2 +1,2 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Immediate_runner (moonpool.Moonpool.Immediate_runner)</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">moonpool</a> &#x00BB; <a href="../index.html">Moonpool</a> &#x00BB; Immediate_runner</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool.Immediate_runner</span></code></h1><p>Runner that runs tasks in the caller thread.</p><p>This is removed since 0.6, and replaced by <a href="../../Moonpool_fib/Main/index.html"><code>Moonpool_fib.Main</code></a>.</p><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> use Moonpool_fib.Main</li></ul></header><div class="odoc-content"></div></body></html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Immediate_runner (moonpool.Moonpool.Immediate_runner)</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">moonpool</a> &#x00BB; <a href="../index.html">Moonpool</a> &#x00BB; Immediate_runner</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool.Immediate_runner</span></code></h1><p>Runner that runs tasks in the caller thread.</p><p>This is removed since 0.6, and replaced by <code>Moonpool_fib.Main</code>.</p><ul class="at-tags"><li class="deprecated"><span class="at-tag">deprecated</span> use Moonpool_fib.Main</li></ul></header><div class="odoc-content"></div></body></html>

View file

@ -0,0 +1,2 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Main (moonpool.Moonpool.Main)</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">moonpool</a> &#x00BB; <a href="../index.html">Moonpool</a> &#x00BB; Main</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool.Main</span></code></h1><p>Main thread.</p><p>This is evolved from <code>Moonpool.Immediate_runner</code>, but unlike it, this API assumes you run it in a thread (possibly the main thread) which will block until the initial computation is done.</p><p>This means it's reasonable to use <code>Main.main (fun () -&gt; do_everything)</code> at the beginning of the program. Other Moonpool pools can be created for background tasks, etc. to do the heavy lifting, and the main thread (inside this immediate runner) can coordinate tasks via <code>Fiber.await</code>.</p><p>Aside from the fact that this blocks the caller thread, it is fairly similar to <a href="../Background_thread/index.html"><code>Background_thread</code></a> in that there's a single worker to process tasks/fibers.</p><p>This handles the concurency effects used in moonpool, including <code>await</code> and <code>yield</code>.</p><p>This module was migrated from the late <code>Moonpool_fib</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.10</li></ul></header><div class="odoc-content"><div class="odoc-spec"><div class="spec value anchored" id="val-main"><a href="#val-main" class="anchor"></a><code><span><span class="keyword">val</span> main : <span><span>(<span><a href="../Runner/index.html#type-t">Runner.t</a> <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>main f</code> runs <code>f()</code> in a scope that handles effects, including <code>Fiber.await</code>.</p><p>This scope can run background tasks as well, in a cooperative fashion.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-main'"><a href="#val-main'" class="anchor"></a><code><span><span class="keyword">val</span> main' : <span><span class="optlabel">?block_signals</span>:bool <span class="arrow">&#45;&gt;</span></span> <span>unit <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><a href="../Runner/index.html#type-t">Runner.t</a> <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>Same as <a href="#val-main"><code>main</code></a> but with room for optional arguments.</p></div></div></div></body></html>

View file

@ -3,7 +3,6 @@
<span><span class="optlabel">?on_init_thread</span>:<span>(<span><span class="label">dom_id</span>:int <span class="arrow">&#45;&gt;</span></span> <span><span class="label">t_id</span>:int <span class="arrow">&#45;&gt;</span></span> <span>unit <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?on_exit_thread</span>:<span>(<span><span class="label">dom_id</span>:int <span class="arrow">&#45;&gt;</span></span> <span><span class="label">t_id</span>:int <span class="arrow">&#45;&gt;</span></span> <span>unit <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?on_exn</span>:<span>(<span>exn <span class="arrow">&#45;&gt;</span></span> <span><a href="../../../ocaml/Stdlib/Printexc/index.html#type-raw_backtrace">Stdlib.Printexc.raw_backtrace</a> <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?around_task</span>:<span>(<span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'b</span>)</span> * <span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'b</span> <span class="arrow">&#45;&gt;</span></span> unit)</span>)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?num_threads</span>:int <span class="arrow">&#45;&gt;</span></span>
<span><span class="optlabel">?name</span>:string <span class="arrow">&#45;&gt;</span></span>
<span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Arguments used in <a href="#val-create"><code>create</code></a>. See <a href="#val-create"><code>create</code></a> for explanations.</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>(<span>unit <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a>, <span class="type-var">_</span>)</span> <a href="#type-create_args">create_args</a></span></span></code></div><div class="spec-doc"><p><code>create ()</code> makes a new thread pool.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">on_init_thread</span> <p>called at the beginning of each new thread in the pool.</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">num_threads</span> <p>size of the pool, ie. number of worker threads. It will be at least <code>1</code> internally, so <code>0</code> or negative values make no sense. The default is <code>Domain.recommended_domain_count()</code>, ie one worker thread per CPU core. On OCaml 4 the default is <code>4</code> (since there is only one domain).</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">on_exit_thread</span> <p>called at the end of each thread in the pool</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">around_task</span> <p>a pair of <code>before, after</code>, where <code>before pool</code> is called before a task is processed, on the worker thread about to run it, and returns <code>x</code>; and <code>after pool x</code> is called by the same thread after the task is over. (since 0.2)</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">name</span> <p>a name for this thread pool, used if tracing is enabled (since 0.6)</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_"><a href="#val-with_" class="anchor"></a><code><span><span class="keyword">val</span> with_ : <span><span>(<span>unit <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><a href="#type-t">t</a> <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 class="type-var">_</span>)</span> <a href="#type-create_args">create_args</a></span></span></code></div><div class="spec-doc"><p><code>with_ () f</code> calls <code>f pool</code>, where <code>pool</code> is obtained via <a href="#val-create"><code>create</code></a>. When <code>f pool</code> returns or fails, <code>pool</code> is shutdown and its resources are released.</p><p>Most parameters are the same as in <a href="#val-create"><code>create</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.3</li></ul></div></div></div></body></html>
<span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Arguments used in <a href="#val-create"><code>create</code></a>. See <a href="#val-create"><code>create</code></a> for explanations.</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>(<span>unit <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a>, <span class="type-var">_</span>)</span> <a href="#type-create_args">create_args</a></span></span></code></div><div class="spec-doc"><p><code>create ()</code> makes a new thread pool.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">on_init_thread</span> <p>called at the beginning of each new thread in the pool.</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">num_threads</span> <p>size of the pool, ie. number of worker threads. It will be at least <code>1</code> internally, so <code>0</code> or negative values make no sense. The default is <code>Domain.recommended_domain_count()</code>, ie one worker thread per CPU core. On OCaml 4 the default is <code>4</code> (since there is only one domain).</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">on_exit_thread</span> <p>called at the end of each thread in the pool</p></li></ul><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">name</span> <p>a name for this thread pool, used if tracing is enabled (since 0.6)</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_"><a href="#val-with_" class="anchor"></a><code><span><span class="keyword">val</span> with_ : <span><span>(<span>unit <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><a href="#type-t">t</a> <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 class="type-var">_</span>)</span> <a href="#type-create_args">create_args</a></span></span></code></div><div class="spec-doc"><p><code>with_ () f</code> calls <code>f pool</code>, where <code>pool</code> is obtained via <a href="#val-create"><code>create</code></a>. When <code>f pool</code> returns or fails, <code>pool</code> is shutdown and its resources are released.</p><p>Most parameters are the same as in <a href="#val-create"><code>create</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.3</li></ul></div></div></div></body></html>

File diff suppressed because one or more lines are too long

View file

@ -1,2 +1,2 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Moonpool_fib__ (moonpool.Moonpool_fib__)</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">moonpool</a> &#x00BB; Moonpool_fib__</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool_fib__</span></code></h1></header><div class="odoc-content"><p>This module is hidden.</p></div></body></html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Moonpool__Main (moonpool.Moonpool__Main)</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">moonpool</a> &#x00BB; Moonpool__Main</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool__Main</span></code></h1></header><div class="odoc-content"><p>This module is hidden.</p></div></body></html>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,2 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Handle (moonpool.Moonpool_fib.Handle)</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">moonpool</a> &#x00BB; <a href="../index.html">Moonpool_fib</a> &#x00BB; Handle</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool_fib.Handle</span></code></h1><p>The unique name of a fiber.</p><p>Each fiber has a unique handle that can be used to refer to it in maps or sets.</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><span> = <span class="keyword">private</span> int</span></code></div><div class="spec-doc"><p>Unique, opaque identifier for a fiber.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-equal"><a href="#val-equal" class="anchor"></a><code><span><span class="keyword">val</span> equal : <span><a href="#type-t">t</a> <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><div class="odoc-spec"><div class="spec value anchored" id="val-compare"><a href="#val-compare" class="anchor"></a><code><span><span class="keyword">val</span> compare : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> int</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-hash"><a href="#val-hash" class="anchor"></a><code><span><span class="keyword">val</span> hash : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> int</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-generate_fresh"><a href="#val-generate_fresh" class="anchor"></a><code><span><span class="keyword">val</span> generate_fresh : <span>unit <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Generate a fresh, unique identifier</p></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Set"><a href="#module-Set" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Set/index.html">Set</a></span><span> : <a href="../../../ocaml/Stdlib/Set/module-type-S/index.html">Set.S</a> <span class="keyword">with</span> <span><span class="keyword">type</span> <a href="../../../ocaml/Stdlib/Set/module-type-S/index.html#type-elt">elt</a> = <a href="#type-t">t</a></span></span></code></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Map"><a href="#module-Map" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Map/index.html">Map</a></span><span> : <a href="../../../ocaml/Stdlib/Map/module-type-S/index.html">Map.S</a> <span class="keyword">with</span> <span><span class="keyword">type</span> <a href="../../../ocaml/Stdlib/Map/module-type-S/index.html#type-key">key</a> = <a href="#type-t">t</a></span></span></code></div></div></div></body></html>

View file

@ -1,2 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Main (moonpool.Moonpool_fib.Main)</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">moonpool</a> &#x00BB; <a href="../index.html">Moonpool_fib</a> &#x00BB; Main</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool_fib.Main</span></code></h1><p>Main thread.</p><p>This is evolved from <code>Moonpool.Immediate_runner</code>, but unlike it, this API assumes you run it in a thread (possibly the main thread) which will block until the initial computation is done.</p><p>This means it's reasonable to use <code>Main.main (fun () -&gt; do_everything)</code> at the beginning of the program. Other Moonpool pools can be created for background tasks, etc. to do the heavy lifting, and the main thread (inside this immediate runner) can coordinate tasks via <code>Fiber.await</code>.</p><p>Aside from the fact that this blocks the caller thread, it is fairly similar to <code>Background_thread</code> in that there's a single worker to process tasks/fibers.</p><p>This handles effects, including the ones in <a href="../Fiber/index.html"><code>Fiber</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.6</li></ul></header><div class="odoc-content"><div class="odoc-spec"><div class="spec value anchored" id="val-main"><a href="#val-main" class="anchor"></a><code><span><span class="keyword">val</span> main : <span><span>(<span><a href="../../Moonpool/Runner/index.html#type-t">Moonpool.Runner.t</a> <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>main f</code> runs <code>f()</code> in a scope that handles effects, including <a href="../Fiber/index.html#val-await"><code>Fiber.await</code></a>.</p><p>This scope can run background tasks as well, in a cooperative fashion.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-main'"><a href="#val-main'" class="anchor"></a><code><span><span class="keyword">val</span> main' : <span><span class="optlabel">?block_signals</span>:bool <span class="arrow">&#45;&gt;</span></span> <span>unit <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><a href="../../Moonpool/Runner/index.html#type-t">Moonpool.Runner.t</a> <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>Same as <a href="#val-main"><code>main</code></a> but with room for optional arguments.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.7</li></ul></div></div></div></body></html>

File diff suppressed because one or more lines are too long

View file

@ -1,2 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Moonpool_fib__Fiber (moonpool.Moonpool_fib__Fiber)</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">moonpool</a> &#x00BB; Moonpool_fib__Fiber</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool_fib__Fiber</span></code></h1></header><div class="odoc-content"><p>This module is hidden.</p></div></body></html>

View file

@ -1,2 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Moonpool_fib__Fls (moonpool.Moonpool_fib__Fls)</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">moonpool</a> &#x00BB; Moonpool_fib__Fls</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool_fib__Fls</span></code></h1></header><div class="odoc-content"><p>This module is hidden.</p></div></body></html>

View file

@ -1,2 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Moonpool_fib__Handle (moonpool.Moonpool_fib__Handle)</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">moonpool</a> &#x00BB; Moonpool_fib__Handle</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool_fib__Handle</span></code></h1></header><div class="odoc-content"><p>This module is hidden.</p></div></body></html>

View file

@ -1,2 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Moonpool_fib__Main (moonpool.Moonpool_fib__Main)</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">moonpool</a> &#x00BB; Moonpool_fib__Main</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool_fib__Main</span></code></h1></header><div class="odoc-content"><p>This module is hidden.</p></div></body></html>

View file

@ -0,0 +1,2 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Signals_ (moonpool.Moonpool_private.Signals_)</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">moonpool</a> &#x00BB; <a href="../index.html">Moonpool_private</a> &#x00BB; Signals_</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool_private.Signals_</span></code></h1></header><div class="odoc-content"><div class="odoc-spec"><div class="spec value anchored" id="val-ignore_signals_"><a href="#val-ignore_signals_" class="anchor"></a><code><span><span class="keyword">val</span> ignore_signals_ : <span>unit <span class="arrow">&#45;&gt;</span></span> unit</span></code></div></div></div></body></html>

View file

@ -1,2 +1,2 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Moonpool_private (moonpool.Moonpool_private)</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">moonpool</a> &#x00BB; Moonpool_private</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool_private</span></code></h1></header><div class="odoc-content"><div class="odoc-spec"><div class="spec module anchored" id="module-Domain_"><a href="#module-Domain_" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Domain_/index.html">Domain_</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Tracing_"><a href="#module-Tracing_" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Tracing_/index.html">Tracing_</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Ws_deque_"><a href="#module-Ws_deque_" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Ws_deque_/index.html">Ws_deque_</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div><div class="spec-doc"><p>Work-stealing deque.</p></div></div></div></body></html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Moonpool_private (moonpool.Moonpool_private)</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">moonpool</a> &#x00BB; Moonpool_private</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool_private</span></code></h1></header><div class="odoc-content"><div class="odoc-spec"><div class="spec module anchored" id="module-Domain_"><a href="#module-Domain_" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Domain_/index.html">Domain_</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Signals_"><a href="#module-Signals_" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Signals_/index.html">Signals_</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Tracing_"><a href="#module-Tracing_" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Tracing_/index.html">Tracing_</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Ws_deque_"><a href="#module-Ws_deque_" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Ws_deque_/index.html">Ws_deque_</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div><div class="spec-doc"><p>Work-stealing deque.</p></div></div></div></body></html>

View file

@ -0,0 +1,2 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Moonpool_private__Signals_ (moonpool.Moonpool_private__Signals_)</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">moonpool</a> &#x00BB; Moonpool_private__Signals_</nav><header class="odoc-preamble"><h1>Module <code><span>Moonpool_private__Signals_</span></code></h1></header><div class="odoc-content"><p>This module is hidden.</p></div></body></html>

View file

@ -1,4 +1,16 @@
# 0.10
- breaking: remove `around_task` from schedulers
- breaking: remove `moonpool.fib` entirely. Please use `picos_std.structured`
if you really need structured concurrency.
- remove deprecated moonpool-io and moonpool.sync
- feat core: add `Main`, salvaged from moonpool.fib
- block signals in background threads
- refactor `chan`; fix bug in `Chan.try_push`
- fix: make `Moonpool_lwt.fut_of_lwt` idempotent
# 0.9
- breaking: require OCaml 5

View file

@ -173,49 +173,9 @@ val expected_sum : int = 5050
We have a `Exn_bt.t` type that comes in handy in many places. It bundles together
an exception and the backtrace associated with the place the exception was caught.
### Fibers
### Local storage
On OCaml 5, Moonpool comes with a library `moonpool.fib` (module `Moonpool_fib`)
which provides _lightweight fibers_
that can run on any Moonpool runner.
These fibers are a sort of lightweight thread, dispatched on the runner's
background thread(s).
Fibers rely on effects to implement `Fiber.await`, suspending themselves until the `await`-ed fiber
is done.
```ocaml
# #require "moonpool.fib";;
...
# (* convenient alias *)
module F = Moonpool_fib;;
module F = Moonpool_fib
# F.main (fun _runner ->
let f1 = F.spawn (fun () -> fib 10) in
let f2 = F.spawn (fun () -> fib 15) in
F.await f1 + F.await f2);;
- : int = 1076
```
Fibers form a _tree_, where a fiber calling `Fiber.spawn` to start a sub-fiber is
the sub-fiber's _parent_.
When a parent fails, all its children are cancelled (forced to fail).
This is a simple form of [Structured Concurrency](https://en.wikipedia.org/wiki/Structured_concurrency).
Like a future, a fiber eventually _resolves_ into a value (or an `Exn_bt.t`) that it's possible
to `await`. With `Fiber.res : 'a Fiber.t -> 'a Fut.t` it's possible to access that result
as a regular future, too.
However, this resolution is only done after all the children of the fiber have
resolved — the lifetime of fibers forms a well-nested tree in that sense.
When a fiber is suspended because it `await`s another fiber (or future), the scheduler's
thread on which it was running becomes available again and can go on process another task.
When the fiber resumes, it will automatically be re-scheduled on the same runner it started on.
This means fibers on pool P1 can await fibers from pool P2 and still be resumed on P1.
In addition to all that, fibers provide _fiber local storage_ (like thread-local storage, but per fiber).
This storage is inherited in `spawn` (as a shallow copy only — it's advisable to only
put persistent data in storage to avoid confusing aliasing).
Moonpool, via picos, provides _task local storage_ (like thread-local storage, but per task).
The storage is convenient for carrying around context for cross-cutting concerns such
as logging or tracing (e.g. a log tag for the current user or request ID, or a tracing
scope).

View file

@ -1,2 +1,2 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>index (moonpool.index)</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; moonpool</nav><header class="odoc-preamble"><h1 id="package-moonpool"><a href="#package-moonpool" class="anchor"></a>Package moonpool <nav><a type="text/plain; charset=UTF-8" href="_doc-dir/CHANGES.md">changes</a> <a href="#package_info">more…</a></nav></h1><ul class="modules"><li><a href="Moonpool/index.html"><code>Moonpool</code></a> <span class="synopsis">Moonpool</span></li><li><a href="Moonpool_dpool/index.html"><code>Moonpool_dpool</code></a> <span class="synopsis">Static pool of domains.</span></li><li><a href="Moonpool_fib/index.html"><code>Moonpool_fib</code></a> <span class="synopsis">Fibers for moonpool.</span></li><li><a href="Moonpool_forkjoin/index.html"><code>Moonpool_forkjoin</code></a> <span class="synopsis">Fork-join primitives.</span></li><li><a href="Moonpool_private/index.html"><code>Moonpool_private</code></a> </li></ul></header><div class="odoc-tocs"><nav class="odoc-toc odoc-local-toc"><ul><li><a href="#package_info">Package info</a></li></ul></nav></div><div class="odoc-content"><h2 id="package_info"><a href="#package_info" class="anchor"></a>Package info</h2><table class="package info"><tr id="info-changes-files"><td><a href="#info-changes-files" aria-hidden="true" class="anchor"></a>changes-files</td><td><ul><li><a type="text/plain; charset=UTF-8" href="_doc-dir/CHANGES.md">CHANGES.md</a></li></ul></td></tr><tr id="info-readme-files"><td><a href="#info-readme-files" aria-hidden="true" class="anchor"></a>readme-files</td><td><ul><li><a type="text/plain; charset=UTF-8" href="_doc-dir/README.md">README.md</a></li></ul></td></tr></table></div></body></html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>index (moonpool.index)</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; moonpool</nav><header class="odoc-preamble"><h1 id="package-moonpool"><a href="#package-moonpool" class="anchor"></a>Package moonpool <nav><a type="text/plain; charset=UTF-8" href="_doc-dir/CHANGES.md">changes</a> <a href="#package_info">more…</a></nav></h1><ul class="modules"><li><a href="Moonpool/index.html"><code>Moonpool</code></a> <span class="synopsis">Moonpool</span></li><li><a href="Moonpool_dpool/index.html"><code>Moonpool_dpool</code></a> <span class="synopsis">Static pool of domains.</span></li><li><a href="Moonpool_forkjoin/index.html"><code>Moonpool_forkjoin</code></a> <span class="synopsis">Fork-join primitives.</span></li><li><a href="Moonpool_private/index.html"><code>Moonpool_private</code></a> </li></ul></header><div class="odoc-tocs"><nav class="odoc-toc odoc-local-toc"><ul><li><a href="#package_info">Package info</a></li></ul></nav></div><div class="odoc-content"><h2 id="package_info"><a href="#package_info" class="anchor"></a>Package info</h2><table class="package info"><tr id="info-changes-files"><td><a href="#info-changes-files" aria-hidden="true" class="anchor"></a>changes-files</td><td><ul><li><a type="text/plain; charset=UTF-8" href="_doc-dir/CHANGES.md">CHANGES.md</a></li></ul></td></tr><tr id="info-readme-files"><td><a href="#info-readme-files" aria-hidden="true" class="anchor"></a>readme-files</td><td><ul><li><a type="text/plain; charset=UTF-8" href="_doc-dir/README.md">README.md</a></li></ul></td></tr></table></div></body></html>