OpenShot Audio Library | OpenShotAudio  0.3.2
juce_Atomic.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
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 
23 namespace 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 //==============================================================================
40 template <typename Type>
41 struct Atomic final
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
58  static_assert (std::atomic<Type>::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  {
98  return value.compare_exchange_strong (valueToCompare, newValue);
99  }
100 
102  Atomic<Type>& operator= (const Atomic& other) noexcept
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 
135  std::atomic<Type> value;
136 
137  //==============================================================================
138  #ifndef DOXYGEN
139  /* This method has been deprecated as there is no equivalent method in
140  std::atomic. Use compareAndSetBool instead.
141  */
142  JUCE_DEPRECATED (Type compareAndSetValue (Type, Type) noexcept);
143  #endif
144 };
145 
146 } // namespace juce
Type get() const noexcept
Definition: juce_Atomic.h:64
Type exchange(Type newValue) noexcept
Definition: juce_Atomic.h:70
Atomic(Type initialValue) noexcept
Definition: juce_Atomic.h:49
void set(Type newValue) noexcept
Definition: juce_Atomic.h:67
Atomic() noexcept
Definition: juce_Atomic.h:46
Atomic< Type > & operator=(const Atomic &other) noexcept
Definition: juce_Atomic.h:102
std::atomic< Type > value
Definition: juce_Atomic.h:135
bool compareAndSetBool(Type newValue, Type valueToCompare) noexcept
Definition: juce_Atomic.h:96
Type operator--() noexcept
Definition: juce_Atomic.h:125
Type operator-=(DiffType amountToSubtract) noexcept
Definition: juce_Atomic.h:119
Type operator+=(DiffType amountToAdd) noexcept
Definition: juce_Atomic.h:116
~Atomic() noexcept
Definition: juce_Atomic.h:55
void memoryBarrier() noexcept
Definition: juce_Atomic.h:132
Type operator++() noexcept
Definition: juce_Atomic.h:122
Atomic(const Atomic &other) noexcept
Definition: juce_Atomic.h:52