2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-03-23 15:45:10 +03:00
|
|
|
#include <AK/StringView.h>
|
2019-02-25 23:19:57 +03:00
|
|
|
#include <Kernel/DoubleBuffer.h>
|
2022-10-05 20:27:36 +03:00
|
|
|
#include <Kernel/InterruptDisabler.h>
|
2018-12-03 03:51:44 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-01-20 18:08:49 +03:00
|
|
|
inline void DoubleBuffer::compute_lockfree_metadata()
|
2019-01-15 23:43:38 +03:00
|
|
|
{
|
2020-01-20 18:08:49 +03: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;
|
|
|
|
}
|
|
|
|
|
2022-04-11 01:08:07 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<DoubleBuffer>> DoubleBuffer::try_create(StringView name, size_t capacity)
|
2021-08-01 11:40:07 +03:00
|
|
|
{
|
2022-04-11 01:08:07 +03:00
|
|
|
auto storage = TRY(KBuffer::try_create_with_size(name, capacity * 2, Memory::Region::Access::ReadWrite));
|
2021-09-07 16:15:08 +03:00
|
|
|
return adopt_nonnull_own_or_enomem(new (nothrow) DoubleBuffer(capacity, move(storage)));
|
2021-08-01 11:40:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
DoubleBuffer::DoubleBuffer(size_t capacity, NonnullOwnPtr<KBuffer> storage)
|
2020-01-20 18:08:49 +03:00
|
|
|
: m_write_buffer(&m_buffer1)
|
|
|
|
, m_read_buffer(&m_buffer2)
|
2021-08-01 11:40:07 +03:00
|
|
|
, m_storage(move(storage))
|
2020-01-20 18:08:49 +03:00
|
|
|
, m_capacity(capacity)
|
|
|
|
{
|
2021-08-01 11:40:07 +03:00
|
|
|
m_buffer1.data = m_storage->data();
|
2020-01-20 18:08:49 +03:00
|
|
|
m_buffer1.size = 0;
|
2021-08-01 11:40:07 +03:00
|
|
|
m_buffer2.data = m_storage->data() + capacity;
|
2020-01-20 18:08:49 +03:00
|
|
|
m_buffer2.size = 0;
|
|
|
|
m_space_for_writing = capacity;
|
2019-01-15 23:43:38 +03:00
|
|
|
}
|
|
|
|
|
2018-12-03 03:51:44 +03:00
|
|
|
void DoubleBuffer::flip()
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_read_buffer_index == m_read_buffer->size);
|
2018-12-03 03:51:44 +03:00
|
|
|
swap(m_read_buffer, m_write_buffer);
|
2020-01-20 18:08:49 +03:00
|
|
|
m_write_buffer->size = 0;
|
2018-12-03 03:51:44 +03:00
|
|
|
m_read_buffer_index = 0;
|
2020-01-20 18:08:49 +03:00
|
|
|
compute_lockfree_metadata();
|
2018-12-03 03:51:44 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<size_t> DoubleBuffer::write(UserOrKernelBuffer const& data, size_t size)
|
2018-12-03 03:51:44 +03:00
|
|
|
{
|
2021-09-07 17:00:39 +03:00
|
|
|
if (!size)
|
2019-03-20 04:12:03 +03:00
|
|
|
return 0;
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_lock);
|
2020-08-04 19:02:23 +03:00
|
|
|
size_t bytes_to_write = min(size, m_space_for_writing);
|
2020-01-20 18:08:49 +03:00
|
|
|
u8* write_ptr = m_write_buffer->data + m_write_buffer->size;
|
2021-09-07 13:09:52 +03:00
|
|
|
TRY(data.read(write_ptr, bytes_to_write));
|
2021-05-13 05:19:53 +03:00
|
|
|
m_write_buffer->size += bytes_to_write;
|
|
|
|
compute_lockfree_metadata();
|
2020-11-30 02:05:27 +03:00
|
|
|
if (m_unblock_callback && !m_empty)
|
|
|
|
m_unblock_callback();
|
2021-06-16 16:33:14 +03:00
|
|
|
return bytes_to_write;
|
2018-12-03 03:51:44 +03:00
|
|
|
}
|
|
|
|
|
2021-11-21 12:50:59 +03:00
|
|
|
ErrorOr<size_t> DoubleBuffer::read_impl(UserOrKernelBuffer& data, size_t size, MutexLocker&, bool advance_buffer_index)
|
2018-12-03 03:51:44 +03:00
|
|
|
{
|
2021-11-21 12:50:59 +03:00
|
|
|
if (size == 0)
|
2019-03-20 04:12:03 +03:00
|
|
|
return 0;
|
2020-01-20 18:08:49 +03:00
|
|
|
if (m_read_buffer_index >= m_read_buffer->size && m_write_buffer->size != 0)
|
2018-12-03 03:51:44 +03:00
|
|
|
flip();
|
2020-01-20 18:08:49 +03:00
|
|
|
if (m_read_buffer_index >= m_read_buffer->size)
|
2018-12-03 03:51:44 +03:00
|
|
|
return 0;
|
2020-08-04 19:02:23 +03:00
|
|
|
size_t nread = min(m_read_buffer->size - m_read_buffer_index, size);
|
2021-09-07 13:09:52 +03:00
|
|
|
TRY(data.write(m_read_buffer->data + m_read_buffer_index, nread));
|
2021-11-21 12:50:59 +03:00
|
|
|
if (advance_buffer_index)
|
|
|
|
m_read_buffer_index += nread;
|
2020-01-20 18:08:49 +03:00
|
|
|
compute_lockfree_metadata();
|
2020-11-30 02:05:27 +03:00
|
|
|
if (m_unblock_callback && m_space_for_writing > 0)
|
|
|
|
m_unblock_callback();
|
2021-06-16 16:33:14 +03:00
|
|
|
return nread;
|
2018-12-03 03:51:44 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-11-21 12:50:59 +03:00
|
|
|
ErrorOr<size_t> DoubleBuffer::read(UserOrKernelBuffer& data, size_t size)
|
|
|
|
{
|
|
|
|
MutexLocker locker(m_lock);
|
|
|
|
return read_impl(data, size, locker, true);
|
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<size_t> DoubleBuffer::peek(UserOrKernelBuffer& data, size_t size)
|
2021-04-29 00:22:21 +03:00
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_lock);
|
2021-11-21 12:50:59 +03:00
|
|
|
return read_impl(data, size, locker, false);
|
2021-04-29 00:22:21 +03:00
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|