Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > 0c2243f8a1696816431e7210e991fa52 > files > 12445

rust-doc-1.35.0-1.mga7.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 `atomic` mod in crate `core`."><meta name="keywords" content="rust, rustlang, rust-lang, atomic"><title>core::sync::atomic - Rust</title><link rel="stylesheet" type="text/css" href="../../../normalize1.35.0.css"><link rel="stylesheet" type="text/css" href="../../../rustdoc1.35.0.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../../dark1.35.0.css"><link rel="stylesheet" type="text/css" href="../../../light1.35.0.css" id="themeStyle"><script src="../../../storage1.35.0.js"></script><noscript><link rel="stylesheet" href="../../../noscript1.35.0.css"></noscript><link rel="shortcut icon" href="../../../favicon1.35.0.ico"><style type="text/css">#crate-search{background-image:url("../../../down-arrow1.35.0.svg");}</style></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><a href='../../../core/index.html'><img src='../../../rust-logo1.35.0.png' alt='logo' width='100'></a><p class='location'>Module atomic</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#constants">Constants</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../../index.html'>core</a>::<wbr><a href='../index.html'>sync</a></p><script>window.sidebarCurrent = {name: 'atomic', ty: 'mod', 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="../../../brush1.35.0.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../../theme1.35.0.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../../settings.html"><img src="../../../wheel1.35.0.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.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#1-2528' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../../index.html'>core</a>::<wbr><a href='../index.html'>sync</a>::<wbr><a class="mod" href=''>atomic</a></span></h1><div class='docblock'><p>Atomic types</p>
<p>Atomic types provide primitive shared-memory communication between
threads, and are the building blocks of other concurrent
types.</p>
<p>This module defines atomic versions of a select number of primitive
types, including <a href="struct.AtomicBool.html"><code>AtomicBool</code></a>, <a href="struct.AtomicIsize.html"><code>AtomicIsize</code></a>, <a href="struct.AtomicUsize.html"><code>AtomicUsize</code></a>,
<a href="struct.AtomicI8.html"><code>AtomicI8</code></a>, <a href="struct.AtomicU16.html"><code>AtomicU16</code></a>, etc.
Atomic types present operations that, when used correctly, synchronize
updates between threads.</p>
<p>Each method takes an <a href="enum.Ordering.html"><code>Ordering</code></a> which represents the strength of
the memory barrier for that operation. These orderings are the
same as <a href="https://llvm.org/docs/LangRef.html#memory-model-for-concurrent-operations">LLVM atomic orderings</a>. For more information see the <a href="../../../nomicon/atomics.html">nomicon</a>.</p>
<p>Atomic variables are safe to share between threads (they implement <a href="../../marker/trait.Sync.html"><code>Sync</code></a>)
but they do not themselves provide the mechanism for sharing and follow the
<a href="../../../std/thread/index.html#the-threading-model">threading model</a> of rust.
The most common way to share an atomic variable is to put it into an <a href="../../../std/sync/struct.Arc.html"><code>Arc</code></a> (an
atomically-reference-counted shared pointer).</p>
<p>Atomic types may be stored in static variables, initialized using
the constant initializers like <a href="struct.AtomicBool.html#method.new"><code>AtomicBool::new</code></a>. Atomic statics
are often used for lazy global initialization.</p>
<h1 id="portability" class="section-header"><a href="#portability">Portability</a></h1>
<p>All atomic types in this module are guaranteed to be <a href="https://en.wikipedia.org/wiki/Non-blocking_algorithm">lock-free</a> if they're
available. This means they don't internally acquire a global mutex. Atomic
types and operations are not guaranteed to be wait-free. This means that
operations like <code>fetch_or</code> may be implemented with a compare-and-swap loop.</p>
<p>Atomic operations may be implemented at the instruction layer with
larger-size atomics. For example some platforms use 4-byte atomic
instructions to implement <code>AtomicI8</code>. Note that this emulation should not
have an impact on correctness of code, it's just something to be aware of.</p>
<p>The atomic types in this module may not be available on all platforms. The
atomic types here are all widely available, however, and can generally be
relied upon existing. Some notable exceptions are:</p>
<ul>
<li>PowerPC and MIPS platforms with 32-bit pointers do not have <code>AtomicU64</code> or
<code>AtomicI64</code> types.</li>
<li>ARM platforms like <code>armv5te</code> that aren't for Linux do not have any atomics
at all.</li>
<li>ARM targets with <code>thumbv6m</code> do not have atomic operations at all.</li>
</ul>
<p>Note that future platforms may be added that also do not have support for
some atomic operations. Maximally portable code will want to be careful
about which atomic types are used. <code>AtomicUsize</code> and <code>AtomicIsize</code> are
generally the most portable, but even then they're not available everywhere.
For reference, the <code>std</code> library requires pointer-sized atomics, although
<code>core</code> does not.</p>
<p>Currently you'll need to use <code>#[cfg(target_arch)]</code> primarily to
conditionally compile in code with atomics. There is an unstable
<code>#[cfg(target_has_atomic)]</code> as well which may be stabilized in the future.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>A simple spinlock:</p>

