Skip to content

Commit 1e2ed00

Browse files
committed
Safe atomics for profiler map
1 parent 547a14e commit 1e2ed00

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

bindings/profilers/wall.cc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,21 @@ static std::atomic<ProfilerMap*> profilers(new ProfilerMap());
4444
static void (*old_handler)(int, siginfo_t*, void*) = nullptr;
4545

4646
static void sighandler(int sig, siginfo_t* info, void* context) {
47-
auto prof_map = profilers.load();
48-
auto prof_it = prof_map->find(Isolate::GetCurrent());
47+
auto isolate = Isolate::GetCurrent();
48+
WallProfiler* prof = nullptr;
49+
auto prof_map = profilers.exchange(nullptr, std::memory_order_acq_rel);
50+
if (prof_map) {
51+
auto prof_it = prof_map->find(isolate);
52+
if (prof_it != prof_map->end()) {
53+
prof = prof_it->second;
54+
}
55+
profilers.store(prof_map, std::memory_order_release);
56+
}
4957
if (old_handler) {
5058
old_handler(sig, info, context);
5159
}
52-
if (prof_it != prof_map->end()) {
53-
prof_it->second->PushContext();
60+
if (prof) {
61+
prof->PushContext();
5462
}
5563
}
5664
#endif
@@ -383,11 +391,15 @@ WallProfiler::~WallProfiler() {
383391

384392
template <typename F>
385393
void updateProfilers(F updateFn) {
386-
auto currProfilers = profilers.load();
394+
auto currProfilers = profilers.load(std::memory_order_acquire);
387395
for (;;) {
396+
while (!currProfilers) {
397+
currProfilers = profilers.load(std::memory_order_relaxed);
398+
}
388399
auto newProfilers = new ProfilerMap(*currProfilers);
389400
updateFn(newProfilers);
390-
if (profilers.compare_exchange_weak(currProfilers, newProfilers)) {
401+
if (profilers.compare_exchange_weak(
402+
currProfilers, newProfilers, std::memory_order_acq_rel)) {
391403
delete currProfilers;
392404
break;
393405
} else {

0 commit comments

Comments
 (0)