2019-02-25 21:19:57 +01:00
|
|
|
#include <Kernel/DoubleBuffer.h>
|
2018-12-03 01:51:44 +01:00
|
|
|
|
2019-01-15 21:43:38 +01:00
|
|
|
inline void DoubleBuffer::compute_emptiness()
|
|
|
|
{
|
|
|
|
m_empty = m_read_buffer_index >= m_read_buffer->size() && m_write_buffer->is_empty();
|
|
|
|
}
|
|
|
|
|
2018-12-03 01:51:44 +01:00
|
|
|
void DoubleBuffer::flip()
|
|
|
|
{
|
|
|
|
ASSERT(m_read_buffer_index == m_read_buffer->size());
|
|
|
|
swap(m_read_buffer, m_write_buffer);
|
2019-01-12 23:23:35 +01:00
|
|
|
if (m_write_buffer->capacity() < 32)
|
|
|
|
m_write_buffer->clear_with_capacity();
|
|
|
|
else
|
|
|
|
m_write_buffer->clear();
|
2018-12-03 01:51:44 +01:00
|
|
|
m_read_buffer_index = 0;
|
2019-01-15 21:43:38 +01:00
|
|
|
compute_emptiness();
|
2018-12-03 01:51:44 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 21:19:57 +01:00
|
|
|
ssize_t DoubleBuffer::write(const byte* data, ssize_t size)
|
2018-12-03 01:51:44 +01:00
|
|
|
{
|
2019-01-15 21:43:38 +01:00
|
|
|
LOCKER(m_lock);
|
2018-12-03 01:51:44 +01:00
|
|
|
m_write_buffer->append(data, size);
|
2019-01-15 21:43:38 +01:00
|
|
|
compute_emptiness();
|
2018-12-03 01:51:44 +01:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-02-25 21:19:57 +01:00
|
|
|
ssize_t DoubleBuffer::read(byte* data, ssize_t size)
|
2018-12-03 01:51:44 +01:00
|
|
|
{
|
2019-01-15 21:43:38 +01:00
|
|
|
LOCKER(m_lock);
|
2018-12-21 02:10:45 +01:00
|
|
|
if (m_read_buffer_index >= m_read_buffer->size() && !m_write_buffer->is_empty())
|
2018-12-03 01:51:44 +01:00
|
|
|
flip();
|
|
|
|
if (m_read_buffer_index >= m_read_buffer->size())
|
|
|
|
return 0;
|
2019-02-25 21:19:57 +01:00
|
|
|
ssize_t nread = min((ssize_t)m_read_buffer->size() - m_read_buffer_index, size);
|
2018-12-03 01:51:44 +01:00
|
|
|
memcpy(data, m_read_buffer->data() + m_read_buffer_index, nread);
|
|
|
|
m_read_buffer_index += nread;
|
2019-01-15 21:43:38 +01:00
|
|
|
compute_emptiness();
|
2018-12-03 01:51:44 +01:00
|
|
|
return nread;
|
|
|
|
}
|