mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-16 00:46:03 +08:00
Updates to atomic classes.
This commit is contained in:
parent
44068d1493
commit
bc0a8a49a7
@ -31,21 +31,17 @@ SOFTWARE.
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
#if ETL_ATOMIC_SUPPORTED == 1
|
||||
#include <atomic>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
typedef std::atomic<uint32_t> atomic_uint32_t;
|
||||
}
|
||||
#if ETL_STD_ATOMIC_SUPPORTED == 1
|
||||
#include "atomic/atomic_std.h"
|
||||
#define ETL_HAS_ATOMIC 1
|
||||
#elif defined(ETL_COMPILER_ARM)
|
||||
#include "atomic/atomic_arm.h"
|
||||
#define ETL_HAS_ATOMIC 1
|
||||
#elif defined(ETL_COMPILER_GCC)
|
||||
#include "atomic/atomic_gcc.h"
|
||||
#elif defined(ETL_COMPILER_MSVC)
|
||||
#include "atomic/atomic_windows.h"
|
||||
#include "atomic/atomic_gcc_sync.h"
|
||||
#define ETL_HAS_ATOMIC 1
|
||||
#else
|
||||
#warning NO ATOMIC SUPPORT DEFINED!
|
||||
#define ETL_HAS_ATOMIC 0
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@ -29,6 +29,6 @@ SOFTWARE.
|
||||
#ifndef __ETL_ATOMIC_ARM__
|
||||
#define __ETL_ATOMIC_ARM__
|
||||
|
||||
#include "atomic_gcc.h"
|
||||
#include "atomic_gcc_atomic.h"
|
||||
|
||||
#endif
|
||||
|
||||
@ -26,23 +26,37 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __ETL_ATOMIC_GCC__
|
||||
#define __ETL_ATOMIC_GCC__
|
||||
#ifndef __ETL_ATOMIC_GCC_SYNC__
|
||||
#define __ETL_ATOMIC_GCC_SYNC__
|
||||
|
||||
#include "../platform.h"
|
||||
#include "../type_traits.h"
|
||||
#include "../char_traits.h"
|
||||
#include "../static_assert.h"
|
||||
#include "../nullptr.h"
|
||||
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
// Atomic type for pre C++11 GCC compilers that support the builtin 'fetch' functions.
|
||||
// Atomic type for pre C++11 GCC compilers that support the builtin '__sync' functions.
|
||||
// Only integral and pointer types are supported.
|
||||
//***************************************************************************
|
||||
|
||||
typedef enum memory_order
|
||||
{
|
||||
memory_order_relaxed,
|
||||
memory_order_consume,
|
||||
memory_order_acquire,
|
||||
memory_order_release,
|
||||
memory_order_acq_rel,
|
||||
memory_order_seq_cst
|
||||
} memory_order;
|
||||
|
||||
template <typename T>
|
||||
class atomic
|
||||
{
|
||||
@ -63,14 +77,14 @@ namespace etl
|
||||
// Assignment
|
||||
T operator =(T v)
|
||||
{
|
||||
__sync_lock_test_and_set(&value, v);
|
||||
store(v);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
T operator =(T v) volatile
|
||||
{
|
||||
__sync_lock_test_and_set(&value, v);
|
||||
store(v);
|
||||
|
||||
return v;
|
||||
}
|
||||
@ -177,12 +191,12 @@ namespace etl
|
||||
// Conversion operator
|
||||
operator T () const
|
||||
{
|
||||
return __sync_fetch_and_add(const_cast<T*>(&value), 0);
|
||||
return __sync_fetch_and_add(&value, 0);
|
||||
}
|
||||
|
||||
operator T() volatile const
|
||||
{
|
||||
return __sync_fetch_and_add(const_cast<T*>(&value), 0);
|
||||
return __sync_fetch_and_add(&value, 0);
|
||||
}
|
||||
|
||||
// Is lock free?
|
||||
@ -197,95 +211,95 @@ namespace etl
|
||||
}
|
||||
|
||||
// Store
|
||||
void store(T v)
|
||||
void store(T v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
__sync_lock_test_and_set(&value, v);
|
||||
}
|
||||
|
||||
void store(T v) volatile
|
||||
void store(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
__sync_lock_test_and_set(&value, v);
|
||||
}
|
||||
|
||||
// Load
|
||||
T load()
|
||||
T load(etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return __sync_fetch_and_add(&value, 0);
|
||||
}
|
||||
|
||||
T load() volatile
|
||||
T load(etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return __sync_fetch_and_add(&value, 0);
|
||||
}
|
||||
|
||||
// Fetch add
|
||||
T fetch_add(T v)
|
||||
T fetch_add(T v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return __sync_fetch_and_add(&value, v);
|
||||
}
|
||||
|
||||
T fetch_add(T v) volatile
|
||||
T fetch_add(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return __sync_fetch_and_add(&value, v);
|
||||
}
|
||||
|
||||
// Fetch subtract
|
||||
T fetch_sub(T v)
|
||||
T fetch_sub(T v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return __sync_fetch_and_sub(&value, v);
|
||||
}
|
||||
|
||||
T fetch_sub(T v) volatile
|
||||
T fetch_sub(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return __sync_fetch_and_sub(&value, v);
|
||||
}
|
||||
|
||||
// Fetch or
|
||||
T fetch_or(T v)
|
||||
T fetch_or(T v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return __sync_fetch_and_or(&value, v);
|
||||
}
|
||||
|
||||
T fetch_or(T v) volatile
|
||||
T fetch_or(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return __sync_fetch_and_or(&value, v);
|
||||
}
|
||||
|
||||
// Fetch and
|
||||
T fetch_and(T v)
|
||||
T fetch_and(T v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return __sync_fetch_and_and(&value, v);
|
||||
}
|
||||
|
||||
T fetch_and(T v) volatile
|
||||
T fetch_and(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return __sync_fetch_and_and(&value, v);
|
||||
}
|
||||
|
||||
// Fetch exclusive or
|
||||
T fetch_xor(T v)
|
||||
T fetch_xor(T v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return __sync_fetch_and_xor(&value, v);
|
||||
}
|
||||
|
||||
T fetch_xor(T v) volatile
|
||||
T fetch_xor(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return __sync_fetch_and_xor(&value, v);
|
||||
}
|
||||
|
||||
// Exchange
|
||||
T exchange(T v)
|
||||
T exchange(T v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return __sync_lock_test_and_set(&value, v);
|
||||
}
|
||||
|
||||
T exchange(T v) volatile
|
||||
T exchange(T v, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return __sync_lock_test_and_set(&value, v);
|
||||
}
|
||||
|
||||
// Compare exchange weak
|
||||
bool compare_exchange_weak(T& expected, T desired)
|
||||
bool compare_exchange_weak(T& expected, T desired, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
T old = __sync_val_compare_and_swap(&value, expected, desired);
|
||||
|
||||
@ -300,7 +314,37 @@ namespace etl
|
||||
}
|
||||
}
|
||||
|
||||
bool compare_exchange_weak(T& expected, T desired) volatile
|
||||
bool compare_exchange_weak(T& expected, T desired, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
T old = __sync_val_compare_and_swap(&value, expected, desired);
|
||||
|
||||
if (old == expected)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
expected = old;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool compare_exchange_weak(T& expected, T desired, etl::memory_order success, etl::memory_order failure)
|
||||
{
|
||||
T old = __sync_val_compare_and_swap(&value, expected, desired);
|
||||
|
||||
if (old == expected)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
expected = old;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool compare_exchange_weak(T& expected, T desired, etl::memory_order success, etl::memory_order failure) volatile
|
||||
{
|
||||
T old = __sync_val_compare_and_swap(&value, expected, desired);
|
||||
|
||||
@ -316,7 +360,7 @@ namespace etl
|
||||
}
|
||||
|
||||
// Compare exchange strong
|
||||
bool compare_exchange_strong(T& expected, T desired)
|
||||
bool compare_exchange_strong(T& expected, T desired, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
T old = expected;
|
||||
|
||||
@ -332,7 +376,39 @@ namespace etl
|
||||
return true;
|
||||
}
|
||||
|
||||
bool compare_exchange_strong(T& expected, T desired) volatile
|
||||
bool compare_exchange_strong(T& expected, T desired, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
T old = expected;
|
||||
|
||||
while (!compare_exchange_weak(old, desired))
|
||||
{
|
||||
if (memcmp(&old, &expected, sizeof(T)))
|
||||
{
|
||||
expected = old;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool compare_exchange_strong(T& expected, T desired, etl::memory_order success, etl::memory_order failure)
|
||||
{
|
||||
T old = expected;
|
||||
|
||||
while (!compare_exchange_weak(old, desired))
|
||||
{
|
||||
if (memcmp(&old, &expected, sizeof(T)))
|
||||
{
|
||||
expected = old;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool compare_exchange_strong(T& expected, T desired, etl::memory_order success, etl::memory_order failure) volatile
|
||||
{
|
||||
T old = expected;
|
||||
|
||||
@ -356,9 +432,6 @@ namespace etl
|
||||
T value;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
// Specialisation for pointers.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
class atomic<T*>
|
||||
{
|
||||
@ -369,66 +442,66 @@ namespace etl
|
||||
{
|
||||
}
|
||||
|
||||
atomic(T v)
|
||||
atomic(T* v)
|
||||
: value(v)
|
||||
{
|
||||
}
|
||||
|
||||
// Assignment
|
||||
T operator =(T* v)
|
||||
T* operator =(T* v)
|
||||
{
|
||||
__sync_lock_test_and_set(&value, v);
|
||||
store(v);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
T operator =(T* v) volatile
|
||||
T* operator =(T* v) volatile
|
||||
{
|
||||
__sync_lock_test_and_set(&value, v);
|
||||
store(v);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
// Pre-increment
|
||||
T operator ++()
|
||||
T* operator ++()
|
||||
{
|
||||
return fetch_add(1) + 1;
|
||||
}
|
||||
|
||||
T operator ++() volatile
|
||||
T* operator ++() volatile
|
||||
{
|
||||
return fetch_add(1) + 1;
|
||||
}
|
||||
|
||||
// Post-increment
|
||||
T operator ++(int)
|
||||
T* operator ++(int)
|
||||
{
|
||||
return fetch_add(1);
|
||||
}
|
||||
|
||||
T operator ++(int) volatile
|
||||
T* operator ++(int) volatile
|
||||
{
|
||||
return fetch_add(1);
|
||||
}
|
||||
|
||||
// Pre-decrement
|
||||
T operator --()
|
||||
T* operator --()
|
||||
{
|
||||
return fetch_sub(1) + 1;
|
||||
}
|
||||
|
||||
T operator --() volatile
|
||||
T* operator --() volatile
|
||||
{
|
||||
return fetch_sub(1) + 1;
|
||||
}
|
||||
|
||||
// Post-decrement
|
||||
T operator --(int)
|
||||
T* operator --(int)
|
||||
{
|
||||
return fetch_sub(1);
|
||||
}
|
||||
|
||||
T operator --(int) volatile
|
||||
T* operator --(int) volatile
|
||||
{
|
||||
return fetch_sub(1);
|
||||
}
|
||||
@ -456,14 +529,14 @@ namespace etl
|
||||
}
|
||||
|
||||
// Conversion operator
|
||||
operator T () const
|
||||
operator T* () const
|
||||
{
|
||||
return __sync_fetch_and_add(const_cast<T**>(&value), 0);
|
||||
return __sync_fetch_and_add(&value, 0);
|
||||
}
|
||||
|
||||
operator T() volatile const
|
||||
operator T*() volatile const
|
||||
{
|
||||
return __sync_fetch_and_add(const_cast<T**>(&value), 0);
|
||||
return __sync_fetch_and_add(&value, 0);
|
||||
}
|
||||
|
||||
// Is lock free?
|
||||
@ -478,79 +551,129 @@ namespace etl
|
||||
}
|
||||
|
||||
// Store
|
||||
void store(T v)
|
||||
void store(T* v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
__sync_lock_test_and_set(&value, v);
|
||||
}
|
||||
|
||||
void store(T v) volatile
|
||||
void store(T* v, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
__sync_lock_test_and_set(&value, v);
|
||||
}
|
||||
|
||||
// Load
|
||||
T load()
|
||||
T* load(etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return __sync_fetch_and_add(&value, 0);
|
||||
}
|
||||
|
||||
T load() volatile
|
||||
T* load(etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return __sync_fetch_and_add(&value, 0);
|
||||
}
|
||||
|
||||
// Fetch add
|
||||
T* fetch_add(std::ptrdiff_t v)
|
||||
T* fetch_add(std::ptrdiff_t v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return __sync_fetch_and_add(&value, v);
|
||||
}
|
||||
|
||||
T* fetch_add(std::ptrdiff_t v) volatile
|
||||
T* fetch_add(std::ptrdiff_t v, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return __sync_fetch_and_add(&value, v);
|
||||
}
|
||||
|
||||
// Fetch subtract
|
||||
T* fetch_sub(std::ptrdiff_t v)
|
||||
T* fetch_sub(std::ptrdiff_t v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return __sync_fetch_and_sub(&value, v);
|
||||
}
|
||||
|
||||
T* fetch_sub(std::ptrdiff_t v) volatile
|
||||
T* fetch_sub(std::ptrdiff_t v, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return __sync_fetch_and_sub(&value, v);
|
||||
}
|
||||
|
||||
// Exchange
|
||||
T exchange(T v)
|
||||
T* exchange(T* v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return __sync_lock_test_and_set(&value, v);
|
||||
}
|
||||
|
||||
T exchange(T v) volatile
|
||||
T* exchange(T* v, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return __sync_lock_test_and_set(&value, v);
|
||||
}
|
||||
|
||||
// Compare exchange weak
|
||||
bool compare_exchange_weak(T& expected, T desired)
|
||||
bool compare_exchange_weak(T*& expected, T* desired, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return __sync_bool_compare_and_swap(&value, expected, desired);
|
||||
T* old = __sync_val_compare_and_swap(&value, expected, desired);
|
||||
|
||||
if (old == expected)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
expected = old;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool compare_exchange_weak(T& expected, T desired) volatile
|
||||
bool compare_exchange_weak(T*& expected, T* desired, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return __sync_bool_compare_and_swap(&value, expected, desired);
|
||||
T* old = __sync_val_compare_and_swap(&value, expected, desired);
|
||||
|
||||
if (old == expected)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
expected = old;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool compare_exchange_weak(T*& expected, T* desired, etl::memory_order success, etl::memory_order failure)
|
||||
{
|
||||
T* old = __sync_val_compare_and_swap(&value, expected, desired);
|
||||
|
||||
if (old == expected)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
expected = old;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool compare_exchange_weak(T*& expected, T* desired, etl::memory_order success, etl::memory_order failure) volatile
|
||||
{
|
||||
T* old = __sync_val_compare_and_swap(&value, expected, desired);
|
||||
|
||||
if (old == expected)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
expected = old;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Compare exchange strong
|
||||
bool compare_exchange_strong(T& expected, T desired)
|
||||
bool compare_exchange_strong(T*& expected, T* desired, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
T old = expected;
|
||||
T* old = expected;
|
||||
|
||||
while (!compare_exchange_weak(old, desired))
|
||||
{
|
||||
if (memcmp(&old, &expected, sizeof(T)))
|
||||
if (memcmp(&old, &expected, sizeof(T*)))
|
||||
{
|
||||
expected = old;
|
||||
return false;
|
||||
@ -560,13 +683,45 @@ namespace etl
|
||||
return true;
|
||||
}
|
||||
|
||||
bool compare_exchange_strong(T& expected, T desired) volatile
|
||||
bool compare_exchange_strong(T*& expected, T* desired, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
T old = expected;
|
||||
T* old = expected;
|
||||
|
||||
while (!compare_exchange_weak(old, desired))
|
||||
{
|
||||
if (memcmp(&old, &expected, sizeof(T)))
|
||||
if (memcmp(&old, &expected, sizeof(T*)))
|
||||
{
|
||||
expected = old;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool compare_exchange_strong(T*& expected, T* desired, etl::memory_order success, etl::memory_order failure)
|
||||
{
|
||||
T* old = expected;
|
||||
|
||||
while (!compare_exchange_weak(old, desired))
|
||||
{
|
||||
if (memcmp(&old, &expected, sizeof(T*)))
|
||||
{
|
||||
expected = old;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool compare_exchange_strong(T*& expected, T* desired, etl::memory_order success, etl::memory_order failure) volatile
|
||||
{
|
||||
T* old = expected;
|
||||
|
||||
while (!compare_exchange_weak(old, desired))
|
||||
{
|
||||
if (memcmp(&old, &expected, sizeof(T*)))
|
||||
{
|
||||
expected = old;
|
||||
return false;
|
||||
@ -630,4 +785,6 @@ namespace etl
|
||||
typedef etl::atomic<uintmax_t> atomic_uintmax_t;
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#endif
|
||||
604
include/etl/atomic/atomic_std.h
Normal file
604
include/etl/atomic/atomic_std.h
Normal file
@ -0,0 +1,604 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2018 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.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __ETL_ATOMIC_STD__
|
||||
#define __ETL_ATOMIC_STD__
|
||||
|
||||
#include "../platform.h"
|
||||
#include "../nullptr.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
// ETL Atomic type for compilers that support std::atomic.
|
||||
// etl::atomic is a simple wrapper around std::atomic.
|
||||
//***************************************************************************
|
||||
|
||||
typedef std::memory_order memory_order;
|
||||
|
||||
static const etl::memory_order memory_order_relaxed = std::memory_order_relaxed;
|
||||
static const etl::memory_order memory_order_consume = std::memory_order_consume;
|
||||
static const etl::memory_order memory_order_acquire = std::memory_order_acquire;
|
||||
static const etl::memory_order memory_order_release = std::memory_order_release;
|
||||
static const etl::memory_order memory_order_acq_rel = std::memory_order_acq_rel;
|
||||
static const 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)
|
||||
{
|
||||
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(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(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 +=(std::ptrdiff_t v)
|
||||
{
|
||||
return value += v;
|
||||
}
|
||||
|
||||
T* operator +=(std::ptrdiff_t v) volatile
|
||||
{
|
||||
return value += v;
|
||||
}
|
||||
|
||||
// Subtract
|
||||
T* operator -=(std::ptrdiff_t v)
|
||||
{
|
||||
return value -= v;
|
||||
}
|
||||
|
||||
T* operator -=(std::ptrdiff_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)
|
||||
{
|
||||
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(std::ptrdiff_t v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return value.fetch_add(v, order);
|
||||
}
|
||||
|
||||
T* fetch_add(std::ptrdiff_t v, etl::memory_order order = etl::memory_order_seq_cst) volatile
|
||||
{
|
||||
return value.fetch_add(v, order);
|
||||
}
|
||||
|
||||
// Fetch subtract
|
||||
T* fetch_sub(std::ptrdiff_t v, etl::memory_order order = etl::memory_order_seq_cst)
|
||||
{
|
||||
return value.fetch_sub(v, order);
|
||||
}
|
||||
|
||||
T* fetch_sub(std::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&);
|
||||
//atomic& operator =(const atomic&) volatile;
|
||||
|
||||
std::atomic<T*> value;
|
||||
};
|
||||
|
||||
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;
|
||||
typedef std::atomic<char16_t> atomic_char16_t;
|
||||
typedef std::atomic<char32_t> atomic_char32_t;
|
||||
typedef std::atomic<uint8_t> atomic_uint8_t;
|
||||
typedef std::atomic<int8_t> atomic_int8_t;
|
||||
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;
|
||||
typedef std::atomic<uint64_t> atomic_uint64_t;
|
||||
typedef std::atomic<int64_t> atomic_int64_t;
|
||||
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;
|
||||
typedef std::atomic<int_least64_t> atomic_int_least64_t;
|
||||
typedef std::atomic<uint_least64_t> atomic_uint_least64_t;
|
||||
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;
|
||||
typedef std::atomic<int_fast64_t> atomic_int_fast64_t;
|
||||
typedef std::atomic<uint_fast64_t> atomic_uint_fast64_t;
|
||||
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;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -1,104 +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.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __ETL_ATOMIC_WINDOWS__
|
||||
#define __ETL_ATOMIC_WINDOWS__
|
||||
|
||||
#include "../platform.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <Windows.h>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
class atomic_uint32_t
|
||||
{
|
||||
public:
|
||||
|
||||
atomic_uint32_t()
|
||||
{
|
||||
InterlockedExchange(&value, 0);
|
||||
}
|
||||
|
||||
atomic_uint32_t(uint32_t v)
|
||||
{
|
||||
InterlockedExchange(&value, v);
|
||||
}
|
||||
|
||||
atomic_uint32_t& operator =(uint32_t v)
|
||||
{
|
||||
InterlockedExchange(&value, v);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
atomic_uint32_t& operator ++()
|
||||
{
|
||||
InterlockedIncrement(&value);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
volatile atomic_uint32_t& operator ++() volatile
|
||||
{
|
||||
InterlockedIncrement(&value);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
atomic_uint32_t& operator --()
|
||||
{
|
||||
InterlockedDecrement(&value);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
volatile atomic_uint32_t& operator --() volatile
|
||||
{
|
||||
InterlockedDecrement(&value);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator uint32_t () const
|
||||
{
|
||||
return InterlockedAdd((volatile long*)&value, 0);
|
||||
}
|
||||
|
||||
operator uint32_t() volatile const
|
||||
{
|
||||
return InterlockedAdd((volatile long*)&value, 0);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
uint32_t value;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -44,7 +44,7 @@ SOFTWARE.
|
||||
#undef ETL_NO_NULLPTR_SUPPORT
|
||||
#undef ETL_NO_LARGE_CHAR_SUPPORT
|
||||
#undef ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED
|
||||
#undef ETL_ATOMIC_SUPPORTED
|
||||
#undef ETL_STD_ATOMIC_SUPPORTED
|
||||
|
||||
#include "etl_profile.h"
|
||||
|
||||
@ -53,6 +53,10 @@ SOFTWARE.
|
||||
#pragma warning(disable : 4996)
|
||||
#endif
|
||||
|
||||
#if defined(ETL_COMPILER_GCC)
|
||||
#define GCC_VERSION ((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__)
|
||||
#endif
|
||||
|
||||
#if ETL_CPP11_SUPPORTED
|
||||
#define ETL_CONSTEXPR constexpr
|
||||
#else
|
||||
|
||||
@ -46,6 +46,6 @@ SOFTWARE.
|
||||
#define ETL_NO_NULLPTR_SUPPORT 1
|
||||
#define ETL_NO_LARGE_CHAR_SUPPORT 1
|
||||
#define ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED 0
|
||||
#define ETL_ATOMIC_SUPPORTED 0
|
||||
#define ETL_STD_ATOMIC_SUPPORTED 0
|
||||
|
||||
#endif
|
||||
|
||||
@ -46,6 +46,6 @@ SOFTWARE.
|
||||
#define ETL_NO_NULLPTR_SUPPORT 1
|
||||
#define ETL_NO_LARGE_CHAR_SUPPORT 1
|
||||
#define ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED 0
|
||||
#define ETL_ATOMIC_SUPPORTED 0
|
||||
#define ETL_STD_ATOMIC_SUPPORTED 0
|
||||
|
||||
#endif
|
||||
|
||||
@ -46,6 +46,6 @@ SOFTWARE.
|
||||
#define ETL_NO_NULLPTR_SUPPORT 0
|
||||
#define ETL_NO_LARGE_CHAR_SUPPORT 0
|
||||
#define ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED 1
|
||||
#define ETL_ATOMIC_SUPPORTED 1
|
||||
#define ETL_STD_ATOMIC_SUPPORTED 1
|
||||
|
||||
#endif
|
||||
|
||||
@ -46,6 +46,6 @@ SOFTWARE.
|
||||
#define ETL_NO_NULLPTR_SUPPORT 1
|
||||
#define ETL_NO_LARGE_CHAR_SUPPORT 1
|
||||
#define ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED 0
|
||||
#define ETL_ATOMIC_SUPPORTED 0
|
||||
#define ETL_STD_ATOMIC_SUPPORTED 0
|
||||
|
||||
#endif
|
||||
|
||||
@ -46,6 +46,6 @@ SOFTWARE.
|
||||
#define ETL_NO_NULLPTR_SUPPORT 0
|
||||
#define ETL_NO_LARGE_CHAR_SUPPORT 0
|
||||
#define ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED 1
|
||||
#define ETL_ATOMIC_SUPPORTED 1
|
||||
#define ETL_STD_ATOMIC_SUPPORTED 1
|
||||
|
||||
#endif
|
||||
|
||||
@ -46,6 +46,6 @@ SOFTWARE.
|
||||
#define ETL_NO_NULLPTR_SUPPORT 0
|
||||
#define ETL_NO_LARGE_CHAR_SUPPORT 0
|
||||
#define ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED 1
|
||||
#define ETL_ATOMIC_SUPPORTED 1
|
||||
#define ETL_STD_ATOMIC_SUPPORTED 1
|
||||
|
||||
#endif
|
||||
|
||||
@ -52,6 +52,6 @@ SOFTWARE.
|
||||
#define ETL_NO_NULLPTR_SUPPORT !ETL_CPP11_SUPPORTED
|
||||
#define ETL_NO_LARGE_CHAR_SUPPORT !ETL_CPP11_SUPPORTED
|
||||
#define ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED ETL_CPP14_SUPPORTED
|
||||
#define ETL_ATOMIC_SUPPORTED ETL_CPP11_SUPPORTED
|
||||
#define ETL_STD_ATOMIC_SUPPORTED ETL_CPP11_SUPPORTED
|
||||
|
||||
#endif
|
||||
|
||||
@ -52,6 +52,6 @@ SOFTWARE.
|
||||
#define ETL_NO_NULLPTR_SUPPORT !ETL_CPP11_SUPPORTED
|
||||
#define ETL_NO_LARGE_CHAR_SUPPORT !ETL_CPP11_SUPPORTED
|
||||
#define ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED ETL_CPP14_SUPPORTED
|
||||
#define ETL_ATOMIC_SUPPORTED ETL_CPP11_SUPPORTED
|
||||
#define ETL_STD_ATOMIC_SUPPORTED ETL_CPP11_SUPPORTED
|
||||
|
||||
#endif
|
||||
|
||||
@ -52,6 +52,6 @@ SOFTWARE.
|
||||
#define ETL_NO_NULLPTR_SUPPORT !ETL_CPP11_SUPPORTED
|
||||
#define ETL_NO_LARGE_CHAR_SUPPORT !ETL_CPP11_SUPPORTED
|
||||
#define ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED ETL_CPP14_SUPPORTED
|
||||
#define ETL_ATOMIC_SUPPORTED ETL_CPP11_SUPPORTED
|
||||
#define ETL_STD_ATOMIC_SUPPORTED ETL_CPP11_SUPPORTED
|
||||
|
||||
#endif
|
||||
|
||||
@ -46,6 +46,6 @@ SOFTWARE.
|
||||
#define ETL_NO_NULLPTR_SUPPORT !ETL_CPP11_SUPPORTED
|
||||
#define ETL_NO_LARGE_CHAR_SUPPORT !ETL_CPP11_SUPPORTED
|
||||
#define ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED ETL_CPP14_SUPPORTED
|
||||
#define ETL_ATOMIC_SUPPORTED ETL_CPP11_SUPPORTED
|
||||
#define ETL_STD_ATOMIC_SUPPORTED ETL_CPP11_SUPPORTED
|
||||
|
||||
#endif
|
||||
|
||||
@ -46,6 +46,6 @@ SOFTWARE.
|
||||
#define ETL_NO_NULLPTR_SUPPORT 1
|
||||
#define ETL_NO_LARGE_CHAR_SUPPORT 1
|
||||
#define ETL_CPP11_TYPE_TRAITS_IS_TRIVIAL_SUPPORTED 0
|
||||
#define ETL_ATOMIC_SUPPORTED 0
|
||||
#define ETL_STD_ATOMIC_SUPPORTED 0
|
||||
|
||||
#endif
|
||||
|
||||
@ -62,8 +62,8 @@
|
||||
<Unit filename="../../include/etl/array_wrapper.h" />
|
||||
<Unit filename="../../include/etl/atomic.h" />
|
||||
<Unit filename="../../include/etl/atomic/atomic_arm.h" />
|
||||
<Unit filename="../../include/etl/atomic/atomic_gcc.h" />
|
||||
<Unit filename="../../include/etl/atomic/atomic_windows.h" />
|
||||
<Unit filename="../../include/etl/atomic/atomic_gcc_sync.h" />
|
||||
<Unit filename="../../include/etl/atomic/atomic_std.h" />
|
||||
<Unit filename="../../include/etl/basic_string.h" />
|
||||
<Unit filename="../../include/etl/binary.h" />
|
||||
<Unit filename="../../include/etl/bitset.h" />
|
||||
@ -296,6 +296,11 @@
|
||||
<Unit filename="../test_array.cpp" />
|
||||
<Unit filename="../test_array_view.cpp" />
|
||||
<Unit filename="../test_array_wrapper.cpp" />
|
||||
<Unit filename="../test_atomic_gcc_sync.cpp" />
|
||||
<Unit filename="../test_atomic_std.cpp">
|
||||
<Option compile="0" />
|
||||
<Option link="0" />
|
||||
</Unit>
|
||||
<Unit filename="../test_binary.cpp" />
|
||||
<Unit filename="../test_bitset.cpp" />
|
||||
<Unit filename="../test_bloom_filter.cpp" />
|
||||
|
||||
@ -1,87 +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++.h"
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
#include "atomic/atomic_gcc.h"
|
||||
|
||||
#include <atomic>
|
||||
|
||||
namespace
|
||||
{
|
||||
SUITE(test_atomic_gcc)
|
||||
{
|
||||
//=========================================================================
|
||||
TEST(test_atomic_compare_exchange_weak_fail)
|
||||
{
|
||||
std::atomic<int> compare;
|
||||
etl::atomic<int> test;
|
||||
|
||||
int actual = 1U;
|
||||
|
||||
compare = actual;
|
||||
test = actual;
|
||||
|
||||
int compare_expected = 2U;
|
||||
int test_expected = 2U;
|
||||
int desired = 3U;
|
||||
|
||||
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 = 1U;
|
||||
|
||||
compare = actual;
|
||||
test = actual;
|
||||
|
||||
int compare_expected = actual;
|
||||
int test_expected = actual;
|
||||
int desired = 3U;
|
||||
|
||||
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());
|
||||
}
|
||||
};
|
||||
}
|
||||
484
test/test_atomic_gcc_sync.cpp
Normal file
484
test/test_atomic_gcc_sync.cpp
Normal file
@ -0,0 +1,484 @@
|
||||
/******************************************************************************
|
||||
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++.h"
|
||||
|
||||
#include "platform.h"
|
||||
#include "atomic/atomic_std.h"
|
||||
|
||||
#include <atomic>
|
||||
|
||||
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(0x0000FFFF);
|
||||
etl::atomic<int> test(0x0000FFFF);
|
||||
|
||||
compare &= 0x55AA55AA;
|
||||
test &= 0x55AA55AA;
|
||||
|
||||
CHECK_EQUAL((int)compare, (int)test);
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_atomic_operator_integer_or_equals)
|
||||
{
|
||||
std::atomic<int> compare(0x0000FFFF);
|
||||
etl::atomic<int> test(0x0000FFFF);
|
||||
|
||||
compare |= 0x55AA55AA;
|
||||
test |= 0x55AA55AA;
|
||||
|
||||
CHECK_EQUAL((int)compare, (int)test);
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_atomic_operator_integer_xor_equals)
|
||||
{
|
||||
std::atomic<int> compare(0x0000FFFF);
|
||||
etl::atomic<int> test(0x0000FFFF);
|
||||
|
||||
compare ^= 0x55AA55AA;
|
||||
test ^= 0x55AA55AA;
|
||||
|
||||
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(0xFFFFFFFF);
|
||||
etl::atomic<int> test(0xFFFFFFFF);
|
||||
|
||||
CHECK_EQUAL((int)compare.fetch_and(0x55AA55AA), (int)test.fetch_and(0x55AA55AA));
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_atomic_operator_fetch_or)
|
||||
{
|
||||
std::atomic<int> compare(0x0000FFFF);
|
||||
etl::atomic<int> test(0x0000FFFF);
|
||||
|
||||
CHECK_EQUAL((int)compare.fetch_or(0x55AA55AA), (int)test.fetch_or(0x55AA55AA));
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_atomic_operator_fetch_xor)
|
||||
{
|
||||
std::atomic<int> compare(0x0000FFFF);
|
||||
etl::atomic<int> test(0x0000FFFF);
|
||||
|
||||
CHECK_EQUAL((int)compare.fetch_xor(0x55AA55AA), (int)test.fetch_xor(0x55AA55AA));
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
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 = 1U;
|
||||
|
||||
compare = actual;
|
||||
test = actual;
|
||||
|
||||
int compare_expected = 2U;
|
||||
int test_expected = 2U;
|
||||
int desired = 3U;
|
||||
|
||||
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 = 1U;
|
||||
|
||||
compare = actual;
|
||||
test = actual;
|
||||
|
||||
int compare_expected = actual;
|
||||
int test_expected = actual;
|
||||
int desired = 3U;
|
||||
|
||||
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 = 1U;
|
||||
|
||||
compare = actual;
|
||||
test = actual;
|
||||
|
||||
int compare_expected = 2U;
|
||||
int test_expected = 2U;
|
||||
int desired = 3U;
|
||||
|
||||
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 = 1U;
|
||||
|
||||
compare = actual;
|
||||
test = actual;
|
||||
|
||||
int compare_expected = actual;
|
||||
int test_expected = actual;
|
||||
int desired = 3U;
|
||||
|
||||
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());
|
||||
}
|
||||
};
|
||||
}
|
||||
484
test/test_atomic_std.cpp
Normal file
484
test/test_atomic_std.cpp
Normal file
@ -0,0 +1,484 @@
|
||||
/******************************************************************************
|
||||
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++.h"
|
||||
|
||||
#include "platform.h"
|
||||
#include "atomic/atomic_std.h"
|
||||
|
||||
#include <atomic>
|
||||
|
||||
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(0x0000FFFF);
|
||||
etl::atomic<int> test(0x0000FFFF);
|
||||
|
||||
compare &= 0x55AA55AA;
|
||||
test &= 0x55AA55AA;
|
||||
|
||||
CHECK_EQUAL((int)compare, (int)test);
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_atomic_operator_integer_or_equals)
|
||||
{
|
||||
std::atomic<int> compare(0x0000FFFF);
|
||||
etl::atomic<int> test(0x0000FFFF);
|
||||
|
||||
compare |= 0x55AA55AA;
|
||||
test |= 0x55AA55AA;
|
||||
|
||||
CHECK_EQUAL((int)compare, (int)test);
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_atomic_operator_integer_xor_equals)
|
||||
{
|
||||
std::atomic<int> compare(0x0000FFFF);
|
||||
etl::atomic<int> test(0x0000FFFF);
|
||||
|
||||
compare ^= 0x55AA55AA;
|
||||
test ^= 0x55AA55AA;
|
||||
|
||||
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(0xFFFFFFFF);
|
||||
etl::atomic<int> test(0xFFFFFFFF);
|
||||
|
||||
CHECK_EQUAL((int)compare.fetch_and(0x55AA55AA), (int)test.fetch_and(0x55AA55AA));
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_atomic_operator_fetch_or)
|
||||
{
|
||||
std::atomic<int> compare(0x0000FFFF);
|
||||
etl::atomic<int> test(0x0000FFFF);
|
||||
|
||||
CHECK_EQUAL((int)compare.fetch_or(0x55AA55AA), (int)test.fetch_or(0x55AA55AA));
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
TEST(test_atomic_operator_fetch_xor)
|
||||
{
|
||||
std::atomic<int> compare(0x0000FFFF);
|
||||
etl::atomic<int> test(0x0000FFFF);
|
||||
|
||||
CHECK_EQUAL((int)compare.fetch_xor(0x55AA55AA), (int)test.fetch_xor(0x55AA55AA));
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
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 = 1U;
|
||||
|
||||
compare = actual;
|
||||
test = actual;
|
||||
|
||||
int compare_expected = 2U;
|
||||
int test_expected = 2U;
|
||||
int desired = 3U;
|
||||
|
||||
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 = 1U;
|
||||
|
||||
compare = actual;
|
||||
test = actual;
|
||||
|
||||
int compare_expected = actual;
|
||||
int test_expected = actual;
|
||||
int desired = 3U;
|
||||
|
||||
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 = 1U;
|
||||
|
||||
compare = actual;
|
||||
test = actual;
|
||||
|
||||
int compare_expected = 2U;
|
||||
int test_expected = 2U;
|
||||
int desired = 3U;
|
||||
|
||||
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 = 1U;
|
||||
|
||||
compare = actual;
|
||||
test = actual;
|
||||
|
||||
int compare_expected = actual;
|
||||
int test_expected = actual;
|
||||
int desired = 3U;
|
||||
|
||||
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());
|
||||
}
|
||||
};
|
||||
}
|
||||
109
test/test_spsc_queue_std_atomic.cpp
Normal file
109
test/test_spsc_queue_std_atomic.cpp
Normal file
@ -0,0 +1,109 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2018 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++.h"
|
||||
|
||||
#include "spsc_queue.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Item
|
||||
{
|
||||
Item(char c_, int i_, double d_)
|
||||
: c(c_),
|
||||
i(i_),
|
||||
d(d_)
|
||||
{
|
||||
}
|
||||
|
||||
char c;
|
||||
int i;
|
||||
double d;
|
||||
};
|
||||
|
||||
bool operator == (const Item& lhs, const Item& rhs)
|
||||
{
|
||||
return (lhs.c == rhs.c) && (lhs.i == rhs.i) && (lhs.d == rhs.d);
|
||||
}
|
||||
|
||||
typedef etl::spsc_queue<Item, 4, etl::spsc_queue_implementation::STD_ATOMIC> Queue;
|
||||
typedef etl::ispsc_queue<Item, etl::spsc_queue_implementation::STD_ATOMIC> IQueue;
|
||||
|
||||
SUITE(test_queue)
|
||||
{
|
||||
Item item1(1, 2, 3.4);
|
||||
Item item2(2, 3, 4.5);
|
||||
Item item3(3, 4, 5.6);
|
||||
Item item4(4, 5, 6.7);
|
||||
|
||||
TEST(test_normal_operation)
|
||||
{
|
||||
Queue queue;
|
||||
|
||||
IQueue& iqueue = queue;
|
||||
|
||||
CHECK(queue.empty());
|
||||
CHECK(!queue.full());
|
||||
|
||||
CHECK_EQUAL(4U, queue.capacity());
|
||||
CHECK_EQUAL(4U, queue.max_size());
|
||||
|
||||
// Fill the queue.
|
||||
CHECK(queue.push(item1));
|
||||
CHECK_EQUAL(1U, queue.size());
|
||||
CHECK_EQUAL(3U, queue.available());
|
||||
CHECK(!queue.empty());
|
||||
CHECK(!queue.full());
|
||||
|
||||
CHECK(iqueue.push(item2));
|
||||
CHECK_EQUAL(2U, iqueue.size());
|
||||
CHECK_EQUAL(2U, iqueue.available());
|
||||
CHECK(!iqueue.empty());
|
||||
CHECK(!iqueue.full());
|
||||
|
||||
CHECK(queue.push(item3));
|
||||
CHECK_EQUAL(3U, queue.size());
|
||||
CHECK_EQUAL(1U, queue.available());
|
||||
CHECK(!queue.empty());
|
||||
CHECK(!queue.full());
|
||||
|
||||
CHECK(iqueue.push(item4));
|
||||
CHECK_EQUAL(4U, iqueue.size());
|
||||
CHECK_EQUAL(0U, iqueue.available());
|
||||
CHECK(!iqueue.empty());
|
||||
CHECK(iqueue.full());
|
||||
|
||||
// Too many
|
||||
CHECK(!queue.push(item4));
|
||||
CHECK_EQUAL(4U, queue.size());
|
||||
CHECK_EQUAL(0U, queue.available());
|
||||
CHECK(!queue.empty());
|
||||
CHECK(queue.full());
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -135,7 +135,8 @@
|
||||
<ClInclude Include="..\..\include\etl\array_wrapper.h" />
|
||||
<ClInclude Include="..\..\include\etl\atomic.h" />
|
||||
<ClInclude Include="..\..\include\etl\atomic\atomic_arm.h" />
|
||||
<ClInclude Include="..\..\include\etl\atomic\atomic_gcc.h" />
|
||||
<ClInclude Include="..\..\include\etl\atomic\atomic_gcc_sync.h" />
|
||||
<ClInclude Include="..\..\include\etl\atomic\atomic_std.h" />
|
||||
<ClInclude Include="..\..\include\etl\callback_timer.h" />
|
||||
<ClInclude Include="..\..\include\etl\compare.h" />
|
||||
<ClInclude Include="..\..\include\etl\constant.h" />
|
||||
@ -150,6 +151,9 @@
|
||||
<ClInclude Include="..\..\include\etl\message_router.h" />
|
||||
<ClInclude Include="..\..\include\etl\message_router_generator.h" />
|
||||
<ClInclude Include="..\..\include\etl\packet.h" />
|
||||
<ClInclude Include="..\..\include\etl\private\spsc_queue_atomic.h" />
|
||||
<ClInclude Include="..\..\include\etl\private\spsc_queue_freertos.h" />
|
||||
<ClInclude Include="..\..\include\etl\private\spsc_queue_isr.h" />
|
||||
<ClInclude Include="..\..\include\etl\profiles\arduino_arm.h" />
|
||||
<ClInclude Include="..\..\include\etl\profiles\armv5.h" />
|
||||
<ClInclude Include="..\..\include\etl\profiles\armv6.h" />
|
||||
@ -163,6 +167,7 @@
|
||||
<ClInclude Include="..\..\include\etl\profiles\ticc.h" />
|
||||
<ClInclude Include="..\..\include\etl\scheduler.h" />
|
||||
<ClInclude Include="..\..\include\etl\smallest_generator.h" />
|
||||
<ClInclude Include="..\..\include\etl\spsc_queue.h" />
|
||||
<ClInclude Include="..\..\include\etl\sqrt.h" />
|
||||
<ClInclude Include="..\..\include\etl\string_view.h" />
|
||||
<ClInclude Include="..\..\include\etl\task.h" />
|
||||
@ -345,6 +350,7 @@
|
||||
</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_bitset.cpp" />
|
||||
<ClCompile Include="..\test_bloom_filter.cpp" />
|
||||
|
||||
@ -561,9 +561,6 @@
|
||||
<ClInclude Include="..\..\include\etl\atomic\atomic_arm.h">
|
||||
<Filter>ETL\Utilities\Atomic</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\atomic\atomic_gcc.h">
|
||||
<Filter>ETL\Utilities\Atomic</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\ecl_user.h">
|
||||
<Filter>Source Files\ECL</Filter>
|
||||
</ClInclude>
|
||||
@ -588,6 +585,24 @@
|
||||
<ClInclude Include="..\..\include\etl\c\ecl_timer.h">
|
||||
<Filter>ECL</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\atomic\atomic_std.h">
|
||||
<Filter>ETL\Utilities\Atomic</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\atomic\atomic_gcc_sync.h">
|
||||
<Filter>ETL\Utilities\Atomic</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\spsc_queue.h">
|
||||
<Filter>ETL\Containers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\private\spsc_queue_atomic.h">
|
||||
<Filter>ETL\Utilities</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\private\spsc_queue_freertos.h">
|
||||
<Filter>ETL\Utilities</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\private\spsc_queue_isr.h">
|
||||
<Filter>ETL\Utilities</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\main.cpp">
|
||||
@ -974,6 +989,9 @@
|
||||
<ClCompile Include="..\test_c_timer_framework.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_atomic_std.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\Doxyfile">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user