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.
This commit is contained in:
2020-04-06 11:12:30 +02:00
parent 8c144a07d7
commit bdb99f1d13
2 changed files with 32 additions and 30 deletions
+29 -29
View File
@@ -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<mutex> 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<FrameMetadata> frame_metadata)
{
if (!ring_buffer_initialized_) {
{
lock_guard<mutex> 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<mutex> lock_slots(ringbuffer_slots_mutex_);