Skip to content

Commit 8818693

Browse files
committed
Relax Arc bounds don't require Sync+Send
Besides the above making sense, it'll also allow us to make `RacyCell` private and use UnsafeCell instead.
1 parent 7df17a2 commit 8818693

File tree

7 files changed

+37
-36
lines changed

7 files changed

+37
-36
lines changed

src/liballoc/arc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ struct ArcInner<T> {
139139
data: T,
140140
}
141141

142-
impl<T: Sync + Send> Arc<T> {
142+
impl<T> Arc<T> {
143143
/// Constructs a new `Arc<T>`.
144144
///
145145
/// # Examples

src/libstd/comm/mod.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,23 +1027,18 @@ impl<T: Send> Drop for Receiver<T> {
10271027

10281028
/// A version of `UnsafeCell` intended for use in concurrent data
10291029
/// structures (for example, you might put it in an `Arc`).
1030-
pub struct RacyCell<T>(pub UnsafeCell<T>);
1030+
struct RacyCell<T>(pub UnsafeCell<T>);
10311031

10321032
impl<T> RacyCell<T> {
1033-
/// DOX
1034-
pub fn new(value: T) -> RacyCell<T> {
1033+
1034+
fn new(value: T) -> RacyCell<T> {
10351035
RacyCell(UnsafeCell { value: value })
10361036
}
10371037

1038-
/// DOX
1039-
pub unsafe fn get(&self) -> *mut T {
1038+
unsafe fn get(&self) -> *mut T {
10401039
self.0.get()
10411040
}
10421041

1043-
/// DOX
1044-
pub unsafe fn into_inner(self) -> T {
1045-
self.0.into_inner()
1046-
}
10471042
}
10481043

10491044
unsafe impl<T:Send> Send for RacyCell<T> { }

src/libstd/sync/mutex.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use prelude::*;
1212

13-
use comm::RacyCell;
1413
use cell::UnsafeCell;
1514
use kinds::marker;
1615
use sync::{poison, AsMutexGuard};
@@ -71,7 +70,7 @@ pub struct Mutex<T> {
7170
// time, so to ensure that the native mutex is used correctly we box the
7271
// inner lock to give it a constant address.
7372
inner: Box<StaticMutex>,
74-
data: RacyCell<T>,
73+
data: UnsafeCell<T>,
7574
}
7675

7776
unsafe impl<T:Send> Send for Mutex<T> { }
@@ -101,7 +100,7 @@ unsafe impl<T:Send> Sync for Mutex<T> { }
101100
/// ```
102101
pub struct StaticMutex {
103102
lock: sys::Mutex,
104-
poison: RacyCell<poison::Flag>,
103+
poison: UnsafeCell<poison::Flag>,
105104
}
106105

107106
unsafe impl Sync for StaticMutex {}
@@ -132,15 +131,15 @@ pub struct StaticMutexGuard {
132131
/// other mutex constants.
133132
pub const MUTEX_INIT: StaticMutex = StaticMutex {
134133
lock: sys::MUTEX_INIT,
135-
poison: RacyCell(UnsafeCell { value: poison::Flag { failed: false } }),
134+
poison: UnsafeCell { value: poison::Flag { failed: false } },
136135
};
137136

138137
impl<T: Send> Mutex<T> {
139138
/// Creates a new mutex in an unlocked state ready for use.
140139
pub fn new(t: T) -> Mutex<T> {
141140
Mutex {
142141
inner: box MUTEX_INIT,
143-
data: RacyCell::new(t),
142+
data: UnsafeCell::new(t),
144143
}
145144
}
146145

src/libstd/sys/unix/mutex.rs

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

11-
use comm::RacyCell;
1211
use cell::UnsafeCell;
1312
use kinds::Sync;
1413
use sys::sync as ffi;
1514
use sys_common::mutex;
1615

17-
pub struct Mutex { inner: RacyCell<ffi::pthread_mutex_t> }
16+
pub struct Mutex { inner: UnsafeCell<ffi::pthread_mutex_t> }
1817

1918
#[inline]
2019
pub unsafe fn raw(m: &Mutex) -> *mut ffi::pthread_mutex_t {
2120
m.inner.get()
2221
}
2322

2423
pub const MUTEX_INIT: Mutex = Mutex {
25-
inner: RacyCell(UnsafeCell { value: ffi::PTHREAD_MUTEX_INITIALIZER }),
24+
inner: UnsafeCell { value: ffi::PTHREAD_MUTEX_INITIALIZER },
2625
};
2726

2827
unsafe impl Sync for Mutex {}

src/libstd/thread.rs

Lines changed: 13 additions & 6 deletions
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 comm::RacyCell;
130+
use cell::UnsafeCell;
131131
use clone::Clone;
132132
use kinds::{Send, Sync};
133133
use ops::{Drop, FnOnce};
@@ -211,8 +211,8 @@ impl Builder {
211211
}
212212

213213
fn spawn_inner<T: Send>(self, f: Thunk<(), T>) -> JoinGuard<T> {
214-
let my_packet = Arc::new(RacyCell::new(None));
215-
let their_packet = my_packet.clone();
214+
let my_packet = Packet(Arc::new(UnsafeCell::new(None)));
215+
let their_packet = Packet(my_packet.0.clone());
216216

217217
let Builder { name, stack_size, stdout, stderr } = self;
218218

@@ -266,7 +266,7 @@ impl Builder {
266266
}
267267
};
268268
unsafe {
269-
*their_packet.get() = Some(match (output, try_result) {
269+
*their_packet.0.get() = Some(match (output, try_result) {
270270
(Some(data), Ok(_)) => Ok(data),
271271
(None, Err(cause)) => Err(cause),
272272
_ => unreachable!()
@@ -383,6 +383,11 @@ impl thread_info::NewThread for Thread {
383383
/// A thread that completes without panicking is considered to exit successfully.
384384
pub type Result<T> = ::result::Result<T, Box<Any + Send>>;
385385

386+
struct Packet<T>(Arc<UnsafeCell<Option<Result<T>>>>);
387+
388+
unsafe impl<T:'static+Send> Send for Packet<T> {}
389+
unsafe impl<T> Sync for Packet<T> {}
390+
386391
#[must_use]
387392
/// An RAII-style guard that will block until thread termination when dropped.
388393
///
@@ -391,9 +396,11 @@ pub struct JoinGuard<T> {
391396
native: imp::rust_thread,
392397
thread: Thread,
393398
joined: bool,
394-
packet: Arc<RacyCell<Option<Result<T>>>>,
399+
packet: Packet<T>,
395400
}
396401

402+
unsafe impl<T: Send> Sync for JoinGuard<T> {}
403+
397404
impl<T: Send> JoinGuard<T> {
398405
/// Extract a handle to the thread this guard will join on.
399406
pub fn thread(&self) -> &Thread {
@@ -410,7 +417,7 @@ impl<T: Send> JoinGuard<T> {
410417
unsafe { imp::join(self.native) };
411418
self.joined = true;
412419
unsafe {
413-
(*self.packet.get()).take().unwrap()
420+
(*self.packet.0.get()).take().unwrap()
414421
}
415422
}
416423

src/libstd/thread_local/scoped.rs

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

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

201201
#[doc(hidden)]
202-
pub struct KeyInner<T> { pub inner: RacyCell<*mut T> }
202+
pub struct KeyInner<T> { pub inner: UnsafeCell<*mut T> }
203203

204204
unsafe impl<T> ::kinds::Sync for KeyInner<T> { }
205205

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,44 @@
99
// except according to those terms.
1010

1111
use std::kinds::marker;
12-
use std::comm::RacyCell;
1312
use std::cell::UnsafeCell;
1413

1514
struct MyUnsafe<T> {
16-
value: RacyCell<T>
15+
value: UnsafeCell<T>
1716
}
1817

1918
impl<T> MyUnsafe<T> {
2019
fn forbidden(&self) {}
2120
}
2221

22+
impl<T: Send> Sync for MyUnsafe<T> {}
23+
2324
enum UnsafeEnum<T> {
2425
VariantSafe,
25-
VariantUnsafe(RacyCell<T>)
26+
VariantUnsafe(UnsafeCell<T>)
2627
}
2728

29+
impl<T: Send> Sync for UnsafeEnum<T> {}
30+
2831
static STATIC1: UnsafeEnum<int> = UnsafeEnum::VariantSafe;
2932

30-
static STATIC2: RacyCell<int> = RacyCell(UnsafeCell { value: 1 });
31-
const CONST: RacyCell<int> = RacyCell(UnsafeCell { value: 1 });
33+
static STATIC2: UnsafeCell<int> = UnsafeCell { value: 1 };
34+
const CONST: UnsafeCell<int> = UnsafeCell { value: 1 };
3235
static STATIC3: MyUnsafe<int> = MyUnsafe{value: CONST};
3336

34-
static STATIC4: &'static RacyCell<int> = &STATIC2;
37+
static STATIC4: &'static UnsafeCell<int> = &STATIC2;
3538

3639
struct Wrap<T> {
3740
value: T
3841
}
3942

4043
unsafe impl<T: Send> Sync for Wrap<T> {}
4144

42-
static UNSAFE: RacyCell<int> = RacyCell(UnsafeCell{value: 1});
43-
static WRAPPED_UNSAFE: Wrap<&'static RacyCell<int>> = Wrap { value: &UNSAFE };
45+
static UNSAFE: UnsafeCell<int> = UnsafeCell{value: 1};
46+
static WRAPPED_UNSAFE: Wrap<&'static UnsafeCell<int>> = Wrap { value: &UNSAFE };
4447

4548
fn main() {
4649
let a = &STATIC1;
4750

4851
STATIC3.forbidden()
4952
}
50-
51-

0 commit comments

Comments
 (0)