Skip to content

Commit e2116c8

Browse files
committed
Move RacyCell to std::comm
RacyCell is not exactly what we'd like as a final implementation for this. Therefore, we're moving it under `std::comm` and also making it private.
1 parent f436f9c commit e2116c8

File tree

8 files changed

+61
-47
lines changed

8 files changed

+61
-47
lines changed

src/libcore/atomic.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,43 @@
1414

1515
pub use self::Ordering::*;
1616

17+
use kinds::Sync;
18+
1719
use intrinsics;
18-
use cell::{UnsafeCell, RacyCell};
20+
use cell::UnsafeCell;
1921

2022
/// A boolean type which can be safely shared between threads.
2123
#[stable]
2224
pub struct AtomicBool {
23-
v: RacyCell<uint>,
25+
v: UnsafeCell<uint>,
2426
}
2527

28+
unsafe impl Sync for AtomicBool {}
29+
2630
/// A signed integer type which can be safely shared between threads.
2731
#[stable]
2832
pub struct AtomicInt {
29-
v: RacyCell<int>,
33+
v: UnsafeCell<int>,
3034
}
3135

36+
unsafe impl Sync for AtomicInt {}
37+
3238
/// An unsigned integer type which can be safely shared between threads.
3339
#[stable]
3440
pub struct AtomicUint {
35-
v: RacyCell<uint>,
41+
v: UnsafeCell<uint>,
3642
}
3743

44+
unsafe impl Sync for AtomicUint {}
45+
3846
/// A raw pointer type which can be safely shared between threads.
3947
#[stable]
4048
pub struct AtomicPtr<T> {
41-
p: RacyCell<uint>,
49+
p: UnsafeCell<uint>,
4250
}
4351