<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">Arc</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">atomic</span>::{<span class="ident">AtomicUsize</span>, <span class="ident">Ordering</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">fn</span> <span class="ident">main</span>() {
    <span class="kw">let</span> <span class="ident">spinlock</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>(<span class="ident">AtomicUsize</span>::<span class="ident">new</span>(<span class="number">1</span>));

    <span class="kw">let</span> <span class="ident">spinlock_clone</span> <span class="op">=</span> <span class="ident">spinlock</span>.<span class="ident">clone</span>();
    <span class="kw">let</span> <span class="ident">thread</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span><span class="op">||</span> {
        <span class="ident">spinlock_clone</span>.<span class="ident">store</span>(<span class="number">0</span>, <span class="ident">Ordering</span>::<span class="ident">SeqCst</span>);
    });

    <span class="comment">// Wait for the other thread to release the lock</span>
    <span class="kw">while</span> <span class="ident">spinlock</span>.<span class="ident">load</span>(<span class="ident">Ordering</span>::<span class="ident">SeqCst</span>) <span class="op">!=</span> <span class="number">0</span> {}

    <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Err</span>(<span class="ident">panic</span>) <span class="op">=</span> <span class="ident">thread</span>.<span class="ident">join</span>() {
        <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Thread had an error: {:?}&quot;</span>, <span class="ident">panic</span>);
    }
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Ause%20std%3A%3Async%3A%3AArc%3B%0Ause%20std%3A%3Async%3A%3Aatomic%3A%3A%7BAtomicUsize%2C%20Ordering%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20spinlock%20%3D%20Arc%3A%3Anew(AtomicUsize%3A%3Anew(1))%3B%0A%0A%20%20%20%20let%20spinlock_clone%20%3D%20spinlock.clone()%3B%0A%20%20%20%20let%20thread%20%3D%20thread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20%20%20%20%20spinlock_clone.store(0%2C%20Ordering%3A%3ASeqCst)%3B%0A%20%20%20%20%7D)%3B%0A%0A%20%20%20%20%2F%2F%20Wait%20for%20the%20other%20thread%20to%20release%20the%20lock%0A%20%20%20%20while%20spinlock.load(Ordering%3A%3ASeqCst)%20!%3D%200%20%7B%7D%0A%0A%20%20%20%20if%20let%20Err(panic)%20%3D%20thread.join()%20%7B%0A%20%20%20%20%20%20%20%20println!(%22Thread%20had%20an%20error%3A%20%7B%3A%3F%7D%22%2C%20panic)%3B%0A%20%20%20%20%7D%0A%7D">Run</a></pre></div>
<p>Keep a global count of live threads:</p>

<div class="example-wrap"><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">AtomicUsize</span>, <span class="ident">Ordering</span>};

<span class="kw">static</span> <span class="ident">GLOBAL_THREAD_COUNT</span>: <span class="ident">AtomicUsize</span> <span class="op">=</span> <span class="ident">AtomicUsize</span>::<span class="ident">new</span>(<span class="number">0</span>);

