Skip to content

Commit dbdeff6

Browse files
committed
rt: Factor out the logic that handles the various magic debug environment variables
1 parent f8007b5 commit dbdeff6

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

src/rt/rust_cc.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Rust cycle collector. Temporary, but will probably stick around for some
22
// time until LLVM's GC infrastructure is more mature.
33

4+
#include "rust_debug.h"
45
#include "rust_gc.h"
56
#include "rust_internal.h"
67
#include "rust_shape.h"
@@ -434,14 +435,8 @@ do_cc(rust_task *task) {
434435

435436
void
436437
maybe_cc(rust_task *task) {
437-
// FIXME: We ought to lock this.
438-
static int zeal = -1;
439-
if (zeal == -1) {
440-
char *ev = getenv("RUST_CC_ZEAL");
441-
zeal = ev && ev[0] != '\0' && ev[0] != '0';
442-
}
443-
444-
if (zeal)
438+
static debug::flag zeal("RUST_CC_ZEAL");
439+
if (*zeal)
445440
do_cc(task);
446441
}
447442

src/rt/rust_debug.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Routines useful when debugging the Rust runtime.
2+
3+
#ifndef RUST_DEBUG_H
4+
#define RUST_DEBUG_H
5+
6+
#include <cstdlib>
7+
8+
namespace debug {
9+
10+
class flag {
11+
private:
12+
const char *name;
13+
bool valid;
14+
bool value;
15+
16+
public:
17+
flag(const char *in_name) : name(in_name), valid(false) {}
18+
19+
bool operator*() {
20+
// FIXME: We ought to lock this.
21+
if (!valid) {
22+
char *ev = getenv(name);
23+
value = ev && ev[0] != '\0' && ev[0] != '0';
24+
valid = true;
25+
}
26+
return value;
27+
}
28+
};
29+
30+
} // end namespace debug
31+
32+
#endif
33+

src/rt/rust_gc.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdint.h>
88

99
#include "rust_abi.h"
10+
#include "rust_debug.h"
1011
#include "rust_gc.h"
1112
#include "rust_internal.h"
1213
#include "rust_shape.h"
@@ -180,14 +181,9 @@ maybe_gc(rust_task *task) {
180181
if (*safe_point_data == NULL)
181182
return;
182183

183-
// FIXME: We ought to lock this.
184-
static int zeal = -1;
185-
if (zeal == -1) {
186-
char *ev = getenv("RUST_GC_ZEAL");
187-
zeal = ev && ev[0] != '\0' && ev[0] != '0';
188-
}
184+
static debug::flag zeal("RUST_GC_ZEAL");
189185

190-
if (zeal) {
186+
if (*zeal) {
191187
gc gc(task);
192188
gc.run();
193189
}

0 commit comments

Comments
 (0)