Skip to content

[libc++] Refactor atomic_{unsigned,signed}_lock_free #73041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 22, 2023

Conversation

ldionne
Copy link
Member

@ldionne ldionne commented Nov 21, 2023

Their definition was a bit roundabout and it was actually wrong since atomic_unsigned_lock_free would be a signed type whenever __cxx_contention_t is lock free, which is most of the time.

Fixes #72968

Their definition was a bit roundabout and it was actually wrong since
atomic_unsigned_lock_free would be a signed type whenever __cxx_contention_t
is lock free, which is most of the time.

Fixes llvm#72968
@ldionne ldionne requested a review from a team as a code owner November 21, 2023 21:21
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Nov 21, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2023

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)

Changes

Their definition was a bit roundabout and it was actually wrong since atomic_unsigned_lock_free would be a signed type whenever __cxx_contention_t is lock free, which is most of the time.

Fixes #72968


Full diff: https://github.com/llvm/llvm-project/pull/73041.diff

3 Files Affected:

  • (modified) libcxx/include/__atomic/aliases.h (+23-28)
  • (modified) libcxx/include/atomic (+3)
  • (modified) libcxx/test/std/atomics/types.pass.cpp (+5)
diff --git a/libcxx/include/__atomic/aliases.h b/libcxx/include/__atomic/aliases.h
index e2f9fae4094ef23..0fa289de54b0f12 100644
--- a/libcxx/include/__atomic/aliases.h
+++ b/libcxx/include/__atomic/aliases.h
@@ -15,6 +15,7 @@
 #include <__atomic/is_always_lock_free.h>
 #include <__config>
 #include <__type_traits/conditional.h>
+#include <__type_traits/make_unsigned.h>
 #include <cstddef>
 #include <cstdint>
 #include <cstdlib>
@@ -80,36 +81,30 @@ using atomic_ptrdiff_t = atomic<ptrdiff_t>;
 using atomic_intmax_t  = atomic<intmax_t>;
 using atomic_uintmax_t = atomic<uintmax_t>;
 
-// atomic_*_lock_free : prefer the contention type most highly, then the largest lock-free type
+// C++20 atomic_{signed,unsigned}_lock_free: prefer the contention type most highly, then the largest lock-free type
+#if _LIBCPP_STD_VER >= 20
+#  if ATOMIC_LLONG_LOCK_FREE == 2
+using __largest_lock_free_type = long long;
+#  elif ATOMIC_INT_LOCK_FREE == 2
+using __largest_lock_free_type = int;
+#  elif ATOMIC_SHORT_LOCK_FREE == 2
+using __largest_lock_free_type = short;
+#  elif ATOMIC_CHAR_LOCK_FREE == 2
+using __largest_lock_free_type = char;
+#  else
+#    define _LIBCPP_NO_LOCK_FREE_TYPES // There are no lockfree types (this can happen in freestanding)
+#  endif
 
-#if _LIBCPP_STD_VER >= 17
-#  define _LIBCPP_CONTENTION_LOCK_FREE ::std::__libcpp_is_always_lock_free<__cxx_contention_t>::__value
-#else
-#  define _LIBCPP_CONTENTION_LOCK_FREE false
-#endif
-
-#if ATOMIC_LLONG_LOCK_FREE == 2
-using __libcpp_signed_lock_free = __conditional_t<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, long long>;
-using __libcpp_unsigned_lock_free =
-    __conditional_t<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, unsigned long long>;
-#elif ATOMIC_INT_LOCK_FREE == 2
-using __libcpp_signed_lock_free   = __conditional_t<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, int>;
-using __libcpp_unsigned_lock_free = __conditional_t<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, unsigned int>;
-#elif ATOMIC_SHORT_LOCK_FREE == 2
-using __libcpp_signed_lock_free   = __conditional_t<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, short>;
-using __libcpp_unsigned_lock_free = __conditional_t<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, unsigned short>;
-#elif ATOMIC_CHAR_LOCK_FREE == 2
-using __libcpp_signed_lock_free   = __conditional_t<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, char>;
-using __libcpp_unsigned_lock_free = __conditional_t<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, unsigned char>;
-#else
-// No signed/unsigned lock-free types
-#  define _LIBCPP_NO_LOCK_FREE_TYPES
-#endif
+#  ifndef _LIBCPP_NO_LOCK_FREE_TYPES
+using __contention_t_or_largest =
+    __conditional_t<__libcpp_is_always_lock_free<__cxx_contention_t>::__value,
+                    __cxx_contention_t,
+                    __largest_lock_free_type>;
 
-#if !defined(_LIBCPP_NO_LOCK_FREE_TYPES)
-using atomic_signed_lock_free   = atomic<__libcpp_signed_lock_free>;
-using atomic_unsigned_lock_free = atomic<__libcpp_unsigned_lock_free>;
-#endif
+using atomic_signed_lock_free   = atomic<__contention_t_or_largest>;
+using atomic_unsigned_lock_free = atomic<make_unsigned_t<__contention_t_or_largest>>;
+#  endif // !_LIBCPP_NO_LOCK_FREE_TYPES
+#endif   // C++20
 
 _LIBCPP_END_NAMESPACE_STD
 
diff --git a/libcxx/include/atomic b/libcxx/include/atomic
index 2f122a707bdc33a..1a4be926e57086a 100644
--- a/libcxx/include/atomic
+++ b/libcxx/include/atomic
@@ -448,6 +448,9 @@ typedef atomic<ptrdiff_t> atomic_ptrdiff_t;
 typedef atomic<intmax_t>  atomic_intmax_t;
 typedef atomic<uintmax_t> atomic_uintmax_t;
 
+typedef see-below         atomic_signed_lock_free;   // since C++20
+typedef see-below         atomic_unsigned_lock_free; // since C++20
+
 // flag type and operations
 
 typedef struct atomic_flag
diff --git a/libcxx/test/std/atomics/types.pass.cpp b/libcxx/test/std/atomics/types.pass.cpp
index deaa45dcddcaa16..cebf66ee7f1af03 100644
--- a/libcxx/test/std/atomics/types.pass.cpp
+++ b/libcxx/test/std/atomics/types.pass.cpp
@@ -175,7 +175,12 @@ int main(int, char**)
 
 #if TEST_STD_VER >= 20
     test<std::atomic_signed_lock_free::value_type>();
+    static_assert(std::is_signed_v<std::atomic_signed_lock_free::value_type>);
+    static_assert(std::is_integral_v<std::atomic_signed_lock_free::value_type>);
+
     test<std::atomic_unsigned_lock_free::value_type>();
+    static_assert(std::is_unsigned_v<std::atomic_unsigned_lock_free::value_type>);
+    static_assert(std::is_integral_v<std::atomic_unsigned_lock_free::value_type>);
 /*
     test<std::shared_ptr<int>>();
 */

@cjdb cjdb self-requested a review November 21, 2023 21:32
Copy link
Contributor

@cjdb cjdb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@huixie90 huixie90 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ldionne ldionne merged commit 6f0c52e into llvm:main Nov 22, 2023
@ldionne ldionne deleted the review/contention-lockfree branch November 22, 2023 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc++] atomic_unsigned_lock_free is usually signed
4 participants