-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathlib.rs
More file actions
336 lines (288 loc) · 9.52 KB
/
lib.rs
File metadata and controls
336 lines (288 loc) · 9.52 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//! # BBQueue
//!
//! BBQueue, short for "BipBuffer Queue", is a Single Producer Single Consumer,
//! lockless, no_std, thread safe, queue, based on [BipBuffers]. For more info on
//! the design of the lock-free algorithm used by bbqueue, see [this blog post].
//!
//! [BipBuffers]: https://www.codeproject.com/Articles/3479/%2FArticles%2F3479%2FThe-Bip-Buffer-The-Circular-Buffer-with-a-Twist
//! [this blog post]: https://ferrous-systems.com/blog/lock-free-ring-buffer/
//!
//! BBQueue is designed (primarily) to be a First-In, First-Out queue for use with DMA on embedded
//! systems.
//!
//! While Circular/Ring Buffers allow you to send data between two threads (or from an interrupt to
//! main code), you must push the data one piece at a time. With BBQueue, you instead are granted a
//! block of contiguous memory, which can be filled (or emptied) by a DMA engine.
//!
//! ## Local usage
//!
//! ```rust
//! // The "Churrasco" flavor has inline storage, hardware atomic
//! // support, no async support, and is not reference counted.
//! use bbq2::nicknames::Churrasco;
//!
//! // Create a buffer with six elements
//! let bb: Churrasco<6> = Churrasco::new();
//! let prod = bb.stream_producer();
//! let cons = bb.stream_consumer();
//!
//! // Request space for one byte
//! let mut wgr = prod.grant_exact(1).unwrap();
//!
//! // Set the data
//! wgr[0] = 123;
//!
//! assert_eq!(wgr.len(), 1);
//!
//! // Make the data ready for consuming
//! wgr.commit(1);
//!
//! // Read all available bytes
//! let rgr = cons.read().unwrap();
//!
//! assert_eq!(rgr[0], 123);
//!
//! // Release the space for later writes
//! rgr.release(1);
//! ```
//!
//! ## Static usage
//!
//! ```rust
//! use bbq2::nicknames::Churrasco;
//! use std::{thread::{sleep, spawn}, time::Duration};
//!
//! // Create a buffer with six elements
//! static BB: Churrasco<6> = Churrasco::new();
//!
//! fn receiver() {
//! let cons = BB.stream_consumer();
//! loop {
//! if let Ok(rgr) = cons.read() {
//! assert_eq!(rgr.len(), 1);
//! assert_eq!(rgr[0], 123);
//! rgr.release(1);
//! break;
//! }
//! // don't do this in real code, use Notify!
//! sleep(Duration::from_millis(10));
//! }
//! }
//!
//! fn main() {
//! let prod = BB.stream_producer();
//!
//! // spawn the consumer
//! let hdl = spawn(receiver);
//!
//! // Request space for one byte
//! let mut wgr = prod.grant_exact(1).unwrap();
//!
//! // Set the data
//! wgr[0] = 123;
//!
//! assert_eq!(wgr.len(), 1);
//!
//! // Make the data ready for consuming
//! wgr.commit(1);
//!
//! // make sure the receiver terminated
//! hdl.join().unwrap();
//! }
//! ```
//!
//! ## Features
//!
//! TODO
#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![deny(missing_docs)]
#![deny(warnings)]
#[cfg(feature = "alloc")]
extern crate alloc;
/// Type aliases for different generic configurations
///
pub mod nicknames;
/// Producer and consumer interfaces
///
pub mod prod_cons;
/// Queue storage
///
pub mod queue;
/// Generic traits
///
pub mod traits;
/// Re-export of external types/traits
///
pub mod export {
pub use const_init::ConstInit;
}
#[cfg(all(test, feature = "alloc"))]
mod test {
use core::{ops::Deref, time::Duration};
use crate::{
queue::{ArcBBQueue, BBQueue},
traits::{
coordination::cas::AtomicCoord,
notifier::maitake::MaiNotSpsc,
storage::{BoxedSlice, Inline},
},
};
#[cfg(all(target_has_atomic = "ptr", feature = "alloc"))]
#[test]
fn ux() {
use crate::traits::{notifier::polling::Polling, storage::BoxedSlice};
static BBQ: BBQueue<Inline<64>, AtomicCoord, Polling> = BBQueue::new();
let _ = BBQ.stream_producer();
let _ = BBQ.stream_consumer();
let buf2 = Inline::<64>::new();
let bbq2: BBQueue<_, AtomicCoord, Polling> = BBQueue::new_with_storage(&buf2);
let _ = bbq2.stream_producer();
let _ = bbq2.stream_consumer();
let buf3 = BoxedSlice::new(64);
let bbq3: BBQueue<_, AtomicCoord, Polling> = BBQueue::new_with_storage(buf3);
let _ = bbq3.stream_producer();
let _ = bbq3.stream_consumer();
}
#[cfg(target_has_atomic = "ptr")]
#[test]
fn smoke() {
use crate::traits::notifier::polling::Polling;
use core::ops::Deref;
static BBQ: BBQueue<Inline<64>, AtomicCoord, Polling> = BBQueue::new();
let prod = BBQ.stream_producer();
let cons = BBQ.stream_consumer();
let write_once = &[0x01, 0x02, 0x03, 0x04, 0x11, 0x12, 0x13, 0x14];
let mut wgr = prod.grant_exact(8).unwrap();
wgr.copy_from_slice(write_once);
wgr.commit(8);
let rgr = cons.read().unwrap();
assert_eq!(rgr.deref(), write_once.as_slice(),);
rgr.release(4);
let rgr = cons.read().unwrap();
assert_eq!(rgr.deref(), &write_once[4..]);
rgr.release(4);
assert!(cons.read().is_err());
}
#[cfg(target_has_atomic = "ptr")]
#[test]
fn smoke_framed() {
use crate::traits::notifier::polling::Polling;
use core::ops::Deref;
static BBQ: BBQueue<Inline<64>, AtomicCoord, Polling> = BBQueue::new();
let prod = BBQ.framed_producer();
let cons = BBQ.framed_consumer();
let write_once = &[0x01, 0x02, 0x03, 0x04, 0x11, 0x12];
let mut wgr = prod.grant(8).unwrap();
wgr[..6].copy_from_slice(write_once);
wgr.commit(6);
let rgr = cons.read().unwrap();
assert_eq!(rgr.deref(), write_once.as_slice());
rgr.release();
assert!(cons.read().is_err());
}
#[cfg(target_has_atomic = "ptr")]
#[test]
fn framed_misuse() {
use crate::traits::notifier::polling::Polling;
static BBQ: BBQueue<Inline<64>, AtomicCoord, Polling> = BBQueue::new();
let prod = BBQ.stream_producer();
let cons = BBQ.framed_consumer();
// Bad grant one: HUGE header value
let write_once = &[0xFF, 0xFF, 0x03, 0x04, 0x11, 0x12];
let mut wgr = prod.grant_exact(6).unwrap();
wgr[..6].copy_from_slice(write_once);
wgr.commit(6);
assert!(cons.read().is_err());
{
// Clear the bad grant
let cons2 = BBQ.stream_consumer();
let rgr = cons2.read().unwrap();
rgr.release(6);
}
// Bad grant two: too small of a grant
let write_once = &[0x00];
let mut wgr = prod.grant_exact(1).unwrap();
wgr[..1].copy_from_slice(write_once);
wgr.commit(1);
assert!(cons.read().is_err());
}
#[tokio::test]
async fn asink() {
static BBQ: BBQueue<Inline<64>, AtomicCoord, MaiNotSpsc> = BBQueue::new();
let prod = BBQ.stream_producer();
let cons = BBQ.stream_consumer();
let rxfut = tokio::task::spawn(async move {
let rgr = cons.wait_read().await;
assert_eq!(rgr.deref(), &[1, 2, 3]);
});
let txfut = tokio::task::spawn(async move {
tokio::time::sleep(Duration::from_millis(500)).await;
let mut wgr = prod.grant_exact(3).unwrap();
wgr.copy_from_slice(&[1, 2, 3]);
wgr.commit(3);
});
// todo: timeouts
rxfut.await.unwrap();
txfut.await.unwrap();
}
#[tokio::test]
async fn asink_framed() {
static BBQ: BBQueue<Inline<64>, AtomicCoord, MaiNotSpsc> = BBQueue::new();
let prod = BBQ.framed_producer();
let cons = BBQ.framed_consumer();
let rxfut = tokio::task::spawn(async move {
let rgr = cons.wait_read().await;
assert_eq!(rgr.deref(), &[1, 2, 3]);
});
let txfut = tokio::task::spawn(async move {
tokio::time::sleep(Duration::from_millis(500)).await;
let mut wgr = prod.grant(3).unwrap();
wgr.copy_from_slice(&[1, 2, 3]);
wgr.commit(3);
});
// todo: timeouts
rxfut.await.unwrap();
txfut.await.unwrap();
}
#[tokio::test]
async fn arc1() {
let bbq: ArcBBQueue<Inline<64>, AtomicCoord, MaiNotSpsc> =
ArcBBQueue::new_with_storage(Inline::new());
let prod = bbq.stream_producer();
let cons = bbq.stream_consumer();
let rxfut = tokio::task::spawn(async move {
let rgr = cons.wait_read().await;
assert_eq!(rgr.deref(), &[1, 2, 3]);
});
let txfut = tokio::task::spawn(async move {
tokio::time::sleep(Duration::from_millis(500)).await;
let mut wgr = prod.grant_exact(3).unwrap();
wgr.copy_from_slice(&[1, 2, 3]);
wgr.commit(3);
});
// todo: timeouts
rxfut.await.unwrap();
txfut.await.unwrap();
}
#[tokio::test]
async fn arc2() {
let bbq: ArcBBQueue<BoxedSlice, AtomicCoord, MaiNotSpsc> =
ArcBBQueue::new_with_storage(BoxedSlice::new(64));
let prod = bbq.stream_producer();
let cons = bbq.stream_consumer();
let rxfut = tokio::task::spawn(async move {
let rgr = cons.wait_read().await;
assert_eq!(rgr.deref(), &[1, 2, 3]);
});
let txfut = tokio::task::spawn(async move {
tokio::time::sleep(Duration::from_millis(500)).await;
let mut wgr = prod.grant_exact(3).unwrap();
wgr.copy_from_slice(&[1, 2, 3]);
wgr.commit(3);
});
// todo: timeouts
rxfut.await.unwrap();
txfut.await.unwrap();
drop(bbq);
}
}