Skip to content

Commit 7115263

Browse files
committed
Add an AtomicCell abstraction
1 parent 31d28f4 commit 7115263

File tree

3 files changed

+79
-22
lines changed

3 files changed

+79
-22
lines changed

Cargo.lock

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ dependencies = [
239239
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
240240
"core-foundation 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
241241
"crates-io 0.24.0",
242-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
242+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
243243
"crypto-hash 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
244244
"curl 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)",
245245
"curl-sys 0.4.15 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -568,13 +568,10 @@ dependencies = [
568568

569569
[[package]]
570570
name = "crossbeam-channel"
571-
version = "0.3.4"
571+
version = "0.3.8"
572572
source = "registry+https://github.com/rust-lang/crates.io-index"
573573
dependencies = [
574-
"crossbeam-epoch 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
575-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
576-
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
577-
"rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
574+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
578575
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
579576
]
580577

@@ -592,8 +589,8 @@ name = "crossbeam-deque"
592589
version = "0.6.3"
593590
source = "registry+https://github.com/rust-lang/crates.io-index"
594591
dependencies = [
595-
"crossbeam-epoch 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
596-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
592+
"crossbeam-epoch 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
593+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
597594
]
598595

599596
[[package]]
@@ -612,12 +609,12 @@ dependencies = [
612609

613610
[[package]]
614611
name = "crossbeam-epoch"
615-
version = "0.7.0"
612+
version = "0.7.1"
616613
source = "registry+https://github.com/rust-lang/crates.io-index"
617614
dependencies = [
618615
"arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
619616
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
620-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
617+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
621618
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
622619
"memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
623620
"scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -633,10 +630,11 @@ dependencies = [
633630

634631
[[package]]
635632
name = "crossbeam-utils"
636-
version = "0.6.2"
633+
version = "0.6.5"
637634
source = "registry+https://github.com/rust-lang/crates.io-index"
638635
dependencies = [
639636
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
637+
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
640638
]
641639

642640
[[package]]
@@ -1122,7 +1120,7 @@ name = "ignore"
11221120
version = "0.4.6"
11231121
source = "registry+https://github.com/rust-lang/crates.io-index"
11241122
dependencies = [
1125-
"crossbeam-channel 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
1123+
"crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
11261124
"globset 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
11271125
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
11281126
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2235,7 +2233,7 @@ dependencies = [
22352233
"cargo 0.36.0",
22362234
"cargo_metadata 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
22372235
"clippy_lints 0.0.212",
2238-
"crossbeam-channel 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
2236+
"crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
22392237
"difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
22402238
"env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
22412239
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2672,6 +2670,7 @@ name = "rustc_data_structures"
26722670
version = "0.0.0"
26732671
dependencies = [
26742672
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
2673+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
26752674
"ena 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
26762675
"graphviz 0.0.0",
26772676
"jobserver 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3585,7 +3584,7 @@ name = "tokio-executor"
35853584
version = "0.1.6"
35863585
source = "registry+https://github.com/rust-lang/crates.io-index"
35873586
dependencies = [
3588-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
3587+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
35893588
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
35903589
]
35913590

@@ -3629,7 +3628,7 @@ name = "tokio-reactor"
36293628
version = "0.1.8"
36303629
source = "registry+https://github.com/rust-lang/crates.io-index"
36313630
dependencies = [
3632-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
3631+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
36333632
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
36343633
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
36353634
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3675,9 +3674,9 @@ name = "tokio-threadpool"
36753674
version = "0.1.10"
36763675
source = "registry+https://github.com/rust-lang/crates.io-index"
36773676
dependencies = [
3678-
"crossbeam-channel 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
3677+
"crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
36793678
"crossbeam-deque 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
3680-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
3679+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
36813680
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
36823681
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
36833682
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3690,7 +3689,7 @@ name = "tokio-timer"
36903689
version = "0.2.8"
36913690
source = "registry+https://github.com/rust-lang/crates.io-index"
36923691
dependencies = [
3693-
"crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
3692+
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
36943693
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
36953694
"slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
36963695
"tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -4035,13 +4034,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
40354034
"checksum core-foundation-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b"
40364035
"checksum crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb"
40374036
"checksum crc32fast 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e91d5240c6975ef33aeb5f148f35275c25eda8e8a5f95abe421978b05b8bf192"
4038-
"checksum crossbeam-channel 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5b2a9ea8f77c7f9efd317a8a5645f515d903a2d86ee14d2337a5facd1bd52c12"
4037+
"checksum crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0f0ed1a4de2235cabda8558ff5840bffb97fcb64c97827f354a451307df5f72b"
40394038
"checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3"
40404039
"checksum crossbeam-deque 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "05e44b8cf3e1a625844d1750e1f7820da46044ff6d28f4d43e455ba3e5bb2c13"
40414040
"checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150"
4042-
"checksum crossbeam-epoch 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f10a4f8f409aaac4b16a5474fb233624238fcdeefb9ba50d5ea059aab63ba31c"
4041+
"checksum crossbeam-epoch 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "04c9e3102cc2d69cd681412141b390abd55a362afc1540965dad0ad4d34280b4"
40434042
"checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9"
4044-
"checksum crossbeam-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e07fc155212827475223f0bcfae57e945e694fc90950ddf3f6695bbfd5555c72"
4043+
"checksum crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f8306fcef4a7b563b76b7dd949ca48f52bc1141aa067d2ea09565f3e2652aa5c"
40454044
"checksum crypto-hash 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "09de9ee0fc255ace04c7fa0763c9395a945c37c8292bb554f8d48361d1dcf1b4"
40464045
"checksum curl 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)" = "c7c9d851c825e0c033979d4516c9173bc19a78a96eb4d6ae51d4045440eafa16"
40474046
"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)