<span class="kw">let</span> <span class="ident">old_thread_count</span> <span class="op">=</span> <span class="ident">GLOBAL_THREAD_COUNT</span>.<span class="ident">fetch_add</span>(<span class="number">1</span>, <span class="ident">Ordering</span>::<span class="ident">SeqCst</span>);
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;live threads: {}&quot;</span>, <span class="ident">old_thread_count</span> <span class="op">+</span> <span class="number">1</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Async%3A%3Aatomic%3A%3A%7BAtomicUsize%2C%20Ordering%7D%3B%0A%0Astatic%20GLOBAL_THREAD_COUNT%3A%20AtomicUsize%20%3D%20AtomicUsize%3A%3Anew(0)%3B%0A%0Alet%20old_thread_count%20%3D%20GLOBAL_THREAD_COUNT.fetch_add(1%2C%20Ordering%3A%3ASeqCst)%3B%0Aprintln!(%22live%20threads%3A%20%7B%7D%22%2C%20old_thread_count%20%2B%201)%3B%0A%7D">Run</a></pre></div>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table><tr class='module-item'><td><a class="struct" href="struct.AtomicBool.html" title='core::sync::atomic::AtomicBool struct'>AtomicBool</a></td><td class='docblock-short'><p>A boolean type which can be safely shared between threads.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.AtomicI8.html" title='core::sync::atomic::AtomicI8 struct'>AtomicI8</a></td><td class='docblock-short'><p>An integer type which can be safely shared between threads.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.AtomicI16.html" title='core::sync::atomic::AtomicI16 struct'>AtomicI16</a></td><td class='docblock-short'><p>An integer type which can be safely shared between threads.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.AtomicI32.html" title='core::sync::atomic::AtomicI32 struct'>AtomicI32</a></td><td class='docblock-short'><p>An integer type which can be safely shared between threads.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.AtomicI64.html" title='core::sync::atomic::AtomicI64 struct'>AtomicI64</a></td><td class='docblock-short'><p>An integer type which can be safely shared between threads.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.AtomicIsize.html" title='core::sync::atomic::AtomicIsize struct'>AtomicIsize</a></td><td class='docblock-short'><p>An integer type which can be safely shared between threads.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.AtomicPtr.html" title='core::sync::atomic::AtomicPtr struct'>AtomicPtr</a></td><td class='docblock-short'><p>A raw pointer type which can be safely shared between threads.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.AtomicU8.html" title='core::sync::atomic::AtomicU8 struct'>AtomicU8</a></td><td class='docblock-short'><p>An integer type which can be safely shared between threads.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.AtomicU16.html" title='core::sync::atomic::AtomicU16 struct'>AtomicU16</a></td><td class='docblock-short'><p>An integer type which can be safely shared between threads.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.AtomicU32.html" title='core::sync::atomic::AtomicU32 struct'>AtomicU32</a></td><td class='docblock-short'><p>An integer type which can be safely shared between threads.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.AtomicU64.html" title='core::sync::atomic::AtomicU64 struct'>AtomicU64</a></td><td class='docblock-short'><p>An integer type which can be safely shared between threads.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.AtomicUsize.html" title='core::sync::atomic::AtomicUsize struct'>AtomicUsize</a></td><td class='docblock-short'><p>An integer type which can be safely shared between threads.</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.Ordering.html" title='core::sync::atomic::Ordering enum'>Ordering</a></td><td class='docblock-short'><p>Atomic memory orderings</p>
</td></tr></table><h2 id='constants' class='section-header'><a href="#constants">Constants</a></h2>
<table><tr class='deprecated module-item'><td><a class="constant" href="constant.ATOMIC_BOOL_INIT.html" title='core::sync::atomic::ATOMIC_BOOL_INIT constant'>ATOMIC_BOOL_INIT</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>An <a href="struct.AtomicBool.html"><code>AtomicBool</code></a> initialized to <code>false</code>.</p>
</td></tr><tr class='deprecated module-item'><td><a class="constant" href="constant.ATOMIC_ISIZE_INIT.html" title='core::sync::atomic::ATOMIC_ISIZE_INIT constant'>ATOMIC_ISIZE_INIT</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>An atomic integer initialized to <code>0</code>.</p>
</td></tr><tr class='deprecated module-item'><td><a class="constant" href="constant.ATOMIC_USIZE_INIT.html" title='core::sync::atomic::ATOMIC_USIZE_INIT constant'>ATOMIC_USIZE_INIT</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>An atomic integer initialized to <code>0</code>.</p>
</td></tr><tr class='unstable deprecated module-item'><td><a class="constant" href="constant.ATOMIC_I8_INIT.html" title='core::sync::atomic::ATOMIC_I8_INIT constant'>ATOMIC_I8_INIT</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><span class="stab unstable">Experimental</span><p>An atomic integer initialized to <code>0</code>.</p>
</td></tr><tr class='unstable deprecated module-item'><td><a class="constant" href="constant.ATOMIC_I16_INIT.html" title='core::sync::atomic::ATOMIC_I16_INIT constant'>ATOMIC_I16_INIT</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><span class="stab unstable">Experimental</span><p>An atomic integer initialized to <code>0</code>.</p>
</td></tr><tr class='unstable deprecated module-item'><td><a class="constant" href="constant.ATOMIC_I32_INIT.html" title='core::sync::atomic::ATOMIC_I32_INIT constant'>ATOMIC_I32_INIT</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><span class="stab unstable">Experimental</span><p>An atomic integer initialized to <code>0</code>.</p>
</td></tr><tr class='unstable deprecated module-item'><td><a class="constant" href="constant.ATOMIC_I64_INIT.html" title='core::sync::atomic::ATOMIC_I64_INIT constant'>ATOMIC_I64_INIT</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><span class="stab unstable">Experimental</span><p>An atomic integer initialized to <code>0</code>.</p>
</td></tr><tr class='unstable deprecated module-item'><td><a class="constant" href="constant.ATOMIC_U8_INIT.html" title='core::sync::atomic::ATOMIC_U8_INIT constant'>ATOMIC_U8_INIT</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><span class="stab unstable">Experimental</span><p>An atomic integer initialized to <code>0</code>.</p>
</td></tr><tr class='unstable deprecated module-item'><td><a class="constant" href="constant.ATOMIC_U16_INIT.html" title='core::sync::atomic::ATOMIC_U16_INIT constant'>ATOMIC_U16_INIT</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><span class="stab unstable">Experimental</span><p>An atomic integer initialized to <code>0</code>.</p>
</td></tr><tr class='unstable deprecated module-item'><td><a class="constant" href="constant.ATOMIC_U32_INIT.html" title='core::sync::atomic::ATOMIC_U32_INIT constant'>ATOMIC_U32_INIT</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><span class="stab unstable">Experimental</span><p>An atomic integer initialized to <code>0</code>.</p>
</td></tr><tr class='unstable deprecated module-item'><td><a class="constant" href="constant.ATOMIC_U64_INIT.html" title='core::sync::atomic::ATOMIC_U64_INIT constant'>ATOMIC_U64_INIT</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><span class="stab unstable">Experimental</span><p>An atomic integer initialized to <code>0</code>.</p>
</td></tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
<table><tr class='module-item'><td><a class="fn" href="fn.compiler_fence.html" title='core::sync::atomic::compiler_fence fn'>compiler_fence</a></td><td class='docblock-short'><p>A compiler memory fence.</p>
</td></tr><tr class='module-item'><td><a class="fn" href="fn.fence.html" title='core::sync::atomic::fence fn'>fence</a></td><td class='docblock-short'><p>An atomic fence.</p>
</td></tr><tr class='module-item'><td><a class="fn" href="fn.spin_loop_hint.html" title='core::sync::atomic::spin_loop_hint fn'>spin_loop_hint</a></td><td class='docblock-short'><p>Signals the processor that it is entering a busy-wait spin-loop.</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><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 = "core";</script><script src="../../../aliases.js"></script><script src="../../../main1.35.0.js"></script><script defer src="../../../search-index.js"></script></body></html>