Skip to content

Commit

Permalink
Rollup merge of rust-lang#72324 - Amanieu:atomic_minmax, r=dtolnay
Browse files Browse the repository at this point in the history
Stabilize AtomicN::fetch_min and AtomicN::fetch_max

Some architectures (ARMv8.1 LSE and RISC-V) have specific instructions for atomic min/max which the compiler can only generate through explicit instrinsics.
  • Loading branch information
JohnTitor committed May 28, 2020
2 parents d7eed17 + 1d7a731 commit 7caf5fb
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/libcore/sync/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,6 @@ using [`Release`] makes the load part [`Relaxed`].
# Examples
```
#![feature(atomic_min_max)]
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(23);
Expand All @@ -1889,7 +1888,6 @@ assert_eq!(foo.load(Ordering::SeqCst), 42);
If you want to obtain the maximum value in one step, you can use the following:
```
#![feature(atomic_min_max)]
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(23);
Expand All @@ -1898,9 +1896,7 @@ let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
assert!(max_foo == 42);
```"),
#[inline]
#[unstable(feature = "atomic_min_max",
reason = "easier and faster min/max than writing manual CAS loop",
issue = "48655")]
#[stable(feature = "atomic_min_max", since = "1.45.0")]
#[$cfg_cas]
pub fn fetch_max(&self, val: $int_type, order: Ordering) -> $int_type {
// SAFETY: data races are prevented by atomic intrinsics.
Expand Down Expand Up @@ -1929,7 +1925,6 @@ using [`Release`] makes the load part [`Relaxed`].
# Examples
```
#![feature(atomic_min_max)]
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(23);
Expand All @@ -1942,7 +1937,6 @@ assert_eq!(foo.load(Ordering::Relaxed), 22);
If you want to obtain the minimum value in one step, you can use the following:
```
#![feature(atomic_min_max)]
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
let foo = ", stringify!($atomic_type), "::new(23);
Expand All @@ -1951,9 +1945,7 @@ let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
assert_eq!(min_foo, 12);
```"),
#[inline]
#[unstable(feature = "atomic_min_max",
reason = "easier and faster min/max than writing manual CAS loop",
issue = "48655")]
#[stable(feature = "atomic_min_max", since = "1.45.0")]
#[$cfg_cas]
pub fn fetch_min(&self, val: $int_type, order: Ordering) -> $int_type {
// SAFETY: data races are prevented by atomic intrinsics.
Expand Down

0 comments on commit 7caf5fb

Please sign in to comment.