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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use core::marker::PhantomData;
use crate::Data64Bit;
portable_atomic::cfg_has_atomic_64! {
use alloc::sync::Arc;
use core::sync::atomic::Ordering;
use core::cell::Cell;
use portable_atomic::AtomicU64;
/// Create a new realtime-safe lock-free triple buffer type that stores 64 bits
/// of data.
///
/// On platforms which natively support 64 bit atomics, this is implemented with
/// a single cheap [`AtomicU64`](core::sync::atomic::AtomicU64) load/store operation
/// (this can optionally use [portable_atomic](https://crates.io/crates/portable-atomic)
/// for maximum compatibility).
///
/// On platforms which do NOT natively support 64 bit atomics, this is implemented
/// using a more expensive triple buffer implementation (using
/// [triple_buffer](https://crates.io/crates/triple_buffer)).
///
/// ## Data Types
///
/// This triple buffer accepts any data type that implements the [`Data64Bit`]
/// trait. Implementations for the following primitives are provided:
/// * [`u64`]
/// * [`i64`]
/// * [`f64`]
/// * [[`u32`]; 2]
/// * [[`i32`]; 2]
/// * [[`f32`]; 2]
/// * [[`u16`]; 4]
/// * [[`i16`]; 4]
/// * [[`u8`]; 8]
/// * [[`i8`]; 8]
/// * [[`bool`]; 8]
///
/// ## Example
/// ```
/// # use triple_buf_64::triple_buffer_64;
/// // Construct an output/input pair with the given 64 bit type.
/// let (mut input, mut output) = triple_buffer_64::<i64>(-3);
///
/// // The output will read the initial value.
/// assert_eq!(output.read(), -3);
/// assert_eq!(output.read(), -3);
///
/// // Write new data into the buffer.
/// input.write(-9845);
///
/// assert_eq!(output.read(), -9845);
/// assert_eq!(output.read(), -9845);
///
/// input.write(69);
/// input.write(68);
/// input.write(67);
///
/// // The output always reads the latest value that was pushed to the buffer.
/// assert_eq!(output.read(), 67);
/// assert_eq!(output.read(), 67);
/// ```
pub fn triple_buffer_64<T: Data64Bit>(val: T) -> (Input64<T>, Output64<T>) {
let val = Arc::new(AtomicU64::new(u64::from_ne_bytes(T::to_ne_bytes(val))));
(
Input64 {
val: Arc::clone(&val),
_data_type: PhantomData,
_non_sync: PhantomData,
},
Output64 {
val,
_data_type: PhantomData,
_non_sync: PhantomData,
},
)
}
/// The producer end of a simple realtime-safe lock-free triple buffer type that
/// stores 64 bits of data.
///
/// On platforms which natively support 64 bit atomics, this is implemented with
/// a single cheap [`AtomicU64`] load/store operation.
///
/// On platforms which do NOT natively support 64 bit atomics, this is implemented
/// using a more expensive triple buffer implementation (using
/// <https://crates.io/crates/triple_buffer>).
///
/// This can be useful when you want to pass a small amount of data between
/// realtime threads (i.e. audio threads) very cheaply on most platforms, but
/// also have a realtime-safe fallback for platforms which do not have 64 bit
/// atomics (i.e. PowerPC).
pub struct Input64<T: Data64Bit> {
val: Arc<AtomicU64>,
_data_type: PhantomData<T>,
// Make this struct !Sync
_non_sync: PhantomData<Cell<()>>,
}
impl<T: Data64Bit> Input64<T> {
/// Write a new value into the buffer.
pub fn write(&mut self, val: T) {
self.val.store(u64::from_ne_bytes(T::to_ne_bytes(val)), Ordering::Relaxed);
}
}
/// The consumer end of a simple realtime-safe lock-free triple buffer type that
/// stores 64 bits of data.
///
/// On platforms which natively support 64 bit atomics, this is implemented with
/// a single cheap [`AtomicU64`] load/store operation.
///
/// On platforms which do NOT natively support 64 bit atomics, this is implemented
/// using a more expensive triple buffer implementation (using
/// <https://crates.io/crates/triple_buffer>).
///
/// This can be useful when you want to pass a small amount of data between
/// realtime threads (i.e. audio threads) very cheaply on most platforms, but
/// also have a realtime-safe fallback for platforms which do not have 64 bit
/// atomics (i.e. PowerPC).
pub struct Output64<T: Data64Bit> {
val: Arc<AtomicU64>,
_data_type: PhantomData<T>,
// Make this struct !Sync
_non_sync: PhantomData<Cell<()>>,
}
impl<T: Data64Bit> Output64<T> {
/// Read the latest value that was written to the buffer.
pub fn read(&mut self) -> T {
T::from_ne_bytes(u64::to_ne_bytes(self.val.load(Ordering::Relaxed)))
}
}
}
portable_atomic::cfg_no_atomic_64! {
use triple_buffer::{Input, Output, TripleBuffer};
/// Create a new realtime-safe lock-free triple buffer type that stores 64 bits
/// of data.
///
/// On platforms which natively support 64 bit atomics, this is implemented with
/// a single cheap [`AtomicU64`](core::sync::atomic::AtomicU64) load/store operation
/// (this can optionally use [portable_atomic](https://crates.io/crates/portable-atomic)
/// for maximum compatibility).
///
/// On platforms which do NOT natively support 64 bit atomics, this is implemented
/// using a more expensive triple buffer implementation (using
/// [triple_buffer](https://crates.io/crates/triple_buffer)).
///
/// This can be useful when you want to pass a small amount of data between
/// realtime threads (i.e. audio threads) very cheaply on most platforms, but
/// also have a realtime-safe fallback for platforms which do not have 64 bit
/// atomics (i.e. PowerPC).
///
/// ## Data Types
///
/// This triple buffer accepts any data type that implements the [`Data64Bit`]
/// trait. Implementations for the following primitives are provided:
/// * [`u64`]
/// * [`i64`]
/// * [`f64`]
/// * [[`u32`]; 2]
/// * [[`i32`]; 2]
/// * [[`f32`]; 2]
/// * [[`u16`]; 4]
/// * [[`i16`]; 4]
/// * [[`u8`]; 8]
/// * [[`i8`]; 8]
/// * [[`bool`]; 8]
///
/// ## Example
/// ```
/// # use triple_buf_64::triple_buffer_64;
/// // Construct an output/input pair with the given 64 bit type.
/// let (mut input, mut output) = triple_buffer_64::<i64>(-3);
///
/// // The output will read the initial value.
/// assert_eq!(output.read(), -3);
/// assert_eq!(output.read(), -3);
///
/// // Write new data into the buffer.
/// input.write(-9845);
///
/// assert_eq!(output.read(), -9845);
/// assert_eq!(output.read(), -9845);
///
/// input.write(69);
/// input.write(68);
/// input.write(67);
///
/// // The output always reads the latest value that was pushed to the buffer.
/// assert_eq!(output.read(), 67);
/// assert_eq!(output.read(), 67);
/// ```
pub fn triple_buffer_64<T: Data64Bit>(val: T) -> (Input64<T>, Output64<T>) {
let data = T::to_ne_bytes(val);
let (input, output) = TripleBuffer::<[u8; 8]>::new(&data).split();
(
Input64 {
input,
_data_type: PhantomData,
},
Output64 {
output,
_data_type: PhantomData,
}
)
}
/// The producer end of a simple realtime-safe lock-free triple buffer type that
/// stores 64 bits of data.
///
/// On platforms which natively support 64 bit atomics, this is implemented with
/// a single cheap [`AtomicU64`](portable_atomic::AtomicU64) load/store operation.
///
/// On platforms which do NOT natively support 64 bit atomics, this is implemented
/// using a more expensive triple buffer implementation (using
/// <https://crates.io/crates/triple_buffer>).
///
/// This can be useful when you want to pass a small amount of data between
/// realtime threads (i.e. audio threads) very cheaply on most platforms, but
/// also have a realtime-safe fallback for platforms which do not have 64 bit
/// atomics (i.e. PowerPC).
pub struct Input64<T: Data64Bit> {
input: Input<[u8; 8]>,
_data_type: PhantomData<T>,
}
impl<T: Data64Bit> Input64<T> {
/// Write a new value into the buffer.
pub fn write(&mut self, val: T) {
self.input.write(T::to_ne_bytes(val));
}
}
/// The consumer end of a simple realtime-safe lock-free triple buffer type that
/// stores 64 bits of data.
///
/// On platforms which natively support 64 bit atomics, this is implemented with
/// a single cheap [`AtomicU64`](portable_atomic::AtomicU64) load/store operation.
///
/// On platforms which do NOT natively support 64 bit atomics, this is implemented
/// using a more expensive triple buffer implementation (using
/// <https://crates.io/crates/triple_buffer>).
///
/// This can be useful when you want to pass a small amount of data between
/// realtime threads (i.e. audio threads) very cheaply on most platforms, but
/// also have a realtime-safe fallback for platforms which do not have 64 bit
/// atomics (i.e. PowerPC).
pub struct Output64<T: Data64Bit> {
output: Output<[u8; 8]>,
_data_type: PhantomData<T>,
}
impl<T: Data64Bit> Output64<T> {
/// Read the latest value that was written to the buffer.
pub fn read(&mut self) -> T {
T::from_ne_bytes(*self.output.read())
}
}
}