Refactor etl::atomic implementations to allow non-(integrals/pointers/bool)

Changed etl::atomic and etl::mutex, with STL enabled, to be template aliases
This commit is contained in:
John Wellbelove 2022-06-05 10:51:19 +01:00
parent 047fc3eb90
commit cf24398209
12 changed files with 1419 additions and 1818 deletions

File diff suppressed because it is too large Load Diff

View File

@ -43,7 +43,10 @@ namespace etl
// etl::atomic is a simple wrapper around std::atomic.
//***************************************************************************
typedef std::memory_order memory_order;
template <typename T>
using atomic = std::atomic<T>;
using memory_order = std::memory_order;
static ETL_CONSTANT etl::memory_order memory_order_relaxed = std::memory_order_relaxed;
static ETL_CONSTANT etl::memory_order memory_order_consume = std::memory_order_consume;
@ -52,570 +55,66 @@ namespace etl
static ETL_CONSTANT etl::memory_order memory_order_acq_rel = std::memory_order_acq_rel;
static ETL_CONSTANT etl::memory_order memory_order_seq_cst = std::memory_order_seq_cst;
template <typename T>
class atomic
{
public:
atomic()
: value(0)
{
}
atomic(T v)
: value(v)
{
}
// Assignment
T operator =(T v)
{
return value = v;
}
T operator =(T v) volatile
{
return value = v;
}
// Pre-increment
T operator ++()
{
return ++value;
}
T operator ++() volatile
{
return ++value;
}
// Post-increment
T operator ++(int)
{
return value++;
}
T operator ++(int) volatile
{
return value++;
}
// Pre-decrement
T operator --()
{
return --value;
}
T operator --() volatile
{
return --value;
}
// Post-decrement
T operator --(int)
{
return value--;
}
T operator --(int) volatile
{
return value--;
}
// Add
T operator +=(T v)
{
return value += v;
}
T operator +=(T v) volatile
{
return value += v;
}
// Subtract
T operator -=(T v)
{
return value -= v;
}
T operator -=(T v) volatile
{
return value -= v;
}
// And
T operator &=(T v)
{
return value &= v;
}
T operator &=(T v) volatile
{
return value &= v;
}
// Or
T operator |=(T v)
{
return value |= v;
}
T operator |=(T v) volatile
{
return value |= v;
}
// Exclusive or
T operator ^=(T v)
{
return value ^= v;
}
T operator ^=(T v) volatile
{
return value ^= v;
}
// Conversion operator
operator T () const
{
return T(value);
}
operator T() volatile const
{
return T(value);
}
// Is lock free?
bool is_lock_free() const
{
return value.is_lock_free();
}
bool is_lock_free() const volatile
{
return value.is_lock_free();
}
// Store
void store(T v, etl::memory_order order = etl::memory_order_seq_cst)
{
value.store(v, order);
}
void store(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
value.store(v, order);
}
// Load
T load(etl::memory_order order = etl::memory_order_seq_cst) const
{
return value.load(order);
}
T load(etl::memory_order order = etl::memory_order_seq_cst) const volatile
{
return value.load(order);
}
// Fetch add
T fetch_add(T v, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.fetch_add(v, order);
}
T fetch_add(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.fetch_add(v, order);
}
// Fetch subtract
T fetch_sub(T v, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.fetch_sub(v, order);
}
T fetch_sub(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.fetch_sub(v, order);
}
// Fetch or
T fetch_or(T v, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.fetch_or(v, order);
}
T fetch_or(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.fetch_or(v, order);
}
// Fetch and
T fetch_and(T v, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.fetch_and(v, order);
}
T fetch_and(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.fetch_and(v, order);
}
// Fetch exclusive or
T fetch_xor(T v, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.fetch_xor(v, order);
}
T fetch_xor(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.fetch_xor(v, order);
}
// Exchange
T exchange(T v, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.exchange(v, order);
}
T exchange(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.exchange(v, order);
}
// Compare exchange weak
bool compare_exchange_weak(T& expected, T desired, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.compare_exchange_weak(expected, desired, order);
}
bool compare_exchange_weak(T& expected, T desired, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.compare_exchange_weak(expected, desired, order);
}
bool compare_exchange_weak(T& expected, T desired, etl::memory_order success, etl::memory_order failure)
{
return value.compare_exchange_weak(expected, desired, success, failure);
}
bool compare_exchange_weak(T& expected, T desired, etl::memory_order success, etl::memory_order failure) volatile
{
return value.compare_exchange_weak(expected, desired, success, failure);
}
// Compare exchange strong
bool compare_exchange_strong(T& expected, T desired, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.compare_exchange_strong(expected, desired, order);
}
bool compare_exchange_strong(T& expected, T desired, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.compare_exchange_strong(expected, desired, order);
}
bool compare_exchange_strong(T& expected, T desired, etl::memory_order success, etl::memory_order failure)
{
return value.compare_exchange_strong(expected, desired, success, failure);
}
bool compare_exchange_strong(T& expected, T desired, etl::memory_order success, etl::memory_order failure) volatile
{
return value.compare_exchange_strong(expected, desired, success, failure);
}
private:
atomic& operator =(const atomic&);
//atomic& operator =(const atomic&) volatile;
std::atomic<T> value;
};
template <typename T>
class atomic<T*>
{
public:
atomic()
: value(ETL_NULLPTR)
{
}
atomic(T* v)
: value(v)
{
}
// Assignment
T* operator =(T* v)
{
return value = v;
}
T* operator =(T* v) volatile
{
return value = v;
}
// Pre-increment
T* operator ++()
{
return ++value;
}
T* operator ++() volatile
{
return ++value;
}
// Post-increment
T* operator ++(int)
{
return value++;
}
T* operator ++(int) volatile
{
return value++;
}
// Pre-decrement
T* operator --()
{
return --value;
}
T* operator --() volatile
{
return --value;
}
// Post-decrement
T* operator --(int)
{
return value--;
}
T* operator --(int) volatile
{
return value--;
}
// Add
T* operator +=(ptrdiff_t v)
{
return value += v;
}
T* operator +=(ptrdiff_t v) volatile
{
return value += v;
}
// Subtract
T* operator -=(ptrdiff_t v)
{
return value -= v;
}
T* operator -=(ptrdiff_t v) volatile
{
return value -= v;
}
// Conversion operator
operator T* () const
{
return static_cast<T*>(value);
}
operator T*() volatile const
{
return static_cast<T*>(value);
}
// Is lock free?
bool is_lock_free() const
{
return value.is_lock_free();
}
bool is_lock_free() const volatile
{
return value.is_lock_free();
}
// Store
void store(T* v, etl::memory_order order = etl::memory_order_seq_cst)
{
value.store(v, order);
}
void store(T* v, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
value.store(v, order);
}
// Load
T* load(etl::memory_order order = etl::memory_order_seq_cst)
{
return value.load(order);
}
T* load(etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.load(order);
}
// Fetch add
T* fetch_add(ptrdiff_t v, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.fetch_add(v, order);
}
T* fetch_add(ptrdiff_t v, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.fetch_add(v, order);
}
// Fetch subtract
T* fetch_sub(ptrdiff_t v, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.fetch_sub(v, order);
}
T* fetch_sub(ptrdiff_t v, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.fetch_sub(v, order);
}
// Exchange
T* exchange(T* v, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.exchange(v, order);
}
T* exchange(T* v, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.exchange(v, order);
}
// Compare exchange weak
bool compare_exchange_weak(T*& expected, T* desired, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.compare_exchange_weak(expected, desired, order);
}
bool compare_exchange_weak(T*& expected, T* desired, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.compare_exchange_weak(expected, desired, order);
}
bool compare_exchange_weak(T*& expected, T* desired, etl::memory_order success, etl::memory_order failure)
{
return value.compare_exchange_weak(expected, desired, success, failure);
}
bool compare_exchange_weak(T*& expected, T* desired, etl::memory_order success, etl::memory_order failure) volatile
{
return value.compare_exchange_weak(expected, desired, success, failure);
}
// Compare exchange strong
bool compare_exchange_strong(T*& expected, T* desired, etl::memory_order order = etl::memory_order_seq_cst)
{
return value.compare_exchange_strong(expected, desired, order);
}
bool compare_exchange_strong(T*& expected, T* desired, etl::memory_order order = etl::memory_order_seq_cst) volatile
{
return value.compare_exchange_strong(expected, desired, order);
}
bool compare_exchange_strong(T*& expected, T* desired, etl::memory_order success, etl::memory_order failure)
{
return value.compare_exchange_strong(expected, desired, success, failure);
}
bool compare_exchange_strong(T*& expected, T* desired, etl::memory_order success, etl::memory_order failure) volatile
{
return value.compare_exchange_strong(expected, desired, success, failure);
}
private:
atomic & operator =(const atomic&) ETL_DELETE;
atomic& operator =(const atomic&) volatile ETL_DELETE;
std::atomic<T*> value;
};
typedef std::atomic<bool> atomic_bool;
typedef std::atomic<char> atomic_char;
typedef std::atomic<signed char> atomic_schar;
typedef std::atomic<unsigned char> atomic_uchar;
typedef std::atomic<short> atomic_short;
typedef std::atomic<unsigned short> atomic_ushort;
typedef std::atomic<int> atomic_int;
typedef std::atomic<unsigned int> atomic_uint;
typedef std::atomic<long> atomic_long;
typedef std::atomic<unsigned long> atomic_ulong;
typedef std::atomic<long long> atomic_llong;
typedef std::atomic<unsigned long long> atomic_ullong;
typedef std::atomic<wchar_t> atomic_wchar_t;
using atomic_bool = std::atomic<bool>;
using atomic_char = std::atomic<char>;
using atomic_schar = std::atomic<signed char>;
using atomic_uchar = std::atomic<unsigned char>;
using atomic_short = std::atomic<short>;
using atomic_ushort = std::atomic<unsigned short>;
using atomic_int = std::atomic<int>;
using atomic_uint = std::atomic<unsigned int>;
using atomic_long = std::atomic<long>;
using atomic_ulong = std::atomic<unsigned long>;
using atomic_llong = std::atomic<long long>;
using atomic_ullong = std::atomic<unsigned long long>;
using atomic_wchar_t = std::atomic<wchar_t>;
#if ETL_HAS_NATIVE_CHAR8_T
typedef std::atomic<char8_t> atomic_char8_t;
using atomic_char8_t = std::atomic<char8_t>;
#endif
#if ETL_HAS_NATIVE_CHAR16_T
typedef std::atomic<char16_t> atomic_char16_t;
using atomic_char16_t = std::atomic<char16_t>;
#endif
#if ETL_HAS_NATIVE_CHAR32_T
typedef std::atomic<char32_t> atomic_char32_t;
using atomic_char32_t = std::atomic<char32_t>;
#endif
#if ETL_USING_8BIT_TYPES
typedef std::atomic<uint8_t> atomic_uint8_t;
typedef std::atomic<int8_t> atomic_int8_t;
using atomic_uint8_t = std::atomic<uint8_t>;
using atomic_int8_t = std::atomic<int8_t>;
#endif
typedef std::atomic<uint16_t> atomic_uint16_t;
typedef std::atomic<int16_t> atomic_int16_t;
typedef std::atomic<uint32_t> atomic_uint32_t;
typedef std::atomic<int32_t> atomic_int32_t;
using atomic_uint16_t = std::atomic<uint16_t>;
using atomic_int16_t = std::atomic<int16_t>;
using atomic_uint32_t = std::atomic<uint32_t>;
using atomic_int32_t = std::atomic<int32_t>;
#if ETL_USING_64BIT_TYPES
typedef std::atomic<uint64_t> atomic_uint64_t;
typedef std::atomic<int64_t> atomic_int64_t;
using atomic_uint64_t = std::atomic<uint64_t>;
using atomic_int64_t = std::atomic<int64_t>;
#endif
typedef std::atomic<int_least8_t> atomic_int_least8_t;
typedef std::atomic<uint_least8_t> atomic_uint_least8_t;
typedef std::atomic<int_least16_t> atomic_int_least16_t;
typedef std::atomic<uint_least16_t> atomic_uint_least16_t;
typedef std::atomic<int_least32_t> atomic_int_least32_t;
typedef std::atomic<uint_least32_t> atomic_uint_least32_t;
using atomic_int_least8_t = std::atomic<int_least8_t>;
using atomic_uint_least8_t = std::atomic<uint_least8_t>;
using atomic_int_least16_t = std::atomic<int_least16_t>;
using atomic_uint_least16_t = std::atomic<uint_least16_t>;
using atomic_int_least32_t = std::atomic<int_least32_t>;
using atomic_uint_least32_t = std::atomic<uint_least32_t>;
#if ETL_USING_64BIT_TYPES
typedef std::atomic<int_least64_t> atomic_int_least64_t;
typedef std::atomic<uint_least64_t> atomic_uint_least64_t;
using atomic_int_least64_t = std::atomic<int_least64_t>;
using atomic_uint_least64_t = std::atomic<uint_least64_t>;
#endif
typedef std::atomic<int_fast8_t> atomic_int_fast8_t;
typedef std::atomic<uint_fast8_t> atomic_uint_fast8_t;
typedef std::atomic<int_fast16_t> atomic_int_fast16_t;
typedef std::atomic<uint_fast16_t> atomic_uint_fast16_t;
typedef std::atomic<int_fast32_t> atomic_int_fast32_t;
typedef std::atomic<uint_fast32_t> atomic_uint_fast32_t;
using atomic_int_fast8_t = std::atomic<int_fast8_t>;
using atomic_uint_fast8_t = std::atomic<uint_fast8_t>;
using atomic_int_fast16_t = std::atomic<int_fast16_t>;
using atomic_uint_fast16_t = std::atomic<uint_fast16_t>;
using atomic_int_fast32_t = std::atomic<int_fast32_t>;
using atomic_uint_fast32_t = std::atomic<uint_fast32_t>;
#if ETL_USING_64BIT_TYPES
typedef std::atomic<int_fast64_t> atomic_int_fast64_t;
typedef std::atomic<uint_fast64_t> atomic_uint_fast64_t;
using atomic_int_fast64_t = std::atomic<int_fast64_t>;
using atomic_uint_fast64_t = std::atomic<uint_fast64_t>;
#endif
typedef std::atomic<intptr_t> atomic_intptr_t;
typedef std::atomic<uintptr_t> atomic_uintptr_t;
typedef std::atomic<size_t> atomic_size_t;
typedef std::atomic<ptrdiff_t> atomic_ptrdiff_t;
typedef std::atomic<intmax_t> atomic_intmax_t;
typedef std::atomic<uintmax_t> atomic_uintmax_t;
using atomic_intptr_t = std::atomic<intptr_t>;
using atomic_uintptr_t = std::atomic<uintptr_t>;
using atomic_size_t = std::atomic<size_t>;
using atomic_ptrdiff_t = std::atomic<ptrdiff_t>;
using atomic_intmax_t = std::atomic<intmax_t>;
using atomic_uintmax_t = std::atomic<uintmax_t>;
}
#endif

