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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 23 additions & 28 deletions libcxx/include/__atomic/aliases.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions libcxx/include/atomic
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions libcxx/test/std/atomics/types.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>>();
*/
Expand Down