From 405383fd2f9757cebd175c5be9cfa65c48fe15f0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 3 Dec 2018 02:24:11 +0100 Subject: [PATCH] Refactor the FIFO implementation to use a DoubleBuffer as backing store. This is considerably more efficient than using a CircularQueue. --- Kernel/FIFO.cpp | 18 +++++------------- Kernel/FIFO.h | 4 ++-- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/Kernel/FIFO.cpp b/Kernel/FIFO.cpp index dbf7c8a0c14..14589d4c10b 100644 --- a/Kernel/FIFO.cpp +++ b/Kernel/FIFO.cpp @@ -46,27 +46,22 @@ void FIFO::close(Direction direction) bool FIFO::can_read() const { - return !m_queue.is_empty() || !m_writers; + return !m_buffer.is_empty() || !m_writers; } bool FIFO::can_write() const { -#ifdef FIFO_DEBUG - dbgprintf("can_write? size(%u) < capacity(%u) || !readers(%u)\n", m_queue.size(), m_queue.capacity(), m_readers); -#endif - return m_queue.size() < m_queue.capacity() || !m_readers; + return true; } ssize_t FIFO::read(byte* buffer, size_t size) { - if (!m_writers && m_queue.is_empty()) + if (!m_writers && m_buffer.is_empty()) return 0; #ifdef FIFO_DEBUG dbgprintf("fifo: read(%u)\n",size); #endif - size_t nread = min(size, m_queue.size()); - for (size_t i = 0; i < nread; ++i) - buffer[i] = m_queue.dequeue(); + size_t nread = m_buffer.read(buffer, size); #ifdef FIFO_DEBUG dbgprintf(" -> read (%c) %u\n", buffer[0], nread); #endif @@ -80,8 +75,5 @@ ssize_t FIFO::write(const byte* buffer, size_t size) #ifdef FIFO_DEBUG dbgprintf("fifo: write(%p, %u)\n", buffer, size); #endif - size_t nwritten = min(size, m_queue.capacity() - m_queue.size()); - for (size_t i = 0; i < nwritten; ++i) - m_queue.enqueue(buffer[i]); - return nwritten; + return m_buffer.write(buffer, size); } diff --git a/Kernel/FIFO.h b/Kernel/FIFO.h index 91392cc5aa8..30a5bd33df2 100644 --- a/Kernel/FIFO.h +++ b/Kernel/FIFO.h @@ -1,6 +1,6 @@ #pragma once -#include +#include "DoubleBuffer.h" #include #include #include @@ -27,5 +27,5 @@ private: unsigned m_writers { 0 }; unsigned m_readers { 0 }; - CircularQueue m_queue; + DoubleBuffer m_buffer; };