Sophie

Sophie

distrib > Mageia > 6 > armv7hl > by-pkgid > 4e2dbb669434a7691662cb2f0ad38972 > files > 14782

rust-doc-1.28.0-1.mga6.armv7hl.rpm

<!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 `compiler_fence` fn in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, compiler_fence"><title>std::sync::atomic::compiler_fence - 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><link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"></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><a href='../../../std/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a><div class="sidebar-elems"><p class='location'><a href='../../index.html'>std</a>::<wbr><a href='../index.html'>sync</a>::<wbr><a href='index.html'>atomic</a></p><script>window.sidebarCurrent = {name: 'compiler_fence', ty: 'fn', 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"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><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='in-band'>Function <a href='../../index.html'>std</a>::<wbr><a href='../index.html'>sync</a>::<wbr><a href='index.html'>atomic</a>::<wbr><a class="fn" href=''>compiler_fence</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.21.0'>1.21.0</span><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/core/sync/atomic.rs.html#2073-2084' title='goto source code'>[src]</a></span></h1><pre class='rust fn'>pub fn compiler_fence(order: <a class="enum" href="../../../std/sync/atomic/enum.Ordering.html" title="enum std::sync::atomic::Ordering">Ordering</a>)</pre><div class='docblock'><p>A compiler memory fence.</p>
<p><code>compiler_fence</code> does not emit any machine code, but restricts the kinds
of memory re-ordering the compiler is allowed to do. Specifically, depending on
the given <a href="enum.Ordering.html"><code>Ordering</code></a> semantics, the compiler may be disallowed from moving reads
or writes from before or after the call to the other side of the call to
<code>compiler_fence</code>. Note that it does <strong>not</strong> prevent the <em>hardware</em>
from doing such re-ordering. This is not a problem in a single-threaded,
execution context, but when other threads may modify memory at the same
time, stronger synchronization primitives such as <a href="fn.fence.html"><code>fence</code></a> are required.</p>
<p>The re-ordering prevented by the different ordering semantics are:</p>
<ul>
<li>with <a href="enum.Ordering.html#variant.SeqCst"><code>SeqCst</code></a>, no re-ordering of reads and writes across this point is allowed.</li>
<li>with <a href="enum.Ordering.html#variant.Release"><code>Release</code></a>, preceding reads and writes cannot be moved past subsequent writes.</li>
<li>with <a href="enum.Ordering.html#variant.Acquire"><code>Acquire</code></a>, subsequent reads and writes cannot be moved ahead of preceding reads.</li>
<li>with <a href="enum.Ordering.html#variant.AcqRel"><code>AcqRel</code></a>, both of the above rules are enforced.</li>
</ul>
<p><code>compiler_fence</code> is generally only useful for preventing a thread from
racing <em>with itself</em>. That is, if a given thread is executing one piece
of code, and is then interrupted, and starts executing code elsewhere
(while still in the same thread, and conceptually still on the same
core). In traditional programs, this can only occur when a signal
handler is registered. In more low-level code, such situations can also
arise when handling interrupts, when implementing green threads with
pre-emption, etc. Curious readers are encouraged to read the Linux kernel's
discussion of <a href="https://www.kernel.org/doc/Documentation/memory-barriers.txt">memory barriers</a>.</p>
<h1 id="panics" class="section-header"><a href="#panics">Panics</a></h1>
<p>Panics if <code>order</code> is <a href="enum.Ordering.html#variant.Relaxed"><code>Relaxed</code></a>.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Without <code>compiler_fence</code>, the <code>assert_eq!</code> in following code
is <em>not</em> guaranteed to succeed, despite everything happening in a single thread.
To see why, remember that the compiler is free to swap the stores to
<code>IMPORTANT_VARIABLE</code> and <code>IS_READ</code> since they are both
<code>Ordering::Relaxed</code>. If it does, and the signal handler is invoked right
after <code>IS_READY</code> is updated, then the signal handler will see
<code>IS_READY=1</code>, but <code>IMPORTANT_VARIABLE=0</code>.
Using a <code>compiler_fence</code> remedies this situation.</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">atomic</span>::{<span class="ident">AtomicBool</span>, <span class="ident">AtomicUsize</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">atomic</span>::{<span class="ident">ATOMIC_BOOL_INIT</span>, <span class="ident">ATOMIC_USIZE_INIT</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="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">atomic</span>::<span class="ident">compiler_fence</span>;

<span class="kw">static</span> <span class="ident">IMPORTANT_VARIABLE</span>: <span class="ident">AtomicUsize</span> <span class="op">=</span> <span class="ident">ATOMIC_USIZE_INIT</span>;
<span class="kw">static</span> <span class="ident">IS_READY</span>: <span class="ident">AtomicBool</span> <span class="op">=</span> <span class="ident">ATOMIC_BOOL_INIT</span>;

<span class="kw">fn</span> <span class="ident">main</span>() {
    <span class="ident">IMPORTANT_VARIABLE</span>.<span class="ident">store</span>(<span class="number">42</span>, <span class="ident">Ordering</span>::<span class="ident">Relaxed</span>);
    <span class="comment">// prevent earlier writes from being moved beyond this point</span>
    <span class="ident">compiler_fence</span>(<span class="ident">Ordering</span>::<span class="ident">Release</span>);
    <span class="ident">IS_READY</span>.<span class="ident">store</span>(<span class="bool-val">true</span>, <span class="ident">Ordering</span>::<span class="ident">Relaxed</span>);
}

<span class="kw">fn</span> <span class="ident">signal_handler</span>() {
    <span class="kw">if</span> <span class="ident">IS_READY</span>.<span class="ident">load</span>(<span class="ident">Ordering</span>::<span class="ident">Relaxed</span>) {
        <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">IMPORTANT_VARIABLE</span>.<span class="ident">load</span>(<span class="ident">Ordering</span>::<span class="ident">Relaxed</span>), <span class="number">42</span>);
    }
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Ause%20std%3A%3Async%3A%3Aatomic%3A%3A%7BAtomicBool%2C%20AtomicUsize%7D%3B%0Ause%20std%3A%3Async%3A%3Aatomic%3A%3A%7BATOMIC_BOOL_INIT%2C%20ATOMIC_USIZE_INIT%7D%3B%0Ause%20std%3A%3Async%3A%3Aatomic%3A%3AOrdering%3B%0Ause%20std%3A%3Async%3A%3Aatomic%3A%3Acompiler_fence%3B%0A%0Astatic%20IMPORTANT_VARIABLE%3A%20AtomicUsize%20%3D%20ATOMIC_USIZE_INIT%3B%0Astatic%20IS_READY%3A%20AtomicBool%20%3D%20ATOMIC_BOOL_INIT%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20IMPORTANT_VARIABLE.store(42%2C%20Ordering%3A%3ARelaxed)%3B%0A%20%20%20%20%2F%2F%20prevent%20earlier%20writes%20from%20being%20moved%20beyond%20this%20point%0A%20%20%20%20compiler_fence(Ordering%3A%3ARelease)%3B%0A%20%20%20%20IS_READY.store(true%2C%20Ordering%3A%3ARelaxed)%3B%0A%7D%0A%0Afn%20signal_handler()%20%7B%0A%20%20%20%20if%20IS_READY.load(Ordering%3A%3ARelaxed)%20%7B%0A%20%20%20%20%20%20%20%20assert_eq!(IMPORTANT_VARIABLE.load(Ordering%3A%3ARelaxed)%2C%2042)%3B%0A%20%20%20%20%7D%0A%7D">Run</a></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><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 = "std";</script><script src="../../../aliases.js"></script><script src="../../../main.js"></script><script defer src="../../../search-index.js"></script></body></html>