1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:17:34 +00:00

Refactor the FIFO implementation to use a DoubleBuffer as backing store.

This is considerably more efficient than using a CircularQueue.
This commit is contained in:
Andreas Kling 2018-12-03 02:24:11 +01:00
parent afc56d151f
commit 405383fd2f
2 changed files with 7 additions and 15 deletions

View file

@ -46,27 +46,22 @@ void FIFO::close(Direction direction)
bool FIFO::can_read() const bool FIFO::can_read() const
{ {
return !m_queue.is_empty() || !m_writers; return !m_buffer.is_empty() || !m_writers;
} }
bool FIFO::can_write() const bool FIFO::can_write() const
{ {
#ifdef FIFO_DEBUG return true;
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;
} }
ssize_t FIFO::read(byte* buffer, size_t size) 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; return 0;
#ifdef FIFO_DEBUG #ifdef FIFO_DEBUG
dbgprintf("fifo: read(%u)\n",size); dbgprintf("fifo: read(%u)\n",size);
#endif #endif
size_t nread = min(size, m_queue.size()); size_t nread = m_buffer.read(buffer, size);
for (size_t i = 0; i < nread; ++i)
buffer[i] = m_queue.dequeue();
#ifdef FIFO_DEBUG #ifdef FIFO_DEBUG
dbgprintf(" -> read (%c) %u\n", buffer[0], nread); dbgprintf(" -> read (%c) %u\n", buffer[0], nread);
#endif #endif
@ -80,8 +75,5 @@ ssize_t FIFO::write(const byte* buffer, size_t size)
#ifdef FIFO_DEBUG #ifdef FIFO_DEBUG
dbgprintf("fifo: write(%p, %u)\n", buffer, size); dbgprintf("fifo: write(%p, %u)\n", buffer, size);
#endif #endif
size_t nwritten = min(size, m_queue.capacity() - m_queue.size()); return m_buffer.write(buffer, size);
for (size_t i = 0; i < nwritten; ++i)
m_queue.enqueue(buffer[i]);
return nwritten;
} }

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <AK/CircularQueue.h> #include "DoubleBuffer.h"
#include <AK/Retainable.h> #include <AK/Retainable.h>
#include <AK/RetainPtr.h> #include <AK/RetainPtr.h>
#include <VirtualFileSystem/UnixTypes.h> #include <VirtualFileSystem/UnixTypes.h>
@ -27,5 +27,5 @@ private:
unsigned m_writers { 0 }; unsigned m_writers { 0 };
unsigned m_readers { 0 }; unsigned m_readers { 0 };
CircularQueue<byte, 16> m_queue; DoubleBuffer m_buffer;
}; };