View File

@ -85,9 +85,7 @@ SOFTWARE.
#define ETL_DECLARE_ENUM_TYPE(TypeName, ValueType) \
typedef ValueType value_type; \
ETL_CONSTEXPR TypeName() : value(static_cast<enum_type>(value_type())) {} \
ETL_CONSTEXPR TypeName(const TypeName &other) : value(other.value) {} \
ETL_CONSTEXPR TypeName(enum_type value_) : value(value_) {} \
ETL_CONSTEXPR14 TypeName& operator=(const TypeName &other) {value = other.value; return *this;} \
ETL_CONSTEXPR explicit TypeName(value_type value_) : value(static_cast<enum_type>(value_)) {} \
ETL_CONSTEXPR operator value_type() const {return static_cast<value_type>(value);} \
ETL_CONSTEXPR value_type get_value() const {return static_cast<value_type>(value);} \

View File

@ -59,6 +59,45 @@ namespace etl
{
static ETL_CONSTANT bool has_mutex = (ETL_HAS_MUTEX == 1);
}
//***************************************************************************
/// lock_guard
/// A mutex wrapper that provides an RAII mechanism for owning a mutex for
/// the duration of a scoped block.
//***************************************************************************
template <typename TMutex>
class lock_guard
{
public:
typedef TMutex mutex_type;
//*****************************************************
/// Constructor
/// Locks the mutex.
//*****************************************************
explicit lock_guard(mutex_type& m_)
: m(m_)
{
m.lock();
}
//*****************************************************
/// Destructor
//*****************************************************
~lock_guard()
{
m.unlock();
}
private:
// Deleted.
lock_guard(const lock_guard&) ETL_DELETE;
mutex_type& m;
};
}
#endif

