mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-12-27 04:19:33 +01:00
88 lines
No EOL
9.9 KiB
HTML
88 lines
No EOL
9.9 KiB
HTML
<!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-b0742ba02757f159.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.83.0 (90b35a623 2024-11-26)" data-channel="1.83.0" data-search-js="search-f0d225181b97f9a4.js" data-settings-js="settings-805db61a62df4bd2.js" ><script src="../../static.files/storage-1d39b6787ed640ff.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../static.files/main-f070b9041d14864c.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-0111fcff984fae8f.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><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Module spsc</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#portability" title="Portability">Portability</a></li><li><a href="#examples" title="Examples">Examples</a></li><li><a href="#benchmarks" title="Benchmarks">Benchmarks</a></li></ul><h3><a href="#structs">Module Items</a></h3><ul class="block"><li><a href="#structs" title="Structs">Structs</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="../index.html">In crate heapless</a></h2></div></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"><span class="rustdoc-breadcrumbs"><a href="../index.html">heapless</a></span><h1>Module <span>spsc</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/heapless/spsc.rs.html#1-908">source</a> </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<u8, <span class="number">4</span>> = 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>&'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">&</span><span class="lifetime">'static </span><span class="kw-2">mut </span>Queue<Event, <span class="number">4</span>> = {
|
||
<span class="kw">static </span><span class="kw-2">mut </span>Q: Queue<Event, <span class="number">4</span>> = Queue::new();
|
||
<span class="kw">unsafe </span>{ <span class="kw-2">&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) => { <span class="comment">/* .. */ </span>},
|
||
<span class="prelude-val">Some</span>(Event::B) => { <span class="comment">/* .. */ </span>},
|
||
<span class="prelude-val">None </span>=> { <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">&mut </span>Producer<<span class="lifetime">'static</span>, Event, <span class="number">4</span>>) {
|
||
|
||
<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<u8>::dequeue</code></td><td>15</td></tr>
|
||
<tr><td><code>Queue<u8>::dequeue</code></td><td>12</td></tr>
|
||
<tr><td><code>Producer<u8>::enqueue</code></td><td>16</td></tr>
|
||
<tr><td><code>Queue<u8>::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::<T>()</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> |