From db8ef89b00d37ecec82e4fda3611f8f5a4f8cbfe Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum <yfeldblum@fb.com> Date: Wed, 10 Nov 2021 16:22:29 -0800 Subject: [PATCH] let c/x expected and value params be dependent Summary: Let their types be determined by the atomic parameter only so that conversions may happen at the call-site. This is what the `std::atomic_compare_exchange` functions do. Those functions are specified to use `typename std::atomic<T>::value_type` rather than `T` but, curiously, not all implementations of `std::atomic` have member type alias `value_type`. So instead of using member type alias `value_type` to get dependency, we use `type_t`. Reviewed By: ot Differential Revision: D32259208 fbshipit-source-id: 413e3580aff3dd0ece5051b7a62dfd16f49eb5a8 --- folly/experimental/coro/Baton.cpp | 2 +- folly/synchronization/AtomicUtil-inl.h | 9 ++++----- folly/synchronization/AtomicUtil.h | 10 ++++++---- folly/synchronization/Baton.h | 2 +- folly/synchronization/SaturatingSemaphore.h | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/folly/experimental/coro/Baton.cpp b/folly/experimental/coro/Baton.cpp index f473b0d07..1e0ba392a 100644 --- a/folly/experimental/coro/Baton.cpp +++ b/folly/experimental/coro/Baton.cpp @@ -60,7 +60,7 @@ bool Baton::waitImpl(WaitOperation* awaiter) const noexcept { } while (!folly::atomic_compare_exchange_weak_explicit( &state_, &oldValue, - static_cast<void*>(awaiter), + awaiter, std::memory_order_release, std::memory_order_acquire)); return true; diff --git a/folly/synchronization/AtomicUtil-inl.h b/folly/synchronization/AtomicUtil-inl.h index 7ae0547ad..9c0395d4a 100644 --- a/folly/synchronization/AtomicUtil-inl.h +++ b/folly/synchronization/AtomicUtil-inl.h @@ -23,7 +23,6 @@ #include <type_traits> #include <folly/Portability.h> -#include <folly/Traits.h> #ifdef _WIN32 #include <intrin.h> @@ -84,8 +83,8 @@ constexpr std::memory_order atomic_compare_exchange_succ( template <template <typename> class Atom, typename T> bool atomic_compare_exchange_weak_explicit( Atom<T>* obj, - T* expected, - T desired, + type_t<T>* expected, + type_t<T> desired, std::memory_order succ, std::memory_order fail) { succ = detail::atomic_compare_exchange_succ(succ, fail); @@ -95,8 +94,8 @@ bool atomic_compare_exchange_weak_explicit( template <template <typename> class Atom, typename T> bool atomic_compare_exchange_strong_explicit( Atom<T>* obj, - T* expected, - T desired, + type_t<T>* expected, + type_t<T> desired, std::memory_order succ, std::memory_order fail) { succ = detail::atomic_compare_exchange_succ(succ, fail); diff --git a/folly/synchronization/AtomicUtil.h b/folly/synchronization/AtomicUtil.h index cce37dbcb..139812bab 100644 --- a/folly/synchronization/AtomicUtil.h +++ b/folly/synchronization/AtomicUtil.h @@ -19,6 +19,8 @@ #include <atomic> #include <cstdint> +#include <folly/Traits.h> + namespace folly { // atomic_compare_exchange_weak_explicit @@ -30,8 +32,8 @@ namespace folly { template <template <typename> class Atom = std::atomic, typename T> bool atomic_compare_exchange_weak_explicit( Atom<T>* obj, - T* expected, - T desired, + type_t<T>* expected, + type_t<T> desired, std::memory_order succ, std::memory_order fail); @@ -44,8 +46,8 @@ bool atomic_compare_exchange_weak_explicit( template <template <typename> class Atom = std::atomic, typename T> bool atomic_compare_exchange_strong_explicit( Atom<T>* obj, - T* expected, - T desired, + type_t<T>* expected, + type_t<T> desired, std::memory_order succ, std::memory_order fail); diff --git a/folly/synchronization/Baton.h b/folly/synchronization/Baton.h index 9699da4aa..5bcf17a76 100644 --- a/folly/synchronization/Baton.h +++ b/folly/synchronization/Baton.h @@ -312,7 +312,7 @@ class Baton { if (!folly::atomic_compare_exchange_strong_explicit<Atom>( &state_, &expected, - static_cast<uint32_t>(WAITING), + WAITING, std::memory_order_relaxed, std::memory_order_acquire)) { // CAS failed. The baton must have been posted between the last spin and diff --git a/folly/synchronization/SaturatingSemaphore.h b/folly/synchronization/SaturatingSemaphore.h index 23399b606..987d7437a 100644 --- a/folly/synchronization/SaturatingSemaphore.h +++ b/folly/synchronization/SaturatingSemaphore.h @@ -300,7 +300,7 @@ FOLLY_NOINLINE bool SaturatingSemaphore<MayBlock, Atom>::tryWaitSlow( !folly::atomic_compare_exchange_weak_explicit<Atom>( &state_, &before, - static_cast<std::uint32_t>(BLOCKED), + BLOCKED, std::memory_order_relaxed, std::memory_order_acquire)) { if (before == READY) { -- 2.26.2