34
34
35
35
use comm:: Port ;
36
36
use kinds:: Send ;
37
+ use mem;
37
38
use ops:: Drop ;
38
39
use option:: { Some , None , Option } ;
39
40
use result:: { Result , Ok , Err } ;
40
41
use rt:: local:: Local ;
41
42
use rt:: task:: { Task , BlockedTask } ;
42
43
use sync:: atomics;
43
- use util;
44
44
45
45
// Various states you can find a port in.
46
46
static EMPTY : uint = 0 ;
@@ -100,10 +100,7 @@ impl<T: Send> Packet<T> {
100
100
self . data = Some ( t) ;
101
101
self . upgrade = SendUsed ;
102
102
103
- // This atomic swap uses a "Release" memory ordering to ensure that all
104
- // our previous memory writes are visible to the other thread (notably
105
- // the write of data/upgrade)
106
- match self . state . swap ( DATA , atomics:: Release ) {
103
+ match self . state . swap ( DATA , atomics:: SeqCst ) {
107
104
// Sent the data, no one was waiting
108
105
EMPTY => true ,
109
106
@@ -141,14 +138,11 @@ impl<T: Send> Packet<T> {
141
138
pub fn recv ( & mut self ) -> Result < T , Failure < T > > {
142
139
// Attempt to not block the task (it's a little expensive). If it looks
143
140
// like we're not empty, then immediately go through to `try_recv`.
144
- //
145
- // These atomics use an Acquire memory ordering in order to have all the
146
- // previous writes of the releasing thread visible to us.
147
- if self . state . load ( atomics:: Acquire ) == EMPTY {
141
+ if self . state . load ( atomics:: SeqCst ) == EMPTY {
148
142
let t: ~Task = Local :: take ( ) ;
149
143
t. deschedule ( 1 , |task| {
150
144
let n = unsafe { task. cast_to_uint ( ) } ;
151
- match self . state . compare_and_swap ( EMPTY , n, atomics:: Acquire ) {
145
+ match self . state . compare_and_swap ( EMPTY , n, atomics:: SeqCst ) {
152
146
// Nothing on the channel, we legitimately block
153
147
EMPTY => Ok ( ( ) ) ,
154
148
@@ -168,8 +162,7 @@ impl<T: Send> Packet<T> {
168
162
}
169
163
170
164
pub fn try_recv ( & mut self ) -> Result < T , Failure < T > > {
171
- // see above for why Acquire is used.
172
- match self . state . load ( atomics:: Acquire ) {
165
+ match self . state . load ( atomics:: SeqCst ) {
173
166
EMPTY => Err ( Empty ) ,
174
167
175
168
// We saw some data on the channel, but the channel can be used
@@ -179,7 +172,7 @@ impl<T: Send> Packet<T> {
179
172
// the state changes under our feet we'd rather just see that state
180
173
// change.
181
174
DATA => {
182
- self . state . compare_and_swap ( DATA , EMPTY , atomics:: Acquire ) ;
175
+ self . state . compare_and_swap ( DATA , EMPTY , atomics:: SeqCst ) ;
183
176
match self . data . take ( ) {
184
177
Some ( data) => Ok ( data) ,
185
178
None => unreachable ! ( ) ,
@@ -194,7 +187,7 @@ impl<T: Send> Packet<T> {
194
187
match self . data . take ( ) {
195
188
Some ( data) => Ok ( data) ,
196
189
None => {
197
- match util :: replace ( & mut self . upgrade , SendUsed ) {
190
+ match mem :: replace ( & mut self . upgrade , SendUsed ) {
198
191
SendUsed | NothingSent => Err ( Disconnected ) ,
199
192
GoUp ( upgrade) => Err ( Upgraded ( upgrade) )
200
193
}
@@ -216,9 +209,7 @@ impl<T: Send> Packet<T> {
216
209
} ;
217
210
self . upgrade = GoUp ( up) ;
218
211
219
- // Use a Release memory ordering in order to make sure that our write to
220
- // `upgrade` is visible to the other thread.
221
- match self . state . swap ( DISCONNECTED , atomics:: Release ) {
212
+ match self . state . swap ( DISCONNECTED , atomics:: SeqCst ) {
222
213
// If the channel is empty or has data on it, then we're good to go.
223
214
// Senders will check the data before the upgrade (in case we
224
215
// plastered over the DATA state).
@@ -246,9 +237,7 @@ impl<T: Send> Packet<T> {
246
237
}
247
238
248
239
pub fn drop_port ( & mut self ) {
249
- // Use an Acquire memory ordering in order to see the data that the
250
- // senders are sending.
251
- match self . state . swap ( DISCONNECTED , atomics:: Acquire ) {
240
+ match self . state . swap ( DISCONNECTED , atomics:: SeqCst ) {
252
241
// An empty channel has nothing to do, and a remotely disconnected
253
242
// channel also has nothing to do b/c we're about to run the drop
254
243
// glue
@@ -271,13 +260,12 @@ impl<T: Send> Packet<T> {
271
260
// If Ok, the value is whether this port has data, if Err, then the upgraded
272
261
// port needs to be checked instead of this one.
273
262
pub fn can_recv ( & mut self ) -> Result < bool , Port < T > > {
274
- // Use Acquire so we can see all previous memory writes
275
- match self . state . load ( atomics:: Acquire ) {
263
+ match self . state . load ( atomics:: SeqCst ) {
276
264
EMPTY => Ok ( false ) , // Welp, we tried
277
265
DATA => Ok ( true ) , // we have some un-acquired data
278
266
DISCONNECTED if self . data . is_some ( ) => Ok ( true ) , // we have data
279
267
DISCONNECTED => {
280
- match util :: replace ( & mut self . upgrade , SendUsed ) {
268
+ match mem :: replace ( & mut self . upgrade , SendUsed ) {
281
269
// The other end sent us an upgrade, so we need to
282
270
// propagate upwards whether the upgrade can receive
283
271
// data
@@ -304,7 +292,7 @@ impl<T: Send> Packet<T> {
304
292
SelCanceled ( unsafe { BlockedTask :: cast_from_uint ( n) } )
305
293
}
306
294
DISCONNECTED => {
307
- match util :: replace ( & mut self . upgrade , SendUsed ) {
295
+ match mem :: replace ( & mut self . upgrade , SendUsed ) {
308
296
// The other end sent us an upgrade, so we need to
309
297
// propagate upwards whether the upgrade can receive
310
298
// data
@@ -331,8 +319,7 @@ impl<T: Send> Packet<T> {
331
319
//
332
320
// The return value indicates whether there's data on this port.
333
321
pub fn abort_selection ( & mut self ) -> Result < bool , Port < T > > {
334
- // use Acquire to make sure we see all previous memory writes
335
- let state = match self . state . load ( atomics:: Acquire ) {
322
+ let state = match self . state . load ( atomics:: SeqCst ) {
336
323
// Each of these states means that no further activity will happen
337
324
// with regard to abortion selection
338
325
s @ EMPTY |
@@ -357,7 +344,7 @@ impl<T: Send> Packet<T> {
357
344
// aborted.
358
345
DISCONNECTED => {
359
346
assert ! ( self . data. is_none( ) ) ;
360
- match util :: replace ( & mut self . upgrade , SendUsed ) {
347
+ match mem :: replace ( & mut self . upgrade , SendUsed ) {
361
348
GoUp ( port) => Err ( port) ,
362
349
_ => Ok ( true ) ,
363
350
}
0 commit comments