mentat/crossbeam_epoch/fn.unprotected.html

197 lines
12 KiB
HTML
Raw Permalink Normal View History

2018-08-22 17:04:13 +00:00
<!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 `unprotected` fn in crate `crossbeam_epoch`.">
<meta name="keywords" content="rust, rustlang, rust-lang, unprotected">
<title>crossbeam_epoch::unprotected - 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 fn">
<!--[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'><a href='index.html'>crossbeam_epoch</a></p><script>window.sidebarCurrent = {name: 'unprotected', ty: 'fn', relpath: ''};</script><script defer src="sidebar-items.js"></script>
</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'>Function <a href='index.html'>crossbeam_epoch</a>::<wbr><a class="fn" href=''>unprotected</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_epoch/guard.rs.html#435-444' title='goto source code'>[src]</a></span></h1>
<pre class='rust fn'>pub unsafe fn unprotected() -&gt; &amp;'static <a class="struct" href="../crossbeam_epoch/struct.Guard.html" title="struct crossbeam_epoch::Guard">Guard</a></pre><div class='docblock'><p>Returns a reference to a dummy guard that allows unprotected access to <a href="struct.Atomic.html"><code>Atomic</code></a>s.</p>
<p>This guard should be used in special occasions only. Note that it doesn't actually keep any
thread pinned - it's just a fake guard that allows loading from <a href="struct.Atomic.html"><code>Atomic</code></a>s unsafely.</p>
<p>Note that calling <a href="struct.Guard.html#method.defer"><code>defer</code></a> with a dummy guard will not defer the function - it will just
execute the function immediately.</p>
<p>If necessary, it's possible to create more dummy guards by cloning: <code>unprotected().clone()</code>.</p>
<h1 id="safety" class="section-header"><a href="#safety">Safety</a></h1>
<p>Loading and dereferencing data from an <a href="struct.Atomic.html"><code>Atomic</code></a> using this guard is safe only if the
<a href="struct.Atomic.html"><code>Atomic</code></a> is not being concurrently modified by other threads.</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_epoch</span>::{<span class="self">self</span> <span class="kw">as</span> <span class="ident">epoch</span>, <span class="ident">Atomic</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">atomic</span>::<span class="ident">Ordering</span>::<span class="ident">Relaxed</span>;
<span class="kw">let</span> <span class="ident">a</span> <span class="op">=</span> <span class="ident">Atomic</span>::<span class="ident">new</span>(<span class="number">7</span>);
<span class="kw">unsafe</span> {
<span class="comment">// Load `a` without pinning the current thread.</span>
<span class="ident">a</span>.<span class="ident">load</span>(<span class="ident">Relaxed</span>, <span class="ident">epoch</span>::<span class="ident">unprotected</span>());
<span class="comment">// It&#39;s possible to create more dummy guards by calling `clone()`.</span>
<span class="kw">let</span> <span class="ident">dummy</span> <span class="op">=</span> <span class="kw-2">&amp;</span><span class="ident">epoch</span>::<span class="ident">unprotected</span>().<span class="ident">clone</span>();
<span class="ident">dummy</span>.<span class="ident">defer</span>(<span class="kw">move</span> <span class="op">||</span> {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;This gets executed immediately.&quot;</span>);
});
<span class="comment">// Dropping `dummy` doesn&#39;t affect the current thread - it&#39;s just a noop.</span>
}</pre>
<p>The most common use of this function is when constructing or destructing a data structure.</p>
<p>For example, we can use a dummy guard in the destructor of a Treiber stack because at that
point no other thread could concurrently modify the <a href="struct.Atomic.html"><code>Atomic</code></a>s we are accessing.</p>
<p>If we were to actually pin the current thread during destruction, that would just unnecessarily
delay garbage collection and incur some performance cost, so in cases like these <code>unprotected</code>
is very helpful.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">crossbeam_epoch</span>::{<span class="self">self</span> <span class="kw">as</span> <span class="ident">epoch</span>, <span class="ident">Atomic</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">mem</span>::<span class="ident">ManuallyDrop</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">atomic</span>::<span class="ident">Ordering</span>::<span class="ident">Relaxed</span>;
<span class="kw">struct</span> <span class="ident">Stack</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="ident">head</span>: <span class="ident">Atomic</span><span class="op">&lt;</span><span class="ident">Node</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;&gt;</span>,
}
<span class="kw">struct</span> <span class="ident">Node</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="ident">data</span>: <span class="ident">ManuallyDrop</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>,
<span class="ident">next</span>: <span class="ident">Atomic</span><span class="op">&lt;</span><span class="ident">Node</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;&gt;</span>,
}
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">Stack</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) {
<span class="kw">unsafe</span> {
<span class="comment">// Unprotected load.</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">node</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">head</span>.<span class="ident">load</span>(<span class="ident">Relaxed</span>, <span class="ident">epoch</span>::<span class="ident">unprotected</span>());
<span class="kw">while</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">n</span>) <span class="op">=</span> <span class="ident">node</span>.<span class="ident">as_ref</span>() {
<span class="comment">// Unprotected load.</span>
<span class="kw">let</span> <span class="ident">next</span> <span class="op">=</span> <span class="ident">n</span>.<span class="ident">next</span>.<span class="ident">load</span>(<span class="ident">Relaxed</span>, <span class="ident">epoch</span>::<span class="ident">unprotected</span>());
<span class="comment">// Take ownership of the node, then drop its data and deallocate it.</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">o</span> <span class="op">=</span> <span class="ident">node</span>.<span class="ident">into_owned</span>();
<span class="ident">ManuallyDrop</span>::<span class="ident">drop</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">o</span>.<span class="ident">data</span>);
<span class="ident">drop</span>(<span class="ident">o</span>);
<span class="ident">node</span> <span class="op">=</span> <span class="ident">next</span>;
}
}
}
}</pre>
</div></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_epoch";
</script>
<script src="../main.js"></script>
<script defer src="../search-index.js"></script>
</body>
</html>