blob: 96d464a2624bff7b0e662c218b5f2e3343750a84 [file] [log] [blame]
<!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="Concurrent work-stealing deques."><meta name="keywords" content="rust, rustlang, rust-lang, crossbeam_deque"><title>crossbeam_deque - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[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">&#9776;</button><a class="sidebar-logo" href="../crossbeam_deque/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../crossbeam_deque/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate crossbeam_deque</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.8.3</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Crate <a class="mod" href="#">crossbeam_deque</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/crossbeam_deque/lib.rs.html#1-110">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><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>
<h2 id="queues"><a href="#queues">Queues</a></h2>
<p><a href="struct.Injector.html" title="Injector"><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" title="Worker"><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" title="Worker"><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" title="Stealer"><code>Stealer</code></a> that may be shared among threads and can only steal
tasks from its <a href="struct.Worker.html" title="Worker"><code>Worker</code></a>. Tasks are stolen from the end opposite to where they get pushed.</p>
<h2 id="stealing"><a href="#stealing">Stealing</a></h2>
<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" title="Steal::Retry"><code>Steal::Retry</code></a>, in
which case the steal operation needs to be retried.</p>
<h2 id="examples"><a href="#examples">Examples</a></h2>
<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"><code><span class="kw">use </span>crossbeam_deque::{Injector, Stealer, Worker};
<span class="kw">use </span>std::iter;
<span class="kw">fn </span>find_task&lt;T&gt;(
local: <span class="kw-2">&amp;</span>Worker&lt;T&gt;,
global: <span class="kw-2">&amp;</span>Injector&lt;T&gt;,
stealers: <span class="kw-2">&amp;</span>[Stealer&lt;T&gt;],
) -&gt; <span class="prelude-ty">Option</span>&lt;T&gt; {
<span class="comment">// Pop a task from the local queue, if not empty.
</span>local.pop().or_else(|| {
<span class="comment">// Otherwise, we need to look for a task elsewhere.
</span>iter::repeat_with(|| {
<span class="comment">// Try stealing a batch of tasks from the global queue.
</span>global.steal_batch_and_pop(local)
<span class="comment">// Or try stealing a task from one of the other threads.
</span>.or_else(|| stealers.iter().map(|s| s.steal()).collect())
})
<span class="comment">// Loop while no task was stolen and any steal operation needs to be retried.
</span>.find(|s| !s.is_retry())
<span class="comment">// Extract the stolen task, if there is one.
</span>.and_then(|s| s.success())
})
}</code></pre></div>
</div></details><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Injector.html" title="crossbeam_deque::Injector struct">Injector</a></div><div class="item-right docblock-short">An injector queue.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Stealer.html" title="crossbeam_deque::Stealer struct">Stealer</a></div><div class="item-right docblock-short">A stealer handle of a worker queue.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Worker.html" title="crossbeam_deque::Worker struct">Worker</a></div><div class="item-right docblock-short">A worker queue.</div></div></div><h2 id="enums" class="small-section-header"><a href="#enums">Enums</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.Steal.html" title="crossbeam_deque::Steal enum">Steal</a></div><div class="item-right docblock-short">Possible outcomes of a steal operation.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="crossbeam_deque" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>