Skip to content

Commit 2be71ad

Browse files
authored
chore: move conditional AtomicU64 impl to new file (tokio-rs#5256)
Keeping the implementation out of a macro lets rustfmt apply to it.
1 parent d1b789f commit 2be71ad

File tree

3 files changed

+74
-71
lines changed

3 files changed

+74
-71
lines changed

Cross.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[build.env]
22
passthrough = [
33
"RUSTFLAGS",
4+
"RUST_BACKTRACE",
45
]

tokio/src/loom/std/atomic_u64.rs

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -11,76 +11,8 @@ cfg_has_atomic_u64! {
1111
}
1212

1313
cfg_not_has_atomic_u64! {
14-
use crate::loom::sync::Mutex;
15-
use std::sync::atomic::Ordering;
14+
#[path = "atomic_u64_as_mutex.rs"]
15+
mod atomic_u64_as_mutex;
1616

17-
#[derive(Debug)]
18-
pub(crate) struct AtomicU64 {
19-
inner: Mutex<u64>,
20-
}
21-
22-
impl AtomicU64 {
23-
pub(crate) fn new(val: u64) -> Self {
24-
Self {
25-
inner: Mutex::new(val),
26-
}
27-
}
28-
29-
pub(crate) fn load(&self, _: Ordering) -> u64 {
30-
*self.inner.lock()
31-
}
32-
33-
pub(crate) fn store(&self, val: u64, _: Ordering) {
34-
*self.inner.lock() = val;
35-
}
36-
37-
pub(crate) fn fetch_add(&self, val: u64, _: Ordering) -> u64 {
38-
let mut lock = self.inner.lock();
39-
let prev = *lock;
40-
*lock = prev + val;
41-
prev
42-
}
43-
44-
pub(crate) fn fetch_or(&self, val: u64, _: Ordering) -> u64 {
45-
let mut lock = self.inner.lock();
46-
let prev = *lock;
47-
*lock = prev | val;
48-
prev
49-
}
50-
51-
pub(crate) fn compare_exchange(
52-
&self,
53-
current: u64,
54-
new: u64,
55-
_success: Ordering,
56-
_failure: Ordering,
57-
) -> Result<u64, u64> {
58-
let mut lock = self.inner.lock();
59-
60-
if *lock == current {
61-
*lock = new;
62-
Ok(current)
63-
} else {
64-
Err(*lock)
65-
}
66-
}
67-
68-
pub(crate) fn compare_exchange_weak(
69-
&self,
70-
current: u64,
71-
new: u64,
72-
success: Ordering,
73-
failure: Ordering,
74-
) -> Result<u64, u64> {
75-
self.compare_exchange(current, new, success, failure)
76-
}
77-
}
78-
79-
impl Default for AtomicU64 {
80-
fn default() -> AtomicU64 {
81-
Self {
82-
inner: Mutex::new(0),
83-
}
84-
}
85-
}
17+
pub(crate) use atomic_u64_as_mutex::AtomicU64;
8618
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use crate::loom::sync::Mutex;
2+
use std::sync::atomic::Ordering;
3+
4+
#[derive(Debug)]
5+
pub(crate) struct AtomicU64 {
6+
inner: Mutex<u64>,
7+
}
8+
9+
impl AtomicU64 {
10+
pub(crate) fn new(val: u64) -> Self {
11+
Self {
12+
inner: Mutex::new(val),
13+
}
14+
}
15+
16+
pub(crate) fn load(&self, _: Ordering) -> u64 {
17+
*self.inner.lock()
18+
}
19+
20+
pub(crate) fn store(&self, val: u64, _: Ordering) {
21+
*self.inner.lock() = val;
22+
}
23+
24+
pub(crate) fn fetch_add(&self, val: u64, _: Ordering) -> u64 {
25+
let mut lock = self.inner.lock();
26+
let prev = *lock;
27+
*lock = prev + val;
28+
prev
29+
}
30+
31+
pub(crate) fn fetch_or(&self, val: u64, _: Ordering) -> u64 {
32+
let mut lock = self.inner.lock();
33+
let prev = *lock;
34+
*lock = prev | val;
35+
prev
36+
}
37+
38+
pub(crate) fn compare_exchange(
39+
&self,
40+
current: u64,
41+
new: u64,
42+
_success: Ordering,
43+
_failure: Ordering,
44+
) -> Result<u64, u64> {
45+
let mut lock = self.inner.lock();
46+
47+
if *lock == current {
48+
*lock = new;
49+
Ok(current)
50+
} else {
51+
Err(*lock)
52+
}
53+
}
54+
55+
pub(crate) fn compare_exchange_weak(
56+
&self,
57+
current: u64,
58+
new: u64,
59+
success: Ordering,
60+
failure: Ordering,
61+
) -> Result<u64, u64> {
62+
self.compare_exchange(current, new, success, failure)
63+
}
64+
}
65+
66+
impl Default for AtomicU64 {
67+
fn default() -> AtomicU64 {
68+
AtomicU64::new(u64::default())
69+
}
70+
}

0 commit comments

Comments
 (0)