diff --git a/src/rt/rust_cc.cpp b/src/rt/rust_cc.cpp index 1b5890358c9..4befb36d819 100644 --- a/src/rt/rust_cc.cpp +++ b/src/rt/rust_cc.cpp @@ -1,6 +1,7 @@ // Rust cycle collector. Temporary, but will probably stick around for some // time until LLVM's GC infrastructure is more mature. +#include "rust_debug.h" #include "rust_gc.h" #include "rust_internal.h" #include "rust_shape.h" @@ -434,14 +435,8 @@ do_cc(rust_task *task) { void maybe_cc(rust_task *task) { - // FIXME: We ought to lock this. - static int zeal = -1; - if (zeal == -1) { - char *ev = getenv("RUST_CC_ZEAL"); - zeal = ev && ev[0] != '\0' && ev[0] != '0'; - } - - if (zeal) + static debug::flag zeal("RUST_CC_ZEAL"); + if (*zeal) do_cc(task); } diff --git a/src/rt/rust_debug.h b/src/rt/rust_debug.h new file mode 100644 index 00000000000..f7c444324f7 --- /dev/null +++ b/src/rt/rust_debug.h @@ -0,0 +1,33 @@ +// Routines useful when debugging the Rust runtime. + +#ifndef RUST_DEBUG_H +#define RUST_DEBUG_H + +#include + +namespace debug { + +class flag { +private: + const char *name; + bool valid; + bool value; + +public: + flag(const char *in_name) : name(in_name), valid(false) {} + + bool operator*() { + // FIXME: We ought to lock this. + if (!valid) { + char *ev = getenv(name); + value = ev && ev[0] != '\0' && ev[0] != '0'; + valid = true; + } + return value; + } +}; + +} // end namespace debug + +#endif + diff --git a/src/rt/rust_gc.cpp b/src/rt/rust_gc.cpp index 6e6a5dd766f..6e698de59db 100644 --- a/src/rt/rust_gc.cpp +++ b/src/rt/rust_gc.cpp @@ -7,6 +7,7 @@ #include #include "rust_abi.h" +#include "rust_debug.h" #include "rust_gc.h" #include "rust_internal.h" #include "rust_shape.h" @@ -180,14 +181,9 @@ maybe_gc(rust_task *task) { if (*safe_point_data == NULL) return; - // FIXME: We ought to lock this. - static int zeal = -1; - if (zeal == -1) { - char *ev = getenv("RUST_GC_ZEAL"); - zeal = ev && ev[0] != '\0' && ev[0] != '0'; - } + static debug::flag zeal("RUST_GC_ZEAL"); - if (zeal) { + if (*zeal) { gc gc(task); gc.run(); }