Simplify getRootCgroup()

Static local initializers are atomic in C++.
This commit is contained in:
Parker Hoyes 2024-09-04 18:11:16 +00:00
parent 4c88deef38
commit 03484641a1

View file

@ -6,7 +6,6 @@
#include <chrono> #include <chrono>
#include <cmath> #include <cmath>
#include <mutex>
#include <regex> #include <regex>
#include <unordered_set> #include <unordered_set>
#include <thread> #include <thread>
@ -158,22 +157,10 @@ std::string getCurrentCgroup()
return ourCgroup; return ourCgroup;
} }
static std::optional<std::string> rootCgroup;
static std::mutex rootCgroupMutex;
std::string getRootCgroup() std::string getRootCgroup()
{ {
{ static std::string rootCgroup = getCurrentCgroup();
std::lock_guard<std::mutex> guard(rootCgroupMutex); return rootCgroup;
if (rootCgroup)
return *rootCgroup;
}
auto current = getCurrentCgroup();
std::lock_guard<std::mutex> guard(rootCgroupMutex);
if (rootCgroup)
return *rootCgroup;
rootCgroup = current;
return current;
} }
} }