From 3c3d09b65fe9736b4282d53bbd098897fba3b9f9 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Fri, 8 Jan 2021 19:36:56 -0800 Subject: [PATCH] Fix a tsan warning in features_t --- src/future_feature_flags.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/future_feature_flags.h b/src/future_feature_flags.h index 0140ea82a..e31b306e7 100644 --- a/src/future_feature_flags.h +++ b/src/future_feature_flags.h @@ -4,6 +4,7 @@ #include +#include #include #include "common.h" @@ -28,13 +29,13 @@ class features_t { /// Return whether a flag is set. bool test(flag_t f) const { assert(f >= 0 && f < flag_count && "Invalid flag"); - return values[f]; + return values[f].load(std::memory_order_relaxed); } /// Set a flag. void set(flag_t f, bool value) { assert(f >= 0 && f < flag_count && "Invalid flag"); - values[f] = value; + values[f].store(value, std::memory_order_relaxed); } /// Parses a comma-separated feature-flag string, updating ourselves with the values. @@ -69,9 +70,20 @@ class features_t { features_t(); + features_t(const features_t &rhs) { *this = rhs; } + + void operator=(const features_t &rhs) { + for (int i = 0; i < flag_count; i++) { + flag_t f = static_cast(i); + this->set(f, rhs.test(f)); + } + } + private: - /// Values for the flags. - bool values[flag_count] = {}; + // Values for the flags. + // These are atomic to "fix" a race reported by tsan where tests of feature flags and other + // tests which use them conceptually race. + std::atomic values[flag_count]{}; }; /// Return the global set of features for fish. This is const to prevent accidental mutation.