Skip to content

Commit

Permalink
Fix atomic_ref::is_lock_free() on x64 (#4729)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Guteniev <gutenev@gmail.com>
  • Loading branch information
StephanTLavavej and AlexGuteniev authored Jun 27, 2024
1 parent d6b0352 commit 14b54ca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion stl/inc/atomic
Original file line number Diff line number Diff line change
Expand Up @@ -2370,8 +2370,10 @@ public:
#else // ^^^ _ATOMIC_HAS_DCAS / !_ATOMIC_HAS_DCAS vvv
if constexpr (is_always_lock_free) {
return true;
} else {
} else if constexpr (_Is_potentially_lock_free) {
return __std_atomic_has_cmpxchg16b() != 0;
} else {
return false;
}
#endif // ^^^ !_ATOMIC_HAS_DCAS ^^^
}
Expand Down
17 changes: 17 additions & 0 deletions tests/std/tests/P0019R8_atomic_ref/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,22 @@ void test_gh_4472() {
assert(ar.is_lock_free());
}

// GH-4728 "<atomic>: On x64, atomic_ref::is_lock_free() incorrectly returns true when it shouldn't"
void test_gh_4728() {
struct Large {
char str[100]{};
};

alignas(std::atomic_ref<Large>::required_alignment) Large lg{};

static_assert(std::atomic_ref<Large>::required_alignment == alignof(Large));

static_assert(!std::atomic_ref<Large>::is_always_lock_free);

std::atomic_ref<Large> ar{lg};
assert(!ar.is_lock_free());
}

int main() {
test_ops<false, char>();
test_ops<false, signed char>();
Expand Down Expand Up @@ -500,6 +516,7 @@ int main() {

test_gh_1497();
test_gh_4472();
test_gh_4728();
}

#endif // ^^^ run test ^^^

0 comments on commit 14b54ca

Please sign in to comment.