Skip to content

Commit

Permalink
Updated according to feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Lucy <[email protected]>
  • Loading branch information
lucypa committed Mar 9, 2022
1 parent 3fea94c commit ded915e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
44 changes: 26 additions & 18 deletions libsharedringbuffer/include/shared_ringbuffer/shared_ringbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ void ring_init(ring_handle_t *ring, ring_buffer_t *avail, ring_buffer_t *used, n
*
* @return true indicates the buffer is empty, false otherwise.
*/
static inline int ring_empty(ring_buffer_t *ring)
static inline bool ring_empty(ring_buffer_t *ring)
{
return !((ring->write_idx - ring->read_idx) % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT);
//return !((ring->write_idx - ring->read_idx) % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT);
return (ring->write_idx == ring->read_idx);
}

/**
Expand All @@ -69,9 +70,10 @@ static inline int ring_empty(ring_buffer_t *ring)
*
* @return true indicates the buffer is full, false otherwise.
*/
static inline int ring_full(ring_buffer_t *ring)
static inline bool ring_full(ring_buffer_t *ring)
{
return !((ring->write_idx - ring->read_idx + 1) % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT);
//return !((ring->write_idx - ring->read_idx + 1) % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT);
return CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT == (ring->write_idx - ring->read_idx);
}

/**
Expand All @@ -82,7 +84,9 @@ static inline int ring_full(ring_buffer_t *ring)
*/
static inline void notify(ring_handle_t *ring)
{
return ring->notify();
if (ring->notify != NULL) {
return ring->notify();
}
}

/**
Expand All @@ -102,11 +106,13 @@ static inline int enqueue(ring_buffer_t *ring, uintptr_t buffer, unsigned int le
return -1;
}

ring->buffers[ring->write_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].encoded_addr = buffer;
ring->buffers[ring->write_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].len = len;
ring->buffers[ring->write_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].cookie = cookie;
buff_desc_t *buff_desc = &(ring->buffers[ring->write_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT]);
buff_desc->encoded_addr = buffer;
buff_desc->len = len;
buff_desc->cookie = cookie;
ring->write_idx++;

/* Ensure data is written back to memory */
THREAD_MEMORY_RELEASE();

return 0;
Expand All @@ -129,10 +135,12 @@ static inline int dequeue(ring_buffer_t *ring, uintptr_t *addr, unsigned int *le
return -1;
}

*addr = ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].encoded_addr;
*len = ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].len;
*cookie = ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].cookie;
buff_desc_t *buff_desc = &(ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT]);
*addr = buff_desc->encoded_addr;
*len = buff_desc->len;
*cookie = buff_desc->cookie;

/* Ensure data is written back to memory */
THREAD_MEMORY_RELEASE();
ring->read_idx++;

Expand Down Expand Up @@ -209,25 +217,25 @@ static inline int dequeue_used(ring_handle_t *ring, uintptr_t *addr, unsigned in
* @param ring Ring buffer to dequeue from.
* @param addr pointer to the address of where to store buffer address.
* @param len pointer to variable to store length of data dequeueing.
* @param cookie pointer to store a pointer to this particular entry.
* @param pointer pointer to store the address to this particular entry.
*
* @return -1 when ring is empty, 0 on success.
*/

static int driver_dequeue(ring_buffer_t *ring, uintptr_t *addr, unsigned int *len, void **cookie)
static int driver_dequeue(ring_buffer_t *ring, uintptr_t *addr, unsigned int *len, void **pointer)
{
if (!((ring->write_idx - ring->read_idx) % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT)) {
ZF_LOGW("write idx = %d, read idx = %d", ring->write_idx, ring->read_idx);
ZF_LOGW("Ring is empty");
return -1;
}

*addr = ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].encoded_addr;
*len = ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT].len;
*cookie = &ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT];
buff_desc_t *buff_desc = &(ring->buffers[ring->read_idx % CONFIG_LIB_SHARED_RINGBUFFER_DESC_COUNT]);
*addr = buff_desc->encoded_addr;
*len = buff_desc->len;
*pointer = buff_desc;

THREAD_MEMORY_RELEASE();
ring->read_idx++;

return 0;
}
}
2 changes: 1 addition & 1 deletion libsharedringbuffer/src/shared_ringbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ void ring_init(ring_handle_t *ring, ring_buffer_t *avail, ring_buffer_t *used, n
ring->avail_ring->write_idx = 0;
ring->avail_ring->read_idx = 0;
}
}
}

0 comments on commit ded915e

Please sign in to comment.