rtic/dev/api/heapless/spsc/index.html

88 lines
9.6 KiB
HTML
Raw Normal View History

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Fixed capacity Single Producer Single Consumer (SPSC) queue"><title>heapless::spsc - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../static.files/rustdoc-492a78a4a87dcc01.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="heapless" data-themes="" data-resource-suffix="" data-rustdoc-version="1.82.0 (f6e511eec 2024-10-15)" data-channel="1.82.0" data-search-js="search-a99f1315e7cc5121.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../static.files/storage-118b08c4c78b968e.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../static.files/main-921df33f47b8780c.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-3b12f09e550e0385.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../heapless/index.html">heapless</a><span class="version">0.8.0</span></h2></div><h2 class="location"><a href="#">Module spsc</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#structs">Structs</a></li></ul></section><h2><a href="../index.html">In crate heapless</a></h2></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Module <a href="../index.html">heapless</a>::<wbr><a class="mod" href="#">spsc</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../../src/heapless/spsc.rs.html#1-908">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Fixed capacity Single Producer Single Consumer (SPSC) queue</p>
<p>Implementation based on <a href="https://www.codeproject.com/Articles/43510/Lock-Free-Single-Producer-Single-Consumer-Circular">https://www.codeproject.com/Articles/43510/Lock-Free-Single-Producer-Single-Consumer-Circular</a></p>
<h2 id="portability"><a class="doc-anchor" href="#portability">§</a>Portability</h2>
<p>This module requires CAS atomic instructions which are not available on all architectures
(e.g. ARMv6-M (<code>thumbv6m-none-eabi</code>) and MSP430 (<code>msp430-none-elf</code>)). These atomics can be
emulated however with <a href="https://crates.io/crates/portable-atomic"><code>portable-atomic</code></a>, which is
enabled with the <code>cas</code> feature and is enabled by default for <code>thumbv6m-none-eabi</code> and <code>riscv32</code>
targets.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<ul>
<li><code>Queue</code> can be used as a plain queue</li>
</ul>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>heapless::spsc::Queue;
<span class="kw">let </span><span class="kw-2">mut </span>rb: Queue&lt;u8, <span class="number">4</span>&gt; = Queue::new();
<span class="macro">assert!</span>(rb.enqueue(<span class="number">0</span>).is_ok());
<span class="macro">assert!</span>(rb.enqueue(<span class="number">1</span>).is_ok());
<span class="macro">assert!</span>(rb.enqueue(<span class="number">2</span>).is_ok());
<span class="macro">assert!</span>(rb.enqueue(<span class="number">3</span>).is_err()); <span class="comment">// full
</span><span class="macro">assert_eq!</span>(rb.dequeue(), <span class="prelude-val">Some</span>(<span class="number">0</span>));</code></pre></div>
<ul>
<li><code>Queue</code> can be <code>split</code> and then be used in Single Producer Single Consumer mode.</li>
</ul>
<p>“no alloc” applications can create a <code>&amp;'static mut</code> reference to a <code>Queue</code> using a static
variable and then <code>split</code> it: this consumes the static reference. The resulting <code>Consumer</code>
and <code>Producer</code> can then be moved into different execution contexts (threads, interrupt handlers,
etc.)</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>heapless::spsc::{Producer, Queue};
<span class="kw">enum </span>Event { A, B }
<span class="kw">fn </span>main() {
<span class="kw">let </span>queue: <span class="kw-2">&amp;</span><span class="lifetime">'static </span><span class="kw-2">mut </span>Queue&lt;Event, <span class="number">4</span>&gt; = {
<span class="kw">static </span><span class="kw-2">mut </span>Q: Queue&lt;Event, <span class="number">4</span>&gt; = Queue::new();
<span class="kw">unsafe </span>{ <span class="kw-2">&amp;mut </span>Q }
};
<span class="kw">let </span>(producer, <span class="kw-2">mut </span>consumer) = queue.split();
<span class="comment">// `producer` can be moved into `interrupt_handler` using a static mutex or the mechanism
// provided by the concurrency framework you are using (e.g. a resource in RTIC)
</span><span class="kw">loop </span>{
<span class="kw">match </span>consumer.dequeue() {
<span class="prelude-val">Some</span>(Event::A) =&gt; { <span class="comment">/* .. */ </span>},
<span class="prelude-val">Some</span>(Event::B) =&gt; { <span class="comment">/* .. */ </span>},
<span class="prelude-val">None </span>=&gt; { <span class="comment">/* sleep */ </span>},
}
}
}
<span class="comment">// this is a different execution context that can preempt `main`
</span><span class="kw">fn </span>interrupt_handler(producer: <span class="kw-2">&amp;mut </span>Producer&lt;<span class="lifetime">'static</span>, Event, <span class="number">4</span>&gt;) {
<span class="comment">// ..
</span><span class="kw">if </span>condition {
producer.enqueue(Event::A).ok().unwrap();
} <span class="kw">else </span>{
producer.enqueue(Event::B).ok().unwrap();
}
<span class="comment">// ..
</span>}</code></pre></div>
<h2 id="benchmarks"><a class="doc-anchor" href="#benchmarks">§</a>Benchmarks</h2>
<p>Measured on a ARM Cortex-M3 core running at 8 MHz and with zero Flash wait cycles</p>
<div><table><thead><tr><th><code>-C opt-level</code></th><th><code>3</code></th></tr></thead><tbody>
<tr><td><code>Consumer&lt;u8&gt;::dequeue</code></td><td>15</td></tr>
<tr><td><code>Queue&lt;u8&gt;::dequeue</code></td><td>12</td></tr>
<tr><td><code>Producer&lt;u8&gt;::enqueue</code></td><td>16</td></tr>
<tr><td><code>Queue&lt;u8&gt;::enqueue</code></td><td>14</td></tr>
</tbody></table>
</div>
<ul>
<li>All execution times are in clock cycles. 1 clock cycle = 125 ns.</li>
<li>Execution time is <em>dependent</em> of <code>mem::size_of::&lt;T&gt;()</code>. Both operations include one
<code>memcpy(T)</code> in their successful path.</li>
<li>The optimization level is indicated in the first row.</li>
<li>The numbers reported correspond to the successful path (i.e. <code>Some</code> is returned by <code>dequeue</code>
and <code>Ok</code> is returned by <code>enqueue</code>).</li>
</ul>
</div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.Consumer.html" title="struct heapless::spsc::Consumer">Consumer</a></div><div class="desc docblock-short">A queue “consumer”; it can dequeue items from the queue
NOTE the consumer semantically owns the <code>head</code> pointer of the queue</div></li><li><div class="item-name"><a class="struct" href="struct.Iter.html" title="struct heapless::spsc::Iter">Iter</a></div><div class="desc docblock-short">An iterator over the items of a queue</div></li><li><div class="item-name"><a class="struct" href="struct.IterMut.html" title="struct heapless::spsc::IterMut">IterMut</a></div><div class="desc docblock-short">A mutable iterator over the items of a queue</div></li><li><div class="item-name"><a class="struct" href="struct.Producer.html" title="struct heapless::spsc::Producer">Producer</a></div><div class="desc docblock-short">A queue “producer”; it can enqueue items into the queue
NOTE the producer semantically owns the <code>tail</code> pointer of the queue</div></li><li><div class="item-name"><a class="struct" href="struct.Queue.html" title="struct heapless::spsc::Queue">Queue</a></div><div class="desc docblock-short">A statically allocated single producer single consumer queue with a capacity of <code>N - 1</code> elements</div></li></ul></section></div></main></body></html>