View File

@ -53,9 +53,6 @@ namespace etl
{
while (__sync_lock_test_and_set(&flag, 1U))
{
while (flag)
{
}
}
}

View File

@ -39,37 +39,7 @@ namespace etl
///\ingroup mutex
///\brief This mutex class is implemented using std::mutex.
//***************************************************************************
class mutex
{
public:
mutex()
: access()
{
}
void lock()
{
access.lock();
}
bool try_lock()
{
return access.try_lock();
}
void unlock()
{
access.unlock();
}
private:
mutex(const mutex&) ETL_DELETE;
mutex& operator=(const mutex&) ETL_DELETE;
std::mutex access;
};
using mutex = std::mutex;
}
#endif

View File

@ -42,7 +42,7 @@ set(TEST_SOURCE_FILES
test_array.cpp
test_array_view.cpp
test_array_wrapper.cpp
test_atomic_std.cpp
test_atomic.cpp
test_binary.cpp
test_bip_buffer_spsc_atomic.cpp
test_bit.cpp
@ -324,16 +324,12 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
message(STATUS "Using GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,undefined")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
message(STATUS "Including test_atomic_gcc_sync")
list(APPEND TEST_SOURCE_FILES "test_atomic_gcc_sync.cpp")
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
message(STATUS "Using Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,undefined")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
message(STATUS "Including test_atomic_clang_sync")
list(APPEND TEST_SOURCE_FILES "test_atomic_clang_sync.cpp")
endif()
add_executable(etl_tests ${TEST_SOURCE_FILES})

View File

@ -5,7 +5,7 @@ Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2017 jwellbelove
Copyright(c) 2022 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
@ -29,10 +29,15 @@ SOFTWARE.
#include "unit_test_framework.h"
#include "etl/platform.h"
#include "etl/enum_type.h"
#if defined(ETL_COMPILER_CLANG)
#include "etl/atomic/atomic_clang_sync.h"
#if defined(ETL_COMPILER_MICROSOFT)
#include "etl/atomic/atomic_std.h"
#elif defined(ETL_COMPILER_GCC)
#include "etl/atomic/atomic_gcc_sync.h"
#elif defined(ETL_COMPILER_CLANG)
#include "etl/atomic/atomic_clang_sync.h"
#endif
#include <atomic>
#include <thread>
@ -45,7 +50,21 @@ SOFTWARE.
namespace
{
SUITE(test_atomic_clang)
struct Enum
{
enum enum_type
{
One = 1,
Two = 2
};
ETL_DECLARE_ENUM_TYPE(Enum, int)
ETL_ENUM_TYPE(One, "1")
ETL_ENUM_TYPE(Two, "2")
ETL_END_ENUM_TYPE
};
SUITE(test_atomic_std)
{
//*************************************************************************
TEST(test_atomic_integer_is_lock_free)
@ -65,15 +84,6 @@ namespace
CHECK_EQUAL(compare.is_lock_free(), test.is_lock_free());
}
//*************************************************************************
TEST(test_atomic_bool_is_lock_free)
{
std::atomic<bool> compare;
etl::atomic<bool> test;
CHECK_EQUAL(compare.is_lock_free(), test.is_lock_free());
}
//*************************************************************************
TEST(test_atomic_integer_load)
{
@ -83,10 +93,19 @@ namespace
CHECK_EQUAL((int)compare.load(), (int)test.load());
}
//*************************************************************************
TEST(test_atomic_enum_load)
{
std::atomic<Enum> compare(Enum::One);
etl::atomic<Enum> test(Enum::One);
CHECK_EQUAL((Enum)compare.load(), (Enum::value_type)test.load());
}
//*************************************************************************
TEST(test_atomic_pointer_load)
{
int i = 1;
int i;
std::atomic<int*> compare(&i);
etl::atomic<int*> test(&i);
@ -97,10 +116,10 @@ namespace
//*************************************************************************
TEST(test_atomic_bool_load)
{
bool i = true;
bool i;
std::atomic<bool> compare(i);
etl::atomic<bool> test(i);
std::atomic<bool> compare(&i);
etl::atomic<bool> test(&i);
CHECK_EQUAL((bool)compare.load(), (bool)test.load());
}
@ -116,6 +135,17 @@ namespace
CHECK_EQUAL((int)compare.load(), (int)test.load());
}
//*************************************************************************
TEST(test_atomic_enum_store)
{
std::atomic<Enum> compare(Enum::One);
etl::atomic<Enum> test(Enum::One);
compare.store(Enum::Two);
test.store(Enum::Two);
CHECK_EQUAL((Enum::value_type)compare.load(), (Enum::value_type)test.load());
}
//*************************************************************************
TEST(test_atomic_pointer_store)
{
@ -133,14 +163,11 @@ namespace
//*************************************************************************
TEST(test_atomic_bool_store)
{
bool i = true;
bool j = false;
std::atomic<bool> compare(false);
etl::atomic<bool> test(false);
std::atomic<bool> compare(i);
etl::atomic<bool> test(i);
compare.store(j);
test.store(j);
compare.store(true);
test.store(true);
CHECK_EQUAL((bool)compare.load(), (bool)test.load());
}
@ -155,6 +182,17 @@ namespace
CHECK_EQUAL((int)compare.load(), (int)test.load());
}
//*************************************************************************
TEST(test_atomic_enum_assignment)
{
std::atomic<Enum> compare(Enum::One);
etl::atomic<Enum> test(Enum::One);
compare = Enum::Two;
test = Enum::Two;
CHECK_EQUAL((Enum::value_type)compare.load(), (Enum::value_type)test.load());
}
//*************************************************************************
TEST(test_atomic_pointer_assignment)
{
@ -172,14 +210,11 @@ namespace
//*************************************************************************
TEST(test_atomic_bool_assignment)
{
bool i = true;
bool j = false;
std::atomic<bool> compare(false);
etl::atomic<bool> test(false);
std::atomic<bool> compare(i);
etl::atomic<bool> test(i);
compare = j;
test = j;
compare = true;
test = true;
CHECK_EQUAL((bool)compare.load(), (bool)test.load());
}
@ -435,6 +470,15 @@ namespace
CHECK_EQUAL((int)compare.exchange(2), (int)test.exchange(2));
}
//*************************************************************************
TEST(test_atomic_enum_exchange)
{
std::atomic<Enum> compare(Enum::One);
etl::atomic<Enum> test(Enum::One);
CHECK_EQUAL((Enum)compare.exchange(Enum::Two), (Enum)test.exchange(Enum::Two));
}
//*************************************************************************
TEST(test_atomic_pointer_exchange)
{
@ -450,13 +494,10 @@ namespace
//*************************************************************************
TEST(test_atomic_bool_exchange)
{
bool i = true;
bool j = false;
std::atomic<bool> compare(false);
etl::atomic<bool> test(false);
std::atomic<bool> compare(i);
etl::atomic<bool> test(i);
CHECK_EQUAL((bool)compare.exchange(j), (bool)test.exchange(j));
CHECK_EQUAL((bool)compare.exchange(true), (bool)test.exchange(true));
}
//*************************************************************************
@ -505,6 +546,52 @@ namespace
CHECK_EQUAL(compare.load(), test.load());
}
//*************************************************************************
TEST(test_atomic_compare_exchange_weak_pass_for_enum)
{
std::atomic<Enum> compare;
etl::atomic<Enum> test;
Enum actual = Enum::One;
compare = actual;
test = actual;
Enum compare_expected = actual;
Enum test_expected = actual;
Enum desired = Enum::Two;
bool compare_result = compare.compare_exchange_weak(compare_expected, desired);
bool test_result = test.compare_exchange_weak(test_expected, desired);
CHECK_EQUAL(compare_result, test_result);
CHECK_EQUAL(compare_expected, test_expected);
CHECK_EQUAL(compare.load(), test.load());
}
//*************************************************************************
TEST(test_atomic_compare_exchange_weak_pass_for_bool)
{
std::atomic<bool> compare;
etl::atomic<bool> test;
bool actual = false;
compare = actual;
test = actual;
bool compare_expected = actual;
bool test_expected = actual;
bool desired = true;
bool compare_result = compare.compare_exchange_weak(compare_expected, desired);
bool test_result = test.compare_exchange_weak(test_expected, desired);
CHECK_EQUAL(compare_result, test_result);
CHECK_EQUAL(compare_expected, test_expected);
CHECK_EQUAL(compare.load(), test.load());
}
//*************************************************************************
TEST(test_atomic_compare_exchange_strong_fail)
{
@ -551,6 +638,52 @@ namespace
CHECK_EQUAL(compare.load(), test.load());
}
//*************************************************************************
TEST(test_atomic_compare_exchange_strong_pass_for_enum)
{
std::atomic<Enum> compare;
etl::atomic<Enum> test;
Enum actual = Enum::One;
compare = actual;
test = actual;
Enum compare_expected = actual;
Enum test_expected = actual;
Enum desired = Enum::Two;
bool compare_result = compare.compare_exchange_strong(compare_expected, desired);
bool test_result = test.compare_exchange_strong(test_expected, desired);
CHECK_EQUAL(compare_result, test_result);
CHECK_EQUAL(compare_expected, test_expected);
CHECK_EQUAL(compare.load(), test.load());
}
//*************************************************************************
TEST(test_atomic_compare_exchange_strong_pass_for_bool)
{
std::atomic<bool> compare;
etl::atomic<bool> test;
bool actual = false;
compare = actual;
test = actual;
bool compare_expected = actual;
bool test_expected = actual;
bool desired = true;
bool compare_result = compare.compare_exchange_strong(compare_expected, desired);
bool test_result = test.compare_exchange_strong(test_expected, desired);
CHECK_EQUAL(compare_result, test_result);
CHECK_EQUAL(compare_expected, test_expected);
CHECK_EQUAL(compare.load(), test.load());
}
//*************************************************************************
#if REALTIME_TEST
@ -608,5 +741,3 @@ namespace
#endif
};
}
#endif

