Skip to content

Commit 73729e9

Browse files
committed
std: Move comm primitives away from UnsafeArc
They currently still use `&mut self`, this migration was aimed towards moving from UnsafeArc<T> to Arc<Unsafe<T>>
1 parent d49aef7 commit 73729e9

File tree

4 files changed

+39
-30
lines changed

4 files changed

+39
-30
lines changed

src/libstd/comm/mod.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@
271271
// And now that you've seen all the races that I found and attempted to fix,
272272
// here's the code for you to find some more!
273273

274+
use alloc::arc::Arc;
275+
274276
use cell::Cell;
275277
use clone::Clone;
276278
use iter::Iterator;
@@ -283,7 +285,6 @@ use owned::Box;
283285
use result::{Ok, Err, Result};
284286
use rt::local::Local;
285287
use rt::task::{Task, BlockedTask};
286-
use sync::arc::UnsafeArc;
287288
use ty::Unsafe;
288289

289290
pub use comm::select::{Select, Handle};
@@ -352,7 +353,7 @@ pub struct Sender<T> {
352353
/// The sending-half of Rust's synchronous channel type. This half can only be
353354
/// owned by one task, but it can be cloned to send to other tasks.
354355
pub struct SyncSender<T> {
355-
inner: UnsafeArc<sync::Packet<T>>,
356+
inner: Arc<Unsafe<sync::Packet<T>>>,
356357
// can't share in an arc
357358
marker: marker::NoShare,
358359
}
@@ -386,10 +387,10 @@ pub enum TrySendError<T> {
386387
}
387388

388389
enum Flavor<T> {
389-
Oneshot(UnsafeArc<oneshot::Packet<T>>),
390-
Stream(UnsafeArc<stream::Packet<T>>),
391-
Shared(UnsafeArc<shared::Packet<T>>),
392-
Sync(UnsafeArc<sync::Packet<T>>),
390+
Oneshot(Arc<Unsafe<oneshot::Packet<T>>>),
391+
Stream(Arc<Unsafe<stream::Packet<T>>>),
392+
Shared(Arc<Unsafe<shared::Packet<T>>>),
393+
Sync(Arc<Unsafe<sync::Packet<T>>>),
393394
}
394395

395396
#[doc(hidden)]
@@ -435,8 +436,8 @@ impl<T> UnsafeFlavor<T> for Receiver<T> {
435436
/// println!("{}", rx.recv());
436437
/// ```
437438
pub fn channel<T: Send>() -> (Sender<T>, Receiver<T>) {
438-
let (a, b) = UnsafeArc::new2(oneshot::Packet::new());
439-
(Sender::new(Oneshot(b)), Receiver::new(Oneshot(a)))
439+
let a = Arc::new(Unsafe::new(oneshot::Packet::new()));
440+
(Sender::new(Oneshot(a.clone())), Receiver::new(Oneshot(a)))
440441
}
441442

442443
/// Creates a new synchronous, bounded channel.
@@ -471,8 +472,8 @@ pub fn channel<T: Send>() -> (Sender<T>, Receiver<T>) {
471472
/// assert_eq!(rx.recv(), 2);
472473
/// ```
473474
pub fn sync_channel<T: Send>(bound: uint) -> (SyncSender<T>, Receiver<T>) {
474-
let (a, b) = UnsafeArc::new2(sync::Packet::new(bound));
475-
(SyncSender::new(a), Receiver::new(Sync(b)))
475+
let a = Arc::new(Unsafe::new(sync::Packet::new(bound)));
476+
(SyncSender::new(a.clone()), Receiver::new(Sync(a)))
476477
}
477478

478479
////////////////////////////////////////////////////////////////////////////////
@@ -557,13 +558,13 @@ impl<T: Send> Sender<T> {
557558

558559
let (new_inner, ret) = match *unsafe { self.inner() } {
559560
Oneshot(ref p) => {
560-
let p = p.get();
561561
unsafe {
562+
let p = p.get();
562563
if !(*p).sent() {
563564
return (*p).send(t);
564565
} else {
565-
let (a, b) = UnsafeArc::new2(stream::Packet::new());
566-
match (*p).upgrade(Receiver::new(Stream(b))) {
566+
let a = Arc::new(Unsafe::new(stream::Packet::new()));
567+
match (*p).upgrade(Receiver::new(Stream(a.clone()))) {
567568
oneshot::UpSuccess => {
568569
let ret = (*a.get()).send(t);
569570
(a, ret)
@@ -598,17 +599,21 @@ impl<T: Send> Clone for Sender<T> {
598599
fn clone(&self) -> Sender<T> {
599600
let (packet, sleeper) = match *unsafe { self.inner() } {
600601
Oneshot(ref p) => {
601-
let (a, b) = UnsafeArc::new2(shared::Packet::new());
602-
match unsafe { (*p.get()).upgrade(Receiver::new(Shared(a))) } {
603-
oneshot::UpSuccess | oneshot::UpDisconnected => (b, None),
604-
oneshot::UpWoke(task) => (b, Some(task))
602+
let a = Arc::new(Unsafe::new(shared::Packet::new()));
603+
match unsafe {
604+
(*p.get()).upgrade(Receiver::new(Shared(a.clone())))
605+
} {
606+
oneshot::UpSuccess | oneshot::UpDisconnected => (a, None),
607+
oneshot::UpWoke(task) => (a, Some(task))
605608
}
606609
}
607610
Stream(ref p) => {
608-
let (a, b) = UnsafeArc::new2(shared::Packet::new());
609-
match unsafe { (*p.get()).upgrade(Receiver::new(Shared(a))) } {
610-
stream::UpSuccess | stream::UpDisconnected => (b, None),
611-
stream::UpWoke(task) => (b, Some(task)),
611+
let a = Arc::new(Unsafe::new(shared::Packet::new()));
612+
match unsafe {
613+
(*p.get()).upgrade(Receiver::new(Shared(a.clone())))
614+
} {
615+
stream::UpSuccess | stream::UpDisconnected => (a, None),
616+
stream::UpWoke(task) => (a, Some(task)),
612617
}
613618
}
614619
Shared(ref p) => {
@@ -645,7 +650,7 @@ impl<T: Send> Drop for Sender<T> {
645650
////////////////////////////////////////////////////////////////////////////////
646651

647652
impl<T: Send> SyncSender<T> {
648-
fn new(inner: UnsafeArc<sync::Packet<T>>) -> SyncSender<T> {
653+
fn new(inner: Arc<Unsafe<sync::Packet<T>>>) -> SyncSender<T> {
649654
SyncSender { inner: inner, marker: marker::NoShare }
650655
}
651656

src/libstd/comm/oneshot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/// this type is to have one and exactly one allocation when the chan/port pair
1616
/// is created.
1717
///
18-
/// Another possible optimization would be to not use an UnsafeArc box because
18+
/// Another possible optimization would be to not use an Arc box because
1919
/// in theory we know when the shared packet can be deallocated (no real need
2020
/// for the atomic reference counting), but I was having trouble how to destroy
2121
/// the data early in a drop of a Port.

src/libstd/sync/mpsc_queue.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,10 @@ impl<T: Send> Drop for Queue<T> {
158158
mod tests {
159159
use prelude::*;
160160

161+
use alloc::arc::Arc;
162+
161163
use native;
162164
use super::{Queue, Data, Empty, Inconsistent};
163-
use sync::arc::UnsafeArc;
164165

165166
#[test]
166167
fn test_full() {
@@ -179,22 +180,22 @@ mod tests {
179180
Inconsistent | Data(..) => fail!()
180181
}
181182
let (tx, rx) = channel();
182-
let q = UnsafeArc::new(q);
183+
let q = Arc::new(q);
183184

184185
for _ in range(0, nthreads) {
185186
let tx = tx.clone();
186187
let q = q.clone();
187188
native::task::spawn(proc() {
188189
for i in range(0, nmsgs) {
189-
unsafe { (*q.get()).push(i); }
190+
q.push(i);
190191
}
191192
tx.send(());
192193
});
193194
}
194195

195196
let mut i = 0u;
196197
while i < nthreads * nmsgs {
197-
match unsafe { (*q.get()).pop() } {
198+
match q.pop() {
198199
Empty | Inconsistent => {},
199200
Data(_) => { i += 1 }
200201
}

src/libstd/sync/spsc_queue.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct Node<T> {
5252
}
5353

5454
/// The single-producer single-consumer queue. This structure is not cloneable,
55-
/// but it can be safely shared in an UnsafeArc if it is guaranteed that there
55+
/// but it can be safely shared in an Arc if it is guaranteed that there
5656
/// is only one popper and one pusher touching the queue at any one point in
5757
/// time.
5858
pub struct Queue<T> {
@@ -227,9 +227,11 @@ impl<T: Send> Drop for Queue<T> {
227227
#[cfg(test)]
228228
mod test {
229229
use prelude::*;
230+
231+
use alloc::arc::Arc;
230232
use native;
233+
231234
use super::Queue;
232-
use sync::arc::UnsafeArc;
233235

234236
#[test]
235237
fn smoke() {
@@ -274,7 +276,8 @@ mod test {
274276
stress_bound(1);
275277

276278
fn stress_bound(bound: uint) {
277-
let (a, b) = UnsafeArc::new2(Queue::new(bound));
279+
let a = Arc::new(Queue::new(bound));
280+
let b = a.clone();
278281
let (tx, rx) = channel();
279282
native::task::spawn(proc() {
280283
for _ in range(0, 100000) {

0 commit comments

Comments
 (0)