Skip to content
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

Fix atomic_ref::is_lock_free() on x64 #4729

Merged
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
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 ^^^