Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/StringView.h>
28#include <Kernel/DoubleBuffer.h>
29
30namespace Kernel {
31
32inline void DoubleBuffer::compute_lockfree_metadata()
33{
34 InterruptDisabler disabler;
35 m_empty = m_read_buffer_index >= m_read_buffer->size && m_write_buffer->size == 0;
36 m_space_for_writing = m_capacity - m_write_buffer->size;
37}
38
39DoubleBuffer::DoubleBuffer(size_t capacity)
40 : m_write_buffer(&m_buffer1)
41 , m_read_buffer(&m_buffer2)
42 , m_storage(KBuffer::create_with_size(capacity * 2, Region::Access::Read | Region::Access::Write, "DoubleBuffer"))
43 , m_capacity(capacity)
44{
45 m_buffer1.data = m_storage.data();
46 m_buffer1.size = 0;
47 m_buffer2.data = m_storage.data() + capacity;
48 m_buffer2.size = 0;
49 m_space_for_writing = capacity;
50}
51
52void DoubleBuffer::flip()
53{
54 ASSERT(m_read_buffer_index == m_read_buffer->size);
55 swap(m_read_buffer, m_write_buffer);
56 m_write_buffer->size = 0;
57 m_read_buffer_index = 0;
58 compute_lockfree_metadata();
59}
60
61ssize_t DoubleBuffer::write(const u8* data, ssize_t size)
62{
63 if (!size)
64 return 0;
65 ASSERT(size > 0);
66 LOCKER(m_lock);
67 ssize_t bytes_to_write = min(static_cast<size_t>(size), m_space_for_writing);
68 u8* write_ptr = m_write_buffer->data + m_write_buffer->size;
69 m_write_buffer->size += bytes_to_write;
70 compute_lockfree_metadata();
71 memcpy(write_ptr, data, bytes_to_write);
72 return bytes_to_write;
73}
74
75ssize_t DoubleBuffer::read(u8* data, ssize_t size)
76{
77 if (!size)
78 return 0;
79 ASSERT(size > 0);
80 LOCKER(m_lock);
81 if (m_read_buffer_index >= m_read_buffer->size && m_write_buffer->size != 0)
82 flip();
83 if (m_read_buffer_index >= m_read_buffer->size)
84 return 0;
85 ssize_t nread = min((ssize_t)m_read_buffer->size - (ssize_t)m_read_buffer_index, size);
86 memcpy(data, m_read_buffer->data + m_read_buffer_index, nread);
87 m_read_buffer_index += nread;
88 compute_lockfree_metadata();
89 return nread;
90}
91
92}