/* * Copyright (c) 2014-2015, Siemens AG. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef EMBB_BASE_INTERNAL_ATOMIC_ATOMIC_BASE_H_ #define EMBB_BASE_INTERNAL_ATOMIC_ATOMIC_BASE_H_ #include #include #include #include #include #include #include #include namespace embb { namespace base { namespace internal { namespace atomic { template class AtomicBase { // The copy constructor is undefined, since it cannot be implemented // atomically. AtomicBase(const AtomicBase&); // The assignment operator is undefined, since it cannot be implemented // atomically. AtomicBase& operator=(const AtomicBase&); protected: typedef typename embb::base::internal::atomic:: AtomicTraits:: NativeType NativeType; mutable NativeType AtomicValue; public: /** * Default constructor. * Initializes the object with zero. */ AtomicBase(); /** * Constructor. * Initializes the object with the passed value. * * \param val Initial value */ explicit AtomicBase(BaseType val); // The members below are documented in atomic.h BaseType operator=(BaseType val); operator BaseType() const; bool IsLockFree() const; bool IsArithmetic() const; bool IsInteger() const; bool IsPointer() const; void Store(BaseType val); BaseType Load() const; BaseType Swap(BaseType val); bool CompareAndSwap(BaseType& expected, BaseType desired); }; template inline AtomicBase::AtomicBase() : AtomicValue(0) { } template inline AtomicBase::AtomicBase(BaseType val) /*: AtomicValue(val)*/ { memcpy(&AtomicValue, &val, sizeof(AtomicValue)); } template inline BaseType AtomicBase::operator=(BaseType val) { Store(val); return val; } template inline AtomicBase::operator BaseType() const { return Load(); } template inline bool AtomicBase::IsArithmetic() const { return false; } template inline bool AtomicBase::IsInteger() const { return false; } template inline bool AtomicBase::IsPointer() const { return false; } template inline void AtomicBase::Store(BaseType val) { NativeType storage_value; // Justification for using memcpy instead of pointer casts // or union type punning: // - with strict aliasing, type punning using pointer casts or // unions is unsafe // - memcpy is not slower, as the compiler will optimize it away // anyway... memcpy(&storage_value, &val, sizeof(storage_value)); store_implementation< NativeType > ::Store(&AtomicValue, storage_value); } template inline BaseType AtomicBase::Load() const { BaseType return_value; NativeType storage_value = load_implementation< NativeType >::Load(&AtomicValue); memcpy(&return_value, &storage_value, sizeof(return_value)); return return_value; } template inline BaseType AtomicBase::Swap(BaseType val) { NativeType storage_value; BaseType return_value; memcpy(&storage_value, &val, sizeof(storage_value)); NativeType storage_value2 = swap_implementation< NativeType > ::Swap(&AtomicValue, storage_value); memcpy(&return_value, &storage_value2, sizeof(return_value)); return return_value; } template inline bool AtomicBase:: CompareAndSwap(BaseType& expected, BaseType desired) { NativeType native_expected; NativeType native_desired; memcpy(&native_expected, &expected, sizeof(expected)); memcpy(&native_desired, &desired, sizeof(desired)); bool return_val = (compare_and_swap_implementation:: compare_and_swap(&AtomicValue, &native_expected, native_desired)) !=0 ? true : false; memcpy(&expected, &native_expected, sizeof(expected)); return return_val; } } // namespace atomic } // namespace internal } // namespace base } // namespace embb #endif // EMBB_BASE_INTERNAL_ATOMIC_ATOMIC_BASE_H_