Skip to content

Commit

Permalink
Fix select deschedule environment race for real this time, in light o…
Browse files Browse the repository at this point in the history
…f task killing.
  • Loading branch information
bblum committed Aug 15, 2013
1 parent 0d817ee commit 65cf75a
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/libstd/select.rs
Expand Up @@ -20,6 +20,7 @@ use rt::select::{SelectInner, SelectPortInner};
use rt::local::Local;
use rt::rtio::EventLoop;
use task;
use unstable::finally::Finally;
use vec::{OwnedVector, MutableVector};

/// Trait for message-passing primitives that can be select()ed on.
Expand Down Expand Up @@ -57,28 +58,32 @@ pub fn select<A: Select>(ports: &mut [A]) -> uint {
let p = Cell::new(p);
let c = Cell::new(c);

let sched = Local::take::<Scheduler>();
do sched.deschedule_running_task_and_then |sched, task| {
let task_handles = task.make_selectable(ports.len());

for (index, (port, task_handle)) in
ports.mut_iter().zip(task_handles.move_iter()).enumerate() {
// If one of the ports has data by now, it will wake the handle.
if port.block_on(sched, task_handle) {
ready_index = index;
break;
do (|| {
let c = Cell::new(c.take());
let sched = Local::take::<Scheduler>();
do sched.deschedule_running_task_and_then |sched, task| {
let task_handles = task.make_selectable(ports.len());

for (index, (port, task_handle)) in
ports.mut_iter().zip(task_handles.move_iter()).enumerate() {
// If one of the ports has data by now, it will wake the handle.
if port.block_on(sched, task_handle) {
ready_index = index;
break;
}
}
}

let c = Cell::new(c.take());
do sched.event_loop.callback { c.take().send_deferred(()) }
let c = Cell::new(c.take());
do sched.event_loop.callback { c.take().send_deferred(()) }
}
}).finally {
let p = Cell::new(p.take());
// Unkillable is necessary not because getting killed is dangerous here,
// but to force the recv not to use the same kill-flag that we used for
// selecting. Otherwise a user-sender could spuriously wakeup us here.
do task::unkillable { p.take().recv(); }
}

// Unkillable is necessary not because getting killed is dangerous here,
// but to force the recv not to use the same kill-flag that we used for
// selecting. Otherwise a user-sender could spuriously wakeup us here.
do task::unkillable { p.take().recv(); }

// Task resumes. Now unblock ourselves from all the ports we blocked on.
// If the success index wasn't reset, 'take' will just take all of them.
// Iterate in reverse so the 'earliest' index that's ready gets returned.
Expand Down

5 comments on commit 65cf75a

@bors
Copy link
Contributor

@bors bors commented on 65cf75a Aug 19, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at bblum@65cf75a

@bors
Copy link
Contributor

@bors bors commented on 65cf75a Aug 19, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging bblum/rust/select-bugfix = 65cf75a into auto

@bors
Copy link
Contributor

@bors bors commented on 65cf75a Aug 19, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bblum/rust/select-bugfix = 65cf75a merged ok, testing candidate = 557ff04

@bors
Copy link
Contributor

@bors bors commented on 65cf75a Aug 19, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 65cf75a Aug 19, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 557ff04

Please sign in to comment.