Skip to content

Commit efbd372

Browse files
committed
std: Rebuild sync::deque on Arc
This also removes the `&mut self` requirement, using the correct `&self` requirement for concurrent types.
1 parent 44fcf46 commit efbd372

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/libstd/sync/deque.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
// FIXME: all atomic operations in this module use a SeqCst ordering. That is
4949
// probably overkill
5050

51+
use alloc::arc::Arc;
52+
5153
use clone::Clone;
5254
use iter::{range, Iterator};
5355
use kinds::Send;
@@ -58,7 +60,6 @@ use owned::Box;
5860
use ptr::RawPtr;
5961
use ptr;
6062
use slice::ImmutableVector;
61-
use sync::arc::UnsafeArc;
6263
use sync::atomics::{AtomicInt, AtomicPtr, SeqCst};
6364
use unstable::sync::Exclusive;
6465
use rt::heap::{allocate, deallocate};
@@ -87,14 +88,14 @@ struct Deque<T> {
8788
///
8889
/// There may only be one worker per deque.
8990
pub struct Worker<T> {
90-
deque: UnsafeArc<Deque<T>>,
91+
deque: Arc<Deque<T>>,
9192
}
9293

9394
/// The stealing half of the work-stealing deque. Stealers have access to the
9495
/// opposite end of the deque from the worker, and they only have access to the
9596
/// `steal` method.
9697
pub struct Stealer<T> {
97-
deque: UnsafeArc<Deque<T>>,
98+
deque: Arc<Deque<T>>,
9899
}
99100

100101
/// When stealing some data, this is an enumeration of the possible outcomes.
@@ -149,12 +150,13 @@ impl<T: Send> BufferPool<T> {
149150

150151
/// Allocates a new work-stealing deque which will send/receiving memory to
151152
/// and from this buffer pool.
152-
pub fn deque(&mut self) -> (Worker<T>, Stealer<T>) {
153-
let (a, b) = UnsafeArc::new2(Deque::new(self.clone()));
153+
pub fn deque(&self) -> (Worker<T>, Stealer<T>) {
154+
let a = Arc::new(Deque::new(self.clone()));
155+
let b = a.clone();
154156
(Worker { deque: a }, Stealer { deque: b })
155157
}
156158

157-
fn alloc(&mut self, bits: int) -> Box<Buffer<T>> {
159+
fn alloc(&self, bits: int) -> Box<Buffer<T>> {
158160
unsafe {
159161
self.pool.with(|pool| {
160162
match pool.iter().position(|x| x.size() >= (1 << bits)) {
@@ -165,7 +167,7 @@ impl<T: Send> BufferPool<T> {
165167
}
166168
}
167169

168-
fn free(&mut self, buf: Box<Buffer<T>>) {
170+
fn free(&self, buf: Box<Buffer<T>>) {
169171
unsafe {
170172
let mut buf = Some(buf);
171173
self.pool.with(|pool| {
@@ -185,34 +187,34 @@ impl<T: Send> Clone for BufferPool<T> {
185187

186188
impl<T: Send> Worker<T> {
187189
/// Pushes data onto the front of this work queue.
188-
pub fn push(&mut self, t: T) {
189-
unsafe { (*self.deque.get()).push(t) }
190+
pub fn push(&self, t: T) {
191+
unsafe { self.deque.push(t) }
190192
}
191193
/// Pops data off the front of the work queue, returning `None` on an empty
192194
/// queue.
193-
pub fn pop(&mut self) -> Option<T> {
194-
unsafe { (*self.deque.get()).pop() }
195+
pub fn pop(&self) -> Option<T> {
196+
unsafe { self.deque.pop() }
195197
}
196198

197199
/// Gets access to the buffer pool that this worker is attached to. This can
198200
/// be used to create more deques which share the same buffer pool as this
199201
/// deque.
200-
pub fn pool<'a>(&'a mut self) -> &'a mut BufferPool<T> {
201-
unsafe { &mut (*self.deque.get()).pool }
202+
pub fn pool<'a>(&'a self) -> &'a BufferPool<T> {
203+
&self.deque.pool
202204
}
203205
}
204206

205207
impl<T: Send> Stealer<T> {
206208
/// Steals work off the end of the queue (opposite of the worker's end)
207-
pub fn steal(&mut self) -> Stolen<T> {
208-
unsafe { (*self.deque.get()).steal() }
209+
pub fn steal(&self) -> Stolen<T> {
210+
unsafe { self.deque.steal() }
209211
}
210212

211213
/// Gets access to the buffer pool that this stealer is attached to. This
212214
/// can be used to create more deques which share the same buffer pool as
213215
/// this deque.
214-
pub fn pool<'a>(&'a mut self) -> &'a mut BufferPool<T> {
215-
unsafe { &mut (*self.deque.get()).pool }
216+
pub fn pool<'a>(&'a self) -> &'a BufferPool<T> {
217+
&self.deque.pool
216218
}
217219
}
218220

@@ -224,7 +226,7 @@ impl<T: Send> Clone for Stealer<T> {
224226
// personally going to heavily comment what's going on here.
225227

226228
impl<T: Send> Deque<T> {
227-
fn new(mut pool: BufferPool<T>) -> Deque<T> {
229+
fn new(pool: BufferPool<T>) -> Deque<T> {
228230
let buf = pool.alloc(MIN_BITS);
229231
Deque {
230232
bottom: AtomicInt::new(0),
@@ -234,7 +236,7 @@ impl<T: Send> Deque<T> {
234236
}
235237
}
236238

237-
unsafe fn push(&mut self, data: T) {
239+
unsafe fn push(&self, data: T) {
238240
let mut b = self.bottom.load(SeqCst);
239241
let t = self.top.load(SeqCst);
240242
let mut a = self.array.load(SeqCst);
@@ -250,7 +252,7 @@ impl<T: Send> Deque<T> {
250252
self.bottom.store(b + 1, SeqCst);
251253
}
252254

253-
unsafe fn pop(&mut self) -> Option<T> {
255+
unsafe fn pop(&self) -> Option<T> {
254256
let b = self.bottom.load(SeqCst);
255257
let a = self.array.load(SeqCst);
256258
let b = b - 1;
@@ -276,7 +278,7 @@ impl<T: Send> Deque<T> {
276278
}
277279
}
278280

279-
unsafe fn steal(&mut self) -> Stolen<T> {
281+
unsafe fn steal(&self) -> Stolen<T> {
280282
let t = self.top.load(SeqCst);
281283
let old = self.array.load(SeqCst);
282284
let b = self.bottom.load(SeqCst);
@@ -298,7 +300,7 @@ impl<T: Send> Deque<T> {
298300
}
299301
}
300302

301-
unsafe fn maybe_shrink(&mut self, b: int, t: int) {
303+
unsafe fn maybe_shrink(&self, b: int, t: int) {
302304
let a = self.array.load(SeqCst);
303305
if b - t < (*a).size() / K && b - t > (1 << MIN_BITS) {
304306
self.swap_buffer(b, a, (*a).resize(b, t, -1));
@@ -312,7 +314,7 @@ impl<T: Send> Deque<T> {
312314
// after this method has called 'free' on it. The continued usage is simply
313315
// a read followed by a forget, but we must make sure that the memory can
314316
// continue to be read after we flag this buffer for reclamation.
315-
unsafe fn swap_buffer(&mut self, b: int, old: *mut Buffer<T>,
317+
unsafe fn swap_buffer(&self, b: int, old: *mut Buffer<T>,
316318
buf: Buffer<T>) -> *mut Buffer<T> {
317319
let newbuf: *mut Buffer<T> = transmute(box buf);
318320
self.array.store(newbuf, SeqCst);
@@ -373,7 +375,7 @@ impl<T: Send> Buffer<T> {
373375

374376
// Unsafe because this unsafely overwrites possibly uninitialized or
375377
// initialized data.
376-
unsafe fn put(&mut self, i: int, t: T) {
378+
unsafe fn put(&self, i: int, t: T) {
377379
let ptr = self.storage.offset(i & self.mask());
378380
ptr::copy_nonoverlapping_memory(ptr as *mut T, &t as *T, 1);
379381
forget(t);
@@ -382,7 +384,7 @@ impl<T: Send> Buffer<T> {
382384
// Again, unsafe because this has incredibly dubious ownership violations.
383385
// It is assumed that this buffer is immediately dropped.
384386
unsafe fn resize(&self, b: int, t: int, delta: int) -> Buffer<T> {
385-
let mut buf = Buffer::new(self.log_size + delta);
387+
let buf = Buffer::new(self.log_size + delta);
386388
for i in range(t, b) {
387389
buf.put(i, self.get(i));
388390
}

0 commit comments

Comments
 (0)