Skip to content

Commit 6d33e5d

Browse files
committed
Fix signal fences (#217)
1 parent 29fe735 commit 6d33e5d

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

bindings/profilers/wall.cc

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -836,10 +836,9 @@ bool WallProfiler::waitForSignal(uint64_t targetCallCount) {
836836
0, std::chrono::nanoseconds(samplingPeriod_ * maxRetries).count()};
837837
nanosleep(&ts, nullptr);
838838
#endif
839-
auto res =
840-
noCollectCallCount_.load(std::memory_order_relaxed) >= targetCallCount;
841-
std::atomic_signal_fence(std::memory_order_release);
842-
return res;
839+
auto res = noCollectCallCount_.load(std::memory_order_relaxed);
840+
std::atomic_signal_fence(std::memory_order_acquire);
841+
return res >= targetCallCount;
843842
}
844843

845844
Result WallProfiler::StopImpl(bool restart, v8::Local<v8::Value>& profile) {
@@ -850,14 +849,14 @@ Result WallProfiler::StopImpl(bool restart, v8::Local<v8::Value>& profile) {
850849
uint64_t callCount = 0;
851850
auto oldProfileId = profileId_;
852851
if (restart && workaroundV8Bug_) {
852+
std::atomic_signal_fence(std::memory_order_release);
853853
collectionMode_.store(CollectionMode::kNoCollect,
854854
std::memory_order_relaxed);
855-
std::atomic_signal_fence(std::memory_order_release);
856855
waitForSignal();
857856
} else if (withContexts_) {
857+
std::atomic_signal_fence(std::memory_order_release);
858858
collectionMode_.store(CollectionMode::kNoCollect,
859859
std::memory_order_relaxed);
860-
std::atomic_signal_fence(std::memory_order_release);
861860

862861
// make sure timestamp changes to avoid having samples from previous profile
863862
auto now = Now();
@@ -899,9 +898,9 @@ Result WallProfiler::StopImpl(bool restart, v8::Local<v8::Value>& profile) {
899898
auto now = Now();
900899
while (Now() == now) {
901900
}
901+
std::atomic_signal_fence(std::memory_order_release);
902902
collectionMode_.store(CollectionMode::kCollectContexts,
903903
std::memory_order_relaxed);
904-
std::atomic_signal_fence(std::memory_order_release);
905904
}
906905

907906
if (withContexts_) {
@@ -936,10 +935,10 @@ Result WallProfiler::StopImpl(bool restart, v8::Local<v8::Value>& profile) {
936935
Dispose(v8::Isolate::GetCurrent(), true);
937936
} else if (workaroundV8Bug_) {
938937
waitForSignal(callCount + 1);
938+
std::atomic_signal_fence(std::memory_order_release);
939939
collectionMode_.store(withContexts_ ? CollectionMode::kCollectContexts
940940
: CollectionMode::kPassThrough,
941941
std::memory_order_relaxed);
942-
std::atomic_signal_fence(std::memory_order_release);
943942
}
944943

945944
started_ = restart;
@@ -1089,18 +1088,12 @@ void WallProfiler::OnGCStart(v8::Isolate* isolate) {
10891088
if (curCount == 0) {
10901089
gcAsyncId = GetAsyncIdNoGC(isolate);
10911090
}
1092-
gcCount.store(curCount + 1, std::memory_order_relaxed);
10931091
std::atomic_signal_fence(std::memory_order_release);
1092+
gcCount.store(curCount + 1, std::memory_order_relaxed);
10941093
}
10951094

10961095
void WallProfiler::OnGCEnd() {
1097-
auto newCount = gcCount.load(std::memory_order_relaxed) - 1;
1098-
std::atomic_signal_fence(std::memory_order_acquire);
1099-
gcCount.store(newCount, std::memory_order_relaxed);
1100-
std::atomic_signal_fence(std::memory_order_release);
1101-
if (newCount == 0) {
1102-
gcAsyncId = -1;
1103-
}
1096+
gcCount.fetch_sub(1, std::memory_order_relaxed);
11041097
}
11051098

11061099
void WallProfiler::PushContext(int64_t time_from,

0 commit comments

Comments
 (0)