Skip to content

Commit 1335b3d

Browse files
Add fetch_nand.
cc #13226 (the tracking issue)
1 parent 3bcda48 commit 1335b3d

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/libcore/sync/atomic.rs

+46
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ macro_rules! atomic_int {
945945
$stable_debug:meta,
946946
$stable_access:meta,
947947
$stable_from:meta,
948+
$stable_nand:meta,
948949
$s_int_type:expr, $int_ref:expr,
949950
$int_type:ident $atomic_type:ident $atomic_init:ident) => {
950951
/// An integer type which can be safely shared between threads.
@@ -1325,6 +1326,29 @@ macro_rules! atomic_int {
13251326
unsafe { atomic_and(self.v.get(), val, order) }
13261327
}
13271328

1329+
/// Bitwise "nand" with the current value.
1330+
///
1331+
/// Performs a bitwise "nand" operation on the current value and the argument `val`, and
1332+
/// sets the new value to the result.
1333+
///
1334+
/// Returns the previous value.
1335+
///
1336+
/// # Examples
1337+
///
1338+
/// ```
1339+
/// #![feature(atomic_nand)]
1340+
///
1341+
/// use std::sync::atomic::{AtomicIsize, Ordering};
1342+
///
1343+
/// let foo = AtomicIsize::new(0xf731);
1344+
/// assert_eq!(foo.fetch_nand(0x137f, Ordering::SeqCst), 0xf731);
1345+
/// assert_eq!(foo.load(Ordering::SeqCst), !(0xf731 & 0x137f));
1346+
#[inline]
1347+
#[$stable_nand]
1348+
pub fn fetch_nand(&self, val: $int_type, order: Ordering) -> $int_type {
1349+
unsafe { atomic_nand(self.v.get(), val, order) }
1350+
}
1351+
13281352
/// Bitwise "or" with the current value.
13291353
///
13301354
/// Performs a bitwise "or" operation on the current value and the argument `val`, and
@@ -1377,6 +1401,7 @@ atomic_int! {
13771401
unstable(feature = "integer_atomics", issue = "32976"),
13781402
unstable(feature = "integer_atomics", issue = "32976"),
13791403
unstable(feature = "integer_atomics", issue = "32976"),
1404+
unstable(feature = "atomic_nand", issue = "13226"),
13801405
"i8", "../../../std/primitive.i8.html",
13811406
i8 AtomicI8 ATOMIC_I8_INIT
13821407
}
@@ -1387,6 +1412,7 @@ atomic_int! {
13871412
unstable(feature = "integer_atomics", issue = "32976"),
13881413
unstable(feature = "integer_atomics", issue = "32976"),
13891414
unstable(feature = "integer_atomics", issue = "32976"),
1415+
unstable(feature = "atomic_nand", issue = "13226"),
13901416
"u8", "../../../std/primitive.u8.html",
13911417
u8 AtomicU8 ATOMIC_U8_INIT
13921418
}
@@ -1397,6 +1423,7 @@ atomic_int! {
13971423
unstable(feature = "integer_atomics", issue = "32976"),
13981424
unstable(feature = "integer_atomics", issue = "32976"),
13991425
unstable(feature = "integer_atomics", issue = "32976"),
1426+
unstable(feature = "atomic_nand", issue = "13226"),
14001427
"i16", "../../../std/primitive.i16.html",
14011428
i16 AtomicI16 ATOMIC_I16_INIT
14021429
}
@@ -1407,6 +1434,7 @@ atomic_int! {
14071434
unstable(feature = "integer_atomics", issue = "32976"),
14081435
unstable(feature = "integer_atomics", issue = "32976"),
14091436
unstable(feature = "integer_atomics", issue = "32976"),
1437+
unstable(feature = "atomic_nand", issue = "13226"),
14101438
"u16", "../../../std/primitive.u16.html",
14111439
u16 AtomicU16 ATOMIC_U16_INIT
14121440
}
@@ -1417,6 +1445,7 @@ atomic_int! {
14171445
unstable(feature = "integer_atomics", issue = "32976"),
14181446
unstable(feature = "integer_atomics", issue = "32976"),
14191447
unstable(feature = "integer_atomics", issue = "32976"),
1448+
unstable(feature = "atomic_nand", issue = "13226"),
14201449
"i32", "../../../std/primitive.i32.html",
14211450
i32 AtomicI32 ATOMIC_I32_INIT
14221451
}
@@ -1427,6 +1456,7 @@ atomic_int! {
14271456
unstable(feature = "integer_atomics", issue = "32976"),
14281457
unstable(feature = "integer_atomics", issue = "32976"),
14291458
unstable(feature = "integer_atomics", issue = "32976"),
1459+
unstable(feature = "atomic_nand", issue = "13226"),
14301460
"u32", "../../../std/primitive.u32.html",
14311461
u32 AtomicU32 ATOMIC_U32_INIT
14321462
}
@@ -1437,6 +1467,7 @@ atomic_int! {
14371467
unstable(feature = "integer_atomics", issue = "32976"),
14381468
unstable(feature = "integer_atomics", issue = "32976"),
14391469
unstable(feature = "integer_atomics", issue = "32976"),
1470+
unstable(feature = "atomic_nand", issue = "13226"),
14401471
"i64", "../../../std/primitive.i64.html",
14411472
i64 AtomicI64 ATOMIC_I64_INIT
14421473
}
@@ -1447,6 +1478,7 @@ atomic_int! {
14471478
unstable(feature = "integer_atomics", issue = "32976"),
14481479
unstable(feature = "integer_atomics", issue = "32976"),
14491480
unstable(feature = "integer_atomics", issue = "32976"),
1481+
unstable(feature = "atomic_nand", issue = "13226"),
14501482
"u64", "../../../std/primitive.u64.html",
14511483
u64 AtomicU64 ATOMIC_U64_INIT
14521484
}
@@ -1457,6 +1489,7 @@ atomic_int!{
14571489
stable(feature = "atomic_debug", since = "1.3.0"),
14581490
stable(feature = "atomic_access", since = "1.15.0"),
14591491
stable(feature = "atomic_from", since = "1.23.0"),
1492+
unstable(feature = "atomic_nand", issue = "13226"),
14601493
"isize", "../../../std/primitive.isize.html",
14611494
isize AtomicIsize ATOMIC_ISIZE_INIT
14621495
}
@@ -1467,6 +1500,7 @@ atomic_int!{
14671500
stable(feature = "atomic_debug", since = "1.3.0"),
14681501
stable(feature = "atomic_access", since = "1.15.0"),
14691502
stable(feature = "atomic_from", since = "1.23.0"),
1503+
unstable(feature = "atomic_nand", issue = "13226"),
14701504
"usize", "../../../std/primitive.usize.html",
14711505
usize AtomicUsize ATOMIC_USIZE_INIT
14721506
}
@@ -1609,6 +1643,18 @@ unsafe fn atomic_and<T>(dst: *mut T, val: T, order: Ordering) -> T {
16091643
}
16101644
}
16111645

