api-impl.h (1769B)
1 // This file is part of Blend2D project <https://blend2d.com> 2 // 3 // See blend2d.h or LICENSE.md for license and copyright information 4 // SPDX-License-Identifier: Zlib 5 6 #ifndef BLEND2D_API_IMPL_H_INCLUDED 7 #define BLEND2D_API_IMPL_H_INCLUDED 8 9 #include "api.h" 10 #include "object.h" 11 12 #include <atomic> 13 14 //! \addtogroup bl_impl 15 //! \{ 16 17 //! \name Atomic Operations 18 //! 19 //! Atomic operations are used extensively across Blend2D for reference counting and caching. You should always 20 //! use operations defined here as they implement all possible cases that Blend2D deals with in a correct way 21 //! (and if there is a bug it makes it fixable in a single place). 22 //! 23 //! \{ 24 25 //! Atomically increments `n` to value `x` (relaxed semantics). The old value is returned. 26 template<typename T> 27 static BL_INLINE T bl_atomic_fetch_add_relaxed(T* x, T n = T(1)) noexcept { 28 return ((std::atomic<T>*)x)->fetch_add(n, std::memory_order_relaxed); 29 } 30 31 //! Atomically increments `n` to value `x` (strong semantics). The old value is returned. 32 template<typename T> 33 static BL_INLINE T bl_atomic_fetch_add_strong(T* x, T n = T(1)) noexcept { 34 return ((std::atomic<T>*)x)->fetch_add(n, std::memory_order_acq_rel); 35 } 36 37 //! Atomically decrements `n` from value `x` (relaxed semantics). The old value is returned. 38 template<typename T> 39 static BL_INLINE T bl_atomic_fetch_sub_relaxed(T* x, T n = T(1)) noexcept { 40 return ((std::atomic<T>*)x)->fetch_sub(n, std::memory_order_relaxed); 41 } 42 43 //! Atomically decrements `n` from value `x` (strong semantics). The old value is returned. 44 template<typename T> 45 static BL_INLINE T bl_atomic_fetch_sub_strong(T* x, T n = T(1)) noexcept { 46 return ((std::atomic<T>*)x)->fetch_sub(n, std::memory_order_acq_rel); 47 } 48 49 //! \} 50 51 //! \} 52 53 #endif // BLEND2D_API_IMPL_H_INCLUDED