jamesmunns / bbqueue

A SPSC, lockless, no_std, thread safe, queue, based on BipBuffers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Store the current active grant for the Producer and the Consumer

justacec opened this issue · comments

Currently, the action of either a Producer grant or a Consumer read is a Grant instance that must be maintained and tracked. I have found this difficult when using the RTFM embedded framework. Since this queue is designed to be SPSC, there can only ever be one write Grant and one read Grant at a time.

If this is the case, I recommend that the Producer and the Consumer internally track the grant.

This would simplify things for the end user in both complexity and the housekeeping of tracking extra symbols.

Thoughts?

As I was indicating earlier, it seems that there can only be a single producer and a single consumer. Because of this, there can really only ever be one producer grant and one consumer grant at a time. Since there can be only one at a time, then the producer and the consumer could just track it internally and expose the grant functionality to the use.

Maybe something line this:

// Create a buffer with six elements
let bb: BBBuffer<U6> = BBBuffer::new();
let (mut prod, mut cons) = bb.try_split().unwrap();

// Request space for one byte
// [The producer would just keep track of the grant internally, so, no GrantW return]
// [Not sure how to capture an error state, but something line this?]
match prod.grant_exact(1) {
    Err() => {
        println!("Something bad happened")
    },
    _ => {}
}

// Set the data
// [Although I liked the Deref, in this case one would always just call buf to get at the data]
prod.buf()[0] = 123;

assert_eq!(prod.buf().len(), 1);

// Make the data ready for consuming
// [Since the producer is tracking the grant, just call commit on the producer]
prod.commit(1);

// Read all available bytes
// [This one is a bit confusing, maybe just calling read would return the buf?]
let rgr = cons.read().unwrap();

assert_eq!(rgr[0], 123);

// Release the space for later writes
// [But the release would occur in the consumer
cons.release(1);