View File

@ -1,531 +0,0 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2017 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include "UnitTest++/UnitTest++.h"
#include "etl/platform.h"
#if defined(ETL_COMPILER_GCC)
#include "etl/atomic/atomic_gcc_sync.h"
#include <atomic>
#include <thread>
#define REALTIME_TEST 0
namespace
{
SUITE(test_atomic_std)
{
//=========================================================================
TEST(test_atomic_integer_is_lock_free)
{
std::atomic<int> compare;
etl::atomic<int> test;
CHECK_EQUAL(compare.is_lock_free(), test.is_lock_free());
}
//=========================================================================
TEST(test_atomic_pointer_is_lock_free)
{
std::atomic<int*> compare;
etl::atomic<int*> test;
CHECK_EQUAL(compare.is_lock_free(), test.is_lock_free());
}
//=========================================================================
TEST(test_atomic_integer_load)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)compare.load(), (int)test.load());
}
//=========================================================================
TEST(test_atomic_pointer_load)
{
int i;
std::atomic<int*> compare(&i);
etl::atomic<int*> test(&i);
CHECK_EQUAL((int*)compare.load(), (int*)test.load());
}
//=========================================================================
TEST(test_atomic_integer_store)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
compare.store(2);
test.store(2);
CHECK_EQUAL((int)compare.load(), (int)test.load());
}
//=========================================================================
TEST(test_atomic_pointer_store)
{
int i;
int j;
std::atomic<int*> compare(&i);
etl::atomic<int*> test(&i);
compare.store(&j);
test.store(&j);
CHECK_EQUAL((int*)compare.load(), (int*)test.load());
}
//=========================================================================
TEST(test_atomic_integer_assignment)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
compare = 2;
test = 2;
CHECK_EQUAL((int)compare.load(), (int)test.load());
}
//=========================================================================
TEST(test_atomic_pointer_assignment)
{
int i;
int j;
std::atomic<int*> compare(&i);
etl::atomic<int*> test(&i);
compare = &j;
test = &j;
CHECK_EQUAL((int*)compare.load(), (int*)test.load());
}
//=========================================================================
TEST(test_atomic_operator_integer_pre_increment)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)++compare, (int)++test);
CHECK_EQUAL((int)++compare, (int)++test);
}
//=========================================================================
TEST(test_atomic_operator_integer_post_increment)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)compare++, (int)test++);
CHECK_EQUAL((int)compare++, (int)test++);
}
//=========================================================================
TEST(test_atomic_operator_integer_pre_decrement)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)--compare, (int)--test);
CHECK_EQUAL((int)--compare, (int)--test);
}
//=========================================================================
TEST(test_atomic_operator_integer_post_decrement)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)compare--, (int)test--);
CHECK_EQUAL((int)compare--, (int)test--);
}
//=========================================================================
TEST(test_atomic_operator_pointer_pre_increment)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[0]);
etl::atomic<int*> test(&data[0]);
CHECK_EQUAL((int*)++compare, (int*)++test);
CHECK_EQUAL((int*)++compare, (int*)++test);
}
//=========================================================================
TEST(test_atomic_operator_pointer_post_increment)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[0]);
etl::atomic<int*> test(&data[0]);
CHECK_EQUAL((int*)compare++, (int*)test++);
CHECK_EQUAL((int*)compare++, (int*)test++);
}
//=========================================================================
TEST(test_atomic_operator_pointer_pre_decrement)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[3]);
etl::atomic<int*> test(&data[3]);
CHECK_EQUAL((int*)--compare, (int*)--test);
CHECK_EQUAL((int*)--compare, (int*)--test);
}
//=========================================================================
TEST(test_atomic_operator_pointer_post_decrement)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[3]);
etl::atomic<int*> test(&data[3]);
CHECK_EQUAL((int*)compare--, (int*)test--);
CHECK_EQUAL((int*)compare--, (int*)test--);
}
//=========================================================================
TEST(test_atomic_operator_integer_fetch_add)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)compare.fetch_add(2), (int)test.fetch_add(2));
}
//=========================================================================
TEST(test_atomic_operator_pointer_fetch_add)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[0]);
etl::atomic<int*> test(&data[0]);
CHECK_EQUAL((int*)compare.fetch_add(std::ptrdiff_t(10)), (int*)test.fetch_add(std::ptrdiff_t(10)));
}
//=========================================================================
TEST(test_atomic_operator_integer_plus_equals)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
compare += 2;
test += 2;
CHECK_EQUAL((int)compare, (int)test);
}
//=========================================================================
TEST(test_atomic_operator_pointer_plus_equals)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[0]);
etl::atomic<int*> test(&data[0]);
compare += 2;
test += 2;
CHECK_EQUAL((int*)compare, (int*)test);
}
//=========================================================================
TEST(test_atomic_operator_integer_minus_equals)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
compare -= 2;
test -= 2;
CHECK_EQUAL((int)compare, (int)test);
}
//=========================================================================
TEST(test_atomic_operator_pointer_minus_equals)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[3]);
etl::atomic<int*> test(&data[3]);
compare -= 2;
test -= 2;
CHECK_EQUAL((int*)compare, (int*)test);
}
//=========================================================================
TEST(test_atomic_operator_integer_and_equals)
{
std::atomic<int> compare(0x0000FFFFUL);
etl::atomic<int> test(0x0000FFFFUL);
compare &= 0x55AA55AAUL;
test &= 0x55AA55AAUL;
CHECK_EQUAL((int)compare, (int)test);
}
//=========================================================================
TEST(test_atomic_operator_integer_or_equals)
{
std::atomic<int> compare(0x0000FFFFUL);
etl::atomic<int> test(0x0000FFFFUL);
compare |= 0x55AA55AAUL;
test |= 0x55AA55AAUL;
CHECK_EQUAL((int)compare, (int)test);
}
//=========================================================================
TEST(test_atomic_operator_integer_xor_equals)
{
std::atomic<int> compare(0x0000FFFFUL);
etl::atomic<int> test(0x0000FFFFUL);
compare ^= 0x55AA55AAUL;
test ^= 0x55AA55AAUL;
CHECK_EQUAL((int)compare, (int)test);
}
//=========================================================================
TEST(test_atomic_operator_integer_fetch_sub)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)compare.fetch_sub(2), (int)test.fetch_sub(2));
}
//=========================================================================
TEST(test_atomic_operator_pointer_fetch_sub)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[0]);
etl::atomic<int*> test(&data[0]);
CHECK_EQUAL((int*)compare.fetch_add(std::ptrdiff_t(10)), (int*)test.fetch_add(std::ptrdiff_t(10)));
}
//=========================================================================
TEST(test_atomic_operator_fetch_and)
{
std::atomic<int> compare(0xFFFFFFFFUL);
etl::atomic<int> test(0xFFFFFFFFUL);
CHECK_EQUAL((int)compare.fetch_and(0x55AA55AAUL), (int)test.fetch_and(0x55AA55AAUL));
}
//=========================================================================
TEST(test_atomic_operator_fetch_or)
{
std::atomic<int> compare(0x0000FFFFUL);
etl::atomic<int> test(0x0000FFFFUL);
CHECK_EQUAL((int)compare.fetch_or(0x55AA55AAUL), (int)test.fetch_or(0x55AA55AAUL));
}
//=========================================================================
TEST(test_atomic_operator_fetch_xor)
{
std::atomic<int> compare(0x0000FFFFUL);
etl::atomic<int> test(0x0000FFFFUL);
CHECK_EQUAL((int)compare.fetch_xor(0x55AA55AAUL), (int)test.fetch_xor(0x55AA55AAUL));
}
//=========================================================================
TEST(test_atomic_integer_exchange)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)compare.exchange(2), (int)test.exchange(2));
}
//=========================================================================
TEST(test_atomic_pointer_exchange)
{
int i;
int j;
std::atomic<int*> compare(&i);
etl::atomic<int*> test(&i);
CHECK_EQUAL((int*)compare.exchange(&j), (int*)test.exchange(&j));
}
//=========================================================================
TEST(test_atomic_compare_exchange_weak_fail)
{
std::atomic<int> compare;
etl::atomic<int> test;
int actual = 1;
compare = actual;
test = actual;
int compare_expected = 2;
int test_expected = 2;
int desired = 3;
bool compare_result = compare.compare_exchange_weak(compare_expected, desired);
bool test_result = test.compare_exchange_weak(test_expected, desired);
CHECK_EQUAL(compare_result, test_result);
CHECK_EQUAL(compare_expected, test_expected);
CHECK_EQUAL(compare.load(), test.load());
}
//=========================================================================
TEST(test_atomic_compare_exchange_weak_pass)
{
std::atomic<int> compare;
etl::atomic<int> test;
int actual = 1;
compare = actual;
test = actual;
int compare_expected = actual;
int test_expected = actual;
int desired = 3;
bool compare_result = compare.compare_exchange_weak(compare_expected, desired);
bool test_result = test.compare_exchange_weak(test_expected, desired);
CHECK_EQUAL(compare_result, test_result);
CHECK_EQUAL(compare_expected, test_expected);
CHECK_EQUAL(compare.load(), test.load());
}
//=========================================================================
TEST(test_atomic_compare_exchange_strong_fail)
{
std::atomic<int> compare;
etl::atomic<int> test;
int actual = 1;
compare = actual;
test = actual;
int compare_expected = 2;
int test_expected = 2;
int desired = 3;
bool compare_result = compare.compare_exchange_strong(compare_expected, desired);
bool test_result = test.compare_exchange_strong(test_expected, desired);
CHECK_EQUAL(compare_result, test_result);
CHECK_EQUAL(compare_expected, test_expected);
CHECK_EQUAL(compare.load(), test.load());
}
//=========================================================================
TEST(test_atomic_compare_exchange_strong_pass)
{
std::atomic<int> compare;
etl::atomic<int> test;
int actual = 1;
compare = actual;
test = actual;
int compare_expected = actual;
int test_expected = actual;
int desired = 3;
bool compare_result = compare.compare_exchange_strong(compare_expected, desired);
bool test_result = test.compare_exchange_strong(test_expected, desired);
CHECK_EQUAL(compare_result, test_result);
CHECK_EQUAL(compare_expected, test_expected);
CHECK_EQUAL(compare.load(), test.load());
}
//=========================================================================
#if REALTIME_TEST
etl::atomic_int32_t atomic_value = 0U;
etl::atomic<int> atomic_flag = false;
void thread1()
{
while (!atomic_flag.load());
for (int i = 0; i < 10000000; ++i)
{
++atomic_value;
}
}
void thread2()
{
while (!atomic_flag.load());
for (int i = 0; i < 10000000; ++i)
{
--atomic_value;
}
}
TEST(test_atomic_multi_thread)
{
std::thread t1(thread1);
std::thread t2(thread2);
atomic_flag.store(true);
t1.join();
t2.join();
CHECK_EQUAL(0, atomic_value.load());
}
#endif
};
}
#endif

