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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! # Triple Buffering Crate
//!
//! This crate provides utilities for implementing triple buffering and generalized
//! multi-buffering patterns. It includes the [`TripleBuffer`] type alias, built on top of
//! the generic [`MultiBuffer`] struct, enabling developers to create and manage buffers
//! designed for high-performance, concurrent, or time-sensitive workloads.
//!
//! The primary purpose of this crate is to facilitate safe, lock-free access to the latest
//! data in one buffer while enabling concurrent modifications in other buffers. Each buffer
//! is associated with a version tag (`BufferTag`), which helps track updates and ensures
//! consumers can identify the most recent data or detect stale reads.
//!
//! ## Main Features
//!
//! - **`TripleBuffer<T>`**: A convenient alias for three buffers (standard triple buffering).
//! - **Generic Multi-buffering**: `MultiBuffer<T, SIZE>` supports any number of buffers
//! through constant generics.
//! - **Buffer tags for tracking updates**: Each buffer has a version tag to help identify the latest state.
//! - **Lock-free concurrency**: Allows safe access without blocking threads.
//! - **Convenience methods**: Intuitive API for buffer access, mutation, and publishing updated buffers.
//!
//! ## Examples
//!
//! ### TripleBuffer Example
//!
//! The following example demonstrates how to use a `TripleBuffer` in typical scenarios:
//!
//! ```rust
//! use multibuffer::TripleBuffer;
//!
//! fn main() {
//! // Create a TripleBuffer with an initial value of 0.
//! let buffer = TripleBuffer::new(|| 0);
//!
//! // Modify a specific buffer (index 1).
//! *buffer.get_mut(1) = 42;
//!
//! // Publish the updated buffer (index 1) with a version tag of 1.
//! buffer.publish(1, 1);
//!
//! // Access the latest published buffer and its tag.
//! let (latest, tag) = buffer.get_latest();
//! assert_eq!(*latest, 42);
//! assert_eq!(tag, 1);
//!
//! // Perform further modifications and publishing.
//! *buffer.get_mut(2) = 100;
//! buffer.publish(2, 2);
//! let (latest, tag) = buffer.get_latest();
//! assert_eq!(*latest, 100);
//! assert_eq!(tag, 2);
//! }
//! ```
//!
//! ## Implementation Details
//!
//! - The [`MultiBuffer`] struct maintains an internal array of buffers, version tags, and
//! a fence pointer to track the latest published buffer.
//! - Buffers are allocated using [`UnsafeCell`] to allow efficient mutable access without requiring
//! synchronization primitives.
//! - The version tags (`BufferTag`) are updated on publishing and help consumers confirm
//! they are reading the most up-to-date data.
//! - The `Sync` implementation is designed with precautions to ensure safe multi-threaded usage,
//! as long as the user adheres to the safety rules outlined in the API documentation.
//!
//! ## Notes
//!
//! - Correctness of the implementation depends on the user following the API's safety guidelines.
//! Specifically, concurrent misuse of the `get_latest` and `get_mut` methods may result in
//! undefined behavior.
//! - The crate relies on Rust's constant generics (`const SIZE: usize`) to specify the number of
//! buffers, which needs to be determined at compile-time.
use UnsafeCell;
use MaybeUninit;
use ;
/// A type alias for a triple buffer, which is a specialized form of the [`MultiBuffer`] struct
/// with a fixed size of three buffers. This is commonly used for triple buffering scenarios.
pub type TripleBuffer<T> = ;
/// A type alias representing the index of a buffer.
///
/// This is used to identify a specific buffer in a multi-buffering structure.
pub type BufferIndex = usize;
/// A type alias for the version tag of a buffer.
///
/// Each buffer in the multi-buffering structure is associated with a `BufferTag`
/// (type alias for `u64`), which tracks the version or state of the buffer.
/// This tag helps consumers determine if a buffer contains updated or stale data.
pub type BufferTag = u64;
/// A generic multi-buffering structure designed for lock-free concurrency, supporting a
/// configurable number of buffers, each tagged with a version number for tracking updates.
///
/// # Type Parameters
/// - `T`: The type of data stored in each buffer.
/// - `SIZE`: The number of buffers in the structure, which is fixed at compile time.
///
/// # Structure
/// - Each buffer has an associated tag (`u64`) to track its version or state. This allows
/// consumers to determine if a specific buffer contains newer data.
/// - An internal fence pointer is used to indicate the latest published buffer.
///
/// # Safety
/// - The buffers are wrapped in [`UnsafeCell`] to allow efficient mutable access without
/// synchronization primitives.
/// - The implementation assumes the user correctly follows the API's safety guarantees to
/// avoid undefined behavior, especially when using methods like `get_mut` and `publish`
/// in a concurrent environment.
/// - Correct use of the API ensures that data remains consistent and race conditions are avoided.
///
/// # Usage
/// This structure is particularly useful in scenarios requiring efficient lock-free
/// communication of data across threads, such as real-time processing or streaming.
// SAFETY: only one thread writes to a buffer at a time, controlled by design
unsafe