mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-11-26 05:29:38 +01:00
572 lines
109 KiB
HTML
572 lines
109 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="An integer type which can be safely shared between threads."><title>AtomicU32 in portable_atomic - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-492a78a4a87dcc01.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="portable_atomic" data-themes="" data-resource-suffix="" data-rustdoc-version="1.82.0 (f6e511eec 2024-10-15)" data-channel="1.82.0" data-search-js="search-a99f1315e7cc5121.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-118b08c4c78b968e.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-921df33f47b8780c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-3b12f09e550e0385.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc struct"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../portable_atomic/index.html">portable_<wbr>atomic</a><span class="version">1.9.0</span></h2></div><h2 class="location"><a href="#">Atomic<wbr>U32</a></h2><div class="sidebar-elems"><section><h3><a href="#implementations">Methods</a></h3><ul class="block method"><li><a href="#method.add">add</a></li><li><a href="#method.and">and</a></li><li><a href="#method.as_ptr">as_ptr</a></li><li><a href="#method.bit_clear">bit_clear</a></li><li><a href="#method.bit_set">bit_set</a></li><li><a href="#method.bit_toggle">bit_toggle</a></li><li><a href="#method.compare_exchange">compare_exchange</a></li><li><a href="#method.compare_exchange_weak">compare_exchange_weak</a></li><li><a href="#method.fetch_add">fetch_add</a></li><li><a href="#method.fetch_and">fetch_and</a></li><li><a href="#method.fetch_max">fetch_max</a></li><li><a href="#method.fetch_min">fetch_min</a></li><li><a href="#method.fetch_nand">fetch_nand</a></li><li><a href="#method.fetch_neg">fetch_neg</a></li><li><a href="#method.fetch_not">fetch_not</a></li><li><a href="#method.fetch_or">fetch_or</a></li><li><a href="#method.fetch_sub">fetch_sub</a></li><li><a href="#method.fetch_update">fetch_update</a></li><li><a href="#method.fetch_xor">fetch_xor</a></li><li><a href="#method.from_ptr">from_ptr</a></li><li><a href="#method.get_mut">get_mut</a></li><li><a href="#method.into_inner">into_inner</a></li><li><a href="#method.is_always_lock_free">is_always_lock_free</a></li><li><a href="#method.is_lock_free">is_lock_free</a></li><li><a href="#method.load">load</a></li><li><a href="#method.neg">neg</a></li><li><a href="#method.new">new</a></li><li><a href="#method.not">not</a></li><li><a href="#method.or">or</a></li><li><a href="#method.store">store</a></li><li><a href="#method.sub">sub</a></li><li><a href="#method.swap">swap</a></li><li><a href="#method.xor">xor</a></li></ul><h3><a href="#trait-implementations">Trait Implementations</a></h3><ul class="block trait-implementation"><li><a href="#impl-Debug-for-AtomicU32">Debug</a></li><li><a href="#impl-Default-for-AtomicU
|
|||
|
<p>This type has the same in-memory representation as the underlying integer type,
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html" title="primitive u32"><code>u32</code></a>.</p>
|
|||
|
<p>If the compiler and the platform support atomic loads and stores of <a href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html" title="primitive u32"><code>u32</code></a>, this type is a wrapper for the standard library’s <code>AtomicU32</code>. If the platform supports it but the compiler does not, atomic operations are implemented using
|
|||
|
inline assembly. Otherwise synchronizes using global locks.
|
|||
|
You can call <a href="struct.AtomicU32.html#method.is_lock_free" title="associated function portable_atomic::AtomicU32::is_lock_free"><code>AtomicU32::is_lock_free()</code></a> to check whether
|
|||
|
atomic instructions or locks will be used.</p>
|
|||
|
</div></details><h2 id="implementations" class="section-header">Implementations<a href="#implementations" class="anchor">§</a></h2><div id="implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-AtomicU32" class="impl"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><a href="#impl-AtomicU32" class="anchor">§</a><h3 class="code-header">impl <a class="struct" href="struct.AtomicU32.html" title="struct portable_atomic::AtomicU32">AtomicU32</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.new" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub const fn <a href="#method.new" class="fn">new</a>(v: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>) -> Self</h4></section></summary><div class="docblock"><p>Creates a new atomic integer.</p>
|
|||
|
<h5 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::AtomicU32;
|
|||
|
|
|||
|
<span class="kw">let </span>atomic_forty_two = AtomicU32::new(<span class="number">42</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.from_ptr" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub unsafe fn <a href="#method.from_ptr" class="fn">from_ptr</a><'a>(ptr: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.pointer.html">*mut </a><a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>) -> &'a Self</h4></section></summary><div class="docblock"><p>Creates a new reference to an atomic integer from a pointer.</p>
|
|||
|
<h5 id="safety"><a class="doc-anchor" href="#safety">§</a>Safety</h5>
|
|||
|
<ul>
|
|||
|
<li><code>ptr</code> must be aligned to <code>align_of::<AtomicU32>()</code> (note that on some platforms this
|
|||
|
can be bigger than <code>align_of::<u32>()</code>).</li>
|
|||
|
<li><code>ptr</code> must be <a href="https://doc.rust-lang.org/1.82.0/core/ptr/index.html#safety" title="mod core::ptr">valid</a> for both reads and writes for the whole lifetime <code>'a</code>.</li>
|
|||
|
<li>If this atomic type is <a href="struct.AtomicU32.html#method.is_lock_free" title="associated function portable_atomic::AtomicU32::is_lock_free">lock-free</a>, non-atomic accesses to the value
|
|||
|
behind <code>ptr</code> must have a happens-before relationship with atomic accesses via
|
|||
|
the returned value (or vice-versa).
|
|||
|
<ul>
|
|||
|
<li>In other words, time periods where the value is accessed atomically may not
|
|||
|
overlap with periods where the value is accessed non-atomically.</li>
|
|||
|
<li>This requirement is trivially satisfied if <code>ptr</code> is never used non-atomically
|
|||
|
for the duration of lifetime <code>'a</code>. Most use cases should be able to follow
|
|||
|
this guideline.</li>
|
|||
|
<li>This requirement is also trivially satisfied if all accesses (atomic or not) are
|
|||
|
done from the same thread.</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li>If this atomic type is <em>not</em> lock-free:
|
|||
|
<ul>
|
|||
|
<li>Any accesses to the value behind <code>ptr</code> must have a happens-before relationship
|
|||
|
with accesses via the returned value (or vice-versa).</li>
|
|||
|
<li>Any concurrent accesses to the value behind <code>ptr</code> for the duration of lifetime <code>'a</code> must
|
|||
|
be compatible with operations performed by this atomic type.</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li>This method must not be used to create overlapping or mixed-size atomic
|
|||
|
accesses, as these are not supported by the memory model.</li>
|
|||
|
</ul>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_lock_free" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.is_lock_free" class="fn">is_lock_free</a>() -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Returns <code>true</code> if operations on values of this type are lock-free.</p>
|
|||
|
<p>If the compiler or the platform doesn’t support the necessary
|
|||
|
atomic instructions, global locks for every potentially
|
|||
|
concurrent atomic operation will be used.</p>
|
|||
|
<h5 id="examples-1"><a class="doc-anchor" href="#examples-1">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::AtomicU32;
|
|||
|
|
|||
|
<span class="kw">let </span>is_lock_free = AtomicU32::is_lock_free();</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_always_lock_free" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub const fn <a href="#method.is_always_lock_free" class="fn">is_always_lock_free</a>() -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Returns <code>true</code> if operations on values of this type are lock-free.</p>
|
|||
|
<p>If the compiler or the platform doesn’t support the necessary
|
|||
|
atomic instructions, global locks for every potentially
|
|||
|
concurrent atomic operation will be used.</p>
|
|||
|
<p><strong>Note:</strong> If the atomic operation relies on dynamic CPU feature detection,
|
|||
|
this type may be lock-free even if the function returns false.</p>
|
|||
|
<h5 id="examples-2"><a class="doc-anchor" href="#examples-2">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::AtomicU32;
|
|||
|
|
|||
|
<span class="kw">const </span>IS_ALWAYS_LOCK_FREE: bool = AtomicU32::is_always_lock_free();</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.get_mut" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.get_mut" class="fn">get_mut</a>(&mut self) -> &mut <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Returns a mutable reference to the underlying integer.</p>
|
|||
|
<p>This is safe because the mutable reference guarantees that no other threads are
|
|||
|
concurrently accessing the atomic data.</p>
|
|||
|
<h5 id="examples-3"><a class="doc-anchor" href="#examples-3">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span><span class="kw-2">mut </span>some_var = AtomicU32::new(<span class="number">10</span>);
|
|||
|
<span class="macro">assert_eq!</span>(<span class="kw-2">*</span>some_var.get_mut(), <span class="number">10</span>);
|
|||
|
<span class="kw-2">*</span>some_var.get_mut() = <span class="number">5</span>;
|
|||
|
<span class="macro">assert_eq!</span>(some_var.load(Ordering::SeqCst), <span class="number">5</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.into_inner" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub const fn <a href="#method.into_inner" class="fn">into_inner</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Consumes the atomic and returns the contained value.</p>
|
|||
|
<p>This is safe because passing <code>self</code> by value guarantees that no other threads are
|
|||
|
concurrently accessing the atomic data.</p>
|
|||
|
<p>This is <code>const fn</code> on Rust 1.56+.</p>
|
|||
|
<h5 id="examples-4"><a class="doc-anchor" href="#examples-4">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::AtomicU32;
|
|||
|
|
|||
|
<span class="kw">let </span>some_var = AtomicU32::new(<span class="number">5</span>);
|
|||
|
<span class="macro">assert_eq!</span>(some_var.into_inner(), <span class="number">5</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.load" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.load" class="fn">load</a>(&self, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Loads a value from the atomic integer.</p>
|
|||
|
<p><code>load</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering of this operation.
|
|||
|
Possible values are <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.SeqCst" title="variant core::sync::atomic::Ordering::SeqCst"><code>SeqCst</code></a>, <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> and <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="panics"><a class="doc-anchor" href="#panics">§</a>Panics</h5>
|
|||
|
<p>Panics if <code>order</code> is <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> or <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.AcqRel" title="variant core::sync::atomic::Ordering::AcqRel"><code>AcqRel</code></a>.</p>
|
|||
|
<h5 id="examples-5"><a class="doc-anchor" href="#examples-5">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>some_var = AtomicU32::new(<span class="number">5</span>);
|
|||
|
|
|||
|
<span class="macro">assert_eq!</span>(some_var.load(Ordering::Relaxed), <span class="number">5</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.store" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.store" class="fn">store</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>)</h4></section></summary><div class="docblock"><p>Stores a value into the atomic integer.</p>
|
|||
|
<p><code>store</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering of this operation.
|
|||
|
Possible values are <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.SeqCst" title="variant core::sync::atomic::Ordering::SeqCst"><code>SeqCst</code></a>, <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> and <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="panics-1"><a class="doc-anchor" href="#panics-1">§</a>Panics</h5>
|
|||
|
<p>Panics if <code>order</code> is <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> or <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.AcqRel" title="variant core::sync::atomic::Ordering::AcqRel"><code>AcqRel</code></a>.</p>
|
|||
|
<h5 id="examples-6"><a class="doc-anchor" href="#examples-6">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>some_var = AtomicU32::new(<span class="number">5</span>);
|
|||
|
|
|||
|
some_var.store(<span class="number">10</span>, Ordering::Relaxed);
|
|||
|
<span class="macro">assert_eq!</span>(some_var.load(Ordering::Relaxed), <span class="number">10</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.swap" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.swap" class="fn">swap</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Stores a value into the atomic integer, returning the previous value.</p>
|
|||
|
<p><code>swap</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="examples-7"><a class="doc-anchor" href="#examples-7">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>some_var = AtomicU32::new(<span class="number">5</span>);
|
|||
|
|
|||
|
<span class="macro">assert_eq!</span>(some_var.swap(<span class="number">10</span>, Ordering::Relaxed), <span class="number">5</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.compare_exchange" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.compare_exchange" class="fn">compare_exchange</a>(
|
|||
|
&self,
|
|||
|
current: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>,
|
|||
|
new: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>,
|
|||
|
success: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>,
|
|||
|
failure: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>,
|
|||
|
) -> <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>></h4></section></summary><div class="docblock"><p>Stores a value into the atomic integer if the current value is the same as
|
|||
|
the <code>current</code> value.</p>
|
|||
|
<p>The return value is a result indicating whether the new value was written and
|
|||
|
containing the previous value. On success this value is guaranteed to be equal to
|
|||
|
<code>current</code>.</p>
|
|||
|
<p><code>compare_exchange</code> takes two <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> arguments to describe the memory
|
|||
|
ordering of this operation. <code>success</code> describes the required ordering for the
|
|||
|
read-modify-write operation that takes place if the comparison with <code>current</code> succeeds.
|
|||
|
<code>failure</code> describes the required ordering for the load operation that takes place when
|
|||
|
the comparison fails. Using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> as success ordering makes the store part
|
|||
|
of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the successful load
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>. The failure ordering can only be <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.SeqCst" title="variant core::sync::atomic::Ordering::SeqCst"><code>SeqCst</code></a>, <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> or <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="panics-2"><a class="doc-anchor" href="#panics-2">§</a>Panics</h5>
|
|||
|
<p>Panics if <code>failure</code> is <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a>, <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.AcqRel" title="variant core::sync::atomic::Ordering::AcqRel"><code>AcqRel</code></a>.</p>
|
|||
|
<h5 id="examples-8"><a class="doc-anchor" href="#examples-8">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>some_var = AtomicU32::new(<span class="number">5</span>);
|
|||
|
|
|||
|
<span class="macro">assert_eq!</span>(
|
|||
|
some_var.compare_exchange(<span class="number">5</span>, <span class="number">10</span>, Ordering::Acquire, Ordering::Relaxed),
|
|||
|
<span class="prelude-val">Ok</span>(<span class="number">5</span>),
|
|||
|
);
|
|||
|
<span class="macro">assert_eq!</span>(some_var.load(Ordering::Relaxed), <span class="number">10</span>);
|
|||
|
|
|||
|
<span class="macro">assert_eq!</span>(
|
|||
|
some_var.compare_exchange(<span class="number">6</span>, <span class="number">12</span>, Ordering::SeqCst, Ordering::Acquire),
|
|||
|
<span class="prelude-val">Err</span>(<span class="number">10</span>),
|
|||
|
);
|
|||
|
<span class="macro">assert_eq!</span>(some_var.load(Ordering::Relaxed), <span class="number">10</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.compare_exchange_weak" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.compare_exchange_weak" class="fn">compare_exchange_weak</a>(
|
|||
|
&self,
|
|||
|
current: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>,
|
|||
|
new: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>,
|
|||
|
success: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>,
|
|||
|
failure: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>,
|
|||
|
) -> <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>></h4></section></summary><div class="docblock"><p>Stores a value into the atomic integer if the current value is the same as
|
|||
|
the <code>current</code> value.
|
|||
|
Unlike <a href="struct.AtomicU32.html#method.compare_exchange" title="method portable_atomic::AtomicU32::compare_exchange"><code>compare_exchange</code></a>
|
|||
|
this function is allowed to spuriously fail even
|
|||
|
when the comparison succeeds, which can result in more efficient code on some
|
|||
|
platforms. The return value is a result indicating whether the new value was
|
|||
|
written and containing the previous value.</p>
|
|||
|
<p><code>compare_exchange_weak</code> takes two <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> arguments to describe the memory
|
|||
|
ordering of this operation. <code>success</code> describes the required ordering for the
|
|||
|
read-modify-write operation that takes place if the comparison with <code>current</code> succeeds.
|
|||
|
<code>failure</code> describes the required ordering for the load operation that takes place when
|
|||
|
the comparison fails. Using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> as success ordering makes the store part
|
|||
|
of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the successful load
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>. The failure ordering can only be <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.SeqCst" title="variant core::sync::atomic::Ordering::SeqCst"><code>SeqCst</code></a>, <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> or <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="panics-3"><a class="doc-anchor" href="#panics-3">§</a>Panics</h5>
|
|||
|
<p>Panics if <code>failure</code> is <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a>, <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.AcqRel" title="variant core::sync::atomic::Ordering::AcqRel"><code>AcqRel</code></a>.</p>
|
|||
|
<h5 id="examples-9"><a class="doc-anchor" href="#examples-9">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>val = AtomicU32::new(<span class="number">4</span>);
|
|||
|
|
|||
|
<span class="kw">let </span><span class="kw-2">mut </span>old = val.load(Ordering::Relaxed);
|
|||
|
<span class="kw">loop </span>{
|
|||
|
<span class="kw">let </span>new = old * <span class="number">2</span>;
|
|||
|
<span class="kw">match </span>val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
|
|||
|
<span class="prelude-val">Ok</span>(<span class="kw">_</span>) => <span class="kw">break</span>,
|
|||
|
<span class="prelude-val">Err</span>(x) => old = x,
|
|||
|
}
|
|||
|
}</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.fetch_add" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.fetch_add" class="fn">fetch_add</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Adds to the current value, returning the previous value.</p>
|
|||
|
<p>This operation wraps around on overflow.</p>
|
|||
|
<p><code>fetch_add</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="examples-10"><a class="doc-anchor" href="#examples-10">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_add(<span class="number">10</span>, Ordering::SeqCst), <span class="number">0</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::SeqCst), <span class="number">10</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.add" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.add" class="fn">add</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>)</h4></section></summary><div class="docblock"><p>Adds to the current value.</p>
|
|||
|
<p>This operation wraps around on overflow.</p>
|
|||
|
<p>Unlike <code>fetch_add</code>, this does not return the previous value.</p>
|
|||
|
<p><code>add</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<p>This function may generate more efficient code than <code>fetch_add</code> on some platforms.</p>
|
|||
|
<ul>
|
|||
|
<li>MSP430: <code>add</code> instead of disabling interrupts ({8,16}-bit atomics)</li>
|
|||
|
</ul>
|
|||
|
<h5 id="examples-11"><a class="doc-anchor" href="#examples-11">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0</span>);
|
|||
|
foo.add(<span class="number">10</span>, Ordering::SeqCst);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::SeqCst), <span class="number">10</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.fetch_sub" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.fetch_sub" class="fn">fetch_sub</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Subtracts from the current value, returning the previous value.</p>
|
|||
|
<p>This operation wraps around on overflow.</p>
|
|||
|
<p><code>fetch_sub</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="examples-12"><a class="doc-anchor" href="#examples-12">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">20</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_sub(<span class="number">10</span>, Ordering::SeqCst), <span class="number">20</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::SeqCst), <span class="number">10</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.sub" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.sub" class="fn">sub</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>)</h4></section></summary><div class="docblock"><p>Subtracts from the current value.</p>
|
|||
|
<p>This operation wraps around on overflow.</p>
|
|||
|
<p>Unlike <code>fetch_sub</code>, this does not return the previous value.</p>
|
|||
|
<p><code>sub</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<p>This function may generate more efficient code than <code>fetch_sub</code> on some platforms.</p>
|
|||
|
<ul>
|
|||
|
<li>MSP430: <code>sub</code> instead of disabling interrupts ({8,16}-bit atomics)</li>
|
|||
|
</ul>
|
|||
|
<h5 id="examples-13"><a class="doc-anchor" href="#examples-13">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">20</span>);
|
|||
|
foo.sub(<span class="number">10</span>, Ordering::SeqCst);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::SeqCst), <span class="number">10</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.fetch_and" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.fetch_and" class="fn">fetch_and</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Bitwise “and” with the current value.</p>
|
|||
|
<p>Performs a bitwise “and” operation on the current value and the argument <code>val</code>, and
|
|||
|
sets the new value to the result.</p>
|
|||
|
<p>Returns the previous value.</p>
|
|||
|
<p><code>fetch_and</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="examples-14"><a class="doc-anchor" href="#examples-14">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0b101101</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_and(<span class="number">0b110011</span>, Ordering::SeqCst), <span class="number">0b101101</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::SeqCst), <span class="number">0b100001</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.and" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.and" class="fn">and</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>)</h4></section></summary><div class="docblock"><p>Bitwise “and” with the current value.</p>
|
|||
|
<p>Performs a bitwise “and” operation on the current value and the argument <code>val</code>, and
|
|||
|
sets the new value to the result.</p>
|
|||
|
<p>Unlike <code>fetch_and</code>, this does not return the previous value.</p>
|
|||
|
<p><code>and</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<p>This function may generate more efficient code than <code>fetch_and</code> on some platforms.</p>
|
|||
|
<ul>
|
|||
|
<li>x86/x86_64: <code>lock and</code> instead of <code>cmpxchg</code> loop ({8,16,32}-bit atomics on x86, but additionally 64-bit atomics on x86_64)</li>
|
|||
|
<li>MSP430: <code>and</code> instead of disabling interrupts ({8,16}-bit atomics)</li>
|
|||
|
</ul>
|
|||
|
<p>Note: On x86/x86_64, the use of either function should not usually
|
|||
|
affect the generated code, because LLVM can properly optimize the case
|
|||
|
where the result is unused.</p>
|
|||
|
<h5 id="examples-15"><a class="doc-anchor" href="#examples-15">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0b101101</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_and(<span class="number">0b110011</span>, Ordering::SeqCst), <span class="number">0b101101</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::SeqCst), <span class="number">0b100001</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.fetch_nand" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.fetch_nand" class="fn">fetch_nand</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Bitwise “nand” with the current value.</p>
|
|||
|
<p>Performs a bitwise “nand” operation on the current value and the argument <code>val</code>, and
|
|||
|
sets the new value to the result.</p>
|
|||
|
<p>Returns the previous value.</p>
|
|||
|
<p><code>fetch_nand</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="examples-16"><a class="doc-anchor" href="#examples-16">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0x13</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_nand(<span class="number">0x31</span>, Ordering::SeqCst), <span class="number">0x13</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::SeqCst), !(<span class="number">0x13 </span>& <span class="number">0x31</span>));</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.fetch_or" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.fetch_or" class="fn">fetch_or</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Bitwise “or” with the current value.</p>
|
|||
|
<p>Performs a bitwise “or” operation on the current value and the argument <code>val</code>, and
|
|||
|
sets the new value to the result.</p>
|
|||
|
<p>Returns the previous value.</p>
|
|||
|
<p><code>fetch_or</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="examples-17"><a class="doc-anchor" href="#examples-17">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0b101101</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_or(<span class="number">0b110011</span>, Ordering::SeqCst), <span class="number">0b101101</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::SeqCst), <span class="number">0b111111</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.or" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.or" class="fn">or</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>)</h4></section></summary><div class="docblock"><p>Bitwise “or” with the current value.</p>
|
|||
|
<p>Performs a bitwise “or” operation on the current value and the argument <code>val</code>, and
|
|||
|
sets the new value to the result.</p>
|
|||
|
<p>Unlike <code>fetch_or</code>, this does not return the previous value.</p>
|
|||
|
<p><code>or</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<p>This function may generate more efficient code than <code>fetch_or</code> on some platforms.</p>
|
|||
|
<ul>
|
|||
|
<li>x86/x86_64: <code>lock or</code> instead of <code>cmpxchg</code> loop ({8,16,32}-bit atomics on x86, but additionally 64-bit atomics on x86_64)</li>
|
|||
|
<li>MSP430: <code>or</code> instead of disabling interrupts ({8,16}-bit atomics)</li>
|
|||
|
</ul>
|
|||
|
<p>Note: On x86/x86_64, the use of either function should not usually
|
|||
|
affect the generated code, because LLVM can properly optimize the case
|
|||
|
where the result is unused.</p>
|
|||
|
<h5 id="examples-18"><a class="doc-anchor" href="#examples-18">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0b101101</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_or(<span class="number">0b110011</span>, Ordering::SeqCst), <span class="number">0b101101</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::SeqCst), <span class="number">0b111111</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.fetch_xor" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.fetch_xor" class="fn">fetch_xor</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Bitwise “xor” with the current value.</p>
|
|||
|
<p>Performs a bitwise “xor” operation on the current value and the argument <code>val</code>, and
|
|||
|
sets the new value to the result.</p>
|
|||
|
<p>Returns the previous value.</p>
|
|||
|
<p><code>fetch_xor</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="examples-19"><a class="doc-anchor" href="#examples-19">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0b101101</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_xor(<span class="number">0b110011</span>, Ordering::SeqCst), <span class="number">0b101101</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::SeqCst), <span class="number">0b011110</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.xor" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.xor" class="fn">xor</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>)</h4></section></summary><div class="docblock"><p>Bitwise “xor” with the current value.</p>
|
|||
|
<p>Performs a bitwise “xor” operation on the current value and the argument <code>val</code>, and
|
|||
|
sets the new value to the result.</p>
|
|||
|
<p>Unlike <code>fetch_xor</code>, this does not return the previous value.</p>
|
|||
|
<p><code>xor</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<p>This function may generate more efficient code than <code>fetch_xor</code> on some platforms.</p>
|
|||
|
<ul>
|
|||
|
<li>x86/x86_64: <code>lock xor</code> instead of <code>cmpxchg</code> loop ({8,16,32}-bit atomics on x86, but additionally 64-bit atomics on x86_64)</li>
|
|||
|
<li>MSP430: <code>xor</code> instead of disabling interrupts ({8,16}-bit atomics)</li>
|
|||
|
</ul>
|
|||
|
<p>Note: On x86/x86_64, the use of either function should not usually
|
|||
|
affect the generated code, because LLVM can properly optimize the case
|
|||
|
where the result is unused.</p>
|
|||
|
<h5 id="examples-20"><a class="doc-anchor" href="#examples-20">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0b101101</span>);
|
|||
|
foo.xor(<span class="number">0b110011</span>, Ordering::SeqCst);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::SeqCst), <span class="number">0b011110</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.fetch_update" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.fetch_update" class="fn">fetch_update</a><F>(
|
|||
|
&self,
|
|||
|
set_order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>,
|
|||
|
fetch_order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>,
|
|||
|
f: F,
|
|||
|
) -> <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>><div class="where">where
|
|||
|
F: <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(<a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>) -> <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>>,</div></h4></section></summary><div class="docblock"><p>Fetches the value, and applies a function to it that returns an optional
|
|||
|
new value. Returns a <code>Result</code> of <code>Ok(previous_value)</code> if the function returned <code>Some(_)</code>, else
|
|||
|
<code>Err(previous_value)</code>.</p>
|
|||
|
<p>Note: This may call the function multiple times if the value has been changed from other threads in
|
|||
|
the meantime, as long as the function returns <code>Some(_)</code>, but the function will have been applied
|
|||
|
only once to the stored value.</p>
|
|||
|
<p><code>fetch_update</code> takes two <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> arguments to describe the memory ordering of this operation.
|
|||
|
The first describes the required ordering for when the operation finally succeeds while the second
|
|||
|
describes the required ordering for loads. These correspond to the success and failure orderings of
|
|||
|
<a href="struct.AtomicU32.html#method.compare_exchange" title="method portable_atomic::AtomicU32::compare_exchange"><code>compare_exchange</code></a> respectively.</p>
|
|||
|
<p>Using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> as success ordering makes the store part
|
|||
|
of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the final successful load
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>. The (failed) load ordering can only be <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.SeqCst" title="variant core::sync::atomic::Ordering::SeqCst"><code>SeqCst</code></a>, <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> or <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="panics-4"><a class="doc-anchor" href="#panics-4">§</a>Panics</h5>
|
|||
|
<p>Panics if <code>fetch_order</code> is <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a>, <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.AcqRel" title="variant core::sync::atomic::Ordering::AcqRel"><code>AcqRel</code></a>.</p>
|
|||
|
<h5 id="considerations"><a class="doc-anchor" href="#considerations">§</a>Considerations</h5>
|
|||
|
<p>This method is not magic; it is not provided by the hardware.
|
|||
|
It is implemented in terms of <a href="struct.AtomicU32.html#method.compare_exchange_weak" title="method portable_atomic::AtomicU32::compare_exchange_weak"><code>compare_exchange_weak</code></a>,
|
|||
|
and suffers from the same drawbacks.
|
|||
|
In particular, this method will not circumvent the <a href="https://en.wikipedia.org/wiki/ABA_problem">ABA Problem</a>.</p>
|
|||
|
<h5 id="examples-21"><a class="doc-anchor" href="#examples-21">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>x = AtomicU32::new(<span class="number">7</span>);
|
|||
|
<span class="macro">assert_eq!</span>(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |<span class="kw">_</span>| <span class="prelude-val">None</span>), <span class="prelude-val">Err</span>(<span class="number">7</span>));
|
|||
|
<span class="macro">assert_eq!</span>(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| <span class="prelude-val">Some</span>(x + <span class="number">1</span>)), <span class="prelude-val">Ok</span>(<span class="number">7</span>));
|
|||
|
<span class="macro">assert_eq!</span>(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| <span class="prelude-val">Some</span>(x + <span class="number">1</span>)), <span class="prelude-val">Ok</span>(<span class="number">8</span>));
|
|||
|
<span class="macro">assert_eq!</span>(x.load(Ordering::SeqCst), <span class="number">9</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.fetch_max" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.fetch_max" class="fn">fetch_max</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Maximum with the current value.</p>
|
|||
|
<p>Finds the maximum of the current value and the argument <code>val</code>, and
|
|||
|
sets the new value to the result.</p>
|
|||
|
<p>Returns the previous value.</p>
|
|||
|
<p><code>fetch_max</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="examples-22"><a class="doc-anchor" href="#examples-22">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">23</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_max(<span class="number">42</span>, Ordering::SeqCst), <span class="number">23</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::SeqCst), <span class="number">42</span>);</code></pre></div>
|
|||
|
<p>If you want to obtain the maximum value in one step, you can use the following:</p>
|
|||
|
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">23</span>);
|
|||
|
<span class="kw">let </span>bar = <span class="number">42</span>;
|
|||
|
<span class="kw">let </span>max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
|
|||
|
<span class="macro">assert!</span>(max_foo == <span class="number">42</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.fetch_min" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.fetch_min" class="fn">fetch_min</a>(&self, val: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Minimum with the current value.</p>
|
|||
|
<p>Finds the minimum of the current value and the argument <code>val</code>, and
|
|||
|
sets the new value to the result.</p>
|
|||
|
<p>Returns the previous value.</p>
|
|||
|
<p><code>fetch_min</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="examples-23"><a class="doc-anchor" href="#examples-23">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">23</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_min(<span class="number">42</span>, Ordering::Relaxed), <span class="number">23</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), <span class="number">23</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_min(<span class="number">22</span>, Ordering::Relaxed), <span class="number">23</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), <span class="number">22</span>);</code></pre></div>
|
|||
|
<p>If you want to obtain the minimum value in one step, you can use the following:</p>
|
|||
|
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">23</span>);
|
|||
|
<span class="kw">let </span>bar = <span class="number">12</span>;
|
|||
|
<span class="kw">let </span>min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
|
|||
|
<span class="macro">assert_eq!</span>(min_foo, <span class="number">12</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.bit_set" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.bit_set" class="fn">bit_set</a>(&self, bit: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Sets the bit at the specified bit-position to 1.</p>
|
|||
|
<p>Returns <code>true</code> if the specified bit was previously set to 1.</p>
|
|||
|
<p><code>bit_set</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<p>This corresponds to x86’s <code>lock bts</code>, and the implementation calls them on x86/x86_64.</p>
|
|||
|
<h5 id="examples-24"><a class="doc-anchor" href="#examples-24">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0b0000</span>);
|
|||
|
<span class="macro">assert!</span>(!foo.bit_set(<span class="number">0</span>, Ordering::Relaxed));
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), <span class="number">0b0001</span>);
|
|||
|
<span class="macro">assert!</span>(foo.bit_set(<span class="number">0</span>, Ordering::Relaxed));
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), <span class="number">0b0001</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.bit_clear" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.bit_clear" class="fn">bit_clear</a>(&self, bit: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Clears the bit at the specified bit-position to 1.</p>
|
|||
|
<p>Returns <code>true</code> if the specified bit was previously set to 1.</p>
|
|||
|
<p><code>bit_clear</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<p>This corresponds to x86’s <code>lock btr</code>, and the implementation calls them on x86/x86_64.</p>
|
|||
|
<h5 id="examples-25"><a class="doc-anchor" href="#examples-25">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0b0001</span>);
|
|||
|
<span class="macro">assert!</span>(foo.bit_clear(<span class="number">0</span>, Ordering::Relaxed));
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), <span class="number">0b0000</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.bit_toggle" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.bit_toggle" class="fn">bit_toggle</a>(&self, bit: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Toggles the bit at the specified bit-position.</p>
|
|||
|
<p>Returns <code>true</code> if the specified bit was previously set to 1.</p>
|
|||
|
<p><code>bit_toggle</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<p>This corresponds to x86’s <code>lock btc</code>, and the implementation calls them on x86/x86_64.</p>
|
|||
|
<h5 id="examples-26"><a class="doc-anchor" href="#examples-26">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0b0000</span>);
|
|||
|
<span class="macro">assert!</span>(!foo.bit_toggle(<span class="number">0</span>, Ordering::Relaxed));
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), <span class="number">0b0001</span>);
|
|||
|
<span class="macro">assert!</span>(foo.bit_toggle(<span class="number">0</span>, Ordering::Relaxed));
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), <span class="number">0b0000</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.fetch_not" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.fetch_not" class="fn">fetch_not</a>(&self, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Logical negates the current value, and sets the new value to the result.</p>
|
|||
|
<p>Returns the previous value.</p>
|
|||
|
<p><code>fetch_not</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="examples-27"><a class="doc-anchor" href="#examples-27">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_not(Ordering::Relaxed), <span class="number">0</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), !<span class="number">0</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.not" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.not" class="fn">not</a>(&self, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>)</h4></section></summary><div class="docblock"><p>Logical negates the current value, and sets the new value to the result.</p>
|
|||
|
<p>Unlike <code>fetch_not</code>, this does not return the previous value.</p>
|
|||
|
<p><code>not</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<p>This function may generate more efficient code than <code>fetch_not</code> on some platforms.</p>
|
|||
|
<ul>
|
|||
|
<li>x86/x86_64: <code>lock not</code> instead of <code>cmpxchg</code> loop ({8,16,32}-bit atomics on x86, but additionally 64-bit atomics on x86_64)</li>
|
|||
|
<li>MSP430: <code>inv</code> instead of disabling interrupts ({8,16}-bit atomics)</li>
|
|||
|
</ul>
|
|||
|
<h5 id="examples-28"><a class="doc-anchor" href="#examples-28">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">0</span>);
|
|||
|
foo.not(Ordering::Relaxed);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), !<span class="number">0</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.fetch_neg" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.fetch_neg" class="fn">fetch_neg</a>(&self, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Negates the current value, and sets the new value to the result.</p>
|
|||
|
<p>Returns the previous value.</p>
|
|||
|
<p><code>fetch_neg</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<h5 id="examples-29"><a class="doc-anchor" href="#examples-29">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">5</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_neg(Ordering::Relaxed), <span class="number">5</span>);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), <span class="number">5_u32</span>.wrapping_neg());
|
|||
|
<span class="macro">assert_eq!</span>(foo.fetch_neg(Ordering::Relaxed), <span class="number">5_u32</span>.wrapping_neg());
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), <span class="number">5</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.neg" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub fn <a href="#method.neg" class="fn">neg</a>(&self, order: <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering">Ordering</a>)</h4></section></summary><div class="docblock"><p>Negates the current value, and sets the new value to the result.</p>
|
|||
|
<p>Unlike <code>fetch_neg</code>, this does not return the previous value.</p>
|
|||
|
<p><code>neg</code> takes an <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html" title="enum core::sync::atomic::Ordering"><code>Ordering</code></a> argument which describes the memory ordering
|
|||
|
of this operation. All ordering modes are possible. Note that using
|
|||
|
<a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Acquire" title="variant core::sync::atomic::Ordering::Acquire"><code>Acquire</code></a> makes the store part of this operation <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>, and
|
|||
|
using <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Release" title="variant core::sync::atomic::Ordering::Release"><code>Release</code></a> makes the load part <a href="https://doc.rust-lang.org/1.82.0/core/sync/atomic/enum.Ordering.html#variant.Relaxed" title="variant core::sync::atomic::Ordering::Relaxed"><code>Relaxed</code></a>.</p>
|
|||
|
<p>This function may generate more efficient code than <code>fetch_neg</code> on some platforms.</p>
|
|||
|
<ul>
|
|||
|
<li>x86/x86_64: <code>lock neg</code> instead of <code>cmpxchg</code> loop ({8,16,32}-bit atomics on x86, but additionally 64-bit atomics on x86_64)</li>
|
|||
|
</ul>
|
|||
|
<h5 id="examples-30"><a class="doc-anchor" href="#examples-30">§</a>Examples</h5>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>portable_atomic::{AtomicU32, Ordering};
|
|||
|
|
|||
|
<span class="kw">let </span>foo = AtomicU32::new(<span class="number">5</span>);
|
|||
|
foo.neg(Ordering::Relaxed);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), <span class="number">5_u32</span>.wrapping_neg());
|
|||
|
foo.neg(Ordering::Relaxed);
|
|||
|
<span class="macro">assert_eq!</span>(foo.load(Ordering::Relaxed), <span class="number">5</span>);</code></pre></div>
|
|||
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.as_ptr" class="method"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><h4 class="code-header">pub const fn <a href="#method.as_ptr" class="fn">as_ptr</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.pointer.html">*mut </a><a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a></h4></section></summary><div class="docblock"><p>Returns a mutable pointer to the underlying integer.</p>
|
|||
|
<p>Returning an <code>*mut</code> pointer from a shared reference to this atomic is
|
|||
|
safe because the atomic types work with interior mutability. Any use of
|
|||
|
the returned raw pointer requires an <code>unsafe</code> block and has to uphold
|
|||
|
the safety requirements. If there is concurrent access, note the following
|
|||
|
additional safety requirements:</p>
|
|||
|
<ul>
|
|||
|
<li>If this atomic type is <a href="struct.AtomicU32.html#method.is_lock_free" title="associated function portable_atomic::AtomicU32::is_lock_free">lock-free</a>, any concurrent
|
|||
|
operations on it must be atomic.</li>
|
|||
|
<li>Otherwise, any concurrent operations on it must be compatible with
|
|||
|
operations performed by this atomic type.</li>
|
|||
|
</ul>
|
|||
|
<p>This is <code>const fn</code> on Rust 1.58+.</p>
|
|||
|
</div></details></div></details></div><h2 id="trait-implementations" class="section-header">Trait Implementations<a href="#trait-implementations" class="anchor">§</a></h2><div id="trait-implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-Debug-for-AtomicU32" class="impl"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><a href="#impl-Debug-for-AtomicU32" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a> for <a class="struct" href="struct.AtomicU32.html" title="struct portable_atomic::AtomicU32">AtomicU32</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.fmt" class="method trait-impl"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><a href="#method.fmt" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.82.0/core/fmt/trait.Debug.html#tymethod.fmt" class="fn">fmt</a>(&self, f: &mut <a class="struct" href="https://doc.rust-lang.org/1.82.0/core/fmt/struct.Formatter.html" title="struct core::fmt::Formatter">Formatter</a><'_>) -> <a class="type" href="https://doc.rust-lang.org/1.82.0/core/fmt/type.Result.html" title="type core::fmt::Result">Result</a></h4></section></summary><div class='docblock'>Formats the value using the given formatter. <a href="https://doc.rust-lang.org/1.82.0/core/fmt/trait.Debug.html#tymethod.fmt">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Default-for-AtomicU32" class="impl"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><a href="#impl-Default-for-AtomicU32" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/default/trait.Default.html" title="trait core::default::Default">Default</a> for <a class="struct" href="struct.AtomicU32.html" title="struct portable_atomic::AtomicU32">AtomicU32</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.default" class="method trait-impl"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><a href="#method.default" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.82.0/core/default/trait.Default.html#tymethod.default" class="fn">default</a>() -> Self</h4></section></summary><div class='docblock'>Returns the “default value” for a type. <a href="https://doc.rust-lang.org/1.82.0/core/default/trait.Default.html#tymethod.default">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-From%3Cu32%3E-for-AtomicU32" class="impl"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><a href="#impl-From%3Cu32%3E-for-AtomicU32" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.From.html" title="trait core::convert::From">From</a><<a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>> for <a class="struct" href="struct.AtomicU32.html" title="struct portable_atomic::AtomicU32">AtomicU32</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from" class="method trait-impl"><a class="src rightside" href="../src/portable_atomic/lib.rs.html#4607-4608">source</a><a href="#method.from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.82.0/core/convert/trait.From.html#tymethod.from" class="fn">from</a>(v: <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.u32.html">u32</a>) -> Self</h4></section></summary><div class='docblock'>Converts to this type from the inp
|
|||
|
T: 'static + ?<a class="trait" href="https://doc.rust-lang.org/1.82.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.type_id" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/any.rs.html#139">source</a><a href="#method.type_id" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.82.0/core/any/trait.Any.html#tymethod.type_id" class="fn">type_id</a>(&self) -> <a class="struct" href="https://doc.rust-lang.org/1.82.0/core/any/struct.TypeId.html" title="struct core::any::TypeId">TypeId</a></h4></section></summary><div class='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a href="https://doc.rust-lang.org/1.82.0/core/any/trait.Any.html#tymethod.type_id">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Borrow%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/borrow.rs.html#209">source</a><a href="#impl-Borrow%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/borrow/trait.Borrow.html" title="trait core::borrow::Borrow">Borrow</a><T> for T<div class="where">where
|
|||
|
T: ?<a class="trait" href="https://doc.rust-lang.org/1.82.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/borrow.rs.html#211">source</a><a href="#method.borrow" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.82.0/core/borrow/trait.Borrow.html#tymethod.borrow" class="fn">borrow</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.reference.html">&T</a></h4></section></summary><div class='docblock'>Immutably borrows from an owned value. <a href="https://doc.rust-lang.org/1.82.0/core/borrow/trait.Borrow.html#tymethod.borrow">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-BorrowMut%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/borrow.rs.html#217">source</a><a href="#impl-BorrowMut%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/borrow/trait.BorrowMut.html" title="trait core::borrow::BorrowMut">BorrowMut</a><T> for T<div class="where">where
|
|||
|
T: ?<a class="trait" href="https://doc.rust-lang.org/1.82.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow_mut" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/borrow.rs.html#218">source</a><a href="#method.borrow_mut" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.82.0/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut" class="fn">borrow_mut</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/1.82.0/core/primitive.reference.html">&mut T</a></h4></section></summary><div class='docblock'>Mutably borrows from an owned value. <a href="https://doc.rust-lang.org/1.82.0/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-From%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/convert/mod.rs.html#765">source</a><a href="#impl-From%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.From.html" title="trait core::convert::From">From</a><T> for T</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from-1" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/convert/mod.rs.html#768">source</a><a href="#method.from-1" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.82.0/core/convert/trait.From.html#tymethod.from" class="fn">from</a>(t: T) -> T</h4></section></summary><div class="docblock"><p>Returns the argument unchanged.</p>
|
|||
|
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Into%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/convert/mod.rs.html#748-750">source</a><a href="#impl-Into%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T, U> <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a><U> for T<div class="where">where
|
|||
|
U: <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.From.html" title="trait core::convert::From">From</a><T>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/convert/mod.rs.html#758">source</a><a href="#method.into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.82.0/core/convert/trait.Into.html#tymethod.into" class="fn">into</a>(self) -> U</h4></section></summary><div class="docblock"><p>Calls <code>U::from(self)</code>.</p>
|
|||
|
<p>That is, this conversion is whatever the implementation of
|
|||
|
<code><a href="https://doc.rust-lang.org/1.82.0/core/convert/trait.From.html" title="trait core::convert::From">From</a><T> for U</code> chooses to do.</p>
|
|||
|
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryFrom%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/convert/mod.rs.html#805-807">source</a><a href="#impl-TryFrom%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T, U> <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><U> for T<div class="where">where
|
|||
|
U: <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a><T>,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error-1" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/convert/mod.rs.html#809">source</a><a href="#associatedtype.Error-1" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryFrom.html#associatedtype.Error" class="associatedtype">Error</a> = <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/convert/enum.Infallible.html" title="enum core::convert::Infallible">Infallible</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_from" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/convert/mod.rs.html#812">source</a><a href="#method.try_from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryFrom.html#tymethod.try_from" class="fn">try_from</a>(value: U) -> <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <T as <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><U>>::<a class="associatedtype" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryInto%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/convert/mod.rs.html#790-792">source</a><a href="#impl-TryInto%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T, U> <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryInto.html" title="trait core::convert::TryInto">TryInto</a><U> for T<div class="where">where
|
|||
|
U: <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/convert/mod.rs.html#794">source</a><a href="#associatedtype.Error" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryInto.html#associatedtype.Error" class="associatedtype">Error</a> = <U as <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>>::<a class="associatedtype" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.82.0/src/core/convert/mod.rs.html#797">source</a><a href="#method.try_into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryInto.html#tymethod.try_into" class="fn">try_into</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/1.82.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><U, <U as <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>>::<a class="associatedtype" href="https://doc.rust-lang.org/1.82.0/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details></div></section></div></main></body></html>
|