Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial support for non-primitive types #65

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bbqtest/src/benches.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bbqueue::{consts::*, BBBuffer};
use bbqueue::{consts::*, GenericBBBuffer};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::cmp::min;

Expand All @@ -17,7 +17,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {

c.bench_function("bbq 2048/4096", |bench| bench.iter(|| chunky(&data, 2048)));

let buffy: BBBuffer<U65536> = BBBuffer::new();
let buffy: GenericBBBuffer<u8, U65536> = GenericBBBuffer::new();
let (mut prod, mut cons) = buffy.try_split().unwrap();

c.bench_function("bbq 8192/65536", |bench| {
Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {

use crossbeam_utils::thread;
fn chunky(data: &[u8], chunksz: usize) {
let buffy: BBBuffer<U4096> = BBBuffer::new();
let buffy: GenericBBBuffer<u8, U4096> = GenericBBBuffer::new();
let (mut prod, mut cons) = buffy.try_split().unwrap();

thread::scope(|sc| {
Expand Down
20 changes: 10 additions & 10 deletions bbqtest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod single_thread;

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

#[test]
fn deref_deref_mut() {
Expand All @@ -34,8 +34,8 @@ 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<U6> = GenericBBBuffer(ConstBBBuffer::new());
static BBQ2: BBBuffer<U6> = GenericBBBuffer(ConstBBBuffer::new());
let (mut prod1, mut cons1) = BBQ1.try_split().unwrap();
let (mut _prod2, mut cons2) = BBQ2.try_split().unwrap();

Expand All @@ -55,8 +55,8 @@ 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: GenericBBBuffer<u8, U6> = GenericBBBuffer(ConstBBBuffer::new());
static BBQ2: GenericBBBuffer<u8, U6> = GenericBBBuffer(ConstBBBuffer::new());
let (prod1, cons1) = BBQ1.try_split().unwrap();
let (prod2, cons2) = BBQ2.try_split().unwrap();

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

Expand Down Expand Up @@ -178,7 +178,7 @@ mod tests {

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

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

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

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

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

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

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

// Create a frame that should take 3 bytes for the header
Expand Down
12 changes: 7 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::*, ConstBBBuffer, Error, GenericBBBuffer};
use rand::prelude::*;
use std::thread::spawn;
use std::time::{Duration, Instant};
Expand All @@ -13,6 +13,8 @@ mod tests {

const RPT_IVAL: usize = ITERS / 100;

// Data type
type DataTy = u8;
// These two should be the same
type QueueSizeTy = U1024;
const QUEUE_SIZE: usize = 1024;
Expand All @@ -29,7 +31,7 @@ mod tests {
println!("RTX: Generating Test Data...");
let gen_start = Instant::now();
let mut data = Vec::with_capacity(ITERS);
(0..ITERS).for_each(|_| data.push(rand::random::<u8>()));
(0..ITERS).for_each(|_| data.push(rand::random::<DataTy>()));
let mut data_rx = data.clone();

let mut trng = thread_rng();
Expand All @@ -50,7 +52,7 @@ mod tests {
#[cfg(feature = "verbose")]
println!("RTX: Running test...");

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

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

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

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

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

#[cfg(feature = "verbose")]
Expand Down
68 changes: 43 additions & 25 deletions bbqtest/src/ring_around_the_senders.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#[cfg(test)]
mod tests {

use core::convert::TryFrom;
use core::fmt::Debug;

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

enum Potato<'a, N>
enum Potato<'a, T, N>
where
N: ArrayLength<u8>,
T: Sized + TryFrom<usize>,
N: ArrayLength<T>,
{
Tx((Producer<'a, N>, u8)),
Rx((Consumer<'a, N>, u8)),
TxG(GrantW<'a, N>),
RxG(GrantR<'a, N>),
Tx((Producer<'a, T, N>, u8)),
Rx((Consumer<'a, T, N>, u8)),
TxG(GrantW<'a, T, N>),
RxG(GrantR<'a, T, N>),
Idle,
Done,
}
Expand All @@ -28,9 +32,10 @@ mod tests {
const BYTES_PER_GRANT: usize = 129;
type BufferSize = U4096;

impl<'a, N> Potato<'a, N>
impl<'a, T, N> Potato<'a, T, N>
where
N: ArrayLength<u8>,
T: Sized + TryFrom<usize> + Debug + PartialEq,
N: ArrayLength<T>,
{
fn work(self) -> (Self, Self) {
match self {
Expand Down Expand Up @@ -66,15 +71,19 @@ mod tests {
gr_w.iter_mut()
.take(BYTES_PER_GRANT)
.enumerate()
.for_each(|(i, by)| *by = i as u8);
.for_each(|(i, by)| {
*by = T::try_from(i).ok().expect("can construct from usize")
});
gr_w.commit(BYTES_PER_GRANT);
(Self::Idle, Self::Idle)
}
Self::RxG(gr_r) => {
gr_r.iter()
.take(BYTES_PER_GRANT)
.enumerate()
.for_each(|(i, by)| assert_eq!(*by, i as u8));
.for_each(|(i, by)| {
assert_eq!(*by, T::try_from(i).ok().expect("can construct from usize"))
});
gr_r.release(BYTES_PER_GRANT);
(Self::Idle, Self::Idle)
}
Expand All @@ -84,39 +93,48 @@ mod tests {
}
}

static BB: BBBuffer<BufferSize> = BBBuffer(ConstBBBuffer::new());
// Data type
type DataTy = u8;
static BB: GenericBBBuffer<DataTy, BufferSize> = GenericBBBuffer(ConstBBBuffer::new());

use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread::spawn;

#[test]
fn hello() {
let (prod, cons) = BB.try_split().unwrap();
generic_hello::<DataTy>(&BB);
}

fn generic_hello<T>(bb: &'static GenericBBBuffer<T, BufferSize>)
where
T: Sized + TryFrom<usize> + Debug + PartialEq,
{
let (prod, cons) = bb.try_split().unwrap();

// create the channels
let (tx_1_2, rx_1_2): (
Sender<Potato<'static, BufferSize>>,
Receiver<Potato<'static, BufferSize>>,
Sender<Potato<'static, T, BufferSize>>,
Receiver<Potato<'static, T, BufferSize>>,
) = channel();
let (tx_2_3, rx_2_3): (
Sender<Potato<'static, BufferSize>>,
Receiver<Potato<'static, BufferSize>>,
Sender<Potato<'static, T, BufferSize>>,
Receiver<Potato<'static, T, BufferSize>>,
) = channel();
let (tx_3_4, rx_3_4): (
Sender<Potato<'static, BufferSize>>,
Receiver<Potato<'static, BufferSize>>,
Sender<Potato<'static, T, BufferSize>>,
Receiver<Potato<'static, T, BufferSize>>,
) = channel();
let (tx_4_1, rx_4_1): (
Sender<Potato<'static, BufferSize>>,
Receiver<Potato<'static, BufferSize>>,
Sender<Potato<'static, T, BufferSize>>,
Receiver<Potato<'static, T, BufferSize>>,
) = 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, T, BufferSize> = Potato::Idle;

loop {
if let Potato::Idle = me {
Expand Down Expand Up @@ -168,9 +186,9 @@ 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;
move |rx: Receiver<Potato<'static, T, BufferSize>>,
tx: Sender<Potato<'static, T, BufferSize>>| {
let mut me: Potato<'static, T, BufferSize> = Potato::Idle;
let mut count = 0;

loop {
Expand Down
Loading