1646+
#[inline]
1647+
unsafe fn atomic_nand<T>(dst: *mut T, val: T, order: Ordering) -> T {
1648+
match order {
1649+
Acquire => intrinsics::atomic_nand_acq(dst, val),
1650+
Release => intrinsics::atomic_nand_rel(dst, val),
1651+
AcqRel => intrinsics::atomic_nand_acqrel(dst, val),
1652+
Relaxed => intrinsics::atomic_nand_relaxed(dst, val),
1653+
SeqCst => intrinsics::atomic_nand(dst, val),
1654+
__Nonexhaustive => panic!("invalid memory ordering"),
1655+
}
1656+
}
1657+
16121658
#[inline]
16131659
unsafe fn atomic_or<T>(dst: *mut T, val: T, order: Ordering) -> T {
16141660
match order {

src/libcore/tests/atomic.rs

+14
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ fn uint_and() {
4848
assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
4949
}
5050

51+
#[test]
52+
fn uint_nand() {
53+
let x = AtomicUsize::new(0xf731);
54+
assert_eq!(x.fetch_nand(0x137f, SeqCst), 0xf731);
55+
assert_eq!(x.load(SeqCst), !(0xf731 & 0x137f));
56+
}
57+
5158
#[test]
5259
fn uint_or() {
5360
let x = AtomicUsize::new(0xf731);
@@ -69,6 +76,13 @@ fn int_and() {
6976
assert_eq!(x.load(SeqCst), 0xf731 & 0x137f);
7077
}
7178

79+
#[test]
80+
fn int_nand() {
81+
let x = AtomicIsize::new(0xf731);
82+
assert_eq!(x.fetch_nand(0x137f, SeqCst), 0xf731);
83+
assert_eq!(x.load(SeqCst), !(0xf731 & 0x137f));
84+
}
85+
7286
#[test]
7387
fn int_or() {
7488
let x = AtomicIsize::new(0xf731);

src/libcore/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#![feature(try_from)]
4343
#![feature(try_trait)]
4444
#![feature(exact_chunks)]
45+
#![feature(atomic_nand)]
4546

4647
extern crate core;
4748
extern crate test;

0 commit comments

Comments
 (0)