JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_Atomic.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26#ifndef DOXYGEN
27 namespace AtomicHelpers
28 {
29 template <typename T> struct DiffTypeHelper { using Type = T; };
30 template <typename T> struct DiffTypeHelper<T*> { using Type = std::ptrdiff_t; };
31 }
32#endif
33
34//==============================================================================
40template <typename Type>
42{
43 using DiffType = typename AtomicHelpers::DiffTypeHelper<Type>::Type;
44
46 Atomic() noexcept : value (Type()) {}
47
49 Atomic (Type initialValue) noexcept : value (initialValue) {}
50
52 Atomic (const Atomic& other) noexcept : value (other.get()) {}
53
55 ~Atomic() noexcept
56 {
57 #if __cpp_lib_atomic_is_always_lock_free
59 "This class can only be used for lock-free types");
60 #endif
61 }
62
64 Type get() const noexcept { return value.load(); }
65
67 void set (Type newValue) noexcept { value = newValue; }
68
70 Type exchange (Type newValue) noexcept { return value.exchange (newValue); }
71
96 bool compareAndSetBool (Type newValue, Type valueToCompare) noexcept
97 {
99 }
100
103 {
104 value = other.value.load();
105 return *this;
106 }
107
109 Atomic<Type>& operator= (Type newValue) noexcept
110 {
111 value = newValue;
112 return *this;
113 }
114
116 Type operator+= (DiffType amountToAdd) noexcept { return value += amountToAdd; }
117
119 Type operator-= (DiffType amountToSubtract) noexcept { return value -= amountToSubtract; }
120
122 Type operator++() noexcept { return ++value; }
123
125 Type operator--() noexcept { return --value; }
126
132 void memoryBarrier() noexcept { atomic_thread_fence (std::memory_order_seq_cst); }
133
136
137 //==============================================================================
138 #ifndef DOXYGEN
139 [[deprecated ("This method has been deprecated as there is no equivalent method in "
140 "std::atomic. Use compareAndSetBool instead.")]]
141 Type compareAndSetValue (Type, Type) noexcept;
142 #endif
143};
144
145} // namespace juce
T compare_exchange_strong(T... args)
T exchange(T... args)
T load(T... args)
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
A simple wrapper around std::atomic.
Definition juce_Atomic.h:42
Type get() const noexcept
Atomically reads and returns the current value.
Definition juce_Atomic.h:64
Type exchange(Type newValue) noexcept
Atomically sets the current value, returning the value that was replaced.
Definition juce_Atomic.h:70
Atomic(Type initialValue) noexcept
Creates a new value, with a given initial value.
Definition juce_Atomic.h:49
void set(Type newValue) noexcept
Atomically sets the current value.
Definition juce_Atomic.h:67
Atomic() noexcept
Creates a new value, initialised to zero.
Definition juce_Atomic.h:46
std::atomic< Type > value
The std::atomic object that this class operates on.
bool compareAndSetBool(Type newValue, Type valueToCompare) noexcept
Atomically compares this value with a target value, and if it is equal, sets this to be equal to a ne...
Definition juce_Atomic.h:96
Atomic< Type > & operator=(const Atomic &other) noexcept
Copies another value into this one (atomically).
Type operator--() noexcept
Atomically decrements this value, returning the new value.
Type operator-=(DiffType amountToSubtract) noexcept
Atomically subtracts a number from this value, returning the new value.
Type operator+=(DiffType amountToAdd) noexcept
Atomically adds a number to this value, returning the new value.
~Atomic() noexcept
Destructor.
Definition juce_Atomic.h:55
void memoryBarrier() noexcept
Implements a memory read/write barrier.
Type operator++() noexcept
Atomically increments this value, returning the new value.
Atomic(const Atomic &other) noexcept
Copies another value (atomically).
Definition juce_Atomic.h:52