Skip to content

Commit 40d951e

Browse files
committed
Add an AtomicCell abstraction
1 parent cd8b437 commit 40d951e

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

Cargo.lock

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ dependencies = [
240240
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
241241
"core-foundation 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
242242
"crates-io 0.24.0",
243-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
243+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
244244
"crypto-hash 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
245245
"curl 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)",
246246
"curl-sys 0.4.15 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -563,7 +563,7 @@ version = "0.3.4"
563563
source = "registry+https://github.com/rust-lang/crates.io-index"
564564
dependencies = [
565565
"crossbeam-epoch 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
566-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
566+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
567567
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
568568
"rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
569569
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -584,7 +584,7 @@ version = "0.6.3"
584584
source = "registry+https://github.com/rust-lang/crates.io-index"
585585
dependencies = [
586586
"crossbeam-epoch 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
587-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
587+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
588588
]
589589

590590
[[package]]
@@ -608,7 +608,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
608608
dependencies = [
609609
"arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
610610
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
611-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
611+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
612612
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
613613
"memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
614614
"scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -624,10 +624,11 @@ dependencies = [
624624

625625
[[package]]
626626
name = "crossbeam-utils"
627-
version = "0.6.2"
627+
version = "0.6.5"
628628
source = "registry+https://github.com/rust-lang/crates.io-index"
629629
dependencies = [
630630
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
631+
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
631632
]
632633

633634
[[package]]
@@ -2644,6 +2645,7 @@ name = "rustc_data_structures"
26442645
version = "0.0.0"
26452646
dependencies = [
26462647
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
2648+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
26472649
"ena 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
26482650
"graphviz 0.0.0",
26492651
"jobserver 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3553,7 +3555,7 @@ name = "tokio-executor"
35533555
version = "0.1.6"
35543556
source = "registry+https://github.com/rust-lang/crates.io-index"
35553557
dependencies = [
3556-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
3558+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
35573559
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
35583560
]
35593561

@@ -3597,7 +3599,7 @@ name = "tokio-reactor"
35973599
version = "0.1.8"
35983600
source = "registry+https://github.com/rust-lang/crates.io-index"
35993601
dependencies = [
3600-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
3602+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
36013603
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
36023604
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
36033605
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3645,7 +3647,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
36453647
dependencies = [
36463648
"crossbeam-channel 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
36473649
"crossbeam-deque 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
3648-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
3650+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
36493651
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
36503652
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
36513653
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3658,7 +3660,7 @@ name = "tokio-timer"
36583660
version = "0.2.8"
36593661
source = "registry+https://github.com/rust-lang/crates.io-index"
36603662
dependencies = [
3661-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
3663+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
36623664
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
36633665
"slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
36643666
"tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -4018,7 +4020,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
40184020
"checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150"
40194021
"checksum crossbeam-epoch 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f10a4f8f409aaac4b16a5474fb233624238fcdeefb9ba50d5ea059aab63ba31c"
40204022
"checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9"
4021-
"checksum crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e07fc155212827475223f0bcfae57e945e694fc90950ddf3f6695bbfd5555c72"
4023+
"checksum crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f8306fcef4a7b563b76b7dd949ca48f52bc1141aa067d2ea09565f3e2652aa5c"
40224024
"checksum crypto-hash 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "09de9ee0fc255ace04c7fa0763c9395a945c37c8292bb554f8d48361d1dcf1b4"
40234025
"checksum curl 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)" = "c7c9d851c825e0c033979d4516c9173bc19a78a96eb4d6ae51d4045440eafa16"
40244026
"checksum curl-sys 0.4.15 (registry+https://github.com/rust-lang/crates.io-index)" = "721c204978be2143fab0a84b708c49d79d1f6100b8785610f456043a90708870"

src/librustc_data_structures/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }
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.1.2", package = "rustc-rayon" }
2324
rayon-core = { version = "0.1.2", package = "rustc-rayon-core" }

src/librustc_data_structures/sync.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,59 @@ cfg_if! {
6767
use std::ops::Add;
6868
use std::panic::{resume_unwind, catch_unwind, AssertUnwindSafe};
6969

70+
#[derive(Debug)]
71+
pub struct AtomicCell<T: Copy>(Cell<T>);
72+
73+
impl<T: Copy> AtomicCell<T> {
74+
#[inline]
75+
pub fn new(v: T) -> Self {
76+
AtomicCell(Cell::new(v))
77+
}
78+
}
79+
80+
impl<T: Copy> AtomicCell<T> {
81+
pub fn into_inner(self) -> T {
82+
self.0.into_inner()
83+
}
84+
85+
#[inline]
86+
pub fn load(&self) -> T {
87+
self.0.get()
88+
}
89+
90+
#[inline]
91+
pub fn store(&self, val: T) {
92+
self.0.set(val)
93+
}
94+
95+
pub fn swap(&self, val: T) -> T {
96+
self.0.replace(val)
97+
}
98+
}
99+
100+
impl<T: Copy + PartialEq> AtomicCell<T> {
101+
pub fn compare_exchange(&self,
102+
current: T,
103+
new: T)
104+
-> Result<T, T> {
105+
let read = self.0.get();
106+
if read == current {
107+
self.0.set(new);
108+
Ok(read)
109+
} else {
110+
Err(read)
111+
}
112+
}
113+
}
114+
115+
impl<T: Add<Output=T> + Copy> AtomicCell<T> {
116+
pub fn fetch_add(&self, val: T) -> T {
117+
let old = self.0.get();
118+
self.0.set(old + val);
119+
old
120+
}
121+
}
122+
70123
#[derive(Debug)]
71124
pub struct Atomic<T: Copy>(Cell<T>);
72125

@@ -77,7 +130,7 @@ cfg_if! {
77130
}
78131
}
79132

80-
impl<T: Copy + PartialEq> Atomic<T> {
133+
impl<T: Copy> Atomic<T> {
81134
pub fn into_inner(self) -> T {
82135
self.0.into_inner()
83136
}
@@ -95,7 +148,9 @@ cfg_if! {
95148
pub fn swap(&self, val: T, _: Ordering) -> T {
96149
self.0.replace(val)
97150
}
151+
}
98152

153+
impl<T: Copy + PartialEq> Atomic<T> {
99154
pub fn compare_exchange(&self,
100155
current: T,
101156
new: T,
@@ -271,6 +326,8 @@ cfg_if! {
271326

272327
pub use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicU32, AtomicU64};
273328

329+
pub use crossbeam_utils::atomic::AtomicCell;
330+
274331
pub use std::sync::Arc as Lrc;
275332
pub use std::sync::Weak as Weak;
276333

0 commit comments

Comments
 (0)