From bdb99f1d138398886d41f2fc74353ee2bdfdf4d1 Mon Sep 17 00:00:00 2001 From: Andrej Babic Date: Mon, 6 Apr 2020 11:12:30 +0200 Subject: [PATCH] Improve RingBuffer initialization Remove automatic initialization on slot reservation - the client will need to take care of the initialization at the most appropriate time - also allows the calling code to define the slot size. --- core-writer/include/RingBuffer.hpp | 4 ++- core-writer/src/RingBuffer.cpp | 58 +++++++++++++++--------------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/core-writer/include/RingBuffer.hpp b/core-writer/include/RingBuffer.hpp index 03dc0bd..14c00b4 100644 --- a/core-writer/include/RingBuffer.hpp +++ b/core-writer/include/RingBuffer.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -39,7 +40,7 @@ class RingBuffer char* frame_data_buffer_ = NULL; size_t write_index_ = 0; size_t buffer_used_slots_ = 0; - bool ring_buffer_initialized_ = false; + std::atomic_bool initialized_ = false; std::list< std::shared_ptr > frame_metadata_queue_; std::mutex frame_metadata_queue_mutex_; @@ -58,6 +59,7 @@ class RingBuffer void release(size_t buffer_slot_index); bool is_empty(); + bool is_initialized(); void clear(); size_t get_slot_size(); }; diff --git a/core-writer/src/RingBuffer.cpp b/core-writer/src/RingBuffer.cpp index 11978b6..48d683b 100644 --- a/core-writer/src/RingBuffer.cpp +++ b/core-writer/src/RingBuffer.cpp @@ -30,53 +30,48 @@ RingBuffer::~RingBuffer() void RingBuffer::initialize(size_t slot_size) { - if (frame_data_buffer_) { - stringstream err_msg; - - using namespace date; - using namespace chrono; - err_msg << "[" << system_clock::now() << "]"; - err_msg << "[RingBuffer::initialize]"; - err_msg << " Ring buffer already initialized." << endl; - - throw runtime_error(err_msg.str()); + if (initialized_.load(memory_order_relaxed)) { + return; + } + + lock_guard lock(ringbuffer_slots_mutex_); + + if (initialized_) { + return; } - #ifdef DEBUG_OUTPUT - using namespace date; - using namespace chrono; - cout << "[" << system_clock::now() << "]"; - cout << "[RingBuffer::initialize]"; - cout << " Initializing ring buffer"; - cout << " with slot_size " << slot_size << endl; - #endif - write_index_ = 0; slot_size_ = slot_size; buffer_size_ = slot_size * n_slots_; frame_data_buffer_ = new char[buffer_size_]; buffer_used_slots_ = 0; - ring_buffer_initialized_ = true; + + initialized_ = true; #ifdef DEBUG_OUTPUT using namespace date; - using namespace chrono; + using namespace chrono; cout << "[" << system_clock::now() << "]"; cout << "[RingBuffer::initialize]"; - cout << " Total buffer_size " << buffer_size_ << endl; + cout << " Ringbuffer initialized"; + cout << " with slot_size " << slot_size_; + cout << " with n_slots " << n_slots_; + cout << " and buffer_size " << buffer_size_; #endif } char* RingBuffer::reserve(shared_ptr frame_metadata) { - if (!ring_buffer_initialized_) { - { - lock_guard lock(ringbuffer_slots_mutex_); + if (!is_initialized()) { + stringstream err_msg; - if (!ring_buffer_initialized_) { - initialize(frame_metadata->frame_bytes_size); - } - } + using namespace date; + using namespace chrono; + err_msg << "[" << system_clock::now() << "]"; + err_msg << "[RingBuffer::reserve]"; + err_msg << " Ringbuffer not initialized."; + + throw runtime_error(err_msg.str()); } if (frame_metadata->frame_bytes_size > slot_size_) { @@ -266,6 +261,11 @@ bool RingBuffer::is_empty() return buffer_used_slots_ == 0; } +bool RingBuffer::is_initialized() +{ + return initialized_.load(memory_order_relaxed); +} + void RingBuffer::clear() { lock_guard lock_slots(ringbuffer_slots_mutex_);