Skip to content

Commit 21a064d

Browse files
committed
Don't require an allocation for on_exit messages
Instead, use an enum to allow running both a procedure and sending the task result over a channel. I expect the common case to be sending on a channel (e.g. task::try), so don't require an extra allocation in the common case. cc #11389
1 parent aaead93 commit 21a064d

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/libgreen/task.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::cast;
2222
use std::rt::Runtime;
2323
use std::rt::rtio;
2424
use std::rt::local::Local;
25-
use std::rt::task::{Task, BlockedTask};
25+
use std::rt::task::{Task, BlockedTask, SendMessage};
2626
use std::task::TaskOpts;
2727
use std::unstable::mutex::Mutex;
2828

@@ -131,8 +131,7 @@ impl GreenTask {
131131
task.stdout = stdout;
132132
match notify_chan {
133133
Some(chan) => {
134-
let on_exit = proc(task_result) { chan.send(task_result) };
135-
task.death.on_exit = Some(on_exit);
134+
task.death.on_exit = Some(SendMessage(chan));
136135
}
137136
None => {}
138137
}

src/libnative/task.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::cast;
1818
use std::rt::env;
1919
use std::rt::local::Local;
2020
use std::rt::rtio;
21-
use std::rt::task::{Task, BlockedTask};
21+
use std::rt::task::{Task, BlockedTask, SendMessage};
2222
use std::rt::thread::Thread;
2323
use std::rt;
2424
use std::task::TaskOpts;
@@ -68,10 +68,7 @@ pub fn spawn_opts(opts: TaskOpts, f: proc()) {
6868
task.stderr = stderr;
6969
task.stdout = stdout;
7070
match notify_chan {
71-
Some(chan) => {
72-
let on_exit = proc(task_result) { chan.send(task_result) };
73-
task.death.on_exit = Some(on_exit);
74-
}
71+
Some(chan) => { task.death.on_exit = Some(SendMessage(chan)); }
7572
None => {}
7673
}
7774

src/libstd/rt/task.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use any::AnyOwnExt;
1717
use cast;
1818
use cleanup;
1919
use clone::Clone;
20+
use comm::Chan;
2021
use io::Writer;
2122
use iter::{Iterator, Take};
2223
use local_data;
@@ -67,11 +68,17 @@ pub enum BlockedTask {
6768
Shared(UnsafeArc<AtomicUint>),
6869
}
6970

71+
pub enum DeathAction {
72+
/// Action to be done with the exit code. If set, also makes the task wait
73+
/// until all its watched children exit before collecting the status.
74+
Execute(proc(TaskResult)),
75+
/// A channel to send the result of the task on when the task exits
76+
SendMessage(Chan<TaskResult>),
77+
}
78+
7079
/// Per-task state related to task death, killing, failure, etc.
7180
pub struct Death {
72-
// Action to be done with the exit code. If set, also makes the task wait
73-
// until all its watched children exit before collecting the status.
74-
on_exit: Option<proc(TaskResult)>,
81+
on_exit: Option<DeathAction>,
7582
}
7683

7784
pub struct BlockedTasks {
@@ -381,7 +388,8 @@ impl Death {
381388
/// Collect failure exit codes from children and propagate them to a parent.
382389
pub fn collect_failure(&mut self, result: TaskResult) {
383390
match self.on_exit.take() {
384-
Some(f) => f(result),
391+
Some(Execute(f)) => f(result),
392+
Some(SendMessage(ch)) => { ch.try_send(result); }
385393
None => {}
386394
}
387395
}

0 commit comments

Comments
 (0)