Skip to content

Commit

Permalink
Replace static local initializers with call_once.
Browse files Browse the repository at this point in the history
This makes potential descheduling more explicit than can occur with static
locals.

PiperOrigin-RevId: 570711087
Change-Id: I10bdfe6f26712d079a68096a3f8b92157708b793
  • Loading branch information
ckennelly authored and copybara-github committed Oct 4, 2023
1 parent d81e70d commit 10e3f89
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 17 deletions.
2 changes: 2 additions & 0 deletions tcmalloc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ cc_library(
"//tcmalloc/internal:config",
"//tcmalloc/internal:environment",
"//tcmalloc/internal:logging",
"@com_google_absl//absl/base",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/functional:function_ref",
"@com_google_absl//absl/strings",
Expand Down Expand Up @@ -1096,6 +1097,7 @@ cc_library(
],
deps = [
"//tcmalloc/internal:parameter_accessors",
"@com_google_absl//absl/base",
"@com_google_absl//absl/base:config",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:malloc_internal",
Expand Down
16 changes: 9 additions & 7 deletions tcmalloc/experiment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <algorithm>
#include <string>

#include "absl/base/attributes.h"
#include "absl/base/call_once.h"
#include "absl/base/macros.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
Expand Down Expand Up @@ -49,16 +51,16 @@ bool LookupExperimentID(absl::string_view label, Experiment* exp) {
}

const bool* GetSelectedExperiments() {
static bool by_id[kNumExperiments];
ABSL_CONST_INIT static bool by_id[kNumExperiments];
ABSL_CONST_INIT static absl::once_flag flag;

static const bool* status = [&]() {
absl::base_internal::LowLevelCallOnce(&flag, [&]() {
const char* active_experiments = thread_safe_getenv(kExperiments);
const char* disabled_experiments = thread_safe_getenv(kDisableExperiments);
return SelectExperiments(by_id,
active_experiments ? active_experiments : "",
disabled_experiments ? disabled_experiments : "");
}();
return status;
SelectExperiments(by_id, active_experiments ? active_experiments : "",
disabled_experiments ? disabled_experiments : "");
});
return by_id;
}

template <typename F>
Expand Down
1 change: 1 addition & 0 deletions tcmalloc/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ cc_library(
],
deps = [
":config",
"@com_google_absl//absl/base",
"@com_google_absl//absl/base:core_headers",
],
)
Expand Down
14 changes: 10 additions & 4 deletions tcmalloc/internal/page_size.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@

#include <unistd.h>

#include "absl/base/attributes.h"
#include "absl/base/call_once.h"

GOOGLE_MALLOC_SECTION_BEGIN
namespace tcmalloc {
namespace tcmalloc_internal {

size_t GetPageSize() {
static const size_t page_size = []() -> size_t {
ABSL_CONST_INIT static size_t page_size;
ABSL_CONST_INIT static absl::once_flag flag;

absl::base_internal::LowLevelCallOnce(&flag, [&]() {
#if defined(__wasm__) || defined(__asmjs__)
return static_cast<size_t>(getpagesize());
page_size = static_cast<size_t>(getpagesize());
#else
return static_cast<size_t>(sysconf(_SC_PAGESIZE));
page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
#endif
}();
});
return page_size;
}

Expand Down
9 changes: 7 additions & 2 deletions tcmalloc/malloc_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <string>

#include "absl/base/attributes.h"
#include "absl/base/call_once.h"
#include "absl/base/internal/low_level_alloc.h"
#include "absl/memory/memory.h"
#include "absl/time/time.h"
Expand Down Expand Up @@ -183,8 +184,12 @@ size_t AddressRegionFactory::InternalBytesAllocated() {

void* AddressRegionFactory::MallocInternal(size_t size) {
// Use arena without malloc hooks to avoid HeapChecker reporting a leak.
static auto* arena =
absl::base_internal::LowLevelAlloc::NewArena(/*flags=*/0);
ABSL_CONST_INIT static absl::base_internal::LowLevelAlloc::Arena* arena;
ABSL_CONST_INIT static absl::once_flag flag;

absl::base_internal::LowLevelCallOnce(&flag, [&]() {
arena = absl::base_internal::LowLevelAlloc::NewArena(/*flags=*/0);
});
void* result =
absl::base_internal::LowLevelAlloc::AllocWithArena(size, arena);
if (result) {
Expand Down
8 changes: 7 additions & 1 deletion tcmalloc/page_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <limits>
#include <new>

#include "absl/base/attributes.h"
#include "absl/base/call_once.h"
#include "tcmalloc/common.h"
#include "tcmalloc/experiment.h"
#include "tcmalloc/experiment_config.h"
Expand Down Expand Up @@ -87,7 +89,11 @@ bool decide_want_hpaa() {
}

bool want_hpaa() {
static bool use = decide_want_hpaa();
ABSL_CONST_INIT static bool use;
ABSL_CONST_INIT static absl::once_flag flag;

absl::base_internal::LowLevelCallOnce(&flag,
[]() { use = decide_want_hpaa(); });

return use;
}
Expand Down
10 changes: 7 additions & 3 deletions tcmalloc/system-alloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <utility>

#include "absl/base/attributes.h"
#include "absl/base/call_once.h"
#include "absl/base/const_init.h"
#include "absl/base/internal/spinlock.h"
#include "absl/base/macros.h"
Expand Down Expand Up @@ -522,7 +523,10 @@ void SetRegionFactory(AddressRegionFactory* factory) {
static uintptr_t RandomMmapHint(size_t size, size_t alignment,
const MemoryTag tag) {
// Rely on kernel's mmap randomization to seed our RNG.
static uintptr_t rnd = []() {
ABSL_CONST_INIT static uintptr_t rnd;
ABSL_CONST_INIT static absl::once_flag flag;

absl::base_internal::LowLevelCallOnce(&flag, [&]() {
void* seed =
mmap(nullptr, kPageSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (seed == MAP_FAILED) {
Expand All @@ -531,8 +535,8 @@ static uintptr_t RandomMmapHint(size_t size, size_t alignment,
kPageSize);
}
munmap(seed, kPageSize);
return reinterpret_cast<uintptr_t>(seed);
}();
rnd = reinterpret_cast<uintptr_t>(seed);
});

#if !defined(MEMORY_SANITIZER) && !defined(THREAD_SANITIZER)
// We don't use the following bits:
Expand Down

0 comments on commit 10e3f89

Please sign in to comment.