1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::Atomic;
use crate::FiniteRangeNumber;
use crate::Number;
use core::sync::atomic::Ordering;
/// An atomic number type.
pub trait AtomicNumber: Atomic
where
Self::NonAtomicType: Number,
{
/// Adds to the current value, returning the previous value.
///
/// This operation wraps around on overflow.
///
/// [`fetch_add`][`AtomicNumber::fetch_add`] an [`Ordering`](`core::sync::atomic::Ordering`) argument
/// which describes the memory ordering of this operation. All ordering
/// modes are possible.
/// Note that using [`Acquire`](`core::sync::atomic::Ordering::Acquire`)
/// makes the store part of this operation
/// [`Relaxed`](`core::sync::atomic::Ordering::Relaxed`), and using
/// [`Release`](`core::sync::atomic::Ordering::Release`) makes the load part
/// [`Relaxed`](`core::sync::atomic::Ordering::Relaxed`).
///
/// Note: This method is only available on platforms that support atomic
/// operations on the given type.
fn fetch_add(&self, value: Self::NonAtomicType, order: Ordering) -> Self::NonAtomicType;
/// Subtracts from the current value, returning the previous value.
///
/// This operation wraps around on overflow.
///
/// Returns the previous value.
///
/// [`fetch_sub`][`AtomicNumber::fetch_sub`] an [`Ordering`](`core::sync::atomic::Ordering`) argument
/// which describes the memory ordering of this operation. All ordering
/// modes are possible.
/// Note that using [`Acquire`](`core::sync::atomic::Ordering::Acquire`)
/// makes the store part of this operation
/// [`Relaxed`](`core::sync::atomic::Ordering::Relaxed`), and using
/// [`Release`](`core::sync::atomic::Ordering::Release`) makes the load part
/// [`Relaxed`](`core::sync::atomic::Ordering::Relaxed`).
///
/// Note: This method is only available on platforms that support atomic
/// operations on the given type.
fn fetch_sub(&self, value: Self::NonAtomicType, order: Ordering) -> Self::NonAtomicType;
/// Maximum with the current value.
///
/// Finds the maximum of the current value and the argument val, and sets
/// the new value to the result.
///
/// Returns the previous value.
///
/// [`AtomicNumber::fetch_max`] an [`Ordering`](`core::sync::atomic::Ordering`) argument
/// which describes the memory ordering of this operation. All ordering
/// modes are possible.
/// Note that using [`Acquire`](`core::sync::atomic::Ordering::Acquire`)
/// makes the store part of this operation
/// [`Relaxed`](`core::sync::atomic::Ordering::Relaxed`), and using
/// [`Release`](`core::sync::atomic::Ordering::Release`) makes the load part
/// [`Relaxed`](`core::sync::atomic::Ordering::Relaxed`).
///
/// Note: This method is only available on platforms that support atomic
/// operations on the given type.
fn fetch_max(&self, value: Self::NonAtomicType, order: Ordering) -> Self::NonAtomicType;
/// Minimum with the current value.
///
/// Finds the minimum of the current value and the argument val, and sets
/// the new value to the result.
///
/// Returns the previous value.
///
/// [`AtomicNumber::fetch_min`] an [`Ordering`](`core::sync::atomic::Ordering`) argument
/// which describes the memory ordering of this operation. All ordering
/// modes are possible.
/// Note that using [`Acquire`](`core::sync::atomic::Ordering::Acquire`)
/// makes the store part of this operation
/// [`Relaxed`](`core::sync::atomic::Ordering::Relaxed`), and using
/// [`Release`](`core::sync::atomic::Ordering::Release`) makes the load part
/// [`Relaxed`](`core::sync::atomic::Ordering::Relaxed`).
///
/// Note: This method is only available on platforms that support atomic
/// operations on the given type.
fn fetch_min(&self, value: Self::NonAtomicType, order: Ordering) -> Self::NonAtomicType;
}
/// An atomic finite number type.
pub trait AtomicFiniteRangeNumber: AtomicNumber
where
Self::NonAtomicType: FiniteRangeNumber,
{
#[inline(always)]
/// Adds to the current value, returning the previous value.
///
/// This operation saturates at the bounds and does not overflow. For floats
/// it saturates at the biggest non-infinity value and NAN are just
/// forwarded.
///
/// This is a convenience method for
/// [`fetch_update`](`Atomic::fetch_update`).
fn fetch_saturating_add(
&self,
value: Self::NonAtomicType,
set_order: Ordering,
fetch_order: Ordering,
) -> Self::NonAtomicType {
let mut base = self.load(fetch_order);
loop {
let new = base.saturating_add(value);
let res = self.compare_exchange_weak(base, new, set_order, fetch_order);
match res {
Ok(val) => return val,
Err(val) => {
base = val;
}
}
}
}
#[inline(always)]
/// Subtract from the current value, returning the previous value.
///
/// This operation saturates at the bounds and does not
/// overflow. For floats it saturates at the biggest non infinity value and
/// NAN are just forwarded.
///
/// This is a convenience method for [`fetch_update`](`Atomic::fetch_update`).
fn fetch_saturating_sub(
&self,
value: Self::NonAtomicType,
set_order: Ordering,
fetch_order: Ordering,
) -> Self::NonAtomicType {
let mut base = self.load(fetch_order);
loop {
let new = base.saturating_sub(value);
let res = self.compare_exchange_weak(base, new, set_order, fetch_order);
match res {
Ok(val) => return val,
Err(val) => {
base = val;
}
}
}
}
#[inline(always)]
/// This is a convenience method for [`fetch_update`](`Atomic::fetch_update`).
fn fetch_saturating_mul(
&self,
value: Self::NonAtomicType,
set_order: Ordering,
fetch_order: Ordering,
) -> Self::NonAtomicType {
let mut base = self.load(fetch_order);
loop {
let new = base.saturating_mul(value);
let res = self.compare_exchange_weak(base, new, set_order, fetch_order);
match res {
Ok(val) => return val,
Err(val) => {
base = val;
}
}
}
}
#[inline(always)]
/// This is a convenience method for [`fetch_update`](`Atomic::fetch_update`).
fn fetch_saturating_div(
&self,
value: Self::NonAtomicType,
set_order: Ordering,
fetch_order: Ordering,
) -> Self::NonAtomicType {
let mut base = self.load(fetch_order);
loop {
let new = base.saturating_div(value);
let res = self.compare_exchange_weak(base, new, set_order, fetch_order);
match res {
Ok(val) => return val,
Err(val) => {
base = val;
}
}
}
}
#[cfg(feature = "std")]
#[inline(always)]
/// This is a convenience method for [`fetch_update`](`Atomic::fetch_update`).
fn fetch_saturating_pow(
&self,
value: Self::NonAtomicType,
set_order: Ordering,
fetch_order: Ordering,
) -> Self::NonAtomicType {
let mut base = self.load(fetch_order);
loop {
let new = base.saturating_pow(value);
let res = self.compare_exchange_weak(base, new, set_order, fetch_order);
match res {
Ok(val) => return val,
Err(val) => {
base = val;
}
}
}
}
}