52+
unsafe impl<T> Sync for AtomicPtr<T> {}
53+
4454
/// Atomic memory orderings
4555
///
4656
/// Memory orderings limit the ways that both the compiler and CPU may reorder
@@ -80,15 +90,15 @@ pub enum Ordering {
8090
/// An `AtomicBool` initialized to `false`.
8191
#[unstable = "may be renamed, pending conventions for static initalizers"]
8292
pub const INIT_ATOMIC_BOOL: AtomicBool =
83-
AtomicBool { v: RacyCell(UnsafeCell { value: 0 }) };
93+
AtomicBool { v: UnsafeCell { value: 0 } };
8494
/// An `AtomicInt` initialized to `0`.
8595
#[unstable = "may be renamed, pending conventions for static initalizers"]
8696
pub const INIT_ATOMIC_INT: AtomicInt =
87-
AtomicInt { v: RacyCell(UnsafeCell { value: 0 }) };
97+
AtomicInt { v: UnsafeCell { value: 0 } };
8898
/// An `AtomicUint` initialized to `0`.
8999
#[unstable = "may be renamed, pending conventions for static initalizers"]
90100
pub const INIT_ATOMIC_UINT: AtomicUint =
91-
AtomicUint { v: RacyCell(UnsafeCell { value: 0 }) };
101+
AtomicUint { v: UnsafeCell { value: 0, } };
92102

93103
// NB: Needs to be -1 (0b11111111...) to make fetch_nand work correctly
94104
const UINT_TRUE: uint = -1;
@@ -108,7 +118,7 @@ impl AtomicBool {
108118
#[stable]
109119
pub fn new(v: bool) -> AtomicBool {
110120
let val = if v { UINT_TRUE } else { 0 };
111-
AtomicBool { v: RacyCell::new(val) }
121+
AtomicBool { v: UnsafeCell::new(val) }
112122
}
113123

114124
/// Loads a value from the bool.
@@ -348,7 +358,7 @@ impl AtomicInt {
348358
#[inline]
349359
#[stable]
350360
pub fn new(v: int) -> AtomicInt {
351-
AtomicInt {v: RacyCell::new(v)}
361+
AtomicInt {v: UnsafeCell::new(v)}
352362
}
353363

354364
/// Loads a value from the int.
@@ -534,7 +544,7 @@ impl AtomicUint {
534544
#[inline]
535545
#[stable]
536546
pub fn new(v: uint) -> AtomicUint {
537-
AtomicUint { v: RacyCell::new(v) }
547+
AtomicUint { v: UnsafeCell::new(v) }
538548
}
539549

540550
/// Loads a value from the uint.
@@ -721,7 +731,7 @@ impl<T> AtomicPtr<T> {
721731
#[inline]
722732
#[stable]
723733
pub fn new(p: *mut T) -> AtomicPtr<T> {
724-
AtomicPtr { p: RacyCell::new(p as uint) }
734+
AtomicPtr { p: UnsafeCell::new(p as uint) }
725735
}
726736

727737
/// Loads a value from the pointer.

src/libcore/cell.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
use clone::Clone;
159159
use cmp::PartialEq;
160160
use default::Default;
161-
use kinds::{marker, Copy, Send, Sync};
161+
use kinds::{marker, Copy};
162162
use ops::{Deref, DerefMut, Drop};
163163
use option::Option;
164164
use option::Option::{None, Some};
@@ -555,28 +555,3 @@ impl<T> UnsafeCell<T> {
555555
#[deprecated = "renamed to into_inner()"]
556556
pub unsafe fn unwrap(self) -> T { self.into_inner() }
557557
}
558-
559-
/// A version of `UnsafeCell` intended for use in concurrent data
560-
/// structures (for example, you might put it in an `Arc`).
561-
pub struct RacyCell<T>(pub UnsafeCell<T>);
562-
563-
impl<T> RacyCell<T> {
564-
/// DOX
565-
pub fn new(value: T) -> RacyCell<T> {
566-
RacyCell(UnsafeCell { value: value })
567-
}
568-
569-
/// DOX
570-
pub unsafe fn get(&self) -> *mut T {
571-
self.0.get()
572-
}
573-
574-
/// DOX
575-
pub unsafe fn into_inner(self) -> T {
576-
self.0.into_inner()
577-
}
578-
}
579-
580-
unsafe impl<T:Send> Send for RacyCell<T> { }
581-
582-
unsafe impl<T> Sync for RacyCell<T> { } // Oh dear

src/libstd/comm/mod.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,10 @@ pub use self::TrySendError::*;
319319
use self::Flavor::*;
320320

321321
use alloc::arc::Arc;
322+
use core::kinds;
322323
use core::kinds::marker;
323324
use core::mem;
324-
use core::cell::{UnsafeCell, RacyCell};
325+
use core::cell::UnsafeCell;
325326

326327
pub use self::select::{Select, Handle};
327328
use self::select::StartResult;
@@ -1024,6 +1025,32 @@ impl<T: Send> Drop for Receiver<T> {
10241025
}
10251026
}
10261027

1028+
/// A version of `UnsafeCell` intended for use in concurrent data
1029+
/// structures (for example, you might put it in an `Arc`).
1030+
pub struct RacyCell<T>(pub UnsafeCell<T>);
1031+
1032+
impl<T> RacyCell<T> {
1033+
/// DOX
1034+
pub fn new(value: T) -> RacyCell<T> {
1035+
RacyCell(UnsafeCell { value: value })
1036+
}
1037+
1038+
/// DOX
1039+
pub unsafe fn get(&self) -> *mut T {
1040+
self.0.get()
1041+
}
1042+
1043+
/// DOX
1044+
pub unsafe fn into_inner(self) -> T {
1045+
self.0.into_inner()
1046+
}
1047+
}
1048+
1049+
unsafe impl<T:Send> Send for RacyCell<T> { }
1050+
1051+
unsafe impl<T> kinds::Sync for RacyCell<T> { } // Oh dear
1052+
1053+
10271054
#[cfg(test)]
10281055
mod test {
10291056
use prelude::*;

src/libstd/sync/mutex.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
use prelude::*;
1212

13-
use cell::{UnsafeCell, RacyCell};
14-
use kinds::{marker, Sync};
13+
use comm::RacyCell;
14+
use cell::UnsafeCell;
15+
use kinds::marker;
1516
use sync::{poison, AsMutexGuard};
1617
use sys_common::mutex as sys;
1718

src/libstd/sys/unix/mutex.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use comm::RacyCell;
12+
use cell::UnsafeCell;
1113
use kinds::Sync;
12-
use cell::{UnsafeCell, RacyCell};
1314
use sys::sync as ffi;
1415
use sys_common::mutex;
1516

src/libstd/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
use any::Any;
128128
use borrow::IntoCow;
129129
use boxed::Box;
130-
use cell::RacyCell;
130+
use comm::RacyCell;
131131
use clone::Clone;
132132
use kinds::{Send, Sync};
133133
use ops::{Drop, FnOnce};

src/libstd/thread_local/scoped.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,10 @@ impl<T> Key<T> {
196196

197197
#[cfg(not(any(windows, target_os = "android", target_os = "ios")))]
198198
mod imp {
199-
use std::cell::UnsafeCell;
199+
use std::comm::RacyCell;
200200

201-
// SNAP c9f6d69 switch to `Cell`
202201
#[doc(hidden)]
203-
pub struct KeyInner<T> { pub inner: UnsafeCell<*mut T> }
202+
pub struct KeyInner<T> { pub inner: RacyCell<*mut T> }
204203

205204
unsafe impl<T> ::kinds::Sync for KeyInner<T> { }
206205

src/test/run-pass/issue-17718-static-unsafe-interior.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
// except according to those terms.
1010

1111
use std::kinds::marker;
12-
use std::cell::{UnsafeCell, RacyCell};
12+
use std::comm::RacyCell;
13+
use std::cell::UnsafeCell;
1314

1415
struct MyUnsafe<T> {
1516
value: RacyCell<T>

0 commit comments

Comments
 (0)