From 5c973142df3661a23a085bfb655300c08ca19764 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 23 Sep 2011 18:30:22 -0700 Subject: [PATCH] rt: Turn on cycle collection at task death; add a test case --- src/rt/rust_cc.cpp | 18 ++++++++++++++++-- src/rt/rust_task.cpp | 4 +++- src/rt/rust_task.h | 1 + src/test/run-pass/cycle-collection.rs | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/test/run-pass/cycle-collection.rs diff --git a/src/rt/rust_cc.cpp b/src/rt/rust_cc.cpp index c53ec1aa51c..70bb5aab1ed 100644 --- a/src/rt/rust_cc.cpp +++ b/src/rt/rust_cc.cpp @@ -17,6 +17,10 @@ #undef DPRINT #define DPRINT(fmt,...) fprintf(stderr, fmt, ##__VA_ARGS__) +// The number of allocations Rust code performs before performing cycle +// collection. +#define RUST_CC_FREQUENCY 5000 + namespace cc { // Internal reference count computation @@ -417,7 +421,7 @@ sweep(rust_task *task, const std::set &marked) { if (marked.find(alloc) == marked.end()) { const type_desc *tydesc = begin->second; - DPRINT("object is part of a cycle: %p\n", alloc); + //DPRINT("object is part of a cycle: %p\n", alloc); // Run the destructor. // TODO: What if it fails? @@ -453,8 +457,18 @@ do_cc(rust_task *task) { void maybe_cc(rust_task *task) { static debug::flag zeal("RUST_CC_ZEAL"); - if (*zeal) + if (*zeal) { do_cc(task); + return; + } + + // FIXME: Needs a snapshot. +#if 0 + if (task->cc_counter++ > RUST_CC_FREQUENCY) { + task->cc_counter = 0; + do_cc(task); + } +#endif } } // end namespace cc diff --git a/src/rt/rust_task.cpp b/src/rt/rust_task.cpp index f2c866263cf..97d68e6be39 100644 --- a/src/rt/rust_task.cpp +++ b/src/rt/rust_task.cpp @@ -1,5 +1,6 @@ #include "rust_internal.h" +#include "rust_cc.h" #include "valgrind.h" #include "memcheck.h" @@ -75,7 +76,8 @@ rust_task::rust_task(rust_scheduler *sched, rust_task_list *state, failed(false), killed(false), propagate_failure(true), - dynastack(this) + dynastack(this), + cc_counter(0) { LOGPTR(sched, "new task", (uintptr_t)this); DLOG(sched, task, "sizeof(task) = %d (0x%x)", sizeof *this, sizeof *this); diff --git a/src/rt/rust_task.h b/src/rt/rust_task.h index f364d2a67f7..fe19e06e657 100644 --- a/src/rt/rust_task.h +++ b/src/rt/rust_task.h @@ -125,6 +125,7 @@ rust_task : public kernel_owned, rust_cond rust_obstack dynastack; std::map local_allocs; + uint32_t cc_counter; debug::task_debug_info debug; diff --git a/src/test/run-pass/cycle-collection.rs b/src/test/run-pass/cycle-collection.rs new file mode 100644 index 00000000000..8cb2bc7979f --- /dev/null +++ b/src/test/run-pass/cycle-collection.rs @@ -0,0 +1,14 @@ +tag taggy { + cons(@mutable taggy); + nil; +} + +fn f() { + let box = @mutable nil; + *box = cons(box); +} + +fn main() { + f(); +} +