Skip to content

Commit 196c2dd

Browse files
committed
---
yaml --- r: 152555 b: refs/heads/try2 c: b755b4d h: refs/heads/master i: 152553: 5f05342 152551: 50ac49b v: v3
1 parent 068a82c commit 196c2dd

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 04eced750e78770e16354c07fddf7ecaaab6ef43
8+
refs/heads/try2: b755b4db4b30f4ab9fe7ddcbea8ee8d54fd4cd9e
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/io/process.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use c_str::CString;
5959
/// ```
6060
pub struct Process {
6161
handle: Box<RtioProcess + Send>,
62+
forget: bool,
6263

6364
/// Handle to the child's stdin, if the `stdin` field of this process's
6465
/// `ProcessConfig` was `CreatePipe`. By default, this handle is `Some`.
@@ -262,6 +263,7 @@ impl Command {
262263
});
263264
Process {
264265
handle: p,
266+
forget: false,
265267
stdin: io.next().unwrap(),
266268
stdout: io.next().unwrap(),
267269
stderr: io.next().unwrap(),
@@ -540,10 +542,23 @@ impl Process {
540542
error: stderr.recv().ok().unwrap_or(Vec::new()),
541543
})
542544
}
545+
546+
/// Forgets this process, allowing it to outlive the parent
547+
///
548+
/// This function will forcefully prevent calling `wait()` on the child
549+
/// process in the destructor, allowing the child to outlive the
550+
/// parent. Note that this operation can easily lead to leaking the
551+
/// resources of the child process, so care must be taken when
552+
/// invoking this method.
553+
pub fn forget(mut self) {
554+
self.forget = true;
555+
}
543556
}
544557

545558
impl Drop for Process {
546559
fn drop(&mut self) {
560+
if self.forget { return }
561+
547562
// Close all I/O before exiting to ensure that the child doesn't wait
548563
// forever to print some text or something similar.
549564
drop(self.stdin.take());
@@ -933,4 +948,12 @@ mod tests {
933948
rx.recv();
934949
rx.recv();
935950
})
951+
952+
iotest!(fn forget() {
953+
let p = sleeper();
954+
let id = p.id();
955+
p.forget();
956+
assert!(Process::kill(id, 0).is_ok());
957+
assert!(Process::kill(id, PleaseExitSignal).is_ok());
958+
})
936959
}

branches/try2/src/libsync/comm/mod.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,52 @@
120120
//! });
121121
//! rx.recv();
122122
//! ```
123+
//!
124+
//! Reading from a channel with a timeout requires to use a Timer together
125+
//! with the channel. You can use the select! macro to select either and
126+
//! handle the timeout case. This first example will break out of the loop
127+
//! after 10 seconds no matter what:
128+
//!
129+
//! ```no_run
130+
//! use std::io::timer::Timer;
131+
//!
132+
//! let (tx, rx) = channel::<int>();
133+
//! let mut timer = Timer::new().unwrap();
134+
//! let timeout = timer.oneshot(10000);
135+
//!
136+
//! loop {
137+
//! select! {
138+
//! val = rx.recv() => println!("Received {}", val),
139+
//! () = timeout.recv() => {
140+
//! println!("timed out, total time was more than 10 seconds")
141+
//! break;
142+
//! }
143+
//! }
144+
//! }
145+
//! ```
146+
//!
147+
//! This second example is more costly since it allocates a new timer every
148+
//! time a message is received, but it allows you to timeout after the channel
149+
//! has been inactive for 5 seconds:
150+
//!
151+
//! ```no_run
152+
//! use std::io::timer::Timer;
153+
//!
154+
//! let (tx, rx) = channel::<int>();
155+
//! let mut timer = Timer::new().unwrap();
156+
//!
157+
//! loop {
158+
//! let timeout = timer.oneshot(5000);
159+
//!
160+
//! select! {
161+
//! val = rx.recv() => println!("Received {}", val),
162+
//! () = timeout.recv() => {
163+
//! println!("timed out, no message received in 5 seconds")
164+
//! break;
165+
//! }
166+
//! }
167+
//! }
168+
//! ```
123169
124170
// A description of how Rust's channel implementation works
125171
//

0 commit comments

Comments
 (0)