Skip to content

Commit 5e1891c

Browse files
authored
Rollup merge of rust-lang#62577 - Zoxc:atomic-cell, r=matthewjasper
Add an AtomicCell abstraction Split out from rust-lang#61923.
2 parents 6b468c6 + 498bdc9 commit 5e1891c

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

Diff for: Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3018,6 +3018,7 @@ name = "rustc_data_structures"
30183018
version = "0.0.0"
30193019
dependencies = [
30203020
"cfg-if 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
3021+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
30213022
"ena 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
30223023
"graphviz 0.0.0",
30233024
"indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",

Diff for: src/librustc_data_structures/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ lazy_static = "1"
1818
serialize = { path = "../libserialize" }
1919
graphviz = { path = "../libgraphviz" }
2020
cfg-if = "0.1.2"
21+
crossbeam-utils = { version = "0.6.5", features = ["nightly"] }
2122
stable_deref_trait = "1.0.0"
2223
rayon = { version = "0.2.0", package = "rustc-rayon" }
2324
rayon-core = { version = "0.2.0", package = "rustc-rayon-core" }

Diff for: src/librustc_data_structures/sync.rs

+54-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,51 @@ cfg_if! {
6767
use std::ops::Add;
6868
use std::panic::{resume_unwind, catch_unwind, AssertUnwindSafe};
6969

70+
/// This is a single threaded variant of AtomicCell provided by crossbeam.
71+
/// Unlike `Atomic` this is intended for all `Copy` types,
72+
/// but it lacks the explicit ordering arguments.
73+
#[derive(Debug)]
74+
pub struct AtomicCell<T: Copy>(Cell<T>);
75+
76+
impl<T: Copy> AtomicCell<T> {
77+
#[inline]
78+
pub fn new(v: T) -> Self {
79+
AtomicCell(Cell::new(v))
80+
}
81+
82+
#[inline]
83+
pub fn get_mut(&mut self) -> &mut T {
84+
self.0.get_mut()
85+
}
86+
}
87+
88+
impl<T: Copy> AtomicCell<T> {
89+
#[inline]
90+
pub fn into_inner(self) -> T {
91+
self.0.into_inner()
92+
}
93+
94+
#[inline]
95+
pub fn load(&self) -> T {
96+
self.0.get()
97+
}
98+
99+
#[inline]
100+
pub fn store(&self, val: T) {
101+
self.0.set(val)
102+
}
103+
104+
#[inline]
105+
pub fn swap(&self, val: T) -> T {
106+
self.0.replace(val)
107+
}
108+
}
109+
110+
/// This is a single threaded variant of `AtomicU64`, `AtomicUsize`, etc.
111+
/// It differs from `AtomicCell` in that it has explicit ordering arguments
112+
/// and is only intended for use with the native atomic types.
113+
/// You should use this type through the `AtomicU64`, `AtomicUsize`, etc, type aliases
114+
/// as it's not intended to be used separately.
70115
#[derive(Debug)]
71116
pub struct Atomic<T: Copy>(Cell<T>);
72117

@@ -77,7 +122,8 @@ cfg_if! {
77122
}
78123
}
79124

80-
impl<T: Copy + PartialEq> Atomic<T> {
125+
impl<T: Copy> Atomic<T> {
126+
#[inline]
81127
pub fn into_inner(self) -> T {
82128
self.0.into_inner()
83129
}
@@ -92,10 +138,14 @@ cfg_if! {
92138
self.0.set(val)
93139
}
94140

141+
#[inline]
95142
pub fn swap(&self, val: T, _: Ordering) -> T {
96143
self.0.replace(val)
97144
}
145+
}
98146

147+
impl<T: Copy + PartialEq> Atomic<T> {
148+
#[inline]
99149
pub fn compare_exchange(&self,
100150
current: T,
101151
new: T,
@@ -113,6 +163,7 @@ cfg_if! {
113163
}
114164

115165
impl<T: Add<Output=T> + Copy> Atomic<T> {
166+
#[inline]
116167
pub fn fetch_add(&self, val: T, _: Ordering) -> T {
117168
let old = self.0.get();
118169
self.0.set(old + val);
@@ -271,6 +322,8 @@ cfg_if! {
271322

272323
pub use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicU32, AtomicU64};
273324

325+
pub use crossbeam_utils::atomic::AtomicCell;
326+
274327
pub use std::sync::Arc as Lrc;
275328
pub use std::sync::Weak as Weak;
276329

0 commit comments

Comments
 (0)