mentat/crossbeam_deque/index.html
2018-08-22 17:04:13 +00:00

211 lines
No EOL
11 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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 `crossbeam_deque` crate.">
<meta name="keywords" content="rust, rustlang, rust-lang, crossbeam_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="../main.css" id="themeStyle">
<script src="../storage.js"></script>
</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>
<p class='location'>Crate crossbeam_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'></p><script>window.sidebarCurrent = {name: 'crossbeam_deque', ty: 'mod', relpath: '../'};</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">
<input class="search-input" name="search"
autocomplete="off"
placeholder="Click or press S to search, ? for more options…"
type="search">
</div>
</form>
</nav>
<section id='main' class="content">
<h1 class='fqn'><span class='in-band'>Crate <a class="mod" href=''>crossbeam_deque</a></span><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-1136' title='goto source code'>[src]</a></span></h1>
<div class='docblock'><p>A concurrent work-stealing deque.</p>
<p>The data structure can be thought of as a dynamically growable and shrinkable buffer that has
two ends: bottom and top. A <a href="struct.Deque.html"><code>Deque</code></a> can <a href="struct.Deque.html#method.push"><code>push</code></a> elements into the bottom and <a href="struct.Deque.html#method.pop"><code>pop</code></a>
elements from the bottom, but it can only <a href="struct.Deque.html#method.steal"><code>steal</code></a> elements from the top.</p>
<p>A <a href="struct.Deque.html"><code>Deque</code></a> doesn't implement <code>Sync</code> so it cannot be shared among multiple threads. However, it
can create <a href="struct.Stealer.html"><code>Stealer</code></a>s, and those can be easily cloned, shared, and sent to other threads.
<a href="struct.Stealer.html"><code>Stealer</code></a>s can only <a href="struct.Stealer.html#method.steal"><code>steal</code></a> elements from the top.</p>
<p>Here's a visualization of the data structure:</p>
<pre><code class="language-text"> top
_
Deque::steal -&gt; | | &lt;- Stealer::steal
| |
| |
| |
Deque::push/pop -&gt; |_|
bottom
</code></pre>
<h1 id="work-stealing-schedulers" class="section-header"><a href="#work-stealing-schedulers">Work-stealing schedulers</a></h1>
<p>Usually, the data structure is used in work-stealing schedulers as follows.</p>
<p>There is a number of threads. Each thread owns a <a href="struct.Deque.html"><code>Deque</code></a> and creates a <a href="struct.Stealer.html"><code>Stealer</code></a> that is
shared among all other threads. Alternatively, it creates multiple <a href="struct.Stealer.html"><code>Stealer</code></a>s - one for each
of the other threads.</p>
<p>Then, all threads are executing in a loop. In the loop, each one attempts to <a href="struct.Deque.html#method.pop"><code>pop</code></a> some work
from its own <a href="struct.Deque.html"><code>Deque</code></a>. But if it is empty, it attempts to <a href="struct.Stealer.html#method.steal"><code>steal</code></a> work from
some other thread instead. When executing work (or being idle), a thread may produce more work,
which gets <a href="struct.Deque.html#method.push"><code>push</code></a>ed into its <a href="struct.Deque.html"><code>Deque</code></a>.</p>
<p>Of course, there are many variations of this strategy. For example, sometimes it may be
beneficial for a thread to always <a href="struct.Deque.html#method.steal"><code>steal</code></a> work from the top of its deque
instead of calling <a href="struct.Deque.html#method.pop"><code>pop</code></a> and taking it from the bottom.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">crossbeam_deque</span>::{<span class="ident">Deque</span>, <span class="ident">Steal</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
<span class="kw">let</span> <span class="ident">d</span> <span class="op">=</span> <span class="ident">Deque</span>::<span class="ident">new</span>();
<span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">d</span>.<span class="ident">stealer</span>();
<span class="ident">d</span>.<span class="ident">push</span>(<span class="string">&#39;a&#39;</span>);
<span class="ident">d</span>.<span class="ident">push</span>(<span class="string">&#39;b&#39;</span>);
<span class="ident">d</span>.<span class="ident">push</span>(<span class="string">&#39;c&#39;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">d</span>.<span class="ident">pop</span>(), <span class="prelude-val">Some</span>(<span class="string">&#39;c&#39;</span>));
<span class="ident">drop</span>(<span class="ident">d</span>);
<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">||</span> {
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">steal</span>(), <span class="ident">Steal</span>::<span class="ident">Data</span>(<span class="string">&#39;a&#39;</span>));
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">steal</span>(), <span class="ident">Steal</span>::<span class="ident">Data</span>(<span class="string">&#39;b&#39;</span>));
}).<span class="ident">join</span>().<span class="ident">unwrap</span>();</pre>
<h1 id="references" class="section-header"><a href="#references">References</a></h1>
<p>The implementation is based on the following work:</p>
<ol>
<li><a href="https://dl.acm.org/citation.cfm?id=1073974">Chase and Lev. Dynamic circular work-stealing deque. SPAA 2005.</a></li>
<li><a href="https://dl.acm.org/citation.cfm?id=2442524">Le, Pop, Cohen, and Nardelli. Correct and efficient work-stealing for weak memory models.
PPoPP 2013.</a></li>
<li><a href="https://dl.acm.org/citation.cfm?id=2509514">Norris and Demsky. CDSchecker: checking concurrent data structures written with C/C++
atomics. OOPSLA 2013.</a></li>
</ol>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
<tr class=' module-item'>
<td><a class="struct" href="struct.Deque.html"
title='struct crossbeam_deque::Deque'>Deque</a></td>
<td class='docblock-short'>
<p>A concurrent work-stealing deque.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Stealer.html"
title='struct crossbeam_deque::Stealer'>Stealer</a></td>
<td class='docblock-short'>
<p>A stealer that steals elements from the top of a deque.</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='enum crossbeam_deque::Steal'>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>
</div>
</div>
</aside>
<script>
window.rootPath = "../";
window.currentCrate = "crossbeam_deque";
</script>
<script src="../main.js"></script>
<script defer src="../search-index.js"></script>
</body>
</html>