diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-21 10:50:59 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-21 20:22:48 +0100 |
commit | b820ae2828379f1b34ee8106256c5f10c243077b (patch) | |
tree | ba7cbd3700cd3bc27963e6819fb27a2da91e9cf5 /Kernel | |
parent | 9387271049debb5f3586736c74c5ee5d3a21d129 (diff) | |
download | serenity-b820ae2828379f1b34ee8106256c5f10c243077b.zip |
Kernel: Share code between DoubleBuffer's read() and peek()
The only difference between these is whether the buffer index is
advanced after the read.
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/DoubleBuffer.cpp | 28 | ||||
-rw-r--r-- | Kernel/DoubleBuffer.h | 2 |
2 files changed, 13 insertions, 17 deletions
diff --git a/Kernel/DoubleBuffer.cpp b/Kernel/DoubleBuffer.cpp index ecd90942c7..864568b593 100644 --- a/Kernel/DoubleBuffer.cpp +++ b/Kernel/DoubleBuffer.cpp @@ -60,40 +60,34 @@ ErrorOr<size_t> DoubleBuffer::write(const UserOrKernelBuffer& data, size_t size) return bytes_to_write; } -ErrorOr<size_t> DoubleBuffer::read(UserOrKernelBuffer& data, size_t size) +ErrorOr<size_t> DoubleBuffer::read_impl(UserOrKernelBuffer& data, size_t size, MutexLocker&, bool advance_buffer_index) { - if (!size) + if (size == 0) return 0; - MutexLocker locker(m_lock); if (m_read_buffer_index >= m_read_buffer->size && m_write_buffer->size != 0) flip(); if (m_read_buffer_index >= m_read_buffer->size) return 0; size_t nread = min(m_read_buffer->size - m_read_buffer_index, size); TRY(data.write(m_read_buffer->data + m_read_buffer_index, nread)); - m_read_buffer_index += nread; + if (advance_buffer_index) + m_read_buffer_index += nread; compute_lockfree_metadata(); if (m_unblock_callback && m_space_for_writing > 0) m_unblock_callback(); return nread; } +ErrorOr<size_t> DoubleBuffer::read(UserOrKernelBuffer& data, size_t size) +{ + MutexLocker locker(m_lock); + return read_impl(data, size, locker, true); +} + ErrorOr<size_t> DoubleBuffer::peek(UserOrKernelBuffer& data, size_t size) { - if (!size) - return 0; MutexLocker locker(m_lock); - if (m_read_buffer_index >= m_read_buffer->size && m_write_buffer->size != 0) { - flip(); - } - if (m_read_buffer_index >= m_read_buffer->size) - return 0; - size_t nread = min(m_read_buffer->size - m_read_buffer_index, size); - TRY(data.write(m_read_buffer->data + m_read_buffer_index, nread)); - compute_lockfree_metadata(); - if (m_unblock_callback && m_space_for_writing > 0) - m_unblock_callback(); - return nread; + return read_impl(data, size, locker, false); } } diff --git a/Kernel/DoubleBuffer.h b/Kernel/DoubleBuffer.h index 958d537d40..3d775be2d4 100644 --- a/Kernel/DoubleBuffer.h +++ b/Kernel/DoubleBuffer.h @@ -54,6 +54,8 @@ private: void flip(); void compute_lockfree_metadata(); + ErrorOr<size_t> read_impl(UserOrKernelBuffer&, size_t, MutexLocker&, bool advance_buffer_index); + struct InnerBuffer { u8* data { nullptr }; size_t size; |