diff options
author | Jamie Liu <jamieliu@google.com> | 2019-01-09 14:26:52 -0800 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2019-01-09 14:27:59 -0800 |
commit | 0676843875b783e8157fc062b2bb322948b4f29a (patch) | |
tree | 8230f29c763bf7aae961776732b153821705a354 | |
parent | 252e57992d0a5872bb0f1f41c38c004662a729de (diff) |
Make CooperativeSaveEnabled() async-signal-safe(r).
The static local variable `enabled` in CooperativeSaveEnabled() is not
initialized until the first call to CooperativeSaveEnabled(), per the
C++14 standard, section 6.7 ("Declaration statement"), paragraph 4. This
initialization is thread-safe as of C++11, but it is *not* required to
be async-signal-safe. Use a namespace-scope variable instead, since this
is guaranteed to be zero-initialized before main() by section 3.6.2
("Initialization of non-local variables").
getenv() is technically not async-signal-safe either, hence the hedging
in the change summary line. However, glibc's implementation of getenv()
appears to be async-signal-safe in the absence of calls to setenv().
PiperOrigin-RevId: 228588617
Change-Id: I669f555d1c91352d55c606970bb237ec888fa7ca
-rw-r--r-- | test/util/save_util.cc | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/test/util/save_util.cc b/test/util/save_util.cc index 71f4078a7..5540e2146 100644 --- a/test/util/save_util.cc +++ b/test/util/save_util.cc @@ -27,9 +27,23 @@ namespace gvisor { namespace testing { namespace { +enum class CooperativeSaveMode { + kUnknown = 0, // cooperative_save_mode is statically-initialized to 0 + kAvailable, + kNotAvailable, +}; + +std::atomic<CooperativeSaveMode> cooperative_save_mode; + bool CooperativeSaveEnabled() { - static bool enabled = getenv(GVISOR_COOPERATIVE_SAVE_TEST) != nullptr; - return enabled; + auto mode = cooperative_save_mode.load(); + if (mode == CooperativeSaveMode::kUnknown) { + mode = (getenv(GVISOR_COOPERATIVE_SAVE_TEST) != nullptr) + ? CooperativeSaveMode::kAvailable + : CooperativeSaveMode::kNotAvailable; + cooperative_save_mode.store(mode); + } + return mode == CooperativeSaveMode::kAvailable; } std::atomic<int> save_disable; |