io: Implement process wait timeouts
This implements set_timeout() for std::io::Process which will affect wait() operations on the process. This follows the same pattern as the rest of the timeouts emerging in std::io::net. The implementation was super easy for everything except libnative on unix (backwards from usual!), which required a good bit of signal handling. There's a doc comment explaining the strategy in libnative. Internally, this also required refactoring the "helper thread" implementation used by libnative to allow for an extra helper thread (not just the timer). This is a breaking change in terms of the io::Process API. It is now possible for wait() to fail, and subsequently wait_with_output(). These two functions now return IoResult<T> due to the fact that they can time out. Additionally, the wait_with_output() function has moved from taking `&mut self` to taking `self`. If a timeout occurs while waiting with output, the semantics are undesirable in almost all cases if attempting to re-wait on the process. Equivalent functionality can still be achieved by dealing with the output handles manually. [breaking-change] cc #13523
This commit is contained in:
parent
9f7caed202
commit
f09592a5d1
23 changed files with 878 additions and 328 deletions
|
@ -52,11 +52,14 @@ use std::os;
|
|||
use std::ptr;
|
||||
use std::rt::rtio;
|
||||
use std::sync::atomics;
|
||||
use std::comm;
|
||||
|
||||
use io::IoResult;
|
||||
use io::c;
|
||||
use io::file::FileDesc;
|
||||
use io::timer_helper;
|
||||
use io::helper_thread::Helper;
|
||||
|
||||
helper_init!(static mut HELPER: Helper<Req>)
|
||||
|
||||
pub struct Timer {
|
||||
id: uint,
|
||||
|
@ -79,9 +82,6 @@ pub enum Req {
|
|||
// Remove a timer based on its id and then send it back on the channel
|
||||
// provided
|
||||
RemoveTimer(uint, Sender<Box<Inner>>),
|
||||
|
||||
// Shut down the loop and then ACK this channel once it's shut down
|
||||
Shutdown,
|
||||
}
|
||||
|
||||
// returns the current time (in milliseconds)
|
||||
|
@ -93,7 +93,7 @@ pub fn now() -> u64 {
|
|||
}
|
||||
}
|
||||
|
||||
fn helper(input: libc::c_int, messages: Receiver<Req>) {
|
||||
fn helper(input: libc::c_int, messages: Receiver<Req>, _: ()) {
|
||||
let mut set: c::fd_set = unsafe { mem::init() };
|
||||
|
||||
let mut fd = FileDesc::new(input, true);
|
||||
|
@ -163,7 +163,7 @@ fn helper(input: libc::c_int, messages: Receiver<Req>) {
|
|||
1 => {
|
||||
loop {
|
||||
match messages.try_recv() {
|
||||
Ok(Shutdown) => {
|
||||
Err(comm::Disconnected) => {
|
||||
assert!(active.len() == 0);
|
||||
break 'outer;
|
||||
}
|
||||
|
@ -202,7 +202,7 @@ fn helper(input: libc::c_int, messages: Receiver<Req>) {
|
|||
|
||||
impl Timer {
|
||||
pub fn new() -> IoResult<Timer> {
|
||||
timer_helper::boot(helper);
|
||||
unsafe { HELPER.boot(|| {}, helper); }
|
||||
|
||||
static mut ID: atomics::AtomicUint = atomics::INIT_ATOMIC_UINT;
|
||||
let id = unsafe { ID.fetch_add(1, atomics::Relaxed) };
|
||||
|
@ -235,7 +235,7 @@ impl Timer {
|
|||
Some(i) => i,
|
||||
None => {
|
||||
let (tx, rx) = channel();
|
||||
timer_helper::send(RemoveTimer(self.id, tx));
|
||||
unsafe { HELPER.send(RemoveTimer(self.id, tx)); }
|
||||
rx.recv()
|
||||
}
|
||||
}
|
||||
|
@ -261,7 +261,7 @@ impl rtio::RtioTimer for Timer {
|
|||
inner.interval = msecs;
|
||||
inner.target = now + msecs;
|
||||
|
||||
timer_helper::send(NewTimer(inner));
|
||||
unsafe { HELPER.send(NewTimer(inner)); }
|
||||
return rx;
|
||||
}
|
||||
|
||||
|
@ -275,7 +275,7 @@ impl rtio::RtioTimer for Timer {
|
|||
inner.interval = msecs;
|
||||
inner.target = now + msecs;
|
||||
|
||||
timer_helper::send(NewTimer(inner));
|
||||
unsafe { HELPER.send(NewTimer(inner)); }
|
||||
return rx;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue