From 594d62f22999814d34b27f09b8bdd68fdee43d88 Mon Sep 17 00:00:00 2001 From: Ismael Gomez Date: Mon, 3 Jun 2019 21:39:24 +0200 Subject: [PATCH] Added read_block function to ringbuffer --- lib/include/srslte/phy/utils/ringbuffer.h | 2 ++ lib/src/phy/utils/ringbuffer.c | 27 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/include/srslte/phy/utils/ringbuffer.h b/lib/include/srslte/phy/utils/ringbuffer.h index 5a9cdc39d..83a9715d2 100644 --- a/lib/include/srslte/phy/utils/ringbuffer.h +++ b/lib/include/srslte/phy/utils/ringbuffer.h @@ -60,6 +60,8 @@ SRSLTE_API int srslte_ringbuffer_read_timed(srslte_ringbuffer_t* q, void* p, int SRSLTE_API int srslte_ringbuffer_read_convert_conj(srslte_ringbuffer_t* q, cf_t* dst_ptr, float norm, int nof_samples); +SRSLTE_API int srslte_ringbuffer_read_block(srslte_ringbuffer_t* q, void** p, int nof_bytes); + SRSLTE_API void srslte_ringbuffer_stop(srslte_ringbuffer_t *q); #ifdef __cplusplus diff --git a/lib/src/phy/utils/ringbuffer.c b/lib/src/phy/utils/ringbuffer.c index d37e12e9f..38f6d6f41 100644 --- a/lib/src/phy/utils/ringbuffer.c +++ b/lib/src/phy/utils/ringbuffer.c @@ -221,3 +221,30 @@ int srslte_ringbuffer_read_convert_conj(srslte_ringbuffer_t* q, cf_t* dst_ptr, f pthread_mutex_unlock(&q->mutex); return nof_samples; } + +/* For this function, the ring buffer capacity must be multiple of block size */ +int srslte_ringbuffer_read_block(srslte_ringbuffer_t* q, void** p, int nof_bytes) +{ + int ret = nof_bytes; + pthread_mutex_lock(&q->mutex); + + /* Wait until enough data is in the buffer */ + while (q->count < nof_bytes && q->active) { + pthread_cond_wait(&q->cvar, &q->mutex); + } + + if (!q->active) { + ret = 0; + } else { + *p = &q->buffer[q->rpm]; + + q->count -= nof_bytes; + q->rpm += nof_bytes; + + if (q->rpm >= q->capacity) { + q->rpm -= q->capacity; + } + } + pthread_mutex_unlock(&q->mutex); + return ret; +}