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; +}