View File

@ -1,547 +0,0 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2017 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include "unit_test_framework.h"
#include "etl/platform.h"
#include "etl/atomic/atomic_std.h"
#include <atomic>
#include <thread>
#if defined(ETL_TARGET_OS_WINDOWS)
#include <Windows.h>
#endif
#define REALTIME_TEST 0
namespace
{
SUITE(test_atomic_std)
{
//*************************************************************************
TEST(test_atomic_integer_is_lock_free)
{
std::atomic<int> compare;
etl::atomic<int> test;
CHECK_EQUAL(compare.is_lock_free(), test.is_lock_free());
}
//*************************************************************************
TEST(test_atomic_pointer_is_lock_free)
{
std::atomic<int*> compare;
etl::atomic<int*> test;
CHECK_EQUAL(compare.is_lock_free(), test.is_lock_free());
}
//*************************************************************************
TEST(test_atomic_integer_load)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)compare.load(), (int)test.load());
}
//*************************************************************************
TEST(test_atomic_pointer_load)
{
int i;
std::atomic<int*> compare(&i);
etl::atomic<int*> test(&i);
CHECK_EQUAL((int*)compare.load(), (int*)test.load());
}
//*************************************************************************
TEST(test_atomic_integer_store)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
compare.store(2);
test.store(2);
CHECK_EQUAL((int)compare.load(), (int)test.load());
}
//*************************************************************************
TEST(test_atomic_pointer_store)
{
int i;
int j;
std::atomic<int*> compare(&i);
etl::atomic<int*> test(&i);
compare.store(&j);
test.store(&j);
CHECK_EQUAL((int*)compare.load(), (int*)test.load());
}
//*************************************************************************
TEST(test_atomic_integer_assignment)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
compare = 2;
test = 2;
CHECK_EQUAL((int)compare.load(), (int)test.load());
}
//*************************************************************************
TEST(test_atomic_pointer_assignment)
{
int i;
int j;
std::atomic<int*> compare(&i);
etl::atomic<int*> test(&i);
compare = &j;
test = &j;
CHECK_EQUAL((int*)compare.load(), (int*)test.load());
}
//*************************************************************************
TEST(test_atomic_operator_integer_pre_increment)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)++compare, (int)++test);
CHECK_EQUAL((int)++compare, (int)++test);
}
//*************************************************************************
TEST(test_atomic_operator_integer_post_increment)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)compare++, (int)test++);
CHECK_EQUAL((int)compare++, (int)test++);
}
//*************************************************************************
TEST(test_atomic_operator_integer_pre_decrement)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)--compare, (int)--test);
CHECK_EQUAL((int)--compare, (int)--test);
}
//*************************************************************************
TEST(test_atomic_operator_integer_post_decrement)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)compare--, (int)test--);
CHECK_EQUAL((int)compare--, (int)test--);
}
//*************************************************************************
TEST(test_atomic_operator_pointer_pre_increment)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[0]);
etl::atomic<int*> test(&data[0]);
CHECK_EQUAL((int*)++compare, (int*)++test);
CHECK_EQUAL((int*)++compare, (int*)++test);
}
//*************************************************************************
TEST(test_atomic_operator_pointer_post_increment)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[0]);
etl::atomic<int*> test(&data[0]);
CHECK_EQUAL((int*)compare++, (int*)test++);
CHECK_EQUAL((int*)compare++, (int*)test++);
}
//*************************************************************************
TEST(test_atomic_operator_pointer_pre_decrement)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[3]);
etl::atomic<int*> test(&data[3]);
CHECK_EQUAL((int*)--compare, (int*)--test);
CHECK_EQUAL((int*)--compare, (int*)--test);
}
//*************************************************************************
TEST(test_atomic_operator_pointer_post_decrement)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[3]);
etl::atomic<int*> test(&data[3]);
CHECK_EQUAL((int*)compare--, (int*)test--);
CHECK_EQUAL((int*)compare--, (int*)test--);
}
//*************************************************************************
TEST(test_atomic_operator_integer_fetch_add)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)compare.fetch_add(2), (int)test.fetch_add(2));
}
//*************************************************************************
TEST(test_atomic_operator_pointer_fetch_add)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[0]);
etl::atomic<int*> test(&data[0]);
CHECK_EQUAL((int*)compare.fetch_add(ptrdiff_t(10)), (int*)test.fetch_add(ptrdiff_t(10)));
}
//*************************************************************************
TEST(test_atomic_operator_integer_plus_equals)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
compare += 2;
test += 2;
CHECK_EQUAL((int)compare, (int)test);
}
//*************************************************************************
TEST(test_atomic_operator_pointer_plus_equals)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[0]);
etl::atomic<int*> test(&data[0]);
compare += 2;
test += 2;
CHECK_EQUAL((int*)compare, (int*)test);
}
//*************************************************************************
TEST(test_atomic_operator_integer_minus_equals)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
compare -= 2;
test -= 2;
CHECK_EQUAL((int)compare, (int)test);
}
//*************************************************************************
TEST(test_atomic_operator_pointer_minus_equals)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[3]);
etl::atomic<int*> test(&data[3]);
compare -= 2;
test -= 2;
CHECK_EQUAL((int*)compare, (int*)test);
}
//*************************************************************************
TEST(test_atomic_operator_integer_and_equals)
{
std::atomic<int> compare(0x0000FFFFUL);
etl::atomic<int> test(0x0000FFFFUL);
compare &= 0x55AA55AAUL;
test &= 0x55AA55AAUL;
CHECK_EQUAL((int)compare, (int)test);
}
//*************************************************************************
TEST(test_atomic_operator_integer_or_equals)
{
std::atomic<int> compare(0x0000FFFFUL);
etl::atomic<int> test(0x0000FFFFUL);
compare |= 0x55AA55AAUL;
test |= 0x55AA55AAUL;
CHECK_EQUAL((int)compare, (int)test);
}
//*************************************************************************
TEST(test_atomic_operator_integer_xor_equals)
{
std::atomic<int> compare(0x0000FFFFUL);
etl::atomic<int> test(0x0000FFFFUL);
compare ^= 0x55AA55AAUL;
test ^= 0x55AA55AAUL;
CHECK_EQUAL((int)compare, (int)test);
}
//*************************************************************************
TEST(test_atomic_operator_integer_fetch_sub)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)compare.fetch_sub(2), (int)test.fetch_sub(2));
}
//*************************************************************************
TEST(test_atomic_operator_pointer_fetch_sub)
{
int data[] = { 1, 2, 3, 4 };
std::atomic<int*> compare(&data[0]);
etl::atomic<int*> test(&data[0]);
CHECK_EQUAL((int*)compare.fetch_add(ptrdiff_t(10)), (int*)test.fetch_add(ptrdiff_t(10)));
}
//*************************************************************************
TEST(test_atomic_operator_fetch_and)
{
std::atomic<int> compare(0xFFFFFFFFUL);
etl::atomic<int> test(0xFFFFFFFFUL);
CHECK_EQUAL((int)compare.fetch_and(0x55AA55AAUL), (int)test.fetch_and(0x55AA55AAUL));
}
//*************************************************************************
TEST(test_atomic_operator_fetch_or)
{
std::atomic<int> compare(0x0000FFFFUL);
etl::atomic<int> test(0x0000FFFFUL);
CHECK_EQUAL((int)compare.fetch_or(0x55AA55AAUL), (int)test.fetch_or(0x55AA55AAUL));
}
//*************************************************************************
TEST(test_atomic_operator_fetch_xor)
{
std::atomic<int> compare(0x0000FFFFUL);
etl::atomic<int> test(0x0000FFFFUL);
CHECK_EQUAL((int)compare.fetch_xor(0x55AA55AAUL), (int)test.fetch_xor(0x55AA55AAUL));
}
//*************************************************************************
TEST(test_atomic_integer_exchange)
{
std::atomic<int> compare(1);
etl::atomic<int> test(1);
CHECK_EQUAL((int)compare.exchange(2), (int)test.exchange(2));
}
//*************************************************************************
TEST(test_atomic_pointer_exchange)
{
int i;
int j;
std::atomic<int*> compare(&i);
etl::atomic<int*> test(&i);
CHECK_EQUAL((int*)compare.exchange(&j), (int*)test.exchange(&j));
}
//*************************************************************************
TEST(test_atomic_compare_exchange_weak_fail)
{
std::atomic<int> compare;
etl::atomic<int> test;
int actual = 1;
compare = actual;
test = actual;
int compare_expected = 2;
int test_expected = 2;
int desired = 3;
bool compare_result = compare.compare_exchange_weak(compare_expected, desired);
bool test_result = test.compare_exchange_weak(test_expected, desired);
CHECK_EQUAL(compare_result, test_result);
CHECK_EQUAL(compare_expected, test_expected);
CHECK_EQUAL(compare.load(), test.load());
}
//*************************************************************************
TEST(test_atomic_compare_exchange_weak_pass)
{
std::atomic<int> compare;
etl::atomic<int> test;
int actual = 1;
compare = actual;
test = actual;
int compare_expected = actual;
int test_expected = actual;
int desired = 3;
bool compare_result = compare.compare_exchange_weak(compare_expected, desired);
bool test_result = test.compare_exchange_weak(test_expected, desired);
CHECK_EQUAL(compare_result, test_result);
CHECK_EQUAL(compare_expected, test_expected);
CHECK_EQUAL(compare.load(), test.load());
}
//*************************************************************************
TEST(test_atomic_compare_exchange_strong_fail)
{
std::atomic<int> compare;
etl::atomic<int> test;
int actual = 1;
compare = actual;
test = actual;
int compare_expected = 2;
int test_expected = 2;
int desired = 3;
bool compare_result = compare.compare_exchange_strong(compare_expected, desired);
bool test_result = test.compare_exchange_strong(test_expected, desired);
CHECK_EQUAL(compare_result, test_result);
CHECK_EQUAL(compare_expected, test_expected);
CHECK_EQUAL(compare.load(), test.load());
}
//*************************************************************************
TEST(test_atomic_compare_exchange_strong_pass)
{
std::atomic<int> compare;
etl::atomic<int> test;
int actual = 1;
compare = actual;
test = actual;
int compare_expected = actual;
int test_expected = actual;
int desired = 3;
bool compare_result = compare.compare_exchange_strong(compare_expected, desired);
bool test_result = test.compare_exchange_strong(test_expected, desired);
CHECK_EQUAL(compare_result, test_result);
CHECK_EQUAL(compare_expected, test_expected);
CHECK_EQUAL(compare.load(), test.load());
}
//*************************************************************************
#if REALTIME_TEST
#if defined(ETL_TARGET_OS_WINDOWS) // Only Windows priority is currently supported
#define RAISE_THREAD_PRIORITY SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST)
#define FIX_PROCESSOR_AFFINITY1 SetThreadAffinityMask(GetCurrentThread(), 1)
#define FIX_PROCESSOR_AFFINITY2 SetThreadAffinityMask(GetCurrentThread(), 2)
#else
#define RAISE_THREAD_PRIORITY
#define FIX_PROCESSOR_AFFINITY1
#define FIX_PROCESSOR_AFFINITY2
#endif
etl::atomic_int32_t atomic_value = 0U;
etl::atomic<int> start = false;
void thread1()
{
RAISE_THREAD_PRIORITY;
FIX_PROCESSOR_AFFINITY1;
while (!start.load());
for (int i = 0; i < 10000000; ++i)
{
++atomic_value;
}
}
void thread2()
{
RAISE_THREAD_PRIORITY;
FIX_PROCESSOR_AFFINITY2;
while (!start.load());
for (int i = 0; i < 10000000; ++i)
{
--atomic_value;
}
}
TEST(test_atomic_multi_thread)
{
std::thread t1(thread1);
std::thread t2(thread2);
start.store(true);
t1.join();
t2.join();
CHECK_EQUAL(0, atomic_value.load());
}
#endif
};
}

