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

relaxed_atomic, atomic with assumed memory_order_relaxed

Summary:
[Folly] `relaxed_atomic`, `atomic` with assumed `memory_order_relaxed`.

This is useful for values which are themselves atomics but which do not guard other non-atomic data, such as counters. In such cases, there is no required sequencing between the atomic value and other memory locations, just between concurrent accesses to the same atomic value, so relaxed operations are sufficient.

Reviewed By: ot

Differential Revision: D6206542

fbshipit-source-id: c820d7fcf189350a1feda4b012cb6a6342e32104
parent 8516df37
...@@ -849,6 +849,7 @@ if (BUILD_TESTS) ...@@ -849,6 +849,7 @@ if (BUILD_TESTS)
TEST baton_test SOURCES BatonTest.cpp TEST baton_test SOURCES BatonTest.cpp
TEST call_once_test SOURCES CallOnceTest.cpp TEST call_once_test SOURCES CallOnceTest.cpp
TEST lifo_sem_test SOURCES LifoSemTests.cpp TEST lifo_sem_test SOURCES LifoSemTests.cpp
TEST relaxed_atomic_test SOURCES RelaxedAtomicTest.cpp
TEST rw_spin_lock_test SOURCES RWSpinLockTest.cpp TEST rw_spin_lock_test SOURCES RWSpinLockTest.cpp
TEST semaphore_test SOURCES SemaphoreTest.cpp TEST semaphore_test SOURCES SemaphoreTest.cpp
......
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <atomic>
#include <cstdint>
namespace folly {
// relaxed_atomic
//
// Like atomic, but without the std::memory_order parameters on any member
// functions. All operations use std::memory_order_relaxed, rather than the
// std::memory_order_seq_cst, which is the default memory order in std::atomic.
//
// Useful for values which may be loaded and stored concurrently, such as
// counters, but which do not guard any associated data.
template <typename T>
struct relaxed_atomic;
namespace detail {
template <typename T>
struct relaxed_atomic_base : protected std::atomic<T> {
private:
using atomic = std::atomic<T>;
public:
using atomic::atomic;
T operator=(T desired) noexcept {
store(desired);
return desired;
}
T operator=(T desired) volatile noexcept {
store(desired);
return desired;
}
bool is_lock_free() const noexcept { return atomic::is_lock_free(); }
bool is_lock_free() const volatile noexcept { return atomic::is_lock_free(); }
void store(T desired) noexcept {
atomic::store(desired, std::memory_order_relaxed);
}
void store(T desired) volatile noexcept {
atomic::store(desired, std::memory_order_relaxed);
}
T load() const noexcept { return atomic::load(std::memory_order_relaxed); }
T load() const volatile noexcept {
return atomic::load(std::memory_order_relaxed);
}
operator T() const noexcept { return load(); }
operator T() const volatile noexcept { return load(); }
T exchange(T desired) noexcept {
return atomic::exchange(desired, std::memory_order_relaxed);
}
T exchange(T desired) volatile noexcept {
return atomic::exchange(desired, std::memory_order_relaxed);
}
bool compare_exchange_weak(T& expected, T desired) noexcept {
return atomic::compare_exchange_weak(
expected, desired, std::memory_order_relaxed);
}
bool compare_exchange_weak(T& expected, T desired) volatile noexcept {
return atomic::compare_exchange_weak(
expected, desired, std::memory_order_relaxed);
}
bool compare_exchange_strong(T& expected, T desired) noexcept {
return atomic::compare_exchange_strong(
expected, desired, std::memory_order_relaxed);
}
bool compare_exchange_strong(T& expected, T desired) volatile noexcept {
return atomic::compare_exchange_strong(
expected, desired, std::memory_order_relaxed);
}
};
template <typename T>
struct relaxed_atomic_integral_base : private relaxed_atomic_base<T> {
private:
using atomic = std::atomic<T>;
using base = relaxed_atomic_base<T>;
public:
using base::relaxed_atomic_base;
using base::operator=;
using base::operator T;
using base::compare_exchange_strong;
using base::compare_exchange_weak;
using base::exchange;
using base::is_lock_free;
using base::load;
using base::store;
T fetch_add(T arg) noexcept {
return atomic::fetch_add(arg, std::memory_order_relaxed);
}
T fetch_add(T arg) volatile noexcept {
return atomic::fetch_add(arg, std::memory_order_relaxed);
}
T fetch_sub(T arg) noexcept {
return atomic::fetch_sub(arg, std::memory_order_relaxed);
}
T fetch_sub(T arg) volatile noexcept {
return atomic::fetch_sub(arg, std::memory_order_relaxed);
}
T fetch_and(T arg) noexcept {
return atomic::fetch_and(arg, std::memory_order_relaxed);
}
T fetch_and(T arg) volatile noexcept {
return atomic::fetch_and(arg, std::memory_order_relaxed);
}
T fetch_or(T arg) noexcept {
return atomic::fetch_or(arg, std::memory_order_relaxed);
}
T fetch_or(T arg) volatile noexcept {
return atomic::fetch_or(arg, std::memory_order_relaxed);
}
T fetch_xor(T arg) noexcept {
return atomic::fetch_xor(arg, std::memory_order_relaxed);
}
T fetch_xor(T arg) volatile noexcept {
return atomic::fetch_xor(arg, std::memory_order_relaxed);
}
T operator++() noexcept { return fetch_add(1) + 1; }
T operator++() volatile noexcept { return fetch_add(1) + 1; }
T operator++(int) noexcept { return fetch_add(1); }
T operator++(int) volatile noexcept { return fetch_add(1); }
T operator--() noexcept { return fetch_sub(1) - 1; }
T operator--() volatile noexcept { return fetch_sub(1) - 1; }
T operator--(int) noexcept { return fetch_sub(1); }
T operator--(int) volatile noexcept { return fetch_sub(1); }
T operator+=(T arg) noexcept { return fetch_add(arg) + arg; }
T operator+=(T arg) volatile noexcept { return fetch_add(arg) + arg; }
T operator-=(T arg) noexcept { return fetch_sub(arg) - arg; }
T operator-=(T arg) volatile noexcept { return fetch_sub(arg) - arg; }
T operator&=(T arg) noexcept { return fetch_and(arg) & arg; }
T operator&=(T arg) volatile noexcept { return fetch_and(arg) & arg; }
T operator|=(T arg) noexcept { return fetch_or(arg) | arg; }
T operator|=(T arg) volatile noexcept { return fetch_or(arg) | arg; }
T operator^=(T arg) noexcept { return fetch_xor(arg) ^ arg; }
T operator^=(T arg) volatile noexcept { return fetch_xor(arg) ^ arg; }
};
} // namespace detail
template <>
struct relaxed_atomic<bool> : detail::relaxed_atomic_base<bool> {
private:
using base = detail::relaxed_atomic_base<bool>;
public:
using base::relaxed_atomic_base;
using base::operator=;
using base::operator bool;
};
using relaxed_atomic_bool = relaxed_atomic<bool>;
template <typename T>
struct relaxed_atomic : detail::relaxed_atomic_base<T> {
private:
using base = detail::relaxed_atomic_base<T>;
public:
using base::relaxed_atomic_base;
using base::operator=;
using base::operator T;
};
template <typename T>
struct relaxed_atomic<T*> : detail::relaxed_atomic_base<T*> {
private:
using atomic = std::atomic<T*>;
using base = detail::relaxed_atomic_base<T*>;
public:
using detail::relaxed_atomic_base<T*>::relaxed_atomic_base;
using base::operator=;
using base::operator T*;
T* fetch_add(std::ptrdiff_t arg) noexcept {
return atomic::fetch_add(arg, std::memory_order_relaxed);
}
T* fetch_add(std::ptrdiff_t arg) volatile noexcept {
return atomic::fetch_add(arg, std::memory_order_relaxed);
}
T* fetch_sub(std::ptrdiff_t arg) noexcept {
return atomic::fetch_sub(arg, std::memory_order_relaxed);
}
T* fetch_sub(std::ptrdiff_t arg) volatile noexcept {
return atomic::fetch_sub(arg, std::memory_order_relaxed);
}
T* operator++() noexcept { return fetch_add(1) + 1; }
T* operator++() volatile noexcept { return fetch_add(1) + 1; }
T* operator++(int) noexcept { return fetch_add(1); }
T* operator++(int) volatile noexcept { return fetch_add(1); }
T* operator--() noexcept { return fetch_sub(1) - 1; }
T* operator--() volatile noexcept { return fetch_sub(1) - 1; }
T* operator--(int) noexcept { return fetch_sub(1); }
T* operator--(int) volatile noexcept { return fetch_sub(1); }
T* operator+=(std::ptrdiff_t arg) noexcept { return fetch_add(arg) + arg; }
T* operator+=(std::ptrdiff_t arg) volatile noexcept {
return fetch_add(arg) + arg;
}
T* operator-=(std::ptrdiff_t arg) noexcept { return fetch_sub(arg) - arg; }
T* operator-=(std::ptrdiff_t arg) volatile noexcept {
return fetch_sub(arg) - arg;
}
};
#define FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION(type) \
template <> \
struct relaxed_atomic<type> : detail::relaxed_atomic_integral_base<type> { \
private: \
using base = detail::relaxed_atomic_integral_base<type>; \
\
public: \
using base::relaxed_atomic_integral_base; \
using base::operator=; \
using base::operator type; \
};
FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION(char)
FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION(signed char)
FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION(unsigned char)
FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION(signed short)
FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION(unsigned short)
FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION(signed int)
FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION(unsigned int)
FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION(signed long)
FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION(unsigned long)
FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION(signed long long)
FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION(unsigned long long)
#undef FOLLY_RELAXED_ATOMIC_DEFINE_INTEGRAL_SPECIALIZATION
using relaxed_atomic_char = relaxed_atomic<char>;
using relaxed_atomic_schar = relaxed_atomic<signed char>;
using relaxed_atomic_uchar = relaxed_atomic<unsigned char>;
using relaxed_atomic_short = relaxed_atomic<short>;
using relaxed_atomic_ushort = relaxed_atomic<unsigned short>;
using relaxed_atomic_int = relaxed_atomic<int>;
using relaxed_atomic_uint = relaxed_atomic<unsigned int>;
using relaxed_atomic_long = relaxed_atomic<long>;
using relaxed_atomic_ulong = relaxed_atomic<unsigned long>;
using relaxed_atomic_llong = relaxed_atomic<long long>;
using relaxed_atomic_ullong = relaxed_atomic<unsigned long long>;
using relaxed_atomic_char16_t = relaxed_atomic<char16_t>;
using relaxed_atomic_char32_t = relaxed_atomic<char32_t>;
using relaxed_atomic_wchar_t = relaxed_atomic<wchar_t>;
using relaxed_atomic_int8_t = relaxed_atomic<std::int8_t>;
using relaxed_atomic_uint8_t = relaxed_atomic<std::uint8_t>;
using relaxed_atomic_int16_t = relaxed_atomic<std::int16_t>;
using relaxed_atomic_uint16_t = relaxed_atomic<std::uint16_t>;
using relaxed_atomic_int32_t = relaxed_atomic<std::int32_t>;
using relaxed_atomic_uint32_t = relaxed_atomic<std::uint32_t>;
using relaxed_atomic_int64_t = relaxed_atomic<std::int64_t>;
using relaxed_atomic_uint64_t = relaxed_atomic<std::uint64_t>;
using relaxed_atomic_int_least8_t = relaxed_atomic<std::int_least8_t>;
using relaxed_atomic_uint_least8_t = relaxed_atomic<std::uint_least8_t>;
using relaxed_atomic_int_least16_t = relaxed_atomic<std::int_least16_t>;
using relaxed_atomic_uint_least16_t = relaxed_atomic<std::uint_least16_t>;
using relaxed_atomic_int_least32_t = relaxed_atomic<std::int_least32_t>;
using relaxed_atomic_uint_least32_t = relaxed_atomic<std::uint_least32_t>;
using relaxed_atomic_int_least64_t = relaxed_atomic<std::int_least64_t>;
using relaxed_atomic_uint_least64_t = relaxed_atomic<std::uint_least64_t>;
using relaxed_atomic_int_fast8_t = relaxed_atomic<std::int_fast8_t>;
using relaxed_atomic_uint_fast8_t = relaxed_atomic<std::uint_fast8_t>;
using relaxed_atomic_int_fast16_t = relaxed_atomic<std::int_fast16_t>;
using relaxed_atomic_uint_fast16_t = relaxed_atomic<std::uint_fast16_t>;
using relaxed_atomic_int_fast32_t = relaxed_atomic<std::int_fast32_t>;
using relaxed_atomic_uint_fast32_t = relaxed_atomic<std::uint_fast32_t>;
using relaxed_atomic_int_fast64_t = relaxed_atomic<std::int_fast64_t>;
using relaxed_atomic_uint_fast64_t = relaxed_atomic<std::uint_fast64_t>;
using relaxed_atomic_intptr_t = relaxed_atomic<std::intptr_t>;
using relaxed_atomic_uintptr_t = relaxed_atomic<std::uintptr_t>;
using relaxed_atomic_size_t = relaxed_atomic<std::size_t>;
using relaxed_atomic_ptrdiff_t = relaxed_atomic<std::ptrdiff_t>;
using relaxed_atomic_intmax_t = relaxed_atomic<std::intmax_t>;
using relaxed_atomic_uintmax_t = relaxed_atomic<std::uintmax_t>;
} // namespace folly
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/synchronization/RelaxedAtomic.h>
#include <folly/portability/GTest.h>
template <typename AtomicType>
struct RelaxedAtomicBooleanTest : testing::Test {};
using RelaxedAtomicBooleanTestTypes = testing::Types<
std::atomic<bool>,
std::atomic<bool> volatile,
folly::relaxed_atomic<bool>,
folly::relaxed_atomic<bool> volatile>;
TYPED_TEST_CASE(RelaxedAtomicBooleanTest, RelaxedAtomicBooleanTestTypes);
TYPED_TEST(RelaxedAtomicBooleanTest, is_lock_free) {
TypeParam v{true};
EXPECT_TRUE(v.is_lock_free());
}
TYPED_TEST(RelaxedAtomicBooleanTest, operator_value) {
TypeParam v{true};
EXPECT_EQ(true, v);
}
TYPED_TEST(RelaxedAtomicBooleanTest, load) {
TypeParam v{true};
EXPECT_EQ(true, v.load());
}
TYPED_TEST(RelaxedAtomicBooleanTest, operator_assign) {
TypeParam v{true};
EXPECT_EQ(false, (v = false));
EXPECT_EQ(false, v);
}
TYPED_TEST(RelaxedAtomicBooleanTest, store) {
TypeParam v{true};
EXPECT_EQ(false, (v.store(false), v));
}
TYPED_TEST(RelaxedAtomicBooleanTest, exchange) {
TypeParam v{true};
EXPECT_EQ(true, v.exchange(false));
EXPECT_EQ(false, v);
}
TYPED_TEST(RelaxedAtomicBooleanTest, compare_exchange_weak) {
TypeParam v{true};
bool e = false;
EXPECT_FALSE(v.compare_exchange_weak(e, false));
EXPECT_TRUE(v.compare_exchange_weak(e, false));
EXPECT_EQ(true, e);
EXPECT_EQ(false, v);
}
TYPED_TEST(RelaxedAtomicBooleanTest, compare_exchange_strong) {
TypeParam v{true};
bool e = false;
EXPECT_FALSE(v.compare_exchange_strong(e, false));
EXPECT_TRUE(v.compare_exchange_strong(e, false));
EXPECT_EQ(true, e);
EXPECT_EQ(false, v);
}
template <typename AtomicType>
struct RelaxedAtomicPointerTest : testing::Test {};
using RelaxedAtomicPointerTestTypes = testing::Types<
std::atomic<int*>,
std::atomic<int*> volatile,
folly::relaxed_atomic<int*>,
folly::relaxed_atomic<int*> volatile>;
TYPED_TEST_CASE(RelaxedAtomicPointerTest, RelaxedAtomicPointerTestTypes);
TYPED_TEST(RelaxedAtomicPointerTest, is_lock_free) {
int n[] = {-1};
TypeParam v{n + 0};
EXPECT_TRUE(v.is_lock_free());
}
TYPED_TEST(RelaxedAtomicPointerTest, operator_value) {
int n[] = {-1};
TypeParam v{n + 0};
EXPECT_EQ(-1, *v);
}
TYPED_TEST(RelaxedAtomicPointerTest, load) {
int n[] = {-1};
TypeParam v{n + 0};
EXPECT_EQ(-1, *v.load());
}
TYPED_TEST(RelaxedAtomicPointerTest, operator_assign) {
int n[] = {-1, -2, -3};
TypeParam v{n + 0};
EXPECT_EQ(-3, *(v = n + 2));
EXPECT_EQ(-3, *v);
}
TYPED_TEST(RelaxedAtomicPointerTest, store) {
int n[] = {-1, -2, -3};
TypeParam v{n + 0};
EXPECT_EQ(-3, *(v.store(n + 2), v));
}
TYPED_TEST(RelaxedAtomicPointerTest, exchange) {
int n[] = {-1, -2, -3};
TypeParam v{n + 0};
EXPECT_EQ(-1, *v.exchange(n + 2));
EXPECT_EQ(-3, *v);
}
TYPED_TEST(RelaxedAtomicPointerTest, compare_exchange_weak) {
int n[] = {-1, -2, -3};
TypeParam v{n + 0};
int* e = n + 1;
EXPECT_FALSE(v.compare_exchange_weak(e, n + 2));
EXPECT_TRUE(v.compare_exchange_weak(e, n + 2));
EXPECT_EQ(-1, *e);
EXPECT_EQ(-3, *v);
}
TYPED_TEST(RelaxedAtomicPointerTest, compare_exchange_strong) {
int n[] = {-1, -2, -3};
TypeParam v{n + 0};
int* e = n + 1;
EXPECT_FALSE(v.compare_exchange_strong(e, n + 2));
EXPECT_TRUE(v.compare_exchange_strong(e, n + 2));
EXPECT_EQ(-1, *e);
EXPECT_EQ(-3, *v);
}
TYPED_TEST(RelaxedAtomicPointerTest, fetch_add) {
int n[] = {-1, -2, -3};
TypeParam v{n + 0};
EXPECT_EQ(-1, *v.fetch_add(2));
EXPECT_EQ(-3, *v);
}
TYPED_TEST(RelaxedAtomicPointerTest, fetch_sub) {
int n[] = {-1, -2, -3};
TypeParam v{n + 2};
EXPECT_EQ(-3, *v.fetch_sub(2));
EXPECT_EQ(-1, *v);
}
TYPED_TEST(RelaxedAtomicPointerTest, operator_incr_pre) {
int n[] = {-1, -2, -3};
TypeParam v{n + 1};
EXPECT_EQ(-3, *++v);
EXPECT_EQ(-3, *v);
}
TYPED_TEST(RelaxedAtomicPointerTest, operator_incr_post) {
int n[] = {-1, -2, -3};
TypeParam v{n + 1};
EXPECT_EQ(-2, *v++);
EXPECT_EQ(-3, *v);
}
TYPED_TEST(RelaxedAtomicPointerTest, operator_decr_pre) {
int n[] = {-1, -2, -3};
TypeParam v{n + 1};
EXPECT_EQ(-1, *--v);
EXPECT_EQ(-1, *v);
}
TYPED_TEST(RelaxedAtomicPointerTest, operator_decr_post) {
int n[] = {-1, -2, -3};
TypeParam v{n + 1};
EXPECT_EQ(-2, *v--);
EXPECT_EQ(-1, *v);
}
TYPED_TEST(RelaxedAtomicPointerTest, operator_add_assign) {
int n[] = {-1, -2, -3};
TypeParam v{n + 0};
EXPECT_EQ(-3, *(v += 2));
EXPECT_EQ(-3, *v);
}
TYPED_TEST(RelaxedAtomicPointerTest, operator_sub_assign) {
int n[] = {-1, -2, -3};
TypeParam v{n + 2};
EXPECT_EQ(-1, *(v -= 2));
EXPECT_EQ(-1, *v);
}
template <typename AtomicType>
struct RelaxedAtomicIntegralTest : testing::Test {};
using RelaxedAtomicIntegralTestTypes = testing::Types<
std::atomic<int>,
std::atomic<int> volatile,
folly::relaxed_atomic<int>,
folly::relaxed_atomic<int> volatile>;
TYPED_TEST_CASE(RelaxedAtomicIntegralTest, RelaxedAtomicIntegralTestTypes);
TYPED_TEST(RelaxedAtomicIntegralTest, is_lock_free) {
TypeParam v{3};
EXPECT_TRUE(v.is_lock_free());
}
TYPED_TEST(RelaxedAtomicIntegralTest, operator_value) {
TypeParam v{3};
EXPECT_EQ(3, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, load) {
TypeParam v{3};
EXPECT_EQ(3, v.load());
}
TYPED_TEST(RelaxedAtomicIntegralTest, operator_assign) {
TypeParam v{3};
EXPECT_EQ(4, (v = 4));
EXPECT_EQ(4, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, store) {
TypeParam v{3};
EXPECT_EQ(4, (v.store(4), v));
}
TYPED_TEST(RelaxedAtomicIntegralTest, exchange) {
TypeParam v{3};
EXPECT_EQ(3, v.exchange(4));
EXPECT_EQ(4, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, compare_exchange_weak) {
TypeParam v{3};
int e = 2;
EXPECT_FALSE(v.compare_exchange_weak(e, 4));
EXPECT_TRUE(v.compare_exchange_weak(e, 4));
EXPECT_EQ(3, e);
EXPECT_EQ(4, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, compare_exchange_strong) {
TypeParam v{3};
int e = 2;
EXPECT_FALSE(v.compare_exchange_strong(e, 4));
EXPECT_TRUE(v.compare_exchange_strong(e, 4));
EXPECT_EQ(3, e);
EXPECT_EQ(4, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, fetch_add) {
TypeParam v{3};
EXPECT_EQ(3, v.fetch_add(2));
EXPECT_EQ(5, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, fetch_sub) {
TypeParam v{3};
EXPECT_EQ(3, v.fetch_sub(2));
EXPECT_EQ(1, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, fetch_and) {
TypeParam v{6};
EXPECT_EQ(6, v.fetch_and(12));
EXPECT_EQ(4, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, fetch_or) {
TypeParam v{5};
EXPECT_EQ(5, v.fetch_or(14));
EXPECT_EQ(15, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, fetch_xor) {
TypeParam v{6};
EXPECT_EQ(6, v.fetch_xor(10));
EXPECT_EQ(12, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, operator_incr_pre) {
TypeParam v{3};
EXPECT_EQ(4, ++v);
EXPECT_EQ(4, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, operator_incr_post) {
TypeParam v{3};
EXPECT_EQ(3, v++);
EXPECT_EQ(4, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, operator_decr_pre) {
TypeParam v{3};
EXPECT_EQ(2, --v);
EXPECT_EQ(2, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, operator_decr_post) {
TypeParam v{3};
EXPECT_EQ(3, v--);
EXPECT_EQ(2, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, operator_add_assign) {
TypeParam v{3};
EXPECT_EQ(5, v += 2);
EXPECT_EQ(5, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, operator_sub_assign) {
TypeParam v{3};
EXPECT_EQ(1, v -= 2);
EXPECT_EQ(1, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, operator_and_assign) {
TypeParam v{6};
EXPECT_EQ(4, v &= 12);
EXPECT_EQ(4, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, operator_or_assign) {
TypeParam v{5};
EXPECT_EQ(15, v |= 14);
EXPECT_EQ(15, v);
}
TYPED_TEST(RelaxedAtomicIntegralTest, operator_xor_assign) {
TypeParam v{6};
EXPECT_EQ(12, v ^= 10);
EXPECT_EQ(12, v);
}
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