You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Trac3r-rust/doc/crossbeam/deque/index.html

72 lines
11 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!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="API documentation for the Rust `deque` mod in crate `crossbeam`."><meta name="keywords" content="rust, rustlang, rust-lang, deque"><title>crossbeam::deque - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="shortcut icon" href="../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><a href='../../crossbeam/index.html'><div class='logo-container'><img src='../../rust-logo.png' alt='logo'></div></a><p class='location'>Module deque</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li></ul></div><p class='location'><a href='../index.html'>crossbeam</a></p><script>window.sidebarCurrent = {name: 'deque', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>&#x2212;</span>]</a></span><a class='srclink' href='../../src/crossbeam_deque/lib.rs.html#1-2008' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../index.html'>crossbeam</a>::<wbr><a class="mod" href=''>deque</a></span></h1><div class='docblock'><p>Concurrent work-stealing deques.</p>
<p>These data structures are most commonly used in work-stealing schedulers. The typical setup
involves a number of threads, each having its own FIFO or LIFO queue (<em>worker</em>). There is also
one global FIFO queue (<em>injector</em>) and a list of references to <em>worker</em> queues that are able to
steal tasks (<em>stealers</em>).</p>
<p>We spawn a new task onto the scheduler by pushing it into the <em>injector</em> queue. Each worker
thread waits in a loop until it finds the next task to run and then runs it. To find a task, it
first looks into its local <em>worker</em> queue, and then into the <em>injector</em> and <em>stealers</em>.</p>
<h1 id="queues" class="section-header"><a href="#queues">Queues</a></h1>
<p><a href="struct.Stealer.html"><code>Injector</code></a> is a FIFO queue, where tasks are pushed and stolen from opposite ends. It is
shared among threads and is usually the entry point for new tasks.</p>
<p><a href="struct.Worker.html"><code>Worker</code></a> has two constructors:</p>
<ul>
<li><a href="struct.Worker.html#method.new_fifo"><code>new_fifo()</code></a> - Creates a FIFO queue, in which tasks are pushed and popped from opposite
ends.</li>
<li><a href="struct.Worker.html#method.new_lifo"><code>new_lifo()</code></a> - Creates a LIFO queue, in which tasks are pushed and popped from the same
end.</li>
</ul>
<p>Each <a href="struct.Worker.html"><code>Worker</code></a> is owned by a single thread and supports only push and pop operations.</p>
<p>Method <a href="struct.Worker.html#method.stealer"><code>stealer()</code></a> creates a <a href="struct.Stealer.html"><code>Stealer</code></a> that may be shared among threads and can only steal
tasks from its <a href="struct.Worker.html"><code>Worker</code></a>. Tasks are stolen from the end opposite to where they get pushed.</p>
<h1 id="stealing" class="section-header"><a href="#stealing">Stealing</a></h1>
<p>Steal operations come in three flavors:</p>
<ol>
<li><a href="struct.Stealer.html#method.steal"><code>steal()</code></a> - Steals one task.</li>
<li><a href="struct.Stealer.html#method.steal_batch"><code>steal_batch()</code></a> - Steals a batch of tasks and moves them into another worker.</li>
<li><a href="struct.Stealer.html#method.steal_batch_and_pop"><code>steal_batch_and_pop()</code></a> - Steals a batch of tasks, moves them into another queue, and pops
one task from that worker.</li>
</ol>
<p>In contrast to push and pop operations, stealing can spuriously fail with <a href="enum.Steal.html#variant.Retry"><code>Steal::Retry</code></a>, in
which case the steal operation needs to be retried.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Suppose a thread in a work-stealing scheduler is idle and looking for the next task to run. To
find an available task, it might do the following:</p>
<ol>
<li>Try popping one task from the local worker queue.</li>
<li>Try stealing a batch of tasks from the global injector queue.</li>
<li>Try stealing one task from another thread using the stealer list.</li>
</ol>
<p>An implementation of this work-stealing strategy:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">crossbeam_deque</span>::{<span class="ident">Injector</span>, <span class="ident">Steal</span>, <span class="ident">Stealer</span>, <span class="ident">Worker</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">iter</span>;
<span class="kw">fn</span> <span class="ident">find_task</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>(
<span class="ident">local</span>: <span class="kw-2">&amp;</span><span class="ident">Worker</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>,
<span class="ident">global</span>: <span class="kw-2">&amp;</span><span class="ident">Injector</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>,
<span class="ident">stealers</span>: <span class="kw-2">&amp;</span>[<span class="ident">Stealer</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>],
) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="comment">// Pop a task from the local queue, if not empty.</span>
<span class="ident">local</span>.<span class="ident">pop</span>().<span class="ident">or_else</span>(<span class="op">|</span><span class="op">|</span> {
<span class="comment">// Otherwise, we need to look for a task elsewhere.</span>
<span class="ident">iter</span>::<span class="ident">repeat_with</span>(<span class="op">|</span><span class="op">|</span> {
<span class="comment">// Try stealing a batch of tasks from the global queue.</span>
<span class="ident">global</span>.<span class="ident">steal_batch_and_pop</span>(<span class="ident">local</span>)
<span class="comment">// Or try stealing a task from one of the other threads.</span>
.<span class="ident">or_else</span>(<span class="op">|</span><span class="op">|</span> <span class="ident">stealers</span>.<span class="ident">iter</span>().<span class="ident">map</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> <span class="ident">s</span>.<span class="ident">steal</span>()).<span class="ident">collect</span>())
})
<span class="comment">// Loop while no task was stolen and any steal operation needs to be retried.</span>
.<span class="ident">find</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> <span class="op">!</span><span class="ident">s</span>.<span class="ident">is_retry</span>())
<span class="comment">// Extract the stolen task, if there is one.</span>
.<span class="ident">and_then</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> <span class="ident">s</span>.<span class="ident">success</span>())
})
}</pre></div>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table><tr class='module-item'><td><a class="struct" href="struct.Injector.html" title='crossbeam::deque::Injector struct'>Injector</a></td><td class='docblock-short'><p>An injector queue.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Stealer.html" title='crossbeam::deque::Stealer struct'>Stealer</a></td><td class='docblock-short'><p>A stealer handle of a worker queue.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Worker.html" title='crossbeam::deque::Worker struct'>Worker</a></td><td class='docblock-short'><p>A worker queue.</p>
</td></tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
<table><tr class='module-item'><td><a class="enum" href="enum.Steal.html" title='crossbeam::deque::Steal enum'>Steal</a></td><td class='docblock-short'><p>Possible outcomes of a steal operation.</p>
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd></kbd></dt><dd>Move up in search results</dd><dt><kbd></kbd></dt><dd>Move down in search results</dd><dt><kbd></kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "crossbeam";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>