View File

@ -10909,8 +10909,7 @@
</ClCompile>
<ClCompile Include="..\test_algorithm.cpp" />
<ClCompile Include="..\test_alignment.cpp" />
<ClCompile Include="..\test_atomic_clang_sync.cpp" />
<ClCompile Include="..\test_atomic_gcc_sync.cpp" />
<ClCompile Include="..\test_atomic.cpp" />
<ClCompile Include="..\test_bit.cpp" />
<ClCompile Include="..\test_bresenham_line.cpp" />
<ClCompile Include="..\test_buffer_descriptors.cpp" />
@ -11009,7 +11008,6 @@
</ClCompile>
<ClCompile Include="..\test_array_view.cpp" />
<ClCompile Include="..\test_array_wrapper.cpp" />
<ClCompile Include="..\test_atomic_std.cpp" />
<ClCompile Include="..\test_binary.cpp" />
<ClCompile Include="..\test_bip_buffer_spsc_atomic.cpp" />
<ClCompile Include="..\test_bitset.cpp" />

View File

@ -1931,15 +1931,6 @@
<ClCompile Include="..\test_bip_buffer_spsc_atomic.cpp">
<Filter>Tests\Queues</Filter>
</ClCompile>
<ClCompile Include="..\test_atomic_clang_sync.cpp">
<Filter>Tests\Atomic</Filter>
</ClCompile>
<ClCompile Include="..\test_atomic_gcc_sync.cpp">
<Filter>Tests\Atomic</Filter>
</ClCompile>
<ClCompile Include="..\test_atomic_std.cpp">
<Filter>Tests\Atomic</Filter>
</ClCompile>
<ClCompile Include="..\test_crc8_ccitt.cpp">
<Filter>Tests\CRC</Filter>
</ClCompile>
@ -3254,6 +3245,9 @@
<ClCompile Include="..\sanity-check\delegate_cpp03.h.t.cpp">
<Filter>Tests\Sanity Checks\Source</Filter>
</ClCompile>
<ClCompile Include="..\test_atomic.cpp">
<Filter>Tests\Atomic</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\library.properties">