Skip to content

Commit 2e6dc16

Browse files
committedJul 31, 2013
Give tasks useful names. #2891
1 parent 389aba0 commit 2e6dc16

File tree

5 files changed

+82
-12
lines changed

5 files changed

+82
-12
lines changed
 

‎src/libstd/rt/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,14 @@ fn run_(main: ~fn(), use_main_sched: bool) -> int {
316316
// Just put an unpinned task onto one of the default schedulers.
317317
let mut main_task = ~Task::new_root(&mut scheds[0].stack_pool, main);
318318
main_task.death.on_exit = Some(on_exit);
319+
main_task.name = Some(~"main");
319320
scheds[0].enqueue_task(main_task);
320321
}
321322
Some(ref mut main_sched) => {
322323
let home = Sched(main_sched.make_handle());
323324
let mut main_task = ~Task::new_root_homed(&mut scheds[0].stack_pool, home, main);
324325
main_task.death.on_exit = Some(on_exit);
326+
main_task.name = Some(~"main");
325327
main_sched.enqueue_task(main_task);
326328
}
327329
};

‎src/libstd/rt/task.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ pub struct Task {
4040
taskgroup: Option<Taskgroup>,
4141
death: Death,
4242
destroyed: bool,
43-
coroutine: Option<~Coroutine>
43+
coroutine: Option<~Coroutine>,
44+
// FIXME(#6874/#7599) use StringRef to save on allocations
45+
name: Option<~str>,
4446
}
4547

4648
pub struct Coroutine {
@@ -90,7 +92,8 @@ impl Task {
9092
taskgroup: None,
9193
death: Death::new(),
9294
destroyed: false,
93-
coroutine: Some(~Coroutine::new(stack_pool, start))
95+
coroutine: Some(~Coroutine::new(stack_pool, start)),
96+
name: None,
9497
}
9598
}
9699

@@ -109,7 +112,8 @@ impl Task {
109112
// FIXME(#7544) make watching optional
110113
death: self.death.new_child(),
111114
destroyed: false,
112-
coroutine: Some(~Coroutine::new(stack_pool, start))
115+
coroutine: Some(~Coroutine::new(stack_pool, start)),
116+
name: None,
113117
}
114118
}
115119

‎src/libstd/sys.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@ impl FailWithCause for &'static str {
137137

138138
// FIXME #4427: Temporary until rt::rt_fail_ goes away
139139
pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
140-
use cell::Cell;
141140
use either::Left;
141+
use option::{Some, None};
142142
use rt::{context, OldTaskContext, TaskContext};
143143
use rt::task::Task;
144144
use rt::local::Local;
145145
use rt::logging::Logger;
146+
use str::Str;
146147

147148
let context = context();
148149
match context {
@@ -159,20 +160,26 @@ pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
159160
let msg = str::raw::from_c_str(msg);
160161
let file = str::raw::from_c_str(file);
161162

162-
let outmsg = fmt!("task failed at '%s', %s:%i",
163-
msg, file, line as int);
164-
165163
// XXX: Logging doesn't work correctly in non-task context because it
166164
// invokes the local heap
167165
if context == TaskContext {
168166
// XXX: Logging doesn't work here - the check to call the log
169167
// function never passes - so calling the log function directly.
170-
let outmsg = Cell::new(outmsg);
171168
do Local::borrow::<Task, ()> |task| {
172-
task.logger.log(Left(outmsg.take()));
169+
let msg = match task.name {
170+
Some(ref name) =>
171+
fmt!("task '%s' failed at '%s', %s:%i",
172+
name.as_slice(), msg, file, line as int),
173+
None =>
174+
fmt!("task <unnamed> failed at '%s', %s:%i",
175+
msg, file, line as int)
176+
};
177+
178+
task.logger.log(Left(msg));
173179
}
174180
} else {
175-
rterrln!("%s", outmsg);
181+
rterrln!("failed in non-task context at '%s', %s:%i",
182+
msg, file, line as int);
176183
}
177184

178185
gc::cleanup_stack_for_failure();

‎src/libstd/task/mod.rs

+57-2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ pub struct SchedOpts {
120120
*
121121
* * notify_chan - Enable lifecycle notifications on the given channel
122122
*
123+
* * name - A name for the task-to-be, for identification in failure messages.
124+
*
123125
* * sched - Specify the configuration of a new scheduler to create the task
124126
* in
125127
*
@@ -139,6 +141,7 @@ pub struct TaskOpts {
139141
watched: bool,
140142
indestructible: bool,
141143
notify_chan: Option<Chan<TaskResult>>,
144+
name: Option<~str>,
142145
sched: SchedOpts
143146
}
144147

@@ -185,23 +188,23 @@ impl TaskBuilder {
185188
self.consumed = true;
186189
let gen_body = self.gen_body.take();
187190
let notify_chan = self.opts.notify_chan.take();
191+
let name = self.opts.name.take();
188192
TaskBuilder {
189193
opts: TaskOpts {
190194
linked: self.opts.linked,
191195
supervised: self.opts.supervised,
192196
watched: self.opts.watched,
193197
indestructible: self.opts.indestructible,
194198
notify_chan: notify_chan,
199+
name: name,
195200
sched: self.opts.sched
196201
},
197202
gen_body: gen_body,
198203
can_not_copy: None,
199204
consumed: false
200205
}
201206
}
202-
}
203207

204-
impl TaskBuilder {
205208
/// Decouple the child task's failure from the parent's. If either fails,
206209
/// the other will not be killed.
207210
pub fn unlinked(&mut self) {
@@ -281,6 +284,12 @@ impl TaskBuilder {
281284
self.opts.notify_chan = Some(notify_pipe_ch);
282285
}
283286

287+
/// Name the task-to-be. Currently the name is used for identification
288+
/// only in failure messages.
289+
pub fn name(&mut self, name: ~str) {
290+
self.opts.name = Some(name);
291+
}
292+
284293
/// Configure a custom scheduler mode for the task.
285294
pub fn sched_mode(&mut self, mode: SchedMode) {
286295
self.opts.sched.mode = mode;
@@ -333,13 +342,15 @@ impl TaskBuilder {
333342
pub fn spawn(&mut self, f: ~fn()) {
334343
let gen_body = self.gen_body.take();
335344
let notify_chan = self.opts.notify_chan.take();
345+
let name = self.opts.name.take();
336346
let x = self.consume();
337347
let opts = TaskOpts {
338348
linked: x.opts.linked,
339349
supervised: x.opts.supervised,
340350
watched: x.opts.watched,
341351
indestructible: x.opts.indestructible,
342352
notify_chan: notify_chan,
353+
name: name,
343354
sched: x.opts.sched
344355
};
345356
let f = match gen_body {
@@ -408,6 +419,7 @@ pub fn default_task_opts() -> TaskOpts {
408419
watched: true,
409420
indestructible: false,
410421
notify_chan: None,
422+
name: None,
411423
sched: SchedOpts {
412424
mode: DefaultScheduler,
413425
}
@@ -507,6 +519,21 @@ pub fn try<T:Send>(f: ~fn() -> T) -> Result<T,()> {
507519

508520
/* Lifecycle functions */
509521

522+
/// Read the name of the current task.
523+
pub fn with_task_name<U>(blk: &fn(Option<&str>) -> U) -> U {
524+
use rt::task::Task;
525+
526+
match context() {
527+
TaskContext => do Local::borrow::<Task, U> |task| {
528+
match task.name {
529+
Some(ref name) => blk(Some(name.as_slice())),
530+
None => blk(None)
531+
}
532+
},
533+
_ => fail!("no task name exists in %?", context()),
534+
}
535+
}
536+
510537
pub fn yield() {
511538
//! Yield control to the task scheduler
512539
@@ -805,6 +832,34 @@ fn test_spawn_linked_sup_propagate_sibling() {
805832
fail!();
806833
}
807834

835+
#[test]
836+
fn test_unnamed_task() {
837+
use rt::test::run_in_newsched_task;
838+
839+
do run_in_newsched_task {
840+
do spawn {
841+
do with_task_name |name| {
842+
assert!(name.is_none());
843+
}
844+
}
845+
}
846+
}
847+
848+
#[test]
849+
fn test_named_task() {
850+
use rt::test::run_in_newsched_task;
851+
852+
do run_in_newsched_task {
853+
let mut t = task();
854+
t.name(~"ada lovelace");
855+
do t.spawn {
856+
do with_task_name |name| {
857+
assert!(name.get() == "ada lovelace");
858+
}
859+
}
860+
}
861+
}
862+
808863
#[test]
809864
fn test_run_basic() {
810865
let (po, ch) = stream::<()>();

‎src/libstd/task/spawn.rs

+2
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,8 @@ fn spawn_raw_newsched(mut opts: TaskOpts, f: ~fn()) {
726726
task.death.on_exit = Some(on_exit);
727727
}
728728

729+
task.name = opts.name.take();
730+
729731
rtdebug!("spawn about to take scheduler");
730732

731733
let sched = Local::take::<Scheduler>();

0 commit comments

Comments
 (0)
Please sign in to comment.