68 lines
7.7 KiB
HTML
68 lines
7.7 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="Returns a reference to a dummy guard that allows unprotected access to `Atomic`s."><title>unprotected in crossbeam_epoch - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="crossbeam_epoch" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc fn"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">unprotected</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../crossbeam_epoch/index.html">crossbeam_<wbr>epoch</a><span class="version">0.9.18</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">unprotected</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#safety" title="Safety">Safety</a></li><li><a href="#examples" title="Examples">Examples</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate crossbeam_<wbr>epoch</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="index.html">crossbeam_epoch</a></div><h1>Function <span class="fn">unprotected</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/crossbeam_epoch/guard.rs.html#513-523">Source</a> </span></div><pre class="rust item-decl"><code>pub unsafe fn unprotected() -> &'static <a class="struct" href="struct.Guard.html" title="struct crossbeam_epoch::Guard">Guard</a></code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Returns a reference to a dummy guard that allows unprotected access to <a href="struct.Atomic.html" title="struct crossbeam_epoch::Atomic"><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" title="struct crossbeam_epoch::Atomic"><code>Atomic</code></a>s unsafely.</p>
|
||
<p>Note that calling <a href="struct.Guard.html#method.defer" title="method crossbeam_epoch::Guard::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>
|
||
<h2 id="safety"><a class="doc-anchor" href="#safety">§</a>Safety</h2>
|
||
<p>Loading and dereferencing data from an <a href="struct.Atomic.html" title="struct crossbeam_epoch::Atomic"><code>Atomic</code></a> using this guard is safe only if the
|
||
<a href="struct.Atomic.html" title="struct crossbeam_epoch::Atomic"><code>Atomic</code></a> is not being concurrently modified by other threads.</p>
|
||
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>crossbeam_epoch::{<span class="self">self </span><span class="kw">as </span>epoch, Atomic};
|
||
<span class="kw">use </span>std::sync::atomic::Ordering::Relaxed;
|
||
|
||
<span class="kw">let </span>a = Atomic::new(<span class="number">7</span>);
|
||
|
||
<span class="kw">unsafe </span>{
|
||
<span class="comment">// Load `a` without pinning the current thread.
|
||
</span>a.load(Relaxed, epoch::unprotected());
|
||
|
||
<span class="comment">// It's possible to create more dummy guards.
|
||
</span><span class="kw">let </span>dummy = epoch::unprotected();
|
||
|
||
dummy.defer(<span class="kw">move </span>|| {
|
||
<span class="macro">println!</span>(<span class="string">"This gets executed immediately."</span>);
|
||
});
|
||
|
||
<span class="comment">// Dropping `dummy` doesn't affect the current thread - it's just a noop.
|
||
</span>}</code></pre></div>
|
||
<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" title="struct crossbeam_epoch::Atomic"><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>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>crossbeam_epoch::{<span class="self">self </span><span class="kw">as </span>epoch, Atomic};
|
||
<span class="kw">use </span>std::mem::ManuallyDrop;
|
||
<span class="kw">use </span>std::sync::atomic::Ordering::Relaxed;
|
||
|
||
<span class="kw">struct </span>Stack<T> {
|
||
head: Atomic<Node<T>>,
|
||
}
|
||
|
||
<span class="kw">struct </span>Node<T> {
|
||
data: ManuallyDrop<T>,
|
||
next: Atomic<Node<T>>,
|
||
}
|
||
|
||
<span class="kw">impl</span><T> Drop <span class="kw">for </span>Stack<T> {
|
||
<span class="kw">fn </span>drop(<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>node = <span class="self">self</span>.head.load(Relaxed, epoch::unprotected());
|
||
|
||
<span class="kw">while let </span><span class="prelude-val">Some</span>(n) = node.as_ref() {
|
||
<span class="comment">// Unprotected load.
|
||
</span><span class="kw">let </span>next = n.next.load(Relaxed, epoch::unprotected());
|
||
|
||
<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>o = node.into_owned();
|
||
ManuallyDrop::drop(<span class="kw-2">&mut </span>o.data);
|
||
drop(o);
|
||
|
||
node = next;
|
||
}
|
||
}
|
||
}
|
||
}</code></pre></div></div></details></section></div></main></body></html> |