Skip to content

Commit

Permalink
Fix acquire/release semantis & guard thread-local instrumentation at …
Browse files Browse the repository at this point in the history
…exit of a thread
  • Loading branch information
SoilRos committed Oct 7, 2024
1 parent 02219f9 commit de883b0
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion memstats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ static std::vector<MemStatsInfo, MallocAllocator<MemStatsInfo>> memstats_events
// Zero- and dynamic-initialization of a thread-local variable does not necessarily happen on any order related to the global ones
static thread_local bool memstats_instrumentation_thread = init_memstats_instrumentation_thread();

// guard thread-local variable to instrument further delets at exit
bool init_memstats_instrumentation_thread_guard()
{
std::atexit([]{ memstats_instrumentation_thread = false; });
return true;
}
const static thread_local bool memstats_instrumentation_thread_guard = init_memstats_instrumentation_thread_guard();

// We need to make absolutely sure this is constinit so that 'memstats_instrumentation_global' is const-initialized,
// otherwise threads will try to syncronize with an uninitialized variable
#if MEMSTAT_ATOMIC_CONSTEXPR
Expand All @@ -154,7 +162,7 @@ bool init_memstats_instrumentation_guard()
else
std::cerr << "Option 'MEMSTATS_ENABLE_INSTRUMENTATION=" << ptr << "' not known. Fallback on default 'false'\n";
}
memstats_instrumentation_global.store(instrument, std::memory_order_acquire);
memstats_instrumentation_global.store(instrument, std::memory_order_release);
return instrument;
}

Expand Down

0 comments on commit de883b0

Please sign in to comment.