48
48
// FIXME: all atomic operations in this module use a SeqCst ordering. That is
49
49
// probably overkill
50
50
51
+ use alloc:: arc:: Arc ;
52
+
51
53
use clone:: Clone ;
52
54
use iter:: { range, Iterator } ;
53
55
use kinds:: Send ;
@@ -58,7 +60,6 @@ use owned::Box;
58
60
use ptr:: RawPtr ;
59
61
use ptr;
60
62
use slice:: ImmutableVector ;
61
- use sync:: arc:: UnsafeArc ;
62
63
use sync:: atomics:: { AtomicInt , AtomicPtr , SeqCst } ;
63
64
use unstable:: sync:: Exclusive ;
64
65
use rt:: heap:: { allocate, deallocate} ;
@@ -87,14 +88,14 @@ struct Deque<T> {
87
88
///
88
89
/// There may only be one worker per deque.
89
90
pub struct Worker < T > {
90
- deque : UnsafeArc < Deque < T > > ,
91
+ deque : Arc < Deque < T > > ,
91
92
}
92
93
93
94
/// The stealing half of the work-stealing deque. Stealers have access to the
94
95
/// opposite end of the deque from the worker, and they only have access to the
95
96
/// `steal` method.
96
97
pub struct Stealer < T > {
97
- deque : UnsafeArc < Deque < T > > ,
98
+ deque : Arc < Deque < T > > ,
98
99
}
99
100
100
101
/// When stealing some data, this is an enumeration of the possible outcomes.
@@ -149,12 +150,13 @@ impl<T: Send> BufferPool<T> {
149
150
150
151
/// Allocates a new work-stealing deque which will send/receiving memory to
151
152
/// 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 ( ) ;
154
156
( Worker { deque : a } , Stealer { deque : b } )
155
157
}
156
158
157
- fn alloc ( & mut self , bits : int ) -> Box < Buffer < T > > {
159
+ fn alloc ( & self , bits : int ) -> Box < Buffer < T > > {
158
160
unsafe {
159
161
self . pool . with ( |pool| {
160
162
match pool. iter ( ) . position ( |x| x. size ( ) >= ( 1 << bits) ) {
@@ -165,7 +167,7 @@ impl<T: Send> BufferPool<T> {
165
167
}
166
168
}
167
169
168
- fn free ( & mut self , buf : Box < Buffer < T > > ) {
170
+ fn free ( & self , buf : Box < Buffer < T > > ) {
169
171
unsafe {
170
172
let mut buf = Some ( buf) ;
171
173
self . pool . with ( |pool| {
@@ -185,34 +187,34 @@ impl<T: Send> Clone for BufferPool<T> {
185
187
186
188
impl < T : Send > Worker < T > {
187
189
/// 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) }
190
192
}
191
193
/// Pops data off the front of the work queue, returning `None` on an empty
192
194
/// 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 ( ) }
195
197
}
196
198
197
199
/// Gets access to the buffer pool that this worker is attached to. This can
198
200
/// be used to create more deques which share the same buffer pool as this
199
201
/// 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
202
204
}
203
205
}
204
206
205
207
impl < T : Send > Stealer < T > {
206
208
/// 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 ( ) }
209
211
}
210
212
211
213
/// Gets access to the buffer pool that this stealer is attached to. This
212
214
/// can be used to create more deques which share the same buffer pool as
213
215
/// 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
216
218
}
217
219
}
218
220
@@ -224,7 +226,7 @@ impl<T: Send> Clone for Stealer<T> {
224
226
// personally going to heavily comment what's going on here.
225
227
226
228
impl < T : Send > Deque < T > {
227
- fn new ( mut pool : BufferPool < T > ) -> Deque < T > {
229
+ fn new ( pool : BufferPool < T > ) -> Deque < T > {
228
230
let buf = pool. alloc ( MIN_BITS ) ;
229
231
Deque {
230
232
bottom : AtomicInt :: new ( 0 ) ,
@@ -234,7 +236,7 @@ impl<T: Send> Deque<T> {
234
236
}
235
237
}
236
238
237
- unsafe fn push ( & mut self , data : T ) {
239
+ unsafe fn push ( & self , data : T ) {
238
240
let mut b = self . bottom . load ( SeqCst ) ;
239
241
let t = self . top . load ( SeqCst ) ;
240
242
let mut a = self . array . load ( SeqCst ) ;
@@ -250,7 +252,7 @@ impl<T: Send> Deque<T> {
250
252
self . bottom . store ( b + 1 , SeqCst ) ;
251
253
}
252
254
253
- unsafe fn pop ( & mut self ) -> Option < T > {
255
+ unsafe fn pop ( & self ) -> Option < T > {
254
256
let b = self . bottom . load ( SeqCst ) ;
255
257
let a = self . array . load ( SeqCst ) ;
256
258
let b = b - 1 ;
@@ -276,7 +278,7 @@ impl<T: Send> Deque<T> {
276
278
}
277
279
}
278
280
279
- unsafe fn steal ( & mut self ) -> Stolen < T > {
281
+ unsafe fn steal ( & self ) -> Stolen < T > {
280
282
let t = self . top . load ( SeqCst ) ;
281
283
let old = self . array . load ( SeqCst ) ;
282
284
let b = self . bottom . load ( SeqCst ) ;
@@ -298,7 +300,7 @@ impl<T: Send> Deque<T> {
298
300
}
299
301
}
300
302
301
- unsafe fn maybe_shrink ( & mut self , b : int , t : int ) {
303
+ unsafe fn maybe_shrink ( & self , b : int , t : int ) {
302
304
let a = self . array . load ( SeqCst ) ;
303
305
if b - t < ( * a) . size ( ) / K && b - t > ( 1 << MIN_BITS ) {
304
306
self . swap_buffer ( b, a, ( * a) . resize ( b, t, -1 ) ) ;
@@ -312,7 +314,7 @@ impl<T: Send> Deque<T> {
312
314
// after this method has called 'free' on it. The continued usage is simply
313
315
// a read followed by a forget, but we must make sure that the memory can
314
316
// 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 > ,
316
318
buf : Buffer < T > ) -> * mut Buffer < T > {
317
319
let newbuf: * mut Buffer < T > = transmute ( box buf) ;
318
320
self . array . store ( newbuf, SeqCst ) ;
@@ -373,7 +375,7 @@ impl<T: Send> Buffer<T> {
373
375
374
376
// Unsafe because this unsafely overwrites possibly uninitialized or
375
377
// initialized data.
376
- unsafe fn put ( & mut self , i : int , t : T ) {
378
+ unsafe fn put ( & self , i : int , t : T ) {
377
379
let ptr = self . storage . offset ( i & self . mask ( ) ) ;
378
380
ptr:: copy_nonoverlapping_memory ( ptr as * mut T , & t as * T , 1 ) ;
379
381
forget ( t) ;
@@ -382,7 +384,7 @@ impl<T: Send> Buffer<T> {
382
384
// Again, unsafe because this has incredibly dubious ownership violations.
383
385
// It is assumed that this buffer is immediately dropped.
384
386
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) ;
386
388
for i in range ( t, b) {
387
389
buf. put ( i, self . get ( i) ) ;
388
390
}
0 commit comments