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
Notes: sideshowbarker 2024-07-19 16:08:50 +09:00
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
{
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);
}

View file

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