From 936001aaf5123889cb81526022b61c5c2379d843 Mon Sep 17 00:00:00 2001 From: Andre Puschmann Date: Fri, 29 Jun 2018 15:52:10 +0200 Subject: [PATCH] add blocking allocation to buffer_pool --- lib/include/srslte/common/buffer_pool.h | 47 ++++++++++++++++--------- lib/include/srslte/common/common.h | 1 + 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/include/srslte/common/buffer_pool.h b/lib/include/srslte/common/buffer_pool.h index 0a87c0df1..283501ff4 100644 --- a/lib/include/srslte/common/buffer_pool.h +++ b/lib/include/srslte/common/buffer_pool.h @@ -63,6 +63,7 @@ public: nof_buffers = (uint32_t) capacity_; } pthread_mutex_init(&mutex, NULL); + pthread_cond_init(&cv_not_empty, NULL); for(uint32_t i=0;i 0) - { + if (available.size() > 0) { b = available.top(); used.push_back(b); available.pop(); - + if (is_almost_empty()) { - printf("Warning buffer pool capacity is %f %%\n", (float) 100*available.size()/capacity); + printf("Warning buffer pool capacity is %f %%\n", (float) 100 * available.size() / capacity); } #ifdef SRSLTE_BUFFER_POOL_LOG_ENABLED - if (debug_name) { - strncpy(b->debug_name, debug_name, SRSLTE_BUFFER_POOL_LOG_NAME_LEN); - b->debug_name[SRSLTE_BUFFER_POOL_LOG_NAME_LEN-1] = 0; - } + if (debug_name) { + strncpy(b->debug_name, debug_name, SRSLTE_BUFFER_POOL_LOG_NAME_LEN); + b->debug_name[SRSLTE_BUFFER_POOL_LOG_NAME_LEN - 1] = 0; + } #endif - - } else { + } else if (blocking) { + // blocking allocation + while(available.size() == 0) { + pthread_cond_wait(&cv_not_empty, &mutex); + } + + // retrieve the new buffer + b = available.top(); + used.push_back(b); + available.pop(); + + // do not print any warning + } + else { printf("Error - buffer pool is empty\n"); #ifdef SRSLTE_BUFFER_POOL_LOG_ENABLED @@ -148,6 +161,7 @@ public: available.push(b); ret = true; } + pthread_cond_signal(&cv_not_empty); pthread_mutex_unlock(&mutex); return ret; } @@ -157,7 +171,8 @@ private: static const int POOL_SIZE = 2048; std::stack available; std::vector used; - pthread_mutex_t mutex; + pthread_mutex_t mutex; + pthread_cond_t cv_not_empty; uint32_t capacity; }; @@ -174,8 +189,8 @@ public: ~byte_buffer_pool() { delete pool; } - byte_buffer_t* allocate(const char *debug_name = NULL) { - return pool->allocate(debug_name); + byte_buffer_t* allocate(const char *debug_name = NULL, bool blocking = false) { + return pool->allocate(debug_name, blocking); } void deallocate(byte_buffer_t *b) { if(!b) { diff --git a/lib/include/srslte/common/common.h b/lib/include/srslte/common/common.h index 3313d0eb4..ed5c9acc1 100644 --- a/lib/include/srslte/common/common.h +++ b/lib/include/srslte/common/common.h @@ -69,6 +69,7 @@ #ifdef SRSLTE_BUFFER_POOL_LOG_ENABLED #define pool_allocate (pool->allocate(__PRETTY_FUNCTION__)) +#define pool_allocate_blocking (pool->allocate(__PRETTY_FUNCTION__, true)) #define SRSLTE_BUFFER_POOL_LOG_NAME_LEN 128 #else #define pool_allocate (pool->allocate())