Skip to content

Commit 8be0f66

Browse files
committed
De-mode pipes
1 parent 51d98d9 commit 8be0f66

File tree

2 files changed

+47
-42
lines changed

2 files changed

+47
-42
lines changed

src/libcore/pipes.rs

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ bounded and unbounded protocols allows for less code duplication.
7272
7373
*/
7474

75+
// NB: transitionary, de-mode-ing.
76+
#[forbid(deprecated_mode)];
77+
#[forbid(deprecated_pattern)];
78+
7579
import unsafe::{forget, reinterpret_cast, transmute};
7680
import either::{either, left, right};
7781
import option::unwrap;
@@ -143,15 +147,15 @@ struct packet_header {
143147
// Returns the old state.
144148
unsafe fn mark_blocked(this: *rust_task) -> state {
145149
rustrt::rust_task_ref(this);
146-
let old_task = swap_task(self.blocked_task, this);
150+
let old_task = swap_task(&mut self.blocked_task, this);
147151
assert old_task.is_null();
148-
swap_state_acq(self.state, blocked)
152+
swap_state_acq(&mut self.state, blocked)
149153
}
150154

151155
unsafe fn unblock() {
152-
let old_task = swap_task(self.blocked_task, ptr::null());
156+
let old_task = swap_task(&mut self.blocked_task, ptr::null());
153157
if !old_task.is_null() { rustrt::rust_task_deref(old_task) }
154-
match swap_state_acq(self.state, empty) {
158+
match swap_state_acq(&mut self.state, empty) {
155159
empty | blocked => (),
156160
terminated => self.state = terminated,
157161
full => self.state = full
@@ -224,7 +228,7 @@ fn packet<T: send>() -> *packet<T> {
224228

225229
#[doc(hidden)]
226230
fn entangle_buffer<T: send, Tstart: send>(
227-
-buffer: ~buffer<T>,
231+
+buffer: ~buffer<T>,
228232
init: fn(*libc::c_void, x: &T) -> *packet<Tstart>)
229233
-> (send_packet_buffered<Tstart, T>, recv_packet_buffered<Tstart, T>)
230234
{
@@ -247,27 +251,27 @@ extern mod rusti {
247251
// If I call the rusti versions directly from a polymorphic function,
248252
// I get link errors. This is a bug that needs investigated more.
249253
#[doc(hidden)]
250-
fn atomic_xchng_rel(&dst: int, src: int) -> int {
251-
rusti::atomic_xchng_rel(dst, src)
254+
fn atomic_xchng_rel(dst: &mut int, src: int) -> int {
255+
rusti::atomic_xchng_rel(*dst, src)
252256
}
253257

254258
#[doc(hidden)]
255-
fn atomic_add_acq(&dst: int, src: int) -> int {
256-
rusti::atomic_add_acq(dst, src)
259+
fn atomic_add_acq(dst: &mut int, src: int) -> int {
260+
rusti::atomic_add_acq(*dst, src)
257261
}
258262

259263
#[doc(hidden)]
260-
fn atomic_sub_rel(&dst: int, src: int) -> int {
261-
rusti::atomic_sub_rel(dst, src)
264+
fn atomic_sub_rel(dst: &mut int, src: int) -> int {
265+
rusti::atomic_sub_rel(*dst, src)
262266
}
263267

264268
#[doc(hidden)]
265-
fn swap_task(&dst: *rust_task, src: *rust_task) -> *rust_task {
269+
fn swap_task(dst: &mut *rust_task, src: *rust_task) -> *rust_task {
266270
// It might be worth making both acquire and release versions of
267271
// this.
268272
unsafe {
269273
reinterpret_cast(rusti::atomic_xchng(
270-
*(ptr::mut_addr_of(dst) as *mut int),
274+
*(ptr::mut_addr_of(*dst) as *mut int),
271275
src as int))
272276
}
273277
}
@@ -302,19 +306,19 @@ fn wait_event(this: *rust_task) -> *libc::c_void {
302306
}
303307

304308
#[doc(hidden)]
305-
fn swap_state_acq(&dst: state, src: state) -> state {
309+
fn swap_state_acq(dst: &mut state, src: state) -> state {
306310
unsafe {
307311
reinterpret_cast(rusti::atomic_xchng_acq(
308-
*(ptr::mut_addr_of(dst) as *mut int),
312+
*(ptr::mut_addr_of(*dst) as *mut int),
309313
src as int))
310314
}
311315
}
312316

313317
#[doc(hidden)]
314-
fn swap_state_rel(&dst: state, src: state) -> state {
318+
fn swap_state_rel(dst: &mut state, src: state) -> state {
315319
unsafe {
316320
reinterpret_cast(rusti::atomic_xchng_rel(
317-
*(ptr::mut_addr_of(dst) as *mut int),
321+
*(ptr::mut_addr_of(*dst) as *mut int),
318322
src as int))
319323
}
320324
}
@@ -329,15 +333,15 @@ struct buffer_resource<T: send> {
329333
new(+b: ~buffer<T>) {
330334
//let p = ptr::addr_of(*b);
331335
//error!{"take %?", p};
332-
atomic_add_acq(b.header.ref_count, 1);
336+
atomic_add_acq(&mut b.header.ref_count, 1);
333337
self.buffer = b;
334338
}
335339

336340
drop unsafe {
337341
let b = move_it!{self.buffer};
338342
//let p = ptr::addr_of(*b);
339343
//error!{"drop %?", p};
340-
let old_count = atomic_sub_rel(b.header.ref_count, 1);
344+
let old_count = atomic_sub_rel(&mut b.header.ref_count, 1);
341345
//let old_count = atomic_xchng_rel(b.header.ref_count, 0);
342346
if old_count == 1 {
343347
// The new count is 0.
@@ -351,15 +355,15 @@ struct buffer_resource<T: send> {
351355
}
352356

353357
#[doc(hidden)]
354-
fn send<T: send, Tbuffer: send>(-p: send_packet_buffered<T, Tbuffer>,
355-
-payload: T) -> bool {
358+
fn send<T: send, Tbuffer: send>(+p: send_packet_buffered<T, Tbuffer>,
359+
+payload: T) -> bool {
356360
let header = p.header();
357361
let p_ = p.unwrap();
358362
let p = unsafe { &*p_ };
359363
assert ptr::addr_of(p.header) == header;
360364
assert p.payload == none;
361365
p.payload <- some(payload);
362-
let old_state = swap_state_rel(p.header.state, full);
366+
let old_state = swap_state_rel(&mut p.header.state, full);
363367
match old_state {
364368
empty => {
365369
// Yay, fastpath.
@@ -371,7 +375,7 @@ fn send<T: send, Tbuffer: send>(-p: send_packet_buffered<T, Tbuffer>,
371375
full => fail ~"duplicate send",
372376
blocked => {
373377
debug!{"waking up task for %?", p_};
374-
let old_task = swap_task(p.header.blocked_task, ptr::null());
378+
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
375379
if !old_task.is_null() {
376380
rustrt::task_signal_event(
377381
old_task, ptr::addr_of(p.header) as *libc::c_void);
@@ -395,7 +399,7 @@ fn send<T: send, Tbuffer: send>(-p: send_packet_buffered<T, Tbuffer>,
395399
Fails if the sender closes the connection.
396400
397401
*/
398-
fn recv<T: send, Tbuffer: send>(-p: recv_packet_buffered<T, Tbuffer>) -> T {
402+
fn recv<T: send, Tbuffer: send>(+p: recv_packet_buffered<T, Tbuffer>) -> T {
399403
option::unwrap_expect(try_recv(p), "connection closed")
400404
}
401405

@@ -405,7 +409,7 @@ Returns `none` if the sender has closed the connection without sending
405409
a message, or `some(T)` if a message was received.
406410
407411
*/
408-
fn try_recv<T: send, Tbuffer: send>(-p: recv_packet_buffered<T, Tbuffer>)
412+
fn try_recv<T: send, Tbuffer: send>(+p: recv_packet_buffered<T, Tbuffer>)
409413
-> option<T>
410414
{
411415
let p_ = p.unwrap();
@@ -417,7 +421,8 @@ fn try_recv<T: send, Tbuffer: send>(-p: recv_packet_buffered<T, Tbuffer>)
417421
drop {
418422
if task::failing() {
419423
self.p.state = terminated;
420-
let old_task = swap_task(self.p.blocked_task, ptr::null());
424+
let old_task = swap_task(&mut self.p.blocked_task,
425+
ptr::null());
421426
if !old_task.is_null() {
422427
rustrt::rust_task_deref(old_task);
423428
}
@@ -443,13 +448,13 @@ fn try_recv<T: send, Tbuffer: send>(-p: recv_packet_buffered<T, Tbuffer>)
443448
let this = rustrt::rust_get_task();
444449
rustrt::task_clear_event_reject(this);
445450
rustrt::rust_task_ref(this);
446-
let old_task = swap_task(p.header.blocked_task, this);
451+
let old_task = swap_task(&mut p.header.blocked_task, this);
447452
assert old_task.is_null();
448453
let mut first = true;
449454
let mut count = SPIN_COUNT;
450455
loop {
451456
rustrt::task_clear_event_reject(this);
452-
let old_state = swap_state_acq(p.header.state,
457+
let old_state = swap_state_acq(&mut p.header.state,
453458
blocked);
454459
match old_state {
455460
empty => {
@@ -474,7 +479,7 @@ fn try_recv<T: send, Tbuffer: send>(-p: recv_packet_buffered<T, Tbuffer>)
474479
full => {
475480
let mut payload = none;
476481
payload <-> p.payload;
477-
let old_task = swap_task(p.header.blocked_task, ptr::null());
482+
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
478483
if !old_task.is_null() {
479484
rustrt::rust_task_deref(old_task);
480485
}
@@ -486,7 +491,7 @@ fn try_recv<T: send, Tbuffer: send>(-p: recv_packet_buffered<T, Tbuffer>)
486491
// casted too big of a number to a state.
487492
assert old_state == terminated;
488493

489-
let old_task = swap_task(p.header.blocked_task, ptr::null());
494+
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
490495
if !old_task.is_null() {
491496
rustrt::rust_task_deref(old_task);
492497
}
@@ -498,7 +503,7 @@ fn try_recv<T: send, Tbuffer: send>(-p: recv_packet_buffered<T, Tbuffer>)
498503
}
499504

500505
/// Returns true if messages are available.
501-
pure fn peek<T: send, Tb: send>(p: recv_packet_buffered<T, Tb>) -> bool {
506+
pure fn peek<T: send, Tb: send>(p: &recv_packet_buffered<T, Tb>) -> bool {
502507
match unsafe {(*p.header()).state} {
503508
empty => false,
504509
blocked => fail ~"peeking on blocked packet",
@@ -508,20 +513,20 @@ pure fn peek<T: send, Tb: send>(p: recv_packet_buffered<T, Tb>) -> bool {
508513

509514
impl<T: send, Tb: send> recv_packet_buffered<T, Tb> {
510515
pure fn peek() -> bool {
511-
peek(self)
516+
peek(&self)
512517
}
513518
}
514519

515520
#[doc(hidden)]
516521
fn sender_terminate<T: send>(p: *packet<T>) {
517522
let p = unsafe { &*p };
518-
match swap_state_rel(p.header.state, terminated) {
523+
match swap_state_rel(&mut p.header.state, terminated) {
519524
empty => {
520525
// The receiver will eventually clean up.
521526
}
522527
blocked => {
523528
// wake up the target
524-
let old_task = swap_task(p.header.blocked_task, ptr::null());
529+
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
525530
if !old_task.is_null() {
526531
rustrt::task_signal_event(
527532
old_task,
@@ -544,13 +549,13 @@ fn sender_terminate<T: send>(p: *packet<T>) {
544549
#[doc(hidden)]
545550
fn receiver_terminate<T: send>(p: *packet<T>) {
546551
let p = unsafe { &*p };
547-
match swap_state_rel(p.header.state, terminated) {
552+
match swap_state_rel(&mut p.header.state, terminated) {
548553
empty => {
549554
assert p.header.blocked_task.is_null();
550555
// the sender will clean up
551556
}
552557
blocked => {
553-
let old_task = swap_task(p.header.blocked_task, ptr::null());
558+
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
554559
if !old_task.is_null() {
555560
rustrt::rust_task_deref(old_task);
556561
assert old_task == rustrt::rust_get_task();
@@ -682,7 +687,7 @@ fn selecti<T: selectable>(endpoints: &[T]) -> uint {
682687
}
683688

684689
/// Returns 0 or 1 depending on which endpoint is ready to receive
685-
fn select2i<A: selectable, B: selectable>(a: A, b: B) -> either<(), ()> {
690+
fn select2i<A: selectable, B: selectable>(a: &A, b: &B) -> either<(), ()> {
686691
match wait_many([a.header(), b.header()]/_) {
687692
0 => left(()),
688693
1 => right(()),
@@ -1004,7 +1009,7 @@ impl<T: send> port<T>: recv<T> {
10041009
let mut endp = none;
10051010
endp <-> self.endp;
10061011
let peek = match endp {
1007-
some(endp) => pipes::peek(endp),
1012+
some(endp) => pipes::peek(&endp),
10081013
none => fail ~"peeking empty stream"
10091014
};
10101015
self.endp <-> endp;
@@ -1122,7 +1127,7 @@ impl<T: send, U: send, Left: selectable recv<T>, Right: selectable recv<U>>
11221127

11231128
fn select() -> either<T, U> {
11241129
match self {
1125-
(lp, rp) => match select2i(lp, rp) {
1130+
(lp, rp) => match select2i(&lp, &rp) {
11261131
left(()) => left (lp.recv()),
11271132
right(()) => right(rp.recv())
11281133
}
@@ -1131,7 +1136,7 @@ impl<T: send, U: send, Left: selectable recv<T>, Right: selectable recv<U>>
11311136

11321137
fn try_select() -> either<option<T>, option<U>> {
11331138
match self {
1134-
(lp, rp) => match select2i(lp, rp) {
1139+
(lp, rp) => match select2i(&lp, &rp) {
11351140
left(()) => left (lp.try_recv()),
11361141
right(()) => right(rp.try_recv())
11371142
}

src/test/run-pass/pipe-peek.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ proto! oneshot {
1313
fn main() {
1414
let (c, p) = oneshot::init();
1515

16-
assert !pipes::peek(p);
16+
assert !pipes::peek(&p);
1717

1818
oneshot::client::signal(c);
1919

20-
assert pipes::peek(p);
20+
assert pipes::peek(&p);
2121
}

0 commit comments

Comments
 (0)