2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-23 12:45:10 +00:00
|
|
|
#include <AK/StringView.h>
|
2019-02-25 20:19:57 +00:00
|
|
|
#include <Kernel/DoubleBuffer.h>
|
2018-12-03 00:51:44 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-01-20 15:08:49 +00:00
|
|
|
inline void DoubleBuffer::compute_lockfree_metadata()
|
2019-01-15 20:43:38 +00:00
|
|
|
{
|
2020-01-20 15:08:49 +00:00
|
|
|
InterruptDisabler disabler;
|
|
|
|
m_empty = m_read_buffer_index >= m_read_buffer->size && m_write_buffer->size == 0;
|
|
|
|
m_space_for_writing = m_capacity - m_write_buffer->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
DoubleBuffer::DoubleBuffer(size_t capacity)
|
|
|
|
: m_write_buffer(&m_buffer1)
|
|
|
|
, m_read_buffer(&m_buffer2)
|
|
|
|
, m_storage(KBuffer::create_with_size(capacity * 2, Region::Access::Read | Region::Access::Write, "DoubleBuffer"))
|
|
|
|
, m_capacity(capacity)
|
|
|
|
{
|
|
|
|
m_buffer1.data = m_storage.data();
|
|
|
|
m_buffer1.size = 0;
|
|
|
|
m_buffer2.data = m_storage.data() + capacity;
|
|
|
|
m_buffer2.size = 0;
|
|
|
|
m_space_for_writing = capacity;
|
2019-01-15 20:43:38 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:51:44 +00:00
|
|
|
void DoubleBuffer::flip()
|
|
|
|
{
|
2020-09-05 21:52:14 +00:00
|
|
|
if (m_storage.is_null())
|
|
|
|
return;
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_read_buffer_index == m_read_buffer->size);
|
2018-12-03 00:51:44 +00:00
|
|
|
swap(m_read_buffer, m_write_buffer);
|
2020-01-20 15:08:49 +00:00
|
|
|
m_write_buffer->size = 0;
|
2018-12-03 00:51:44 +00:00
|
|
|
m_read_buffer_index = 0;
|
2020-01-20 15:08:49 +00:00
|
|
|
compute_lockfree_metadata();
|
2018-12-03 00:51:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:11:07 +00:00
|
|
|
ssize_t DoubleBuffer::write(const UserOrKernelBuffer& data, size_t size)
|
2018-12-03 00:51:44 +00:00
|
|
|
{
|
2020-09-05 21:52:14 +00:00
|
|
|
if (!size || m_storage.is_null())
|
2019-03-20 01:12:03 +00:00
|
|
|
return 0;
|
2021-04-24 22:27:32 +00:00
|
|
|
Locker locker(m_lock);
|
2020-08-04 16:02:23 +00:00
|
|
|
size_t bytes_to_write = min(size, m_space_for_writing);
|
2020-01-20 15:08:49 +00:00
|
|
|
u8* write_ptr = m_write_buffer->data + m_write_buffer->size;
|
2020-02-01 08:43:28 +00:00
|
|
|
m_write_buffer->size += bytes_to_write;
|
2020-01-20 15:08:49 +00:00
|
|
|
compute_lockfree_metadata();
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!data.read(write_ptr, bytes_to_write))
|
|
|
|
return -EFAULT;
|
2020-11-29 23:05:27 +00:00
|
|
|
if (m_unblock_callback && !m_empty)
|
|
|
|
m_unblock_callback();
|
2020-09-12 03:11:07 +00:00
|
|
|
return (ssize_t)bytes_to_write;
|
2018-12-03 00:51:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:11:07 +00:00
|
|
|
ssize_t DoubleBuffer::read(UserOrKernelBuffer& data, size_t size)
|
2018-12-03 00:51:44 +00:00
|
|
|
{
|
2020-09-05 21:52:14 +00:00
|
|
|
if (!size || m_storage.is_null())
|
2019-03-20 01:12:03 +00:00
|
|
|
return 0;
|
2021-04-24 22:27:32 +00:00
|
|
|
Locker locker(m_lock);
|
2020-01-20 15:08:49 +00:00
|
|
|
if (m_read_buffer_index >= m_read_buffer->size && m_write_buffer->size != 0)
|
2018-12-03 00:51:44 +00:00
|
|
|
flip();
|
2020-01-20 15:08:49 +00:00
|
|
|
if (m_read_buffer_index >= m_read_buffer->size)
|
2018-12-03 00:51:44 +00:00
|
|
|
return 0;
|
2020-08-04 16:02:23 +00:00
|
|
|
size_t nread = min(m_read_buffer->size - m_read_buffer_index, size);
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!data.write(m_read_buffer->data + m_read_buffer_index, nread))
|
|
|
|
return -EFAULT;
|
2018-12-03 00:51:44 +00:00
|
|
|
m_read_buffer_index += nread;
|
2020-01-20 15:08:49 +00:00
|
|
|
compute_lockfree_metadata();
|
2020-11-29 23:05:27 +00:00
|
|
|
if (m_unblock_callback && m_space_for_writing > 0)
|
|
|
|
m_unblock_callback();
|
2020-09-12 03:11:07 +00:00
|
|
|
return (ssize_t)nread;
|
2018-12-03 00:51:44 +00:00
|
|
|
}
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2021-04-28 21:22:21 +00:00
|
|
|
ssize_t DoubleBuffer::peek(UserOrKernelBuffer& data, size_t size)
|
|
|
|
{
|
|
|
|
if (!size || m_storage.is_null())
|
|
|
|
return 0;
|
|
|
|
Locker 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);
|
|
|
|
if (!data.write(m_read_buffer->data + m_read_buffer_index, nread))
|
|
|
|
return -EFAULT;
|
|
|
|
compute_lockfree_metadata();
|
|
|
|
if (m_unblock_callback && m_space_for_writing > 0)
|
|
|
|
m_unblock_callback();
|
|
|
|
return (ssize_t)nread;
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|