]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/ceph_context: Fix std::atomic<std::shared_ptr> compatibility 62493/head
authorKefu Chai <tchaikov@gmail.com>
Tue, 25 Mar 2025 13:40:00 +0000 (21:40 +0800)
committerKefu Chai <tchaikov@gmail.com>
Thu, 27 Mar 2025 00:05:55 +0000 (08:05 +0800)
Previously, we relied on the __GNUC__ macro to check for std::atomic<std::shared_ptr>
support, which was inaccurate. This approach failed with Clang builds using libstdc++,
even when the feature was implemented. The warning looks like:

```
/usr/lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/shared_ptr_atomic.h:131:5: note: 'atomic_load_explicit<std::vector<entity_addrvec_t>>' has been explicitly marked deprecated here
  131 |     _GLIBCXX20_DEPRECATED_SUGGEST("std::atomic<std::shared_ptr<T>>")
      |     ^
```

This change uses a standard-compliant feature test macro (__cpp_lib_atomic_shared_ptr)
to correctly detect support for std::atomic<std::shared_ptr>. This resolves compilation
issues and improves portability across different compilers and standard library
implementations.

Refs 5b0d849730ce20d68ffafcb612c5f6fc8b87dd9a

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/common/ceph_context.h

index 103f39fb2913ec2384b7d127dfdc363ae54a5304..5a2619a8f2be6795060e3dfb5df7cf2f22989d7e 100644 (file)
@@ -283,19 +283,17 @@ public:
   void set_mon_addrs(const MonMap& mm);
   void set_mon_addrs(const std::vector<entity_addrvec_t>& in) {
     auto ptr = std::make_shared<std::vector<entity_addrvec_t>>(in);
-#if defined(__GNUC__) && __GNUC__ < 12
-    // workaround for GCC 11 bug
-    atomic_store_explicit(&_mon_addrs, std::move(ptr), std::memory_order_relaxed);
-#else
+#ifdef __cpp_lib_atomic_shared_ptr
     _mon_addrs.store(std::move(ptr), std::memory_order_relaxed);
+#else
+    atomic_store_explicit(&_mon_addrs, std::move(ptr), std::memory_order_relaxed);
 #endif
   }
   std::shared_ptr<std::vector<entity_addrvec_t>> get_mon_addrs() const {
-#if defined(__GNUC__) && __GNUC__ < 12
-    // workaround for GCC 11 bug
-    auto ptr = atomic_load_explicit(&_mon_addrs, std::memory_order_relaxed);
-#else
+#ifdef __cpp_lib_atomic_shared_ptr
     auto ptr = _mon_addrs.load(std::memory_order_relaxed);
+#else
+    auto ptr = atomic_load_explicit(&_mon_addrs, std::memory_order_relaxed);
 #endif
     return ptr;
   }
@@ -317,11 +315,10 @@ private:
 
   int _crypto_inited;
 
-#if defined(__GNUC__) && __GNUC__ < 12
-  // workaround for GCC 11 bug
-  std::shared_ptr<std::vector<entity_addrvec_t>> _mon_addrs;
-#else
+#ifdef __cpp_lib_atomic_shared_ptr
   std::atomic<std::shared_ptr<std::vector<entity_addrvec_t>>> _mon_addrs;
+#else
+  std::shared_ptr<std::vector<entity_addrvec_t>> _mon_addrs;
 #endif
 
   /* libcommon service thread.