diff options
author | ayles <ayles@yandex-team.com> | 2022-09-19 12:27:43 +0300 |
---|---|---|
committer | ayles <ayles@yandex-team.com> | 2022-09-19 12:27:43 +0300 |
commit | ff49fbec3c655b16509b12a0f3a82efacd521771 (patch) | |
tree | e14ab4fda50eaf34e8f2c2be86ab2cb055eb96e2 | |
parent | 94de3c44fbfb54ffa1ea7a8baa73d174ee6f40b1 (diff) | |
download | ydb-ff49fbec3c655b16509b12a0f3a82efacd521771.tar.gz |
Fix tsan warnings in yt intrusive ptr
For example, https://github.com/google/sanitizers/issues/1352 - tsan is not working well with std::atomic_thread_fence
Minimal reproducible example (and one that bothers most in every fiber-aware service):
```
auto threadPool = NYT::New<NYT::NConcurrency::TThreadPool>(2, "thread");
TVector<NYT::TFuture<void>> futures;
for (size_t i = 0; i < 100000; ++i) {
futures.emplace_back(BIND([]() {
}).AsyncVia(threadPool->GetInvoker()).Run());
}
for (auto& future : futures) {
future.Get().ThrowOnError();
}
```
-rw-r--r-- | library/cpp/yt/memory/ref_counted-inl.h | 4 | ||||
-rw-r--r-- | util/system/sanitizers.h | 21 |
2 files changed, 25 insertions, 0 deletions
diff --git a/library/cpp/yt/memory/ref_counted-inl.h b/library/cpp/yt/memory/ref_counted-inl.h index 1ad7433ca9..bb29f70585 100644 --- a/library/cpp/yt/memory/ref_counted-inl.h +++ b/library/cpp/yt/memory/ref_counted-inl.h @@ -4,6 +4,8 @@ #include "ref_counted.h" #endif +#include <util/system/sanitizers.h> + namespace NYT { //////////////////////////////////////////////////////////////////////////////// @@ -97,6 +99,7 @@ Y_FORCE_INLINE bool TRefCounter::Unref(int n) const YT_ASSERT(oldStrongCount >= n); if (oldStrongCount == n) { std::atomic_thread_fence(std::memory_order_acquire); + NSan::Acquire(&StrongCount_); return true; } else { return false; @@ -120,6 +123,7 @@ Y_FORCE_INLINE bool TRefCounter::WeakUnref() const YT_ASSERT(oldWeakCount > 0); if (oldWeakCount == 1) { std::atomic_thread_fence(std::memory_order_acquire); + NSan::Acquire(&WeakCount_); return true; } else { return false; diff --git a/util/system/sanitizers.h b/util/system/sanitizers.h index 965e5c751e..3ff42ca15e 100644 --- a/util/system/sanitizers.h +++ b/util/system/sanitizers.h @@ -14,6 +14,11 @@ extern "C" { // sanitizers API void __msan_check_mem_is_initialized(const volatile void* x, size_t size); #endif +#if defined(_tsan_enabled_) + void __tsan_acquire(void* a); + void __tsan_release(void* a); +#endif + }; // sanitizers API namespace NSan { @@ -138,4 +143,20 @@ namespace NSan { Y_UNUSED(description); } #endif + + inline static void Acquire(void* a) { +#if defined(_tsan_enabled_) + __tsan_acquire(a); +#else + Y_UNUSED(a); +#endif + } + + inline static void Release(void* a) { +#if defined(_tsan_enabled_) + __tsan_release(a); +#else + Y_UNUSED(a); +#endif + } } |