Skip to content
Merged
Changes from 4 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
50 changes: 30 additions & 20 deletions libc/src/__support/CPP/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ enum class MemoryScope : int {
#endif
};

namespace atomic {

LIBC_INLINE constexpr int order(MemoryOrder mem_ord) {
return static_cast<int>(mem_ord);
}

LIBC_INLINE constexpr int scope(MemoryScope mem_scope) {
return static_cast<int>(mem_scope);
}

template <class T> LIBC_INLINE T *addressof(T &ref) {
return __builtin_addressof(ref);
}

LIBC_INLINE constexpr int infer_failure_order(MemoryOrder mem_ord) {
if (mem_ord == MemoryOrder::RELEASE)
return order(MemoryOrder::RELAXED);
if (mem_ord == MemoryOrder::ACQ_REL)
return order(MemoryOrder::ACQUIRE);
return order(mem_ord);
}

template <typename T> struct Atomic {
static_assert(is_trivially_copyable_v<T> && is_copy_constructible_v<T> &&
is_move_constructible_v<T> && is_copy_assignable_v<T> &&
Expand All @@ -54,15 +76,6 @@ template <typename T> struct Atomic {

private:
// type conversion helper to avoid long c++ style casts
LIBC_INLINE static int order(MemoryOrder mem_ord) {
return static_cast<int>(mem_ord);
}

LIBC_INLINE static int scope(MemoryScope mem_scope) {
return static_cast<int>(mem_scope);
}

LIBC_INLINE static T *addressof(T &ref) { return __builtin_addressof(ref); }

// Require types that are 1, 2, 4, 8, or 16 bytes in length to be aligned to
// at least their size to be potentially used lock-free.
Expand Down Expand Up @@ -129,7 +142,7 @@ template <typename T> struct Atomic {
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
return __atomic_compare_exchange(addressof(val), addressof(expected),
addressof(desired), false, order(mem_ord),
order(mem_ord));
infer_failure_order(mem_ord));
}

// Atomic compare exchange (separate success and failure memory orders)
Expand All @@ -148,7 +161,7 @@ template <typename T> struct Atomic {
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
return __atomic_compare_exchange(addressof(val), addressof(expected),
addressof(desired), true, order(mem_ord),
order(mem_ord));
infer_failure_order(mem_ord));
}

// Atomic compare exchange (weak version with separate success and failure
Expand Down Expand Up @@ -244,14 +257,6 @@ template <typename T> struct AtomicRef {
private:
T *ptr;

LIBC_INLINE static int order(MemoryOrder mem_ord) {
return static_cast<int>(mem_ord);
}

LIBC_INLINE static int scope(MemoryScope mem_scope) {
return static_cast<int>(mem_scope);
}

public:
// Constructor from T reference
LIBC_INLINE explicit constexpr AtomicRef(T &obj) : ptr(&obj) {}
Expand Down Expand Up @@ -298,7 +303,8 @@ template <typename T> struct AtomicRef {
T &expected, T desired, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) const {
return __atomic_compare_exchange(ptr, &expected, &desired, false,
order(mem_ord), order(mem_ord));
order(mem_ord),
infer_failure_order(mem_ord));
}

// Atomic compare exchange (strong, separate success/failure memory orders)
Expand Down Expand Up @@ -402,6 +408,10 @@ LIBC_INLINE void atomic_signal_fence([[maybe_unused]] MemoryOrder mem_ord) {
asm volatile("" ::: "memory");
#endif
}
} // namespace atomic

using atomic::Atomic;
using atomic::AtomicRef;

} // namespace cpp
} // namespace LIBC_NAMESPACE_DECL
Expand Down
Loading