Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/core/include/core/buffer/buffer_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class BufferInterface : public BufferBase {
/// \return 0 if failed, otherwise the number of bytes written (1)
virtual std::size_t Write(const T& data) = 0;

/// \brief Peek at the most recent data without consuming it
/// \param data reference to store the peeked data
/// \return 0 if failed, otherwise 1
virtual std::size_t Peek(T& data) const = 0;

/// \brief Read data from the buffer (burst read)
/// \param data
/// \return 0 if failed, otherwise the number of bytes read
Expand Down
13 changes: 12 additions & 1 deletion src/core/include/core/buffer/double_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,22 @@ class DoubleBuffer : public BufferInterface<T> {
return true;
}

std::size_t Peek(T& data) const {
std::lock_guard<std::mutex> lock(mutex_);
if (!ready_.load(std::memory_order_acquire)) {
return 0;
}

int read_index = 1 - write_index_;
data = buffer_[read_index];
return 1;
}

private:
T buffer_[2];
int write_index_;
std::atomic<bool> ready_{false};
std::mutex mutex_;
mutable std::mutex mutex_;
std::condition_variable cond_var_;
};
} // namespace quickviz
Expand Down
9 changes: 9 additions & 0 deletions src/core/include/core/buffer/ring_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ class RingBuffer : public BufferInterface<T> {
return count;
}

// Get the most recent data
std::size_t Peek(T& data) const override {
std::lock_guard<std::mutex> lock(buffer_mutex_);
std::size_t occupied_size = (write_index_ - read_index_) & size_mask_;
if (occupied_size == 0) return 0;
data = buffer_[(write_index_ - 1) & size_mask_];
return 1;
}

std::size_t Write(const std::vector<T>& new_data, std::size_t btw) override {
assert(new_data.size() >= btw);
std::lock_guard<std::mutex> lock(buffer_mutex_);
Expand Down