Loading...
Searching...
No Matches
juce_Atomic.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
#ifndef DOXYGEN
namespace AtomicHelpers
{
template <typename T> struct DiffTypeHelper { using Type = T; };
template <typename T> struct DiffTypeHelper<T*> { using Type = std::ptrdiff_t; };
}
#endif
//==============================================================================
template <typename Type>
struct Atomic final
{
using DiffType = typename AtomicHelpers::DiffTypeHelper<Type>::Type;
Atomic() noexcept : value (Type()) {}
Atomic (Type initialValue) noexcept : value (initialValue) {}
Atomic (const Atomic& other) noexcept : value (other.get()) {}
~Atomic() noexcept
{
#if __cpp_lib_atomic_is_always_lock_free
static_assert (std::atomic<Type>::is_always_lock_free,
"This class can only be used for lock-free types");
#endif
}
Type get() const noexcept { return value.load(); }
void set (Type newValue) noexcept { value = newValue; }
Type exchange (Type newValue) noexcept { return value.exchange (newValue); }
bool compareAndSetBool (Type newValue, Type valueToCompare) noexcept
{
return value.compare_exchange_strong (valueToCompare, newValue);
}
Atomic<Type>& operator= (const Atomic& other) noexcept
{
value = other.value.load();
return *this;
}
Atomic<Type>& operator= (Type newValue) noexcept
{
value = newValue;
return *this;
}
Type operator+= (DiffType amountToAdd) noexcept { return value += amountToAdd; }
Type operator-= (DiffType amountToSubtract) noexcept { return value -= amountToSubtract; }
Type operator++() noexcept { return ++value; }
Type operator--() noexcept { return --value; }
void memoryBarrier() noexcept { atomic_thread_fence (std::memory_order_seq_cst); }
std::atomic<Type> value;
//==============================================================================
#ifndef DOXYGEN
[[deprecated ("This method has been deprecated as there is no equivalent method in "
"std::atomic. Use compareAndSetBool instead.")]]
Type compareAndSetValue (Type, Type) noexcept;
#endif
};
} // namespace juce
juce::Atomic::get
Type get() const noexcept
Definition juce_Atomic.h:64
juce::Atomic::exchange
Type exchange(Type newValue) noexcept
Definition juce_Atomic.h:70
juce::Atomic::Atomic
Atomic(Type initialValue) noexcept
Definition juce_Atomic.h:49
juce::Atomic::set
void set(Type newValue) noexcept
Definition juce_Atomic.h:67
juce::Atomic::Atomic
Atomic() noexcept
Definition juce_Atomic.h:46
juce::Atomic::value
std::atomic< Type > value
Definition juce_Atomic.h:135
juce::Atomic::compareAndSetBool
bool compareAndSetBool(Type newValue, Type valueToCompare) noexcept
Definition juce_Atomic.h:96
juce::Atomic::operator=
Atomic< Type > & operator=(const Atomic &other) noexcept
Definition juce_Atomic.h:102
juce::Atomic::operator--
Type operator--() noexcept
Definition juce_Atomic.h:125
juce::Atomic::operator-=
Type operator-=(DiffType amountToSubtract) noexcept
Definition juce_Atomic.h:119
juce::Atomic::operator+=
Type operator+=(DiffType amountToAdd) noexcept
Definition juce_Atomic.h:116
juce::Atomic::~Atomic
~Atomic() noexcept
Definition juce_Atomic.h:55
juce::Atomic::memoryBarrier
void memoryBarrier() noexcept
Definition juce_Atomic.h:132
juce::Atomic::operator++
Type operator++() noexcept
Definition juce_Atomic.h:122
juce::Atomic::Atomic
Atomic(const Atomic &other) noexcept
Definition juce_Atomic.h:52