Skip to content

Commit

Permalink
Implement storing data in a pointer location
Browse files Browse the repository at this point in the history
  • Loading branch information
mattico committed Feb 15, 2021
1 parent 1c88192 commit 7310553
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 221 deletions.
34 changes: 20 additions & 14 deletions bbqtest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ mod single_thread;

#[cfg(test)]
mod tests {
use bbqueue::{consts::*, BBBuffer, ConstBBBuffer, Error as BBQError};
use bbqueue::{
consts::*, ArrayStorage, BBBuffer, ConstBBBuffer, Error as BBQError, GenericArray,
};

#[test]
fn deref_deref_mut() {
let bb: BBBuffer<U6> = BBBuffer::new();
let bb: BBBuffer<ArrayStorage<GenericArray<u8, U6>>> = BBBuffer::new();
let (mut prod, mut cons) = bb.try_split().unwrap();

let mut wgr = prod.grant_exact(1).unwrap();
Expand All @@ -35,8 +37,10 @@ mod tests {
#[test]
fn static_allocator() {
// Check we can make multiple static items...
static BBQ1: BBBuffer<U6> = BBBuffer(ConstBBBuffer::new());
static BBQ2: BBBuffer<U6> = BBBuffer(ConstBBBuffer::new());
static BBQ1: BBBuffer<ArrayStorage<GenericArray<u8, U6>>> =
BBBuffer(ConstBBBuffer::new(ArrayStorage::new()));
static BBQ2: BBBuffer<ArrayStorage<GenericArray<u8, U6>>> =
BBBuffer(ConstBBBuffer::new(ArrayStorage::new()));
let (mut prod1, mut cons1) = BBQ1.try_split().unwrap();
let (mut _prod2, mut cons2) = BBQ2.try_split().unwrap();

Expand All @@ -56,8 +60,10 @@ mod tests {
#[test]
fn release() {
// Check we can make multiple static items...
static BBQ1: BBBuffer<U6> = BBBuffer(ConstBBBuffer::new());
static BBQ2: BBBuffer<U6> = BBBuffer(ConstBBBuffer::new());
static BBQ1: BBBuffer<ArrayStorage<GenericArray<u8, U6>>> =
BBBuffer(ConstBBBuffer::new(ArrayStorage::new()));
static BBQ2: BBBuffer<ArrayStorage<GenericArray<u8, U6>>> =
BBBuffer(ConstBBBuffer::new(ArrayStorage::new()));
let (prod1, cons1) = BBQ1.try_split().unwrap();
let (prod2, cons2) = BBQ2.try_split().unwrap();

Expand Down Expand Up @@ -94,21 +100,21 @@ mod tests {
#[test]
fn direct_usage_sanity() {
// Initialize
let bb: BBBuffer<U6> = BBBuffer::new();
let bb: BBBuffer<ArrayStorage<GenericArray<u8, U6>>> = BBBuffer::new();
let (mut prod, mut cons) = bb.try_split().unwrap();
assert_eq!(cons.read(), Err(BBQError::InsufficientSize));
assert_eq!(cons.read().unwrap_err(), BBQError::InsufficientSize);

// Initial grant, shouldn't roll over
let mut x = prod.grant_exact(4).unwrap();

// Still no data available yet
assert_eq!(cons.read(), Err(BBQError::InsufficientSize));
assert_eq!(cons.read().unwrap_err(), BBQError::InsufficientSize);

// Add full data from grant
x.copy_from_slice(&[1, 2, 3, 4]);

// Still no data available yet
assert_eq!(cons.read(), Err(BBQError::InsufficientSize));
assert_eq!(cons.read().unwrap_err(), BBQError::InsufficientSize);

// Commit data
x.commit(4);
Expand Down Expand Up @@ -179,7 +185,7 @@ mod tests {

#[test]
fn zero_sized_grant() {
let bb: BBBuffer<U1000> = BBBuffer::new();
let bb: BBBuffer<ArrayStorage<GenericArray<u8, U1000>>> = BBBuffer::new();
let (mut prod, mut _cons) = bb.try_split().unwrap();

let size = 1000;
Expand All @@ -192,7 +198,7 @@ mod tests {

#[test]
fn frame_sanity() {
let bb: BBBuffer<U1000> = BBBuffer::new();
let bb: BBBuffer<ArrayStorage<GenericArray<u8, U1000>>> = BBBuffer::new();
let (mut prod, mut cons) = bb.try_split_framed().unwrap();

// One frame in, one frame out
Expand Down Expand Up @@ -239,7 +245,7 @@ mod tests {

#[test]
fn frame_wrap() {
let bb: BBBuffer<U22> = BBBuffer::new();
let bb: BBBuffer<ArrayStorage<GenericArray<u8, U22>>> = BBBuffer::new();
let (mut prod, mut cons) = bb.try_split_framed().unwrap();

// 10 + 1 used
Expand Down Expand Up @@ -305,7 +311,7 @@ mod tests {

#[test]
fn frame_big_little() {
let bb: BBBuffer<U65536> = BBBuffer::new();
let bb: BBBuffer<ArrayStorage<GenericArray<u8, U65536>>> = BBBuffer::new();
let (mut prod, mut cons) = bb.try_split_framed().unwrap();

// Create a frame that should take 3 bytes for the header
Expand Down
10 changes: 5 additions & 5 deletions bbqtest/src/multi_thread.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg_attr(not(feature = "verbose"), allow(unused_variables))]
#[cfg(test)]
mod tests {
use bbqueue::{consts::*, BBBuffer, ConstBBBuffer, Error};
use bbqueue::{consts::*, ArrayStorage, BBBuffer, ConstBBBuffer, Error, GenericArray};
use rand::prelude::*;
use std::thread::spawn;
use std::time::{Duration, Instant};
Expand All @@ -14,7 +14,7 @@ mod tests {
const RPT_IVAL: usize = ITERS / 100;

// These two should be the same
type QueueSizeTy = U1024;
type QueueStorage = ArrayStorage<GenericArray<u8, U1024>>;
const QUEUE_SIZE: usize = 1024;

const TIMEOUT_NODATA: Duration = Duration::from_millis(10_000);
Expand Down Expand Up @@ -50,7 +50,7 @@ mod tests {
#[cfg(feature = "verbose")]
println!("RTX: Running test...");

static BB: BBBuffer<QueueSizeTy> = BBBuffer(ConstBBBuffer::new());
static BB: BBBuffer<QueueStorage> = BBBuffer(ConstBBBuffer::new(ArrayStorage::new()));
let (mut tx, mut rx) = BB.try_split().unwrap();

let mut last_tx = Instant::now();
Expand Down Expand Up @@ -142,7 +142,7 @@ mod tests {

#[test]
fn sanity_check() {
static BB: BBBuffer<QueueSizeTy> = BBBuffer(ConstBBBuffer::new());
static BB: BBBuffer<QueueStorage> = BBBuffer(ConstBBBuffer::new(ArrayStorage::new()));
let (mut tx, mut rx) = BB.try_split().unwrap();

let mut last_tx = Instant::now();
Expand Down Expand Up @@ -236,7 +236,7 @@ mod tests {

#[test]
fn sanity_check_grant_max() {
static BB: BBBuffer<QueueSizeTy> = BBBuffer(ConstBBBuffer::new());
static BB: BBBuffer<QueueStorage> = BBBuffer(ConstBBBuffer::new(ArrayStorage::new()));
let (mut tx, mut rx) = BB.try_split().unwrap();

#[cfg(feature = "verbose")]
Expand Down
114 changes: 57 additions & 57 deletions bbqtest/src/ring_around_the_senders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
mod tests {

use bbqueue::{
consts::*, ArrayLength, BBBuffer, ConstBBBuffer, Consumer, GrantR, GrantW, Producer,
consts::*, ArrayStorage, BBBuffer, BBStorage, ConstBBBuffer, Consumer, GenericArray,
GrantR, GrantW, Producer,
};

enum Potato<'a, N>
enum Potato<'a, A>
where
N: ArrayLength<u8>,
A: BBStorage,
{
Tx((Producer<'a, N>, u8)),
Rx((Consumer<'a, N>, u8)),
TxG(GrantW<'a, N>),
RxG(GrantR<'a, N>),
Tx((Producer<'a, A>, u8)),
Rx((Consumer<'a, A>, u8)),
TxG(GrantW<'a, A>),
RxG(GrantR<'a, A>),
Idle,
Done,
}
Expand All @@ -26,11 +27,11 @@ mod tests {
const TX_GRANTS_PER_RING: u8 = 3;
const RX_GRANTS_PER_RING: u8 = 3;
const BYTES_PER_GRANT: usize = 129;
type BufferSize = U4096;
type Buffer = ArrayStorage<GenericArray<u8, U4096>>;

impl<'a, N> Potato<'a, N>
impl<'a, A> Potato<'a, A>
where
N: ArrayLength<u8>,
A: BBStorage,
{
fn work(self) -> (Self, Self) {
match self {
Expand Down Expand Up @@ -84,7 +85,7 @@ mod tests {
}
}

static BB: BBBuffer<BufferSize> = BBBuffer(ConstBBBuffer::new());
static BB: BBBuffer<Buffer> = BBBuffer(ConstBBBuffer::new(ArrayStorage::new()));

use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread::spawn;
Expand All @@ -95,28 +96,28 @@ mod tests {

// create the channels
let (tx_1_2, rx_1_2): (
Sender<Potato<'static, BufferSize>>,
Receiver<Potato<'static, BufferSize>>,
Sender<Potato<'static, Buffer>>,
Receiver<Potato<'static, Buffer>>,
) = channel();
let (tx_2_3, rx_2_3): (
Sender<Potato<'static, BufferSize>>,
Receiver<Potato<'static, BufferSize>>,
Sender<Potato<'static, Buffer>>,
Receiver<Potato<'static, Buffer>>,
) = channel();
let (tx_3_4, rx_3_4): (
Sender<Potato<'static, BufferSize>>,
Receiver<Potato<'static, BufferSize>>,
Sender<Potato<'static, Buffer>>,
Receiver<Potato<'static, Buffer>>,
) = channel();
let (tx_4_1, rx_4_1): (
Sender<Potato<'static, BufferSize>>,
Receiver<Potato<'static, BufferSize>>,
Sender<Potato<'static, Buffer>>,
Receiver<Potato<'static, Buffer>>,
) = channel();

tx_1_2.send(Potato::Tx((prod, 3))).unwrap();
tx_1_2.send(Potato::Rx((cons, 3))).unwrap();

let thread_1 = spawn(move || {
let mut count = TOTAL_RINGS;
let mut me: Potato<'static, BufferSize> = Potato::Idle;
let mut me: Potato<'static, Buffer> = Potato::Idle;

loop {
if let Potato::Idle = me {
Expand Down Expand Up @@ -167,50 +168,49 @@ mod tests {
}
});

let closure_2_3_4 =
move |rx: Receiver<Potato<'static, BufferSize>>,
tx: Sender<Potato<'static, BufferSize>>| {
let mut me: Potato<'static, BufferSize> = Potato::Idle;
let mut count = 0;

loop {
if let Potato::Idle = me {
if let Ok(new) = rx.try_recv() {
if let Potato::Tx(_) = &new {
count += 1;
}
me = new;
} else {
continue;
}
}
let (new_me, send) = me.work();
let closure_2_3_4 = move |rx: Receiver<Potato<'static, Buffer>>,
tx: Sender<Potato<'static, Buffer>>| {
let mut me: Potato<'static, Buffer> = Potato::Idle;
let mut count = 0;

let we_done = if let Potato::Done = &send {
true
loop {
if let Potato::Idle = me {
if let Ok(new) = rx.try_recv() {
if let Potato::Tx(_) = &new {
count += 1;
}
me = new;
} else {
false
};
continue;
}
}
let (new_me, send) = me.work();

let nop = if let Potato::Idle = &send {
true
} else {
false
};
let we_done = if let Potato::Done = &send {
true
} else {
false
};

if !nop {
tx.send(send).ok();
}
let nop = if let Potato::Idle = &send {
true
} else {
false
};

if we_done {
assert_eq!(count, TOTAL_RINGS);
println!("We good.");
return;
}
if !nop {
tx.send(send).ok();
}

me = new_me;
if we_done {
assert_eq!(count, TOTAL_RINGS);
println!("We good.");
return;
}
};

me = new_me;
}
};

let thread_2 = spawn(move || closure_2_3_4(rx_1_2, tx_2_3));
let thread_3 = spawn(move || closure_2_3_4(rx_2_3, tx_3_4));
Expand Down
4 changes: 2 additions & 2 deletions bbqtest/src/single_thread.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#[cfg(test)]
mod tests {
use bbqueue::{consts::*, BBBuffer};
use bbqueue::{consts::*, ArrayStorage, BBBuffer, GenericArray};

#[test]
fn sanity_check() {
let bb: BBBuffer<U6> = BBBuffer::new();
let bb: BBBuffer<ArrayStorage<GenericArray<u8, U6>>> = BBBuffer::new();
let (mut prod, mut cons) = bb.try_split().unwrap();

const ITERS: usize = 100000;
Expand Down
Loading

0 comments on commit 7310553

Please sign in to comment.