Skip to content

rustrt: Reorganize task usage #14886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libgreen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub fn start(argc: int, argv: **u8,
let mut ret = None;
simple::task().run(|| {
ret = Some(run(event_loop_factory, main.take_unwrap()));
});
}).destroy();
// unsafe is ok b/c we're sure that the runtime is gone
unsafe { rt::cleanup() }
ret.unwrap()
Expand Down
4 changes: 2 additions & 2 deletions src/libgreen/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ extern fn bootstrap_green_task(task: uint, code: *(), env: *()) -> ! {
// requested. This is the "try/catch" block for this green task and
// is the wrapper for *all* code run in the task.
let mut start = Some(start);
let task = task.swap().run(|| start.take_unwrap()());
let task = task.swap().run(|| start.take_unwrap()()).destroy();

// Once the function has exited, it's time to run the termination
// routine. This means we need to context switch one more time but
Expand All @@ -120,7 +120,7 @@ extern fn bootstrap_green_task(task: uint, code: *(), env: *()) -> ! {
// this we could add a `terminate` function to the `Runtime` trait
// in libstd, but that seems less appropriate since the coversion
// method exists.
GreenTask::convert(task).terminate()
GreenTask::convert(task).terminate();
}

impl GreenTask {
Expand Down
5 changes: 2 additions & 3 deletions src/libnative/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,12 @@ pub fn start(argc: int, argv: **u8, main: proc()) -> int {
let mut main = Some(main);
let mut task = task::new((my_stack_bottom, my_stack_top));
task.name = Some(str::Slice("<main>"));
let t = task.run(|| {
drop(task.run(|| {
unsafe {
rt::stack::record_stack_bounds(my_stack_bottom, my_stack_top);
}
exit_code = Some(run(main.take_unwrap()));
});
drop(t);
}).destroy());
unsafe { rt::cleanup(); }
// If the exit code wasn't set, then the task block must have failed.
return exit_code.unwrap_or(rt::DEFAULT_ERROR_CODE);
Expand Down
3 changes: 1 addition & 2 deletions src/libnative/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ pub fn spawn_opts(opts: TaskOpts, f: proc():Send) {
let mut f = Some(f);
let mut task = task;
task.put_runtime(ops);
let t = task.run(|| { f.take_unwrap()() });
drop(t);
drop(task.run(|| { f.take_unwrap()() }).destroy());
bookkeeping::decrement();
})
}
Expand Down
24 changes: 18 additions & 6 deletions src/librustrt/local_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ impl LocalHeap {
self.memory_region.free(alloc);
}

pub unsafe fn annihilate(&mut self) {
/// Immortalize all pending allocations, forcing them to live forever.
///
/// This function will freeze all allocations to prevent all pending
/// allocations from being deallocated. This is used in preparation for when
/// a task is about to destroy TLD.
pub unsafe fn immortalize(&mut self) {
let mut n_total_boxes = 0u;

// Pass 1: Make all boxes immortal.
Expand All @@ -122,6 +127,17 @@ impl LocalHeap {
(*alloc).ref_count = RC_IMMORTAL;
});

if debug_mem() {
// We do logging here w/o allocation.
rterrln!("total boxes annihilated: {}", n_total_boxes);
}
}

/// Continues deallocation of the all pending allocations in this arena.
///
/// This is invoked from the destructor, and requires that `immortalize` has
/// been called previously.
unsafe fn annihilate(&mut self) {
// Pass 2: Drop all boxes.
//
// In this pass, unique-managed boxes may get freed, but not
Expand All @@ -142,11 +158,6 @@ impl LocalHeap {
self.each_live_alloc(true, |me, alloc| {
me.free(alloc);
});

if debug_mem() {
// We do logging here w/o allocation.
rterrln!("total boxes annihilated: {}", n_total_boxes);
}
}

unsafe fn each_live_alloc(&mut self, read_next_before: bool,
Expand All @@ -170,6 +181,7 @@ impl LocalHeap {

impl Drop for LocalHeap {
fn drop(&mut self) {
unsafe { self.annihilate() }
assert!(self.live_allocs.is_null());
}
}
Expand Down
Loading