Commit 33403f55 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

atomic_ref::compare_exchange_strong

Summary: [Folly] `atomic_ref::compare_exchange_strong` and `atomic_ref::compare_exchange_weak`.

Differential Revision: D21923944

fbshipit-source-id: 55620473ea253782cc1dbb95baef38c96a9fbc70
parent e223d1a4
......@@ -43,6 +43,37 @@ struct atomic_ref_base {
return atomic().load(order);
}
bool compare_exchange_weak(
T& expected,
T desired,
std::memory_order success,
std::memory_order failure) const noexcept {
return atomic().compare_exchange_weak(expected, desired, success, failure);
}
bool compare_exchange_weak(
T& expected,
T desired,
std::memory_order order = std::memory_order_seq_cst) const noexcept {
return atomic().compare_exchange_weak(expected, desired, order);
}
bool compare_exchange_strong(
T& expected,
T desired,
std::memory_order success,
std::memory_order failure) const noexcept {
return atomic().compare_exchange_strong(
expected, desired, success, failure);
}
bool compare_exchange_strong(
T& expected,
T desired,
std::memory_order order = std::memory_order_seq_cst) const noexcept {
return atomic().compare_exchange_strong(expected, desired, order);
}
std::atomic<T>& atomic() const noexcept {
return reinterpret_cast<std::atomic<T>&>(ref_); // ub dragons be here
}
......
......@@ -45,3 +45,89 @@ TEST_F(AtomicRefTest, integer) {
EXPECT_EQ(13, value);
}
}
TEST_F(AtomicRefTest, integer_compare_exchange_weak) {
auto const relaxed = std::memory_order_relaxed;
{
long value = 17;
auto ref = folly::make_atomic_ref(value);
auto expected = value;
auto done = ref.compare_exchange_weak(expected, 19);
EXPECT_TRUE(done);
EXPECT_EQ(19, value);
EXPECT_EQ(17, expected);
done = ref.compare_exchange_weak(expected, 21);
EXPECT_FALSE(done);
EXPECT_EQ(19, value);
EXPECT_EQ(19, expected);
}
{
long value = 17;
auto ref = folly::make_atomic_ref(value);
auto expected = value;
auto done = ref.compare_exchange_weak(expected, 19, relaxed);
EXPECT_TRUE(done);
EXPECT_EQ(19, value);
EXPECT_EQ(17, expected);
done = ref.compare_exchange_weak(expected, 21, relaxed);
EXPECT_FALSE(done);
EXPECT_EQ(19, value);
EXPECT_EQ(19, expected);
}
{
long value = 17;
auto ref = folly::make_atomic_ref(value);
auto expected = value;
auto done = ref.compare_exchange_weak(expected, 19, relaxed, relaxed);
EXPECT_TRUE(done);
EXPECT_EQ(19, value);
EXPECT_EQ(17, expected);
done = ref.compare_exchange_weak(expected, 21, relaxed, relaxed);
EXPECT_FALSE(done);
EXPECT_EQ(19, value);
EXPECT_EQ(19, expected);
}
}
TEST_F(AtomicRefTest, integer_compare_exchange_strong) {
auto const relaxed = std::memory_order_relaxed;
{
long value = 17;
auto ref = folly::make_atomic_ref(value);
auto expected = value;
auto done = ref.compare_exchange_strong(expected, 19);
EXPECT_TRUE(done);
EXPECT_EQ(19, value);
EXPECT_EQ(17, expected);
done = ref.compare_exchange_strong(expected, 21);
EXPECT_FALSE(done);
EXPECT_EQ(19, value);
EXPECT_EQ(19, expected);
}
{
long value = 17;
auto ref = folly::make_atomic_ref(value);
auto expected = value;
auto done = ref.compare_exchange_strong(expected, 19, relaxed);
EXPECT_TRUE(done);
EXPECT_EQ(19, value);
EXPECT_EQ(17, expected);
done = ref.compare_exchange_strong(expected, 21, relaxed);
EXPECT_FALSE(done);
EXPECT_EQ(19, value);
EXPECT_EQ(19, expected);
}
{
long value = 17;
auto ref = folly::make_atomic_ref(value);
auto expected = value;
auto done = ref.compare_exchange_strong(expected, 19, relaxed, relaxed);
EXPECT_TRUE(done);
EXPECT_EQ(19, value);
EXPECT_EQ(17, expected);
done = ref.compare_exchange_strong(expected, 21, relaxed, relaxed);
EXPECT_FALSE(done);
EXPECT_EQ(19, value);
EXPECT_EQ